2
* mdadm - manage Linux "md" devices aka RAID arrays.
4
* Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 2 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
* Email: <neilb@cse.unsw.edu.au>
24
* School of Computer Science and Engineering
25
* The University of New South Wales
34
#include <sys/signal.h>
37
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd);
39
static char *percentalerts[] = {
47
int Monitor(mddev_dev_t devlist,
48
char *mailaddr, char *alert_cmd,
49
int period, int daemonise, int scan, int oneshot,
50
char *config, int test, char* pidfile)
53
* Every few seconds, scan every md device looking for changes
54
* When a change is found, log it, possibly run the alert command,
55
* and possibly send Email
57
* For each array, we record:
59
* active/working/failed/spare drives
60
* State of each device.
61
* %rebuilt if rebuilding
63
* If the update time changes, check out all the data again
64
* It is possible that we cannot get the state of each device
65
* due to bugs in the md kernel module.
66
* We also read /proc/mdstat to get rebuild percent,
67
* and to get state on all active devices incase of kernel bug.
71
* An active device had Faulty set or Active/Sync removed
73
* A spare device had Faulty set
75
* An active device had a reverse transition
77
* percent went from -1 to +ve
78
* Rebuild20 Rebuild40 Rebuild60 Rebuild80
79
* percent went from below to not-below that number
81
* Couldn't access a device which was previously visible
83
* if we detect an array with active<raid and spare==0
84
* we look at other arrays that have same spare-group
85
* If we find one with active==raid and spare>0,
86
* and if we can get_disk_info and find a name
87
* Then we hot-remove and hot-add to the other array
89
* If devlist is NULL, then we can monitor everything because --scan
90
* was given. We get an initial list from config file and add anything
91
* that appears in /proc/mdstat
96
int devnum; /* to sync with mdstat info */
100
int active, working, failed, spare, raid;
102
int devstate[MD_SB_DISKS];
103
int devid[MD_SB_DISKS];
108
struct mdstat_ent *mdstat = NULL;
111
mailaddr = conf_get_mailaddr(config);
112
if (mailaddr && ! scan)
113
fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
117
alert_cmd = conf_get_program(config);
118
if (alert_cmd && ! scan)
119
fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
122
if (scan && !mailaddr && !alert_cmd) {
123
fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
134
pid_file=fopen(pidfile, "w");
136
perror("cannot create pid file");
138
fprintf(pid_file,"%d\n", pid);
149
open("/dev/null", 3);
155
if (devlist == NULL) {
156
mddev_ident_t mdlist = conf_get_ident(config, NULL);
157
for (; mdlist; mdlist=mdlist->next) {
158
struct state *st = malloc(sizeof *st);
161
st->devname = strdup(mdlist->devname);
163
st->next = statelist;
167
st->expected_spares = mdlist->spare_disks;
168
if (mdlist->spare_group)
169
st->spare_group = strdup(mdlist->spare_group);
171
st->spare_group = NULL;
176
for (dv=devlist ; dv; dv=dv->next) {
177
mddev_ident_t mdlist = conf_get_ident(config, dv->devname);
178
struct state *st = malloc(sizeof *st);
181
st->devname = strdup(dv->devname);
183
st->next = statelist;
187
st->expected_spares = -1;
188
st->spare_group = NULL;
190
st->expected_spares = mdlist->spare_disks;
191
if (mdlist->spare_group)
192
st->spare_group = strdup(mdlist->spare_group);
205
mdstat = mdstat_read(oneshot?0:1);
207
for (st=statelist; st; st=st->next) {
208
mdu_array_info_t array;
209
struct mdstat_ent *mse = NULL, *mse2;
210
char *dev = st->devname;
215
alert("TestMessage", dev, NULL, mailaddr, alert_cmd);
216
fd = open(dev, O_RDONLY);
219
alert("DeviceDisappeared", dev, NULL,
220
mailaddr, alert_cmd);
221
/* fprintf(stderr, Name ": cannot open %s: %s\n",
222
dev, strerror(errno));
226
if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
228
alert("DeviceDisappeared", dev, NULL,
229
mailaddr, alert_cmd);
230
/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
231
dev, strerror(errno));
236
if (array.level != 1 && array.level != 5 && array.level != -4 &&
237
array.level != 6 && array.level != 10) {
239
alert("DeviceDisappeared", dev, "Wrong-Level",
240
mailaddr, alert_cmd);
245
if (st->devnum == MAXINT) {
247
if (fstat(fd, &stb) == 0 &&
248
(S_IFMT&stb.st_mode)==S_IFBLK) {
249
if (major(stb.st_rdev) == MD_MAJOR)
250
st->devnum = minor(stb.st_rdev);
252
st->devnum = -1- (minor(stb.st_rdev)>>6);
256
for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
257
if (mse2->devnum == st->devnum) {
258
mse2->devnum = MAXINT; /* flag it as "used" */
262
if (st->utime == array.utime &&
263
st->failed == array.failed_disks &&
264
st->working == array.working_disks &&
265
st->spare == array.spare_disks &&
267
mse->percent == st->percent
273
if (st->utime == 0 && /* new array */
274
mse && /* is in /proc/mdstat */
275
mse->pattern && strchr(mse->pattern, '_') /* degraded */
277
alert("DegradedArray", dev, NULL, mailaddr, alert_cmd);
279
if (st->utime == 0 && /* new array */
280
st->expected_spares > 0 &&
281
array.spare_disks < st->expected_spares)
282
alert("SparesMissing", dev, NULL, mailaddr, alert_cmd);
286
alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd);
290
(mse->percent / 20) > (st->percent / 20))
291
alert(percentalerts[mse->percent/20],
292
dev, NULL, mailaddr, alert_cmd);
295
mse->percent == -1 &&
297
alert("RebuildFinished", dev, NULL, mailaddr, alert_cmd);
300
st->percent = mse->percent;
302
for (i=0; i<MD_SB_DISKS; i++) {
303
mdu_disk_info_t disc;
308
if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
309
newstate = disc.state;
310
dv = map_dev(disc.major, disc.minor);
311
} else if (mse && mse->pattern && i < strlen(mse->pattern))
312
switch(mse->pattern[i]) {
313
case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
314
case '_': newstate = 0; break;
316
change = newstate ^ st->devstate[i];
317
if (st->utime && change && !st->err) {
318
if (i < (unsigned)array.raid_disks &&
319
(((newstate&change)&(1<<MD_DISK_FAULTY)) ||
320
((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
321
((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
323
alert("Fail", dev, dv, mailaddr, alert_cmd);
324
else if (i >= (unsigned)array.raid_disks &&
325
(disc.major || disc.minor) &&
326
st->devid[i] == makedev(disc.major, disc.minor) &&
327
((newstate&change)&(1<<MD_DISK_FAULTY))
329
alert("FailSpare", dev, dv, mailaddr, alert_cmd);
330
else if (i < (unsigned)array.raid_disks &&
331
(((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
332
((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
333
((newstate&change)&(1<<MD_DISK_SYNC)))
335
alert("SpareActive", dev, dv, mailaddr, alert_cmd);
337
st->devstate[i] = disc.state;
338
st->devid[i] = makedev(disc.major, disc.minor);
341
st->active = array.active_disks;
342
st->working = array.working_disks;
343
st->spare = array.spare_disks;
344
st->failed = array.failed_disks;
345
st->utime = array.utime;
346
st->raid = array.raid_disks;
349
/* now check if there are any new devices found in mdstat */
351
struct mdstat_ent *mse;
352
for (mse=mdstat; mse; mse=mse->next)
353
if (mse->devnum != MAXINT &&
354
(strcmp(mse->level, "raid1")==0 ||
355
strcmp(mse->level, "raid5")==0 ||
356
strcmp(mse->level, "multipath")==0)
358
struct state *st = malloc(sizeof *st);
359
mdu_array_info_t array;
363
st->devname = strdup(get_md_name(mse->devnum));
364
if ((fd = open(st->devname, O_RDONLY)) < 0 ||
365
ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
367
if (fd >=0) close(fd);
374
st->next = statelist;
376
st->devnum = mse->devnum;
378
st->spare_group = NULL;
379
st->expected_spares = -1;
381
alert("NewArray", st->devname, NULL, mailaddr, alert_cmd);
385
/* If an array has active < raid && spare == 0 && spare_group != NULL
386
* Look for another array with spare > 0 and active == raid and same spare_group
387
* if found, choose a device and hotremove/hotadd
389
for (st = statelist; st; st=st->next)
390
if (st->active < st->raid &&
392
st->spare_group != NULL) {
394
for (st2=statelist ; st2 ; st2=st2->next)
397
st2->active == st2->raid &&
398
st2->spare_group != NULL &&
399
strcmp(st->spare_group, st2->spare_group) == 0) {
400
/* try to remove and add */
401
int fd1 = open(st->devname, O_RDONLY);
402
int fd2 = open(st2->devname, O_RDONLY);
405
if (fd1 < 0 || fd2 < 0) {
406
if (fd1>=0) close(fd1);
407
if (fd2>=0) close(fd2);
410
for (d=st2->raid; d<MD_SB_DISKS; d++) {
411
if (st2->devid[d] > 0 &&
412
st2->devstate[d] == 0) {
418
if (ioctl(fd2, HOT_REMOVE_DISK,
419
(unsigned long)dev) == 0) {
420
if (ioctl(fd1, HOT_ADD_DISK,
421
(unsigned long)dev) == 0) {
422
alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd);
427
else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
448
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd)
450
if (!cmd && !mailaddr) {
451
time_t now = time(0);
453
printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
459
waitpid(pid, NULL, 0);
464
execl(cmd, cmd, event, dev, disc, NULL);
469
(strncmp(event, "Fail", 4)==0 ||
470
strncmp(event, "Test", 4)==0 ||
471
strncmp(event, "Degrade", 7)==0)) {
472
FILE *mp = popen(Sendmail, "w");
475
gethostname(hname, sizeof(hname));
476
signal(SIGPIPE, SIG_IGN);
477
fprintf(mp, "From: " Name " monitoring <root>\n");
478
fprintf(mp, "To: %s\n", mailaddr);
479
fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
481
fprintf(mp, "This is an automatically generated mail message from " Name "\n");
482
fprintf(mp, "running on %s\n\n", hname);
484
fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
487
fprintf(mp, "It could be related to component device %s.\n\n", disc);
489
fprintf(mp, "Faithfully yours, etc.\n");
494
/* FIXME log the event to syslog maybe */