]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
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>
38 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *cmd
,
41 static char *percentalerts
[] = {
49 int Monitor(mddev_dev_t devlist
,
50 char *mailaddr
, char *alert_cmd
,
51 int period
, int daemonise
, int scan
, int oneshot
,
52 int dosyslog
, char *config
, int test
, char* pidfile
)
55 * Every few seconds, scan every md device looking for changes
56 * When a change is found, log it, possibly run the alert command,
57 * and possibly send Email
59 * For each array, we record:
61 * active/working/failed/spare drives
62 * State of each device.
63 * %rebuilt if rebuilding
65 * If the update time changes, check out all the data again
66 * It is possible that we cannot get the state of each device
67 * due to bugs in the md kernel module.
68 * We also read /proc/mdstat to get rebuild percent,
69 * and to get state on all active devices incase of kernel bug.
73 * An active device had Faulty set or Active/Sync removed
75 * A spare device had Faulty set
77 * An active device had a reverse transition
79 * percent went from -1 to +ve
80 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
81 * percent went from below to not-below that number
83 * Couldn't access a device which was previously visible
85 * if we detect an array with active<raid and spare==0
86 * we look at other arrays that have same spare-group
87 * If we find one with active==raid and spare>0,
88 * and if we can get_disk_info and find a name
89 * Then we hot-remove and hot-add to the other array
91 * If devlist is NULL, then we can monitor everything because --scan
92 * was given. We get an initial list from config file and add anything
93 * that appears in /proc/mdstat
98 int devnum
; /* to sync with mdstat info */
102 int active
, working
, failed
, spare
, raid
;
104 int devstate
[MD_SB_DISKS
];
105 int devid
[MD_SB_DISKS
];
110 struct mdstat_ent
*mdstat
= NULL
;
113 mailaddr
= conf_get_mailaddr(config
);
114 if (mailaddr
&& ! scan
)
115 fprintf(stderr
, Name
": Monitor using email address \"%s\" from config file\n",
119 alert_cmd
= conf_get_program(config
);
120 if (alert_cmd
&& ! scan
)
121 fprintf(stderr
, Name
": Monitor using program \"%s\" from config file\n",
124 if (scan
&& !mailaddr
&& !alert_cmd
) {
125 fprintf(stderr
, Name
": No mail address or alert command - not monitoring.\n");
136 pid_file
=fopen(pidfile
, "w");
138 perror("cannot create pid file");
140 fprintf(pid_file
,"%d\n", pid
);
151 open("/dev/null", 3);
157 if (devlist
== NULL
) {
158 mddev_ident_t mdlist
= conf_get_ident(config
, NULL
);
159 for (; mdlist
; mdlist
=mdlist
->next
) {
160 struct state
*st
= malloc(sizeof *st
);
163 st
->devname
= strdup(mdlist
->devname
);
165 st
->next
= statelist
;
169 st
->expected_spares
= mdlist
->spare_disks
;
170 if (mdlist
->spare_group
)
171 st
->spare_group
= strdup(mdlist
->spare_group
);
173 st
->spare_group
= NULL
;
178 for (dv
=devlist
; dv
; dv
=dv
->next
) {
179 mddev_ident_t mdlist
= conf_get_ident(config
, dv
->devname
);
180 struct state
*st
= malloc(sizeof *st
);
183 st
->devname
= strdup(dv
->devname
);
185 st
->next
= statelist
;
189 st
->expected_spares
= -1;
190 st
->spare_group
= NULL
;
192 st
->expected_spares
= mdlist
->spare_disks
;
193 if (mdlist
->spare_group
)
194 st
->spare_group
= strdup(mdlist
->spare_group
);
207 mdstat
= mdstat_read(oneshot
?0:1, 0);
209 for (st
=statelist
; st
; st
=st
->next
) {
210 mdu_array_info_t array
;
211 struct mdstat_ent
*mse
= NULL
, *mse2
;
212 char *dev
= st
->devname
;
217 alert("TestMessage", dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
218 fd
= open(dev
, O_RDONLY
);
221 alert("DeviceDisappeared", dev
, NULL
,
222 mailaddr
, alert_cmd
, dosyslog
);
223 /* fprintf(stderr, Name ": cannot open %s: %s\n",
224 dev, strerror(errno));
228 if (ioctl(fd
, GET_ARRAY_INFO
, &array
)<0) {
230 alert("DeviceDisappeared", dev
, NULL
,
231 mailaddr
, alert_cmd
, dosyslog
);
232 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
233 dev, strerror(errno));
238 if (array
.level
!= 1 && array
.level
!= 5 && array
.level
!= -4 &&
239 array
.level
!= 6 && array
.level
!= 10) {
241 alert("DeviceDisappeared", dev
, "Wrong-Level",
242 mailaddr
, alert_cmd
, dosyslog
);
247 if (st
->devnum
== MAXINT
) {
249 if (fstat(fd
, &stb
) == 0 &&
250 (S_IFMT
&stb
.st_mode
)==S_IFBLK
) {
251 if (major(stb
.st_rdev
) == MD_MAJOR
)
252 st
->devnum
= minor(stb
.st_rdev
);
254 st
->devnum
= -1- (minor(stb
.st_rdev
)>>6);
258 for (mse2
= mdstat
; mse2
; mse2
=mse2
->next
)
259 if (mse2
->devnum
== st
->devnum
) {
260 mse2
->devnum
= MAXINT
; /* flag it as "used" */
264 if (st
->utime
== array
.utime
&&
265 st
->failed
== array
.failed_disks
&&
266 st
->working
== array
.working_disks
&&
267 st
->spare
== array
.spare_disks
&&
269 mse
->percent
== st
->percent
275 if (st
->utime
== 0 && /* new array */
276 mse
&& /* is in /proc/mdstat */
277 mse
->pattern
&& strchr(mse
->pattern
, '_') /* degraded */
279 alert("DegradedArray", dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
281 if (st
->utime
== 0 && /* new array */
282 st
->expected_spares
> 0 &&
283 array
.spare_disks
< st
->expected_spares
)
284 alert("SparesMissing", dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
288 alert("RebuildStarted", dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
292 (mse
->percent
/ 20) > (st
->percent
/ 20))
293 alert(percentalerts
[mse
->percent
/20],
294 dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
297 mse
->percent
== -1 &&
299 alert("RebuildFinished", dev
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
302 st
->percent
= mse
->percent
;
304 for (i
=0; i
<MD_SB_DISKS
; i
++) {
305 mdu_disk_info_t disc
;
310 if (ioctl(fd
, GET_DISK_INFO
, &disc
)>= 0) {
311 newstate
= disc
.state
;
312 dv
= map_dev(disc
.major
, disc
.minor
);
313 } else if (mse
&& mse
->pattern
&& i
< strlen(mse
->pattern
))
314 switch(mse
->pattern
[i
]) {
315 case 'U': newstate
= 6 /* ACTIVE/SYNC */; break;
316 case '_': newstate
= 0; break;
318 if (dv
== NULL
&& st
->devid
[i
])
319 dv
= map_dev(major(st
->devid
[i
]),
320 minor(st
->devid
[i
]));
321 change
= newstate
^ st
->devstate
[i
];
322 if (st
->utime
&& change
&& !st
->err
) {
323 if (i
< (unsigned)array
.raid_disks
&&
324 (((newstate
&change
)&(1<<MD_DISK_FAULTY
)) ||
325 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_ACTIVE
)) ||
326 ((st
->devstate
[i
]&change
)&(1<<MD_DISK_SYNC
)))
328 alert("Fail", dev
, dv
, mailaddr
, alert_cmd
, dosyslog
);
329 else if (i
>= (unsigned)array
.raid_disks
&&
330 (disc
.major
|| disc
.minor
) &&
331 st
->devid
[i
] == makedev(disc
.major
, disc
.minor
) &&
332 ((newstate
&change
)&(1<<MD_DISK_FAULTY
))
334 alert("FailSpare", dev
, dv
, mailaddr
, alert_cmd
, dosyslog
);
335 else if (i
< (unsigned)array
.raid_disks
&&
336 (((st
->devstate
[i
]&change
)&(1<<MD_DISK_FAULTY
)) ||
337 ((newstate
&change
)&(1<<MD_DISK_ACTIVE
)) ||
338 ((newstate
&change
)&(1<<MD_DISK_SYNC
)))
340 alert("SpareActive", dev
, dv
, mailaddr
, alert_cmd
, dosyslog
);
342 st
->devstate
[i
] = disc
.state
;
343 st
->devid
[i
] = makedev(disc
.major
, disc
.minor
);
346 st
->active
= array
.active_disks
;
347 st
->working
= array
.working_disks
;
348 st
->spare
= array
.spare_disks
;
349 st
->failed
= array
.failed_disks
;
350 st
->utime
= array
.utime
;
351 st
->raid
= array
.raid_disks
;
354 /* now check if there are any new devices found in mdstat */
356 struct mdstat_ent
*mse
;
357 for (mse
=mdstat
; mse
; mse
=mse
->next
)
358 if (mse
->devnum
!= MAXINT
&&
359 (strcmp(mse
->level
, "raid1")==0 ||
360 strcmp(mse
->level
, "raid5")==0 ||
361 strcmp(mse
->level
, "multipath")==0)
363 struct state
*st
= malloc(sizeof *st
);
364 mdu_array_info_t array
;
368 st
->devname
= strdup(get_md_name(mse
->devnum
));
369 if ((fd
= open(st
->devname
, O_RDONLY
)) < 0 ||
370 ioctl(fd
, GET_ARRAY_INFO
, &array
)< 0) {
372 if (fd
>=0) close(fd
);
379 st
->next
= statelist
;
381 st
->devnum
= mse
->devnum
;
383 st
->spare_group
= NULL
;
384 st
->expected_spares
= -1;
386 alert("NewArray", st
->devname
, NULL
, mailaddr
, alert_cmd
, dosyslog
);
390 /* If an array has active < raid && spare == 0 && spare_group != NULL
391 * Look for another array with spare > 0 and active == raid and same spare_group
392 * if found, choose a device and hotremove/hotadd
394 for (st
= statelist
; st
; st
=st
->next
)
395 if (st
->active
< st
->raid
&&
397 st
->spare_group
!= NULL
) {
399 for (st2
=statelist
; st2
; st2
=st2
->next
)
402 st2
->active
== st2
->raid
&&
403 st2
->spare_group
!= NULL
&&
404 strcmp(st
->spare_group
, st2
->spare_group
) == 0) {
405 /* try to remove and add */
406 int fd1
= open(st
->devname
, O_RDONLY
);
407 int fd2
= open(st2
->devname
, O_RDONLY
);
410 if (fd1
< 0 || fd2
< 0) {
411 if (fd1
>=0) close(fd1
);
412 if (fd2
>=0) close(fd2
);
415 for (d
=st2
->raid
; d
<MD_SB_DISKS
; d
++) {
416 if (st2
->devid
[d
] > 0 &&
417 st2
->devstate
[d
] == 0) {
423 if (ioctl(fd2
, HOT_REMOVE_DISK
,
424 (unsigned long)dev
) == 0) {
425 if (ioctl(fd1
, HOT_ADD_DISK
,
426 (unsigned long)dev
) == 0) {
427 alert("MoveSpare", st
->devname
, st2
->devname
, mailaddr
, alert_cmd
, dosyslog
);
432 else ioctl(fd2
, HOT_ADD_DISK
, (unsigned long) dev
);
453 static void alert(char *event
, char *dev
, char *disc
, char *mailaddr
, char *cmd
,
458 if (!cmd
&& !mailaddr
) {
459 time_t now
= time(0);
461 printf("%1.15s: %s on %s %s\n", ctime(&now
)+4, event
, dev
, disc
?disc
:"unknown device");
467 waitpid(pid
, NULL
, 0);
472 execl(cmd
, cmd
, event
, dev
, disc
, NULL
);
477 (strncmp(event
, "Fail", 4)==0 ||
478 strncmp(event
, "Test", 4)==0 ||
479 strncmp(event
, "Degrade", 7)==0)) {
480 FILE *mp
= popen(Sendmail
, "w");
483 gethostname(hname
, sizeof(hname
));
484 signal(SIGPIPE
, SIG_IGN
);
485 fprintf(mp
, "From: " Name
" monitoring <root>\n");
486 fprintf(mp
, "To: %s\n", mailaddr
);
487 fprintf(mp
, "Subject: %s event on %s:%s\n\n", event
, dev
, hname
);
489 fprintf(mp
, "This is an automatically generated mail message from " Name
"\n");
490 fprintf(mp
, "running on %s\n\n", hname
);
492 fprintf(mp
, "A %s event had been detected on md device %s.\n\n", event
, dev
);
495 fprintf(mp
, "It could be related to component device %s.\n\n", disc
);
497 fprintf(mp
, "Faithfully yours, etc.\n");
503 /* log the event to syslog maybe */
505 /* Log at a different severity depending on the event.
507 * These are the critical events: */
508 if (strncmp(event
, "Fail", 4)==0 ||
509 strncmp(event
, "Degrade", 7)==0 ||
510 strncmp(event
, "DeviceDisappeared", 17)==0)
512 /* Good to know about, but are not failures: */
513 else if (strncmp(event
, "Rebuild", 7)==0 ||
514 strncmp(event
, "MoveSpare", 9)==0)
515 priority
= LOG_WARNING
;
516 /* Everything else: */
521 syslog(priority
, "%s event detected on md device %s, component device %s", event
, dev
, disc
);
523 syslog(priority
, "%s event detected on md device %s", event
, dev
);