]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
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(mddev_dev_t 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
)
47 * Every few seconds, scan every md device looking for changes
48 * When a change is found, log it, possibly run the alert command,
49 * and possibly send Email
51 * For each array, we record:
53 * active/working/failed/spare drives
54 * State of each device.
55 * %rebuilt if rebuilding
57 * If the update time changes, check out all the data again
58 * It is possible that we cannot get the state of each device
59 * due to bugs in the md kernel module.
60 * We also read /proc/mdstat to get rebuild percent,
61 * and to get state on all active devices incase of kernel bug.
65 * An active device had Faulty set or Active/Sync removed
67 * A spare device had Faulty set
69 * An active device had a reverse transition
71 * percent went from -1 to +ve
73 * percent went from below to not-below NN%
75 * Couldn't access a device which was previously visible
77 * if we detect an array with active<raid and spare==0
78 * we look at other arrays that have same spare-group
79 * If we find one with active==raid and spare>0,
80 * and if we can get_disk_info and find a name
81 * Then we hot-remove and hot-add to the other array
83 * If devlist is NULL, then we can monitor everything because --scan
84 * was given. We get an initial list from config file and add anything
85 * that appears in /proc/mdstat
90 int devnum
; /* to sync with mdstat info */
94 int active
, working
, failed
, spare
, raid
;
96 int devstate
[MaxDisks
];
102 struct mdstat_ent
*mdstat
= NULL
;
103 char *mailfrom
= NULL
;
106 mailaddr
= conf_get_mailaddr();
107 if (mailaddr
&& ! scan
)
108 fprintf(stderr
, Name
": Monitor using email address \"%s\" from config file\n",
111 mailfrom
= conf_get_mailfrom();
114 alert_cmd
= conf_get_program();
115 if (alert_cmd
&& ! scan
)
116 fprintf(stderr
, Name
": Monitor using program \"%s\" from config file\n",
119 if (scan
&& !mailaddr
&& !alert_cmd
) {
120 fprintf(stderr
, Name
": No mail address or alert command - not monitoring.\n");
131 pid_file
=fopen(pidfile
, "w");
133 perror("cannot create pid file");
135 fprintf(pid_file
,"%d\n", pid
);
146 open("/dev/null", O_RDWR
);
152 if (devlist
== NULL
) {
153 mddev_ident_t mdlist
= conf_get_ident(NULL
);
154 for (; mdlist
; mdlist
=mdlist
->next
) {
156 if (mdlist
->devname
== NULL
)
158 if (strcasecmp(mdlist
->devname
, "<ignore>") == 0)
160 st
= malloc(sizeof *st
);
163 if (mdlist
->devname
[0] == '/')
164 st
->devname
= strdup(mdlist
->devname
);
166 st
->devname
= malloc(8+strlen(mdlist
->devname
)+1);
167 strcpy(strcpy(st
->devname
, "/dev/md/"),
171 st
->next
= statelist
;
173 st
->devnum
= INT_MAX
;
175 st
->expected_spares
= mdlist
->spare_disks
;
176 if (mdlist
->spare_group
)
177 st
->spare_group
= strdup(mdlist
->spare_group
);
179 st
->spare_group
= NULL
;
184 for (dv
=devlist
; dv
; dv
=dv
->next
) {
185 mddev_ident_t mdlist
= conf_get_ident(dv
->devname
);
186 struct state
*st
= malloc(sizeof *st
);
189 st
->devname
= strdup(dv
->devname
);
191 st
->next
= statelist
;
193 st
->devnum
= INT_MAX
;
195 st
->expected_spares
= -1;
196 st
->spare_group
= NULL
;
198 st
->expected_spares
= mdlist
->spare_disks
;
199 if (mdlist
->spare_group
)
200 st
->spare_group
= strdup(mdlist
->spare_group
);
213 mdstat
= mdstat_read(oneshot
?0:1, 0);
215 for (st
=statelist
; st
; st
=st
->next
) {
216 struct { int state
, major
, minor
; } info
[MaxDisks
];
217 mdu_array_info_t array
;
218 struct mdstat_ent
*mse
= NULL
, *mse2
;
219 char *dev
= st
->devname
;
224 alert("TestMessage", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
225 fd
= open(dev
, O_RDONLY
);
228 alert("DeviceDisappeared", dev
, NULL
,
229 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
230 /* fprintf(stderr, Name ": cannot open %s: %s\n",
231 dev, strerror(errno));
235 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
236 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
238 alert("DeviceDisappeared", dev
, NULL
,
239 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
240 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
241 dev, strerror(errno));
246 /* It's much easier to list what array levels can't
247 * have a device disappear than all of them that can
249 if (array
.level
== 0 || array
.level
== -1) {
251 alert("DeviceDisappeared", dev
, "Wrong-Level",
252 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
257 if (st
->devnum
== INT_MAX
) {
259 if (fstat(fd
, &stb
) == 0 &&
260 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
261 if (major(stb
.st_rdev
) == MD_MAJOR
)
262 st
->devnum
= minor(stb
.st_rdev
);
264 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
268 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
269 if (mse2
->devnum
== st
->devnum
) {
270 mse2
->devnum
= INT_MAX
; /* flag it as "used" */
274 if (array
.utime
== 0)
275 /* external arrays don't update utime */
276 array
.utime
= time(0);
278 if (st
->utime
== array
.utime
&&
279 st
->failed
== array
.failed_disks
&&
280 st
->working
== array
.working_disks
&&
281 st
->spare
== array
.spare_disks
&&
283 mse
->percent
== st
->percent
289 if (st
->utime
== 0 && /* new array */
290 mse
&& /* is in /proc/mdstat */
291 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
293 alert("DegradedArray", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
295 if (st
->utime
== 0 && /* new array */
296 st
->expected_spares
> 0 &&
297 array
.spare_disks
< st
->expected_spares
)
298 alert("SparesMissing", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
302 alert("RebuildStarted", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
306 (mse
->percent
/ increments
) > (st
->percent
/ increments
)) {
307 char percentalert
[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
309 if((mse
->percent
/ increments
) == 0)
310 snprintf(percentalert
, sizeof(percentalert
), "RebuildStarted");
312 snprintf(percentalert
, sizeof(percentalert
), "Rebuild%02d", mse
->percent
);
315 dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
319 mse
->percent
== -1 &&
321 /* Rebuild/sync/whatever just finished.
322 * If there is a number in /mismatch_cnt,
323 * we should report that.
326 sysfs_read(-1, st
->devnum
, GET_MISMATCH
);
327 if (sra
&& sra
->mismatch_cnt
> 0) {
329 sprintf(cnt
, " mismatches found: %d", sra
->mismatch_cnt
);
330 alert("RebuildFinished", dev
, cnt
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
332 alert("RebuildFinished", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
338 st
->percent
= mse
->percent
;
341 for (i
=0; i
<MaxDisks
&& i
<= array
.raid_disks
+ array
.nr_disks
;
343 mdu_disk_info_t disc
;
345 if (ioctl(fd
, GET_DISK_INFO
, &disc
) >= 0) {
346 info
[i
].state
= disc
.state
;
347 info
[i
].major
= disc
.major
;
348 info
[i
].minor
= disc
.minor
;
350 info
[i
].major
= info
[i
].minor
= 0;
354 for (i
=0; i
<MaxDisks
; i
++) {
355 mdu_disk_info_t disc
= {0};
360 if (i
> array
.raid_disks
+ array
.nr_disks
) {
362 disc
.major
= disc
.minor
= 0;
363 } else if (info
[i
].major
|| info
[i
].minor
) {
364 newstate
= info
[i
].state
;
365 dv
= map_dev(info
[i
].major
, info
[i
].minor
, 1);
366 disc
.state
= newstate
;
367 disc
.major
= info
[i
].major
;
368 disc
.minor
= info
[i
].minor
;
369 } else if (mse
&& mse
->pattern
&& i
< strlen(mse
->pattern
)) {
370 switch(mse
->pattern
[i
]) {
371 case 'U': newstate
= 6 /* ACTIVE/SYNC */; break;
372 case '_': newstate
= 0; break;
374 disc
.major
= disc
.minor
= 0;
376 if (dv
== NULL
&& st
->devid
[i
])
377 dv
= map_dev(major(st
->devid
[i
]),
378 minor(st
->devid
[i
]), 1);
379 change
= newstate
^ st
->devstate
[i
];
380 if (st
->utime
&& change
&& !st
->err
) {
381 if (i
< (unsigned)array
.raid_disks
&&
382 (((newstate
&change
)&(1<<MD_DISK_FAULTY
)) ||
383 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_ACTIVE
)) ||
384 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
)))
386 alert("Fail", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
387 else if (i
>= (unsigned)array
.raid_disks
&&
388 (disc
.major
|| disc
.minor
) &&
389 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
) &&
390 ((newstate
&change
)&(1<<MD_DISK_FAULTY
))
392 alert("FailSpare", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
393 else if (i
< (unsigned)array
.raid_disks
&&
394 ! (newstate
& (1<<MD_DISK_REMOVED
)) &&
395 (((st
->devstate
[i
]&change
)&(1<<MD_DISK_FAULTY
)) ||
396 ((newstate
&change
)&(1<<MD_DISK_ACTIVE
)) ||
397 ((newstate
&change
)&(1<<MD_DISK_SYNC
)))
399 alert("SpareActive", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
401 st
->devstate
[i
] = newstate
;
402 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
404 st
->active
= array
.active_disks
;
405 st
->working
= array
.working_disks
;
406 st
->spare
= array
.spare_disks
;
407 st
->failed
= array
.failed_disks
;
408 st
->utime
= array
.utime
;
409 st
->raid
= array
.raid_disks
;
412 /* now check if there are any new devices found in mdstat */
414 struct mdstat_ent
*mse
;
415 for (mse
=mdstat
; mse
; mse
=mse
->next
)
416 if (mse
->devnum
!= INT_MAX
&&
418 (strcmp(mse
->level
, "raid0")!=0 &&
419 strcmp(mse
->level
, "linear")!=0)
421 struct state
*st
= malloc(sizeof *st
);
422 mdu_array_info_t array
;
426 st
->devname
= strdup(get_md_name(mse
->devnum
));
427 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
428 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
430 if (fd
>=0) close(fd
);
431 put_md_name(st
->devname
);
438 st
->next
= statelist
;
440 st
->devnum
= mse
->devnum
;
442 st
->spare_group
= NULL
;
443 st
->expected_spares
= -1;
446 alert("TestMessage", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
447 alert("NewArray", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
451 /* If an array has active < raid && spare == 0 && spare_group != NULL
452 * Look for another array with spare > 0 and active == raid and same spare_group
453 * if found, choose a device and hotremove/hotadd
455 for (st
= statelist
; st
; st
=st
->next
)
456 if (st
->active
< st
->raid
&&
458 st
->spare_group
!= NULL
) {
460 for (st2
=statelist
; st2
; st2
=st2
->next
)
463 st2
->active
== st2
->raid
&&
464 st2
->spare_group
!= NULL
&&
465 strcmp(st
->spare_group
, st2
->spare_group
) == 0) {
466 /* try to remove and add */
467 int fd1
= open(st
->devname
, O_RDONLY
);
468 int fd2
= open(st2
->devname
, O_RDONLY
);
471 if (fd1
< 0 || fd2
< 0) {
472 if (fd1
>=0) close(fd1
);
473 if (fd2
>=0) close(fd2
);
476 for (d
=st2
->raid
; d
< MaxDisks
; d
++) {
477 if (st2
->devid
[d
] > 0 &&
478 st2
->devstate
[d
] == 0) {
484 struct mddev_dev_s devlist
;
489 devlist
.writemostly
= 0;
490 devlist
.devname
= devname
;
491 sprintf(devname
, "%d:%d", major(dev
), minor(dev
));
493 devlist
.disposition
= 'r';
494 if (Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1) == 0) {
495 devlist
.disposition
= 'a';
496 if (Manage_subdevs(st
->devname
, fd1
, &devlist
, -1) == 0) {
497 alert("MoveSpare", st
->devname
, st2
->devname
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
502 else Manage_subdevs(st2
->devname
, fd2
, &devlist
, -1);
523 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
, char *cmd
,
528 if (!cmd
&& !mailaddr
) {
529 time_t now
= time(0);
531 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
537 waitpid(pid
, NULL
, 0);
542 execl(cmd
, cmd
, event
, dev
, disc
, NULL
);
547 (strncmp(event
, "Fail", 4)==0 ||
548 strncmp(event
, "Test", 4)==0 ||
549 strncmp(event
, "Spares", 6)==0 ||
550 strncmp(event
, "Degrade", 7)==0)) {
551 FILE *mp
= popen(Sendmail
, "w");
555 gethostname(hname
, sizeof(hname
));
556 signal(SIGPIPE
, SIG_IGN
);
558 fprintf(mp
, "From: %s\n", mailfrom
);
560 fprintf(mp
, "From: " Name
" monitoring <root>\n");
561 fprintf(mp
, "To: %s\n", mailaddr
);
562 fprintf(mp
, "Subject: %s event on %s:%s\n\n", event
, dev
, hname
);
564 fprintf(mp
, "This is an automatically generated mail message from " Name
"\n");
565 fprintf(mp
, "running on %s\n\n", hname
);
567 fprintf(mp
, "A %s event had been detected on md device %s.\n\n", event
, dev
);
569 if (disc
&& disc
[0] != ' ')
570 fprintf(mp
, "It could be related to component device %s.\n\n", disc
);
571 if (disc
&& disc
[0] == ' ')
572 fprintf(mp
, "Extra information:%s.\n\n", disc
);
574 fprintf(mp
, "Faithfully yours, etc.\n");
576 mdstat
= fopen("/proc/mdstat", "r");
580 fprintf(mp
, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
581 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
582 n
=fwrite(buf
, 1, n
, mp
); /* yes, i don't care about the result */
590 /* log the event to syslog maybe */
592 /* Log at a different severity depending on the event.
594 * These are the critical events: */
595 if (strncmp(event
, "Fail", 4)==0 ||
596 strncmp(event
, "Degrade", 7)==0 ||
597 strncmp(event
, "DeviceDisappeared", 17)==0)
599 /* Good to know about, but are not failures: */
600 else if (strncmp(event
, "Rebuild", 7)==0 ||
601 strncmp(event
, "MoveSpare", 9)==0 ||
602 strncmp(event
, "Spares", 6) != 0)
603 priority
= LOG_WARNING
;
604 /* Everything else: */
609 syslog(priority
, "%s event detected on md device %s, component device %s", event
, dev
, disc
);
611 syslog(priority
, "%s event detected on md device %s", event
, dev
);
615 /* Not really Monitor but ... */
622 if (stat(dev
, &stb
) != 0) {
623 fprintf(stderr
, Name
": Cannot find %s: %s\n", dev
,
627 devnum
= stat2devnum(&stb
);
630 struct mdstat_ent
*ms
= mdstat_read(1, 0);
631 struct mdstat_ent
*e
;
633 for (e
=ms
; e
; e
=e
->next
)
634 if (e
->devnum
== devnum
)
637 if (!e
|| e
->percent
< 0) {
638 if (e
&& e
->metadata_version
&&
639 strncmp(e
->metadata_version
, "external:", 9) == 0) {
640 if (is_subarray(&e
->metadata_version
[9]))
641 ping_monitor(&e
->metadata_version
[9]);
643 ping_monitor(devnum2devname(devnum
));