]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
658ba9cbde5821bdbd330ed45d3c3f374630f640
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 /* The largest number of disks current arrays can manage is 384
34 * This really should be dynamically, but that will have to wait
35 * At least it isn't MD_SB_DISKS.
40 int devnum
; /* to sync with mdstat info */
44 int active
, working
, failed
, spare
, raid
;
46 int devstate
[MaxDisks
];
47 unsigned devid
[MaxDisks
];
49 int parent_dev
; /* For subarray, devnum of parent.
52 struct supertype
*metadata
;
53 struct state
*subarray
;/* for a container it is a link to first subarray
54 * for a subarray it is a link to next subarray
55 * in the same container */
56 struct state
*parent
; /* for a subarray it is a link to its container
61 static int make_daemon(char *pidfile
);
62 static int check_one_sharer(int scan
);
63 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
,
64 char *cmd
, int dosyslog
);
65 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
66 int test
, char *mailaddr
,
67 char *mailfrom
, char *alert_cmd
, int dosyslog
,
69 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
*statelist
,
70 int test
, char *mailaddr
, char *mailfrom
,
71 char *alert_cmd
, int dosyslog
);
72 static void try_spare_migration(struct state
*statelist
,
73 char *mailaddr
, char *mailfrom
,
74 char *alert_cmd
, int dosyslog
);
75 static void link_containers_with_subarrays(struct state
*list
);
77 int Monitor(struct mddev_dev
*devlist
,
78 char *mailaddr
, char *alert_cmd
,
79 int period
, int daemonise
, int scan
, int oneshot
,
80 int dosyslog
, int test
, char *pidfile
, int increments
,
84 * Every few seconds, scan every md device looking for changes
85 * When a change is found, log it, possibly run the alert command,
86 * and possibly send Email
88 * For each array, we record:
90 * active/working/failed/spare drives
91 * State of each device.
92 * %rebuilt if rebuilding
94 * If the update time changes, check out all the data again
95 * It is possible that we cannot get the state of each device
96 * due to bugs in the md kernel module.
97 * We also read /proc/mdstat to get rebuild percent,
98 * and to get state on all active devices incase of kernel bug.
102 * An active device had Faulty set or Active/Sync removed
104 * A spare device had Faulty set
106 * An active device had a reverse transition
108 * percent went from -1 to +ve
110 * percent went from below to not-below NN%
112 * Couldn't access a device which was previously visible
114 * if we detect an array with active<raid and spare==0
115 * we look at other arrays that have same spare-group
116 * If we find one with active==raid and spare>0,
117 * and if we can get_disk_info and find a name
118 * Then we hot-remove and hot-add to the other array
120 * If devlist is NULL, then we can monitor everything because --scan
121 * was given. We get an initial list from config file and add anything
122 * that appears in /proc/mdstat
125 struct state
*statelist
= NULL
;
127 struct mdstat_ent
*mdstat
= NULL
;
128 char *mailfrom
= NULL
;
131 mailaddr
= conf_get_mailaddr();
132 if (mailaddr
&& ! scan
)
133 fprintf(stderr
, Name
": Monitor using email address \"%s\" from config file\n",
136 mailfrom
= conf_get_mailfrom();
139 alert_cmd
= conf_get_program();
140 if (alert_cmd
&& ! scan
)
141 fprintf(stderr
, Name
": Monitor using program \"%s\" from config file\n",
144 if (scan
&& !mailaddr
&& !alert_cmd
) {
145 fprintf(stderr
, Name
": No mail address or alert command - not monitoring.\n");
150 if (make_daemon(pidfile
))
154 if (check_one_sharer(scan
))
157 if (devlist
== NULL
) {
158 struct mddev_ident
*mdlist
= conf_get_ident(NULL
);
159 for (; mdlist
; mdlist
=mdlist
->next
) {
161 if (mdlist
->devname
== NULL
)
163 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
165 st
= calloc(1, sizeof *st
);
168 if (mdlist
->devname
[0] == '/')
169 st
->devname
= strdup(mdlist
->devname
);
171 st
->devname
= malloc(8+strlen(mdlist
->devname
)+1);
172 strcpy(strcpy(st
->devname
, "/dev/md/"),
175 st
->next
= statelist
;
176 st
->devnum
= INT_MAX
;
178 st
->expected_spares
= mdlist
->spare_disks
;
179 if (mdlist
->spare_group
)
180 st
->spare_group
= strdup(mdlist
->spare_group
);
184 struct mddev_dev
*dv
;
185 for (dv
=devlist
; dv
; dv
=dv
->next
) {
186 struct mddev_ident
*mdlist
= conf_get_ident(dv
->devname
);
187 struct state
*st
= calloc(1, sizeof *st
);
190 st
->devname
= strdup(dv
->devname
);
191 st
->next
= statelist
;
192 st
->devnum
= INT_MAX
;
194 st
->expected_spares
= -1;
196 st
->expected_spares
= mdlist
->spare_disks
;
197 if (mdlist
->spare_group
)
198 st
->spare_group
= strdup(mdlist
->spare_group
);
212 mdstat
= mdstat_read(oneshot
?0:1, 0);
214 for (st
=statelist
; st
; st
=st
->next
)
215 if (check_array(st
, mdstat
, test
, mailaddr
, mailfrom
,
216 alert_cmd
, dosyslog
, increments
))
219 /* now check if there are any new devices found in mdstat */
221 new_found
= add_new_arrays(mdstat
, statelist
, test
,
222 mailaddr
, mailfrom
, alert_cmd
,
225 /* If an array has active < raid && spare == 0 && spare_group != NULL
226 * Look for another array with spare > 0 and active == raid and same spare_group
227 * if found, choose a device and hotremove/hotadd
229 if (share
&& anydegraded
)
230 try_spare_migration(statelist
, mailaddr
, mailfrom
,
231 alert_cmd
, dosyslog
);
245 static int make_daemon(char *pidfile
)
253 pid_file
=fopen(pidfile
, "w");
255 perror("cannot create pid file");
257 fprintf(pid_file
,"%d\n", pid
);
268 open("/dev/null", O_RDWR
);
275 static int check_one_sharer(int scan
)
281 fp
= fopen("/var/run/mdadm/autorebuild.pid", "r");
283 fscanf(fp
, "%d", &pid
);
284 sprintf(dir
, "/proc/%d", pid
);
285 rv
= stat(dir
, &buf
);
288 fprintf(stderr
, Name
": Only one "
289 "autorebuild process allowed"
290 " in scan mode, aborting\n");
294 fprintf(stderr
, Name
": Warning: One"
295 " autorebuild process already"
302 fp
= fopen("/var/run/mdadm/autorebuild.pid", "w");
304 fprintf(stderr
, Name
": Cannot create"
309 fprintf(fp
, "%d\n", pid
);
316 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
, char *cmd
,
321 if (!cmd
&& !mailaddr
) {
322 time_t now
= time(0);
324 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
330 waitpid(pid
, NULL
, 0);
335 execl(cmd
, cmd
, event
, dev
, disc
, NULL
);
340 (strncmp(event
, "Fail", 4)==0 ||
341 strncmp(event
, "Test", 4)==0 ||
342 strncmp(event
, "Spares", 6)==0 ||
343 strncmp(event
, "Degrade", 7)==0)) {
344 FILE *mp
= popen(Sendmail
, "w");
348 gethostname(hname
, sizeof(hname
));
349 signal(SIGPIPE
, SIG_IGN
);
351 fprintf(mp
, "From: %s\n", mailfrom
);
353 fprintf(mp
, "From: " Name
" monitoring <root>\n");
354 fprintf(mp
, "To: %s\n", mailaddr
);
355 fprintf(mp
, "Subject: %s event on %s:%s\n\n", event
, dev
, hname
);
357 fprintf(mp
, "This is an automatically generated mail message from " Name
"\n");
358 fprintf(mp
, "running on %s\n\n", hname
);
360 fprintf(mp
, "A %s event had been detected on md device %s.\n\n", event
, dev
);
362 if (disc
&& disc
[0] != ' ')
363 fprintf(mp
, "It could be related to component device %s.\n\n", disc
);
364 if (disc
&& disc
[0] == ' ')
365 fprintf(mp
, "Extra information:%s.\n\n", disc
);
367 fprintf(mp
, "Faithfully yours, etc.\n");
369 mdstat
= fopen("/proc/mdstat", "r");
373 fprintf(mp
, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
374 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
375 n
=fwrite(buf
, 1, n
, mp
); /* yes, i don't care about the result */
383 /* log the event to syslog maybe */
385 /* Log at a different severity depending on the event.
387 * These are the critical events: */
388 if (strncmp(event
, "Fail", 4)==0 ||
389 strncmp(event
, "Degrade", 7)==0 ||
390 strncmp(event
, "DeviceDisappeared", 17)==0)
392 /* Good to know about, but are not failures: */
393 else if (strncmp(event
, "Rebuild", 7)==0 ||
394 strncmp(event
, "MoveSpare", 9)==0 ||
395 strncmp(event
, "Spares", 6) != 0)
396 priority
= LOG_WARNING
;
397 /* Everything else: */
402 syslog(priority
, "%s event detected on md device %s, component device %s", event
, dev
, disc
);
404 syslog(priority
, "%s event detected on md device %s", event
, dev
);
408 static int check_array(struct state
*st
, struct mdstat_ent
*mdstat
,
409 int test
, char *mailaddr
,
410 char *mailfrom
, char *alert_cmd
, int dosyslog
,
413 struct { int state
, major
, minor
; } info
[MaxDisks
];
414 mdu_array_info_t array
;
415 struct mdstat_ent
*mse
= NULL
, *mse2
;
416 char *dev
= st
->devname
;
421 alert("TestMessage", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
422 fd
= open(dev
, O_RDONLY
);
425 alert("DeviceDisappeared", dev
, NULL
,
426 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
427 /* fprintf(stderr, Name ": cannot open %s: %s\n",
428 dev, strerror(errno));
432 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
433 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
435 alert("DeviceDisappeared", dev
, NULL
,
436 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
437 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
438 dev, strerror(errno));
443 /* It's much easier to list what array levels can't
444 * have a device disappear than all of them that can
446 if (array
.level
== 0 || array
.level
== -1) {
448 alert("DeviceDisappeared", dev
, "Wrong-Level",
449 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
454 if (st
->devnum
== INT_MAX
) {
456 if (fstat(fd
, &stb
) == 0 &&
457 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
458 if (major(stb
.st_rdev
) == MD_MAJOR
)
459 st
->devnum
= minor(stb
.st_rdev
);
461 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
465 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
466 if (mse2
->devnum
== st
->devnum
) {
467 mse2
->devnum
= INT_MAX
; /* flag it as "used" */
472 /* duplicated array in statelist
473 * or re-created after reading mdstat*/
478 /* this array is in /proc/mdstat */
479 if (array
.utime
== 0)
480 /* external arrays don't update utime, so
481 * just make sure it is always different. */
482 array
.utime
= st
->utime
+ 1;;
484 if (st
->utime
== array
.utime
&&
485 st
->failed
== array
.failed_disks
&&
486 st
->working
== array
.working_disks
&&
487 st
->spare
== array
.spare_disks
&&
489 mse
->percent
== st
->percent
495 if (st
->utime
== 0 && /* new array */
496 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
498 alert("DegradedArray", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
500 if (st
->utime
== 0 && /* new array */
501 st
->expected_spares
> 0 &&
502 array
.spare_disks
< st
->expected_spares
)
503 alert("SparesMissing", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
504 if (st
->percent
== -1 &&
506 alert("RebuildStarted", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
507 if (st
->percent
>= 0 &&
509 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
510 char percentalert
[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
512 if((mse
->percent
/ increments
) == 0)
513 snprintf(percentalert
, sizeof(percentalert
), "RebuildStarted");
515 snprintf(percentalert
, sizeof(percentalert
), "Rebuild%02d", mse
->percent
);
518 dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
521 if (mse
->percent
== -1 &&
523 /* Rebuild/sync/whatever just finished.
524 * If there is a number in /mismatch_cnt,
525 * we should report that.
528 sysfs_read(-1, st
->devnum
, GET_MISMATCH
);
529 if (sra
&& sra
->mismatch_cnt
> 0) {
531 sprintf(cnt
, " mismatches found: %d", sra
->mismatch_cnt
);
532 alert("RebuildFinished", dev
, cnt
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
534 alert("RebuildFinished", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
538 st
->percent
= mse
->percent
;
540 for (i
=0; i
<MaxDisks
&& i
<= array
.raid_disks
+ array
.nr_disks
;
542 mdu_disk_info_t disc
;
544 if (ioctl(fd
, GET_DISK_INFO
, &disc
) >= 0) {
545 info
[i
].state
= disc
.state
;
546 info
[i
].major
= disc
.major
;
547 info
[i
].minor
= disc
.minor
;
549 info
[i
].major
= info
[i
].minor
= 0;
552 if (strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
553 is_subarray(mse
->metadata_version
+9))
555 devname2devnum(mse
->metadata_version
+10);
557 st
->parent_dev
= NoMdDev
;
558 if (st
->metadata
== NULL
&&
559 st
->parent_dev
== NoMdDev
)
560 st
->metadata
= super_by_fd(fd
, NULL
);
564 for (i
=0; i
<MaxDisks
; i
++) {
565 mdu_disk_info_t disc
= {0,0,0,0,0};
570 if (i
> array
.raid_disks
+ array
.nr_disks
) {
572 disc
.major
= disc
.minor
= 0;
573 } else if (info
[i
].major
|| info
[i
].minor
) {
574 newstate
= info
[i
].state
;
575 dv
= map_dev(info
[i
].major
, info
[i
].minor
, 1);
576 disc
.state
= newstate
;
577 disc
.major
= info
[i
].major
;
578 disc
.minor
= info
[i
].minor
;
579 } else if (mse
&& mse
->pattern
&& i
< (int)strlen(mse
->pattern
)) {
580 switch(mse
->pattern
[i
]) {
581 case 'U': newstate
= 6 /* ACTIVE/SYNC */; break;
582 case '_': newstate
= 0; break;
584 disc
.major
= disc
.minor
= 0;
586 if (dv
== NULL
&& st
->devid
[i
])
587 dv
= map_dev(major(st
->devid
[i
]),
588 minor(st
->devid
[i
]), 1);
589 change
= newstate
^ st
->devstate
[i
];
590 if (st
->utime
&& change
&& !st
->err
) {
591 if (i
< array
.raid_disks
&&
592 (((newstate
&change
)&(1<<MD_DISK_FAULTY
)) ||
593 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_ACTIVE
)) ||
594 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
)))
596 alert("Fail", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
597 else if (i
>= array
.raid_disks
&&
598 (disc
.major
|| disc
.minor
) &&
599 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
) &&
600 ((newstate
&change
)&(1<<MD_DISK_FAULTY
))
602 alert("FailSpare", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
603 else if (i
< array
.raid_disks
&&
604 ! (newstate
& (1<<MD_DISK_REMOVED
)) &&
605 (((st
->devstate
[i
]&change
)&(1<<MD_DISK_FAULTY
)) ||
606 ((newstate
&change
)&(1<<MD_DISK_ACTIVE
)) ||
607 ((newstate
&change
)&(1<<MD_DISK_SYNC
)))
609 alert("SpareActive", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
611 st
->devstate
[i
] = newstate
;
612 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
614 st
->active
= array
.active_disks
;
615 st
->working
= array
.working_disks
;
616 st
->spare
= array
.spare_disks
;
617 st
->failed
= array
.failed_disks
;
618 st
->utime
= array
.utime
;
619 st
->raid
= array
.raid_disks
;
621 if ((st
->active
< st
->raid
) && st
->spare
== 0)
626 static int add_new_arrays(struct mdstat_ent
*mdstat
, struct state
*statelist
,
627 int test
, char *mailaddr
, char *mailfrom
,
628 char *alert_cmd
, int dosyslog
)
630 struct mdstat_ent
*mse
;
633 for (mse
=mdstat
; mse
; mse
=mse
->next
)
634 if (mse
->devnum
!= INT_MAX
&&
635 (!mse
->level
|| /* retrieve containers */
636 (strcmp(mse
->level
, "raid0") != 0 &&
637 strcmp(mse
->level
, "linear") != 0))
639 struct state
*st
= calloc(1, sizeof *st
);
640 mdu_array_info_t array
;
644 st
->devname
= strdup(get_md_name(mse
->devnum
));
645 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
646 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
648 if (fd
>=0) close(fd
);
649 put_md_name(st
->devname
);
652 st
->metadata
->ss
->free_super(st
->metadata
);
659 st
->next
= statelist
;
661 st
->devnum
= mse
->devnum
;
663 st
->expected_spares
= -1;
664 if (strncmp(mse
->metadata_version
, "external:", 9) == 0 &&
665 is_subarray(mse
->metadata_version
+9))
667 devname2devnum(mse
->metadata_version
+10);
669 st
->parent_dev
= NoMdDev
;
672 alert("TestMessage", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
673 alert("NewArray", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
679 static void try_spare_migration(struct state
*statelist
,
680 char *mailaddr
, char *mailfrom
,
681 char *alert_cmd
, int dosyslog
)
685 link_containers_with_subarrays(statelist
);
686 for (st
= statelist
; st
; st
=st
->next
)
687 if (st
->active
< st
->raid
&&
689 st
->spare_group
!= NULL
) {
691 for (st2
=statelist
; st2
; st2
=st2
->next
)
694 st2
->active
== st2
->raid
&&
695 st2
->spare_group
!= NULL
&&
696 strcmp(st
->spare_group
, st2
->spare_group
) == 0) {
697 /* try to remove and add */
698 int fd1
= open(st
->devname
, O_RDONLY
);
699 int fd2
= open(st2
->devname
, O_RDONLY
);
702 if (fd1
< 0 || fd2
< 0) {
703 if (fd1
>=0) close(fd1
);
704 if (fd2
>=0) close(fd2
);
707 for (d
=st2
->raid
; d
< MaxDisks
; d
++) {
708 if (st2
->devid
[d
] > 0 &&
709 st2
->devstate
[d
] == 0) {
715 struct mddev_dev devlist
;
720 devlist
.writemostly
= 0;
721 devlist
.devname
= devname
;
722 sprintf(devname
, "%d:%d", major(dev
), minor(dev
));
724 devlist
.disposition
= 'r';
725 if (Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1, 0) == 0) {
726 devlist
.disposition
= 'a';
727 if (Manage_subdevs(st
->devname
, fd1
, &devlist
, -1, 0) == 0) {
728 alert("MoveSpare", st
->devname
, st2
->devname
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
733 else Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1, 0);
742 /* search the statelist to connect external
743 * metadata subarrays with their containers
744 * We always completely rebuild the tree from scratch as
745 * that is safest considering the possibility of entries
746 * disappearing or changing.
748 static void link_containers_with_subarrays(struct state
*list
)
752 for (st
= list
; st
; st
= st
->next
) {
756 for (st
= list
; st
; st
= st
->next
)
757 if (st
->parent_dev
!= NoMdDev
)
758 for (cont
= list
; cont
; cont
= cont
->next
)
760 cont
->parent_dev
== NoMdDev
&&
761 cont
->devnum
== st
->parent_dev
) {
763 st
->subarray
= cont
->subarray
;
769 /* Not really Monitor but ... */
776 if (stat(dev
, &stb
) != 0) {
777 fprintf(stderr
, Name
": Cannot find %s: %s\n", dev
,
781 devnum
= stat2devnum(&stb
);
784 struct mdstat_ent
*ms
= mdstat_read(1, 0);
785 struct mdstat_ent
*e
;
787 for (e
=ms
; e
; e
=e
->next
)
788 if (e
->devnum
== devnum
)
791 if (!e
|| e
->percent
< 0) {
792 if (e
&& e
->metadata_version
&&
793 strncmp(e
->metadata_version
, "external:", 9) == 0) {
794 if (is_subarray(&e
->metadata_version
[9]))
795 ping_monitor(&e
->metadata_version
[9]);
797 ping_monitor(devnum2devname(devnum
));