]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
f7bb48ce96986ae187f94ed0e667a775d23b0f94
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
34 #include <sys/signal.h>
38 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
,
39 char *cmd
, int dosyslog
);
41 static char *percentalerts
[] = {
49 /* The largest number of disks current arrays can manage is 384
50 * This really should be dynamically, but that will have to wait
51 * At least it isn't MD_SB_DISKS.
54 int Monitor(mddev_dev_t devlist
,
55 char *mailaddr
, char *alert_cmd
,
56 int period
, int daemonise
, int scan
, int oneshot
,
57 int dosyslog
, int test
, char* pidfile
)
60 * Every few seconds, scan every md device looking for changes
61 * When a change is found, log it, possibly run the alert command,
62 * and possibly send Email
64 * For each array, we record:
66 * active/working/failed/spare drives
67 * State of each device.
68 * %rebuilt if rebuilding
70 * If the update time changes, check out all the data again
71 * It is possible that we cannot get the state of each device
72 * due to bugs in the md kernel module.
73 * We also read /proc/mdstat to get rebuild percent,
74 * and to get state on all active devices incase of kernel bug.
78 * An active device had Faulty set or Active/Sync removed
80 * A spare device had Faulty set
82 * An active device had a reverse transition
84 * percent went from -1 to +ve
85 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
86 * percent went from below to not-below that number
88 * Couldn't access a device which was previously visible
90 * if we detect an array with active<raid and spare==0
91 * we look at other arrays that have same spare-group
92 * If we find one with active==raid and spare>0,
93 * and if we can get_disk_info and find a name
94 * Then we hot-remove and hot-add to the other array
96 * If devlist is NULL, then we can monitor everything because --scan
97 * was given. We get an initial list from config file and add anything
98 * that appears in /proc/mdstat
103 int devnum
; /* to sync with mdstat info */
107 int active
, working
, failed
, spare
, raid
;
109 int devstate
[MaxDisks
];
115 struct mdstat_ent
*mdstat
= NULL
;
116 char *mailfrom
= NULL
;
119 mailaddr
= conf_get_mailaddr();
120 if (mailaddr
&& ! scan
)
121 fprintf(stderr
, Name
": Monitor using email address \"%s\" from config file\n",
124 mailfrom
= conf_get_mailfrom();
127 alert_cmd
= conf_get_program();
128 if (alert_cmd
&& ! scan
)
129 fprintf(stderr
, Name
": Monitor using program \"%s\" from config file\n",
132 if (scan
&& !mailaddr
&& !alert_cmd
) {
133 fprintf(stderr
, Name
": No mail address or alert command - not monitoring.\n");
144 pid_file
=fopen(pidfile
, "w");
146 perror("cannot create pid file");
148 fprintf(pid_file
,"%d\n", pid
);
159 open("/dev/null", 3);
165 if (devlist
== NULL
) {
166 mddev_ident_t mdlist
= conf_get_ident(NULL
);
167 for (; mdlist
; mdlist
=mdlist
->next
) {
168 struct state
*st
= malloc(sizeof *st
);
171 st
->devname
= strdup(mdlist
->devname
);
173 st
->next
= statelist
;
177 st
->expected_spares
= mdlist
->spare_disks
;
178 if (mdlist
->spare_group
)
179 st
->spare_group
= strdup(mdlist
->spare_group
);
181 st
->spare_group
= NULL
;
186 for (dv
=devlist
; dv
; dv
=dv
->next
) {
187 mddev_ident_t mdlist
= conf_get_ident(dv
->devname
);
188 struct state
*st
= malloc(sizeof *st
);
191 st
->devname
= strdup(dv
->devname
);
193 st
->next
= statelist
;
197 st
->expected_spares
= -1;
198 st
->spare_group
= NULL
;
200 st
->expected_spares
= mdlist
->spare_disks
;
201 if (mdlist
->spare_group
)
202 st
->spare_group
= strdup(mdlist
->spare_group
);
215 mdstat
= mdstat_read(oneshot
?0:1, 0);
217 for (st
=statelist
; st
; st
=st
->next
) {
218 mdu_array_info_t array
;
219 struct mdstat_ent
*mse
= NULL
, *mse2
;
220 char *dev
= st
->devname
;
225 alert("TestMessage", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
226 fd
= open(dev
, O_RDONLY
);
229 alert("DeviceDisappeared", dev
, NULL
,
230 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
231 /* fprintf(stderr, Name ": cannot open %s: %s\n",
232 dev, strerror(errno));
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 if (array
.level
!= 1 && array
.level
!= 5 && array
.level
!= -4 &&
247 array
.level
!= 6 && array
.level
!= 10) {
249 alert("DeviceDisappeared", dev
, "Wrong-Level",
250 mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
255 if (st
->devnum
== MAXINT
) {
257 if (fstat(fd
, &stb
) == 0 &&
258 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
259 if (major(stb
.st_rdev
) == MD_MAJOR
)
260 st
->devnum
= minor(stb
.st_rdev
);
262 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
266 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
267 if (mse2
->devnum
== st
->devnum
) {
268 mse2
->devnum
= MAXINT
; /* flag it as "used" */
272 if (st
->utime
== array
.utime
&&
273 st
->failed
== array
.failed_disks
&&
274 st
->working
== array
.working_disks
&&
275 st
->spare
== array
.spare_disks
&&
277 mse
->percent
== st
->percent
283 if (st
->utime
== 0 && /* new array */
284 mse
&& /* is in /proc/mdstat */
285 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
287 alert("DegradedArray", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
289 if (st
->utime
== 0 && /* new array */
290 st
->expected_spares
> 0 &&
291 array
.spare_disks
< st
->expected_spares
)
292 alert("SparesMissing", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
296 alert("RebuildStarted", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
300 (mse
->percent
/ 20) > (st
->percent
/ 20))
301 alert(percentalerts
[mse
->percent
/20],
302 dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
305 mse
->percent
== -1 &&
307 /* Rebuild/sync/whatever just finished.
308 * If there is a number in /mismatch_cnt,
309 * we should report that.
311 struct sysarray
*sra
=
312 sysfs_read(-1, st
->devnum
, GET_MISMATCH
);
313 if (sra
&& sra
->mismatch_cnt
> 0) {
315 sprintf(cnt
, " mismatches found: %d", sra
->mismatch_cnt
);
316 alert("RebuildFinished", dev
, cnt
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
318 alert("RebuildFinished", dev
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
324 st
->percent
= mse
->percent
;
326 for (i
=0; i
<MaxDisks
; i
++) {
327 mdu_disk_info_t disc
;
332 if (i
> array
.raid_disks
+ array
.nr_disks
) {
334 disc
.major
= disc
.minor
= 0;
335 } else if (ioctl(fd
, GET_DISK_INFO
, &disc
)>= 0) {
336 newstate
= disc
.state
;
337 dv
= map_dev(disc
.major
, disc
.minor
, 1);
338 } else if (mse
&& mse
->pattern
&& i
< strlen(mse
->pattern
)) {
339 switch(mse
->pattern
[i
]) {
340 case 'U': newstate
= 6 /* ACTIVE/SYNC */; break;
341 case '_': newstate
= 0; break;
343 disc
.major
= disc
.minor
= 0;
345 if (dv
== NULL
&& st
->devid
[i
])
346 dv
= map_dev(major(st
->devid
[i
]),
347 minor(st
->devid
[i
]), 1);
348 change
= newstate
^ st
->devstate
[i
];
349 if (st
->utime
&& change
&& !st
->err
) {
350 if (i
< (unsigned)array
.raid_disks
&&
351 (((newstate
&change
)&(1<<MD_DISK_FAULTY
)) ||
352 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_ACTIVE
)) ||
353 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
)))
355 alert("Fail", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
356 else if (i
>= (unsigned)array
.raid_disks
&&
357 (disc
.major
|| disc
.minor
) &&
358 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
) &&
359 ((newstate
&change
)&(1<<MD_DISK_FAULTY
))
361 alert("FailSpare", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
362 else if (i
< (unsigned)array
.raid_disks
&&
363 (((st
->devstate
[i
]&change
)&(1<<MD_DISK_FAULTY
)) ||
364 ((newstate
&change
)&(1<<MD_DISK_ACTIVE
)) ||
365 ((newstate
&change
)&(1<<MD_DISK_SYNC
)))
367 alert("SpareActive", dev
, dv
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
369 st
->devstate
[i
] = disc
.state
;
370 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
373 st
->active
= array
.active_disks
;
374 st
->working
= array
.working_disks
;
375 st
->spare
= array
.spare_disks
;
376 st
->failed
= array
.failed_disks
;
377 st
->utime
= array
.utime
;
378 st
->raid
= array
.raid_disks
;
381 /* now check if there are any new devices found in mdstat */
383 struct mdstat_ent
*mse
;
384 for (mse
=mdstat
; mse
; mse
=mse
->next
)
385 if (mse
->devnum
!= MAXINT
&&
386 (strcmp(mse
->level
, "raid1")==0 ||
387 strcmp(mse
->level
, "raid5")==0 ||
388 strcmp(mse
->level
, "multipath")==0)
390 struct state
*st
= malloc(sizeof *st
);
391 mdu_array_info_t array
;
395 st
->devname
= strdup(get_md_name(mse
->devnum
));
396 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
397 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
399 if (fd
>=0) close(fd
);
400 put_md_name(st
->devname
);
407 st
->next
= statelist
;
409 st
->devnum
= mse
->devnum
;
411 st
->spare_group
= NULL
;
412 st
->expected_spares
= -1;
414 alert("NewArray", st
->devname
, NULL
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
418 /* If an array has active < raid && spare == 0 && spare_group != NULL
419 * Look for another array with spare > 0 and active == raid and same spare_group
420 * if found, choose a device and hotremove/hotadd
422 for (st
= statelist
; st
; st
=st
->next
)
423 if (st
->active
< st
->raid
&&
425 st
->spare_group
!= NULL
) {
427 for (st2
=statelist
; st2
; st2
=st2
->next
)
430 st2
->active
== st2
->raid
&&
431 st2
->spare_group
!= NULL
&&
432 strcmp(st
->spare_group
, st2
->spare_group
) == 0) {
433 /* try to remove and add */
434 int fd1
= open(st
->devname
, O_RDONLY
);
435 int fd2
= open(st2
->devname
, O_RDONLY
);
438 if (fd1
< 0 || fd2
< 0) {
439 if (fd1
>=0) close(fd1
);
440 if (fd2
>=0) close(fd2
);
443 for (d
=st2
->raid
; d
< MaxDisks
; d
++) {
444 if (st2
->devid
[d
] > 0 &&
445 st2
->devstate
[d
] == 0) {
451 if (ioctl(fd2
, HOT_REMOVE_DISK
,
452 (unsigned long)dev
) == 0) {
453 if (ioctl(fd1
, HOT_ADD_DISK
,
454 (unsigned long)dev
) == 0) {
455 alert("MoveSpare", st
->devname
, st2
->devname
, mailaddr
, mailfrom
, alert_cmd
, dosyslog
);
460 else ioctl(fd2
, HOT_ADD_DISK
, (unsigned long) dev
);
481 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *mailfrom
, char *cmd
,
486 if (!cmd
&& !mailaddr
) {
487 time_t now
= time(0);
489 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
495 waitpid(pid
, NULL
, 0);
500 execl(cmd
, cmd
, event
, dev
, disc
, NULL
);
505 (strncmp(event
, "Fail", 4)==0 ||
506 strncmp(event
, "Test", 4)==0 ||
507 strncmp(event
, "Spares", 6)==0 ||
508 strncmp(event
, "Degrade", 7)==0)) {
509 FILE *mp
= popen(Sendmail
, "w");
513 gethostname(hname
, sizeof(hname
));
514 signal(SIGPIPE
, SIG_IGN
);
516 fprintf(mp
, "From: %s\n", mailfrom
);
518 fprintf(mp
, "From: " Name
" monitoring <root>\n");
519 fprintf(mp
, "To: %s\n", mailaddr
);
520 fprintf(mp
, "Subject: %s event on %s:%s\n\n", event
, dev
, hname
);
522 fprintf(mp
, "This is an automatically generated mail message from " Name
"\n");
523 fprintf(mp
, "running on %s\n\n", hname
);
525 fprintf(mp
, "A %s event had been detected on md device %s.\n\n", event
, dev
);
527 if (disc
&& disc
[0] != ' ')
528 fprintf(mp
, "It could be related to component device %s.\n\n", disc
);
529 if (disc
&& disc
[0] == ' ')
530 fprintf(mp
, "Extra information:%s.\n\n", disc
);
532 fprintf(mp
, "Faithfully yours, etc.\n");
534 mdstat
= fopen("/proc/mdstat", "r");
538 fprintf(mp
, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
539 while ( (n
=fread(buf
, 1, sizeof(buf
), mdstat
)) > 0)
540 n
=fwrite(buf
, 1, n
, mp
); /* yes, i don't care about the result */
548 /* log the event to syslog maybe */
550 /* Log at a different severity depending on the event.
552 * These are the critical events: */
553 if (strncmp(event
, "Fail", 4)==0 ||
554 strncmp(event
, "Degrade", 7)==0 ||
555 strncmp(event
, "DeviceDisappeared", 17)==0)
557 /* Good to know about, but are not failures: */
558 else if (strncmp(event
, "Rebuild", 7)==0 ||
559 strncmp(event
, "MoveSpare", 9)==0 ||
560 strncmp(event
, "Spares", 6) != 0)
561 priority
= LOG_WARNING
;
562 /* Everything else: */
567 syslog(priority
, "%s event detected on md device %s, component device %s", event
, dev
, disc
);
569 syslog(priority
, "%s event detected on md device %s", event
, dev
);
573 /* Not really Monitor but ... */
580 if (stat(dev
, &stb
) != 0) {
581 fprintf(stderr
, Name
": Cannot find %s: %s\n", dev
,
585 if (major(stb
.st_rdev
) == MD_MAJOR
)
586 devnum
= minor(stb
.st_rdev
);
588 devnum
= -minor(stb
.st_rdev
)/16;
591 struct mdstat_ent
*ms
= mdstat_read(1, 0);
592 struct mdstat_ent
*e
;
594 for (e
=ms
; e
; e
=e
->next
)
595 if (e
->devnum
== devnum
)
598 if (!e
|| e
->percent
< 0) {