]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
bbf46d6ee31efcab3d15921fcc75831f883283f7
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
33 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
,
34 char *cmd
, int dosyslog
);
36 /* The largest number of disks current arrays can manage is 384
37 * This really should be dynamically, but that will have to wait
38 * At least it isn't MD_SB_DISKS.
41 int Monitor(struct mddev_dev
*devlist
,
42 char *mailaddr
, char *alert_cmd
,
43 int period
, int daemonise
, int scan
, int oneshot
,
44 int dosyslog
, int test
, char *pidfile
, int increments
,
48 * Every few seconds, scan every md device looking for changes
49 * When a change is found, log it, possibly run the alert command,
50 * and possibly send Email
52 * For each array, we record:
54 * active/working/failed/spare drives
55 * State of each device.
56 * %rebuilt if rebuilding
58 * If the update time changes, check out all the data again
59 * It is possible that we cannot get the state of each device
60 * due to bugs in the md kernel module.
61 * We also read /proc/mdstat to get rebuild percent,
62 * and to get state on all active devices incase of kernel bug.
66 * An active device had Faulty set or Active/Sync removed
68 * A spare device had Faulty set
70 * An active device had a reverse transition
72 * percent went from -1 to +ve
74 * percent went from below to not-below NN%
76 * Couldn't access a device which was previously visible
78 * if we detect an array with active<raid and spare==0
79 * we look at other arrays that have same spare-group
80 * If we find one with active==raid and spare>0,
81 * and if we can get_disk_info and find a name
82 * Then we hot-remove and hot-add to the other array
84 * If devlist is NULL, then we can monitor everything because --scan
85 * was given. We get an initial list from config file and add anything
86 * that appears in /proc/mdstat
91 int devnum
; /* to sync with mdstat info */
95 int active
, working
, failed
, spare
, raid
;
97 int devstate
[MaxDisks
];
98 unsigned devid
[MaxDisks
];
103 struct mdstat_ent
*mdstat
= NULL
;
104 char *mailfrom
= NULL
;
107 mailaddr
= conf_get_mailaddr();
108 if (mailaddr
&& ! scan
)
109 fprintf(stderr
, Name
": Monitor using email address \"%s\" from config file\n",
112 mailfrom
= conf_get_mailfrom();
115 alert_cmd
= conf_get_program();
116 if (alert_cmd
&& ! scan
)
117 fprintf(stderr
, Name
": Monitor using program \"%s\" from config file\n",
120 if (scan
&& !mailaddr
&& !alert_cmd
) {
121 fprintf(stderr
, Name
": No mail address or alert command - not monitoring.\n");
132 pid_file
=fopen(pidfile
, "w");
134 perror("cannot create pid file");
136 fprintf(pid_file
,"%d\n", pid
);
147 open("/dev/null", O_RDWR
);
158 fp
= fopen("/var/run/mdadm/autorebuild.pid", "r");
160 fscanf(fp
, "%d", &pid
);
161 sprintf(dir
, "/proc/%d", pid
);
162 rv
= stat(dir
, &buf
);
165 fprintf(stderr
, Name
": Only one "
166 "autorebuild process allowed"
167 " in scan mode, aborting\n");
171 fprintf(stderr
, Name
": Warning: One"
172 " autorebuild process already"
179 fp
= fopen("/var/run/mdadm/autorebuild.pid", "w");
181 fprintf(stderr
, Name
": Cannot create"
186 fprintf(fp
, "%d\n", pid
);
192 if (devlist
== NULL
) {
193 struct mddev_ident
*mdlist
= conf_get_ident(NULL
);
194 for (; mdlist
; mdlist
=mdlist
->next
) {
196 if (mdlist
->devname
== NULL
)
198 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
200 st
= malloc(sizeof *st
);
203 if (mdlist
->devname
[0] == '/')
204 st
->devname
= strdup(mdlist
->devname
);
206 st
->devname
= malloc(8+strlen(mdlist
->devname
)+1);
207 strcpy(strcpy(st
->devname
, "/dev/md/"),
211 st
->next
= statelist
;
213 st
->devnum
= INT_MAX
;
215 st
->expected_spares
= mdlist
->spare_disks
;
216 if (mdlist
->spare_group
)
217 st
->spare_group
= strdup(mdlist
->spare_group
);
219 st
->spare_group
= NULL
;
223 struct mddev_dev
*dv
;
224 for (dv
=devlist
; dv
; dv
=dv
->next
) {
225 struct mddev_ident
*mdlist
= conf_get_ident(dv
->devname
);
226 struct state
*st
= malloc(sizeof *st
);
229 st
->devname
= strdup(dv
->devname
);
231 st
->next
= statelist
;
233 st
->devnum
= INT_MAX
;
235 st
->expected_spares
= -1;
236 st
->spare_group
= NULL
;
238 st
->expected_spares
= mdlist
->spare_disks
;
239 if (mdlist
->spare_group
)
240 st
->spare_group
= strdup(mdlist
->spare_group
);
253 mdstat
= mdstat_read(oneshot
?0:1, 0);
255 for (st
=statelist
; st
; st
=st
->next
) {
256 struct { int state
, major
, minor
; } info
[MaxDisks
];
257 mdu_array_info_t array
;
258 struct mdstat_ent
*mse
= NULL
, *mse2
;
259 char *dev
= st
->devname
;
264 alert("TestMessage", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
265 fd
= open(dev
, O_RDONLY
);
268 alert("DeviceDisappeared", dev
, NULL
,
269 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
270 /* fprintf(stderr, Name ": cannot open %s: %s\n",
271 dev, strerror(errno));
275 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
276 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
278 alert("DeviceDisappeared", dev
, NULL
,
279 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
280 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
281 dev, strerror(errno));
286 /* It's much easier to list what array levels can't
287 * have a device disappear than all of them that can
289 if (array
.level
== 0 || array
.level
== -1) {
291 alert("DeviceDisappeared", dev
, "Wrong-Level",
292 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
297 if (st
->devnum
== INT_MAX
) {
299 if (fstat(fd
, &stb
) == 0 &&
300 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
301 if (major(stb
.st_rdev
) == MD_MAJOR
)
302 st
->devnum
= minor(stb
.st_rdev
);
304 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
308 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
309 if (mse2
->devnum
== st
->devnum
) {
310 mse2
->devnum
= INT_MAX
; /* flag it as "used" */
315 /* duplicated array in statelist
316 * or re-created after reading mdstat*/
321 /* this array is in /proc/mdstat */
322 if (array
.utime
== 0)
323 /* external arrays don't update utime, so
324 * just make sure it is always different. */
325 array
.utime
= st
->utime
+ 1;;
327 if (st
->utime
== array
.utime
&&
328 st
->failed
== array
.failed_disks
&&
329 st
->working
== array
.working_disks
&&
330 st
->spare
== array
.spare_disks
&&
332 mse
->percent
== st
->percent
338 if (st
->utime
== 0 && /* new array */
339 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
341 alert("DegradedArray", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
343 if (st
->utime
== 0 && /* new array */
344 st
->expected_spares
> 0 &&
345 array
.spare_disks
< st
->expected_spares
)
346 alert("SparesMissing", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
347 if (st
->percent
== -1 &&
349 alert("RebuildStarted", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
350 if (st
->percent
>= 0 &&
352 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
353 char percentalert
[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
355 if((mse
->percent
/ increments
) == 0)
356 snprintf(percentalert
, sizeof(percentalert
), "RebuildStarted");
358 snprintf(percentalert
, sizeof(percentalert
), "Rebuild%02d", mse
->percent
);
361 dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
364 if (mse
->percent
== -1 &&
366 /* Rebuild/sync/whatever just finished.
367 * If there is a number in /mismatch_cnt,
368 * we should report that.
371 sysfs_read(-1, st
->devnum
, GET_MISMATCH
);
372 if (sra
&& sra
->mismatch_cnt
> 0) {
374 sprintf(cnt
, " mismatches found: %d", sra
->mismatch_cnt
);
375 alert("RebuildFinished", dev
, cnt
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
377 alert("RebuildFinished", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
381 st
->percent
= mse
->percent
;
383 for (i
=0; i
<MaxDisks
&& i
<= array
.raid_disks
+ array
.nr_disks
;
385 mdu_disk_info_t disc
;
387 if (ioctl(fd
, GET_DISK_INFO
, &disc
) >= 0) {
388 info
[i
].state
= disc
.state
;
389 info
[i
].major
= disc
.major
;
390 info
[i
].minor
= disc
.minor
;
392 info
[i
].major
= info
[i
].minor
= 0;
396 for (i
=0; i
<MaxDisks
; i
++) {
397 mdu_disk_info_t disc
= {0,0,0,0,0};
402 if (i
> array
.raid_disks
+ array
.nr_disks
) {
404 disc
.major
= disc
.minor
= 0;
405 } else if (info
[i
].major
|| info
[i
].minor
) {
406 newstate
= info
[i
].state
;
407 dv
= map_dev(info
[i
].major
, info
[i
].minor
, 1);
408 disc
.state
= newstate
;
409 disc
.major
= info
[i
].major
;
410 disc
.minor
= info
[i
].minor
;
411 } else if (mse
&& mse
->pattern
&& i
< (int)strlen(mse
->pattern
)) {
412 switch(mse
->pattern
[i
]) {
413 case 'U': newstate
= 6 /* ACTIVE/SYNC */; break;
414 case '_': newstate
= 0; break;
416 disc
.major
= disc
.minor
= 0;
418 if (dv
== NULL
&& st
->devid
[i
])
419 dv
= map_dev(major(st
->devid
[i
]),
420 minor(st
->devid
[i
]), 1);
421 change
= newstate
^ st
->devstate
[i
];
422 if (st
->utime
&& change
&& !st
->err
) {
423 if (i
< array
.raid_disks
&&
424 (((newstate
&change
)&(1<<MD_DISK_FAULTY
)) ||
425 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_ACTIVE
)) ||
426 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
)))
428 alert("Fail", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
429 else if (i
>= array
.raid_disks
&&
430 (disc
.major
|| disc
.minor
) &&
431 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
) &&
432 ((newstate
&change
)&(1<<MD_DISK_FAULTY
))
434 alert("FailSpare", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
435 else if (i
< array
.raid_disks
&&
436 ! (newstate
& (1<<MD_DISK_REMOVED
)) &&
437 (((st
->devstate
[i
]&change
)&(1<<MD_DISK_FAULTY
)) ||
438 ((newstate
&change
)&(1<<MD_DISK_ACTIVE
)) ||
439 ((newstate
&change
)&(1<<MD_DISK_SYNC
)))
441 alert("SpareActive", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
443 st
->devstate
[i
] = newstate
;
444 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
446 st
->active
= array
.active_disks
;
447 st
->working
= array
.working_disks
;
448 st
->spare
= array
.spare_disks
;
449 st
->failed
= array
.failed_disks
;
450 st
->utime
= array
.utime
;
451 st
->raid
= array
.raid_disks
;
454 /* now check if there are any new devices found in mdstat */
456 struct mdstat_ent
*mse
;
457 for (mse
=mdstat
; mse
; mse
=mse
->next
)
458 if (mse
->devnum
!= INT_MAX
&&
459 (!mse
->level
|| /* retrieve containers */
460 (strcmp(mse
->level
, "raid0") != 0 &&
461 strcmp(mse
->level
, "linear") != 0))
463 struct state
*st
= malloc(sizeof *st
);
464 mdu_array_info_t array
;
468 st
->devname
= strdup(get_md_name(mse
->devnum
));
469 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
470 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
472 if (fd
>=0) close(fd
);
473 put_md_name(st
->devname
);
480 st
->next
= statelist
;
482 st
->devnum
= mse
->devnum
;
484 st
->spare_group
= NULL
;
485 st
->expected_spares
= -1;
488 alert("TestMessage", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
489 alert("NewArray", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
493 /* If an array has active < raid && spare == 0 && spare_group != NULL
494 * Look for another array with spare > 0 and active == raid and same spare_group
495 * if found, choose a device and hotremove/hotadd
497 if (share
) for (st
= statelist
; st
; st
=st
->next
)
498 if (st
->active
< st
->raid
&&
500 st
->spare_group
!= NULL
) {
502 for (st2
=statelist
; st2
; st2
=st2
->next
)
505 st2
->active
== st2
->raid
&&
506 st2
->spare_group
!= NULL
&&
507 strcmp(st
->spare_group
, st2
->spare_group
) == 0) {
508 /* try to remove and add */
509 int fd1
= open(st
->devname
, O_RDONLY
);
510 int fd2
= open(st2
->devname
, O_RDONLY
);
513 if (fd1
< 0 || fd2
< 0) {
514 if (fd1
>=0) close(fd1
);
515 if (fd2
>=0) close(fd2
);
518 for (d
=st2
->raid
; d
< MaxDisks
; d
++) {
519 if (st2
->devid
[d
] > 0 &&
520 st2
->devstate
[d
] == 0) {
526 struct mddev_dev devlist
;
531 devlist
.writemostly
= 0;
532 devlist
.devname
= devname
;
533 sprintf(devname
, "%d:%d", major(dev
), minor(dev
));
535 devlist
.disposition
= 'r';
536 if (Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1, 0) == 0) {
537 devlist
.disposition
= 'a';
538 if (Manage_subdevs(st
->devname
, fd1
, &devlist
, -1, 0) == 0) {
539 alert("MoveSpare", st
->devname
, st2
->devname
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
544 else Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1, 0);
565 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
, char *cmd
,
570 if (!cmd
&& !mailaddr
) {
571 time_t now
= time(0);
573 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
579 waitpid(pid
, NULL
, 0);
584 execl(cmd
, cmd
, event
, dev
, disc
, NULL
);
589 (strncmp(event
, "Fail", 4)==0 ||
590 strncmp(event
, "Test", 4)==0 ||
591 strncmp(event
, "Spares", 6)==0 ||
592 strncmp(event
, "Degrade", 7)==0)) {
593 FILE *mp
= popen(Sendmail
, "w");
597 gethostname(hname
, sizeof(hname
));
598 signal(SIGPIPE
, SIG_IGN
);
600 fprintf(mp
, "From: %s\n", mailfrom
);
602 fprintf(mp
, "From: " Name
" monitoring <root>\n");
603 fprintf(mp
, "To: %s\n", mailaddr
);
604 fprintf(mp
, "Subject: %s event on %s:%s\n\n", event
, dev
, hname
);
606 fprintf(mp
, "This is an automatically generated mail message from " Name
"\n");
607 fprintf(mp
, "running on %s\n\n", hname
);
609 fprintf(mp
, "A %s event had been detected on md device %s.\n\n", event
, dev
);
611 if (disc
&& disc
[0] != ' ')
612 fprintf(mp
, "It could be related to component device %s.\n\n", disc
);
613 if (disc
&& disc
[0] == ' ')
614 fprintf(mp
, "Extra information:%s.\n\n", disc
);
616 fprintf(mp
, "Faithfully yours, etc.\n");
618 mdstat
= fopen("/proc/mdstat", "r");
622 fprintf(mp
, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
623 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
624 n
=fwrite(buf
, 1, n
, mp
); /* yes, i don't care about the result */
632 /* log the event to syslog maybe */
634 /* Log at a different severity depending on the event.
636 * These are the critical events: */
637 if (strncmp(event
, "Fail", 4)==0 ||
638 strncmp(event
, "Degrade", 7)==0 ||
639 strncmp(event
, "DeviceDisappeared", 17)==0)
641 /* Good to know about, but are not failures: */
642 else if (strncmp(event
, "Rebuild", 7)==0 ||
643 strncmp(event
, "MoveSpare", 9)==0 ||
644 strncmp(event
, "Spares", 6) != 0)
645 priority
= LOG_WARNING
;
646 /* Everything else: */
651 syslog(priority
, "%s event detected on md device %s, component device %s", event
, dev
, disc
);
653 syslog(priority
, "%s event detected on md device %s", event
, dev
);
657 /* Not really Monitor but ... */
664 if (stat(dev
, &stb
) != 0) {
665 fprintf(stderr
, Name
": Cannot find %s: %s\n", dev
,
669 devnum
= stat2devnum(&stb
);
672 struct mdstat_ent
*ms
= mdstat_read(1, 0);
673 struct mdstat_ent
*e
;
675 for (e
=ms
; e
; e
=e
->next
)
676 if (e
->devnum
== devnum
)
679 if (!e
|| e
->percent
< 0) {
680 if (e
&& e
->metadata_version
&&
681 strncmp(e
->metadata_version
, "external:", 9) == 0) {
682 if (is_subarray(&e
->metadata_version
[9]))
683 ping_monitor(&e
->metadata_version
[9]);
685 ping_monitor(devnum2devname(devnum
));