]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
[PATCH] Get the name for partitioned devices in sysfs correct.
[thirdparty/mdadm.git] / Monitor.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdadm.h"
31 #include "md_p.h"
32 #include "md_u.h"
33 #include <sys/wait.h>
34 #include <sys/signal.h>
35 #include <values.h>
36 #include <syslog.h>
37
38 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd,
39 int dosyslog);
40
41 static char *percentalerts[] = {
42 "RebuildStarted",
43 "Rebuild20",
44 "Rebuild40",
45 "Rebuild60",
46 "Rebuild80",
47 };
48
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.
52 */
53 #define MaxDisks 384
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, char *config, int test, char* pidfile)
58 {
59 /*
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
63 *
64 * For each array, we record:
65 * Update time
66 * active/working/failed/spare drives
67 * State of each device.
68 * %rebuilt if rebuilding
69 *
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.
75 *
76 * Events are:
77 * Fail
78 * An active device had Faulty set or Active/Sync removed
79 * FailSpare
80 * A spare device had Faulty set
81 * SpareActive
82 * An active device had a reverse transition
83 * RebuildStarted
84 * percent went from -1 to +ve
85 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
86 * percent went from below to not-below that number
87 * DeviceDisappeared
88 * Couldn't access a device which was previously visible
89 *
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
95 *
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
99 */
100
101 struct state {
102 char *devname;
103 int devnum; /* to sync with mdstat info */
104 long utime;
105 int err;
106 char *spare_group;
107 int active, working, failed, spare, raid;
108 int expected_spares;
109 int devstate[MaxDisks];
110 int devid[MaxDisks];
111 int percent;
112 struct state *next;
113 } *statelist = NULL;
114 int finished = 0;
115 struct mdstat_ent *mdstat = NULL;
116
117 if (!mailaddr) {
118 mailaddr = conf_get_mailaddr(config);
119 if (mailaddr && ! scan)
120 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
121 mailaddr);
122 }
123 if (!alert_cmd) {
124 alert_cmd = conf_get_program(config);
125 if (alert_cmd && ! scan)
126 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
127 alert_cmd);
128 }
129 if (scan && !mailaddr && !alert_cmd) {
130 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
131 return 1;
132 }
133
134 if (daemonise) {
135 int pid = fork();
136 if (pid > 0) {
137 if (!pidfile)
138 printf("%d\n", pid);
139 else {
140 FILE *pid_file;
141 pid_file=fopen(pidfile, "w");
142 if (!pid_file)
143 perror("cannot create pid file");
144 else {
145 fprintf(pid_file,"%d\n", pid);
146 fclose(pid_file);
147 }
148 }
149 return 0;
150 }
151 if (pid < 0) {
152 perror("daemonise");
153 return 1;
154 }
155 close(0);
156 open("/dev/null", 3);
157 dup2(0,1);
158 dup2(0,2);
159 setsid();
160 }
161
162 if (devlist == NULL) {
163 mddev_ident_t mdlist = conf_get_ident(config, NULL);
164 for (; mdlist; mdlist=mdlist->next) {
165 struct state *st = malloc(sizeof *st);
166 if (st == NULL)
167 continue;
168 st->devname = strdup(mdlist->devname);
169 st->utime = 0;
170 st->next = statelist;
171 st->err = 0;
172 st->devnum = MAXINT;
173 st->percent = -2;
174 st->expected_spares = mdlist->spare_disks;
175 if (mdlist->spare_group)
176 st->spare_group = strdup(mdlist->spare_group);
177 else
178 st->spare_group = NULL;
179 statelist = st;
180 }
181 } else {
182 mddev_dev_t dv;
183 for (dv=devlist ; dv; dv=dv->next) {
184 mddev_ident_t mdlist = conf_get_ident(config, dv->devname);
185 struct state *st = malloc(sizeof *st);
186 if (st == NULL)
187 continue;
188 st->devname = strdup(dv->devname);
189 st->utime = 0;
190 st->next = statelist;
191 st->err = 0;
192 st->devnum = MAXINT;
193 st->percent = -2;
194 st->expected_spares = -1;
195 st->spare_group = NULL;
196 if (mdlist) {
197 st->expected_spares = mdlist->spare_disks;
198 if (mdlist->spare_group)
199 st->spare_group = strdup(mdlist->spare_group);
200 }
201 statelist = st;
202 }
203 }
204
205
206 while (! finished) {
207 int new_found = 0;
208 struct state *st;
209
210 if (mdstat)
211 free_mdstat(mdstat);
212 mdstat = mdstat_read(oneshot?0:1, 0);
213
214 for (st=statelist; st; st=st->next) {
215 mdu_array_info_t array;
216 struct mdstat_ent *mse = NULL, *mse2;
217 char *dev = st->devname;
218 int fd;
219 unsigned int i;
220
221 if (test)
222 alert("TestMessage", dev, NULL, mailaddr, alert_cmd, dosyslog);
223 fd = open(dev, O_RDONLY);
224 if (fd < 0) {
225 if (!st->err)
226 alert("DeviceDisappeared", dev, NULL,
227 mailaddr, alert_cmd, dosyslog);
228 /* fprintf(stderr, Name ": cannot open %s: %s\n",
229 dev, strerror(errno));
230 */ st->err=1;
231 continue;
232 }
233 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
234 if (!st->err)
235 alert("DeviceDisappeared", dev, NULL,
236 mailaddr, alert_cmd, dosyslog);
237 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
238 dev, strerror(errno));
239 */ st->err=1;
240 close(fd);
241 continue;
242 }
243 if (array.level != 1 && array.level != 5 && array.level != -4 &&
244 array.level != 6 && array.level != 10) {
245 if (!st->err)
246 alert("DeviceDisappeared", dev, "Wrong-Level",
247 mailaddr, alert_cmd, dosyslog);
248 st->err = 1;
249 close(fd);
250 continue;
251 }
252 if (st->devnum == MAXINT) {
253 struct stat stb;
254 if (fstat(fd, &stb) == 0 &&
255 (S_IFMT&stb.st_mode)==S_IFBLK) {
256 if (major(stb.st_rdev) == MD_MAJOR)
257 st->devnum = minor(stb.st_rdev);
258 else
259 st->devnum = -1- (minor(stb.st_rdev)>>6);
260 }
261 }
262
263 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
264 if (mse2->devnum == st->devnum) {
265 mse2->devnum = MAXINT; /* flag it as "used" */
266 mse = mse2;
267 }
268
269 if (st->utime == array.utime &&
270 st->failed == array.failed_disks &&
271 st->working == array.working_disks &&
272 st->spare == array.spare_disks &&
273 (mse == NULL || (
274 mse->percent == st->percent
275 ))) {
276 close(fd);
277 st->err = 0;
278 continue;
279 }
280 if (st->utime == 0 && /* new array */
281 mse && /* is in /proc/mdstat */
282 mse->pattern && strchr(mse->pattern, '_') /* degraded */
283 )
284 alert("DegradedArray", dev, NULL, mailaddr, alert_cmd, dosyslog);
285
286 if (st->utime == 0 && /* new array */
287 st->expected_spares > 0 &&
288 array.spare_disks < st->expected_spares)
289 alert("SparesMissing", dev, NULL, mailaddr, alert_cmd, dosyslog);
290 if (mse &&
291 st->percent == -1 &&
292 mse->percent >= 0)
293 alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd, dosyslog);
294 if (mse &&
295 st->percent >= 0 &&
296 mse->percent >= 0 &&
297 (mse->percent / 20) > (st->percent / 20))
298 alert(percentalerts[mse->percent/20],
299 dev, NULL, mailaddr, alert_cmd, dosyslog);
300
301 if (mse &&
302 mse->percent == -1 &&
303 st->percent >= 0)
304 alert("RebuildFinished", dev, NULL, mailaddr, alert_cmd, dosyslog);
305
306 if (mse)
307 st->percent = mse->percent;
308
309 for (i=0; i<MaxDisks; i++) {
310 mdu_disk_info_t disc;
311 int newstate=0;
312 int change;
313 char *dv = NULL;
314 disc.number = i;
315 if (i > array.raid_disks + array.nr_disks) {
316 newstate = 0;
317 disc.major = disc.minor = 0;
318 } else if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
319 newstate = disc.state;
320 dv = map_dev(disc.major, disc.minor, 1);
321 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
322 switch(mse->pattern[i]) {
323 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
324 case '_': newstate = 0; break;
325 }
326 disc.major = disc.minor = 0;
327 }
328 if (dv == NULL && st->devid[i])
329 dv = map_dev(major(st->devid[i]),
330 minor(st->devid[i]), 1);
331 change = newstate ^ st->devstate[i];
332 if (st->utime && change && !st->err) {
333 if (i < (unsigned)array.raid_disks &&
334 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
335 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
336 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
337 )
338 alert("Fail", dev, dv, mailaddr, alert_cmd, dosyslog);
339 else if (i >= (unsigned)array.raid_disks &&
340 (disc.major || disc.minor) &&
341 st->devid[i] == makedev(disc.major, disc.minor) &&
342 ((newstate&change)&(1<<MD_DISK_FAULTY))
343 )
344 alert("FailSpare", dev, dv, mailaddr, alert_cmd, dosyslog);
345 else if (i < (unsigned)array.raid_disks &&
346 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
347 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
348 ((newstate&change)&(1<<MD_DISK_SYNC)))
349 )
350 alert("SpareActive", dev, dv, mailaddr, alert_cmd, dosyslog);
351 }
352 st->devstate[i] = disc.state;
353 st->devid[i] = makedev(disc.major, disc.minor);
354 }
355 close(fd);
356 st->active = array.active_disks;
357 st->working = array.working_disks;
358 st->spare = array.spare_disks;
359 st->failed = array.failed_disks;
360 st->utime = array.utime;
361 st->raid = array.raid_disks;
362 st->err = 0;
363 }
364 /* now check if there are any new devices found in mdstat */
365 if (scan) {
366 struct mdstat_ent *mse;
367 for (mse=mdstat; mse; mse=mse->next)
368 if (mse->devnum != MAXINT &&
369 (strcmp(mse->level, "raid1")==0 ||
370 strcmp(mse->level, "raid5")==0 ||
371 strcmp(mse->level, "multipath")==0)
372 ) {
373 struct state *st = malloc(sizeof *st);
374 mdu_array_info_t array;
375 int fd;
376 if (st == NULL)
377 continue;
378 st->devname = strdup(get_md_name(mse->devnum));
379 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
380 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
381 /* no such array */
382 if (fd >=0) close(fd);
383 free(st->devname);
384 free(st);
385 continue;
386 }
387 close(fd);
388 st->utime = 0;
389 st->next = statelist;
390 st->err = 1;
391 st->devnum = mse->devnum;
392 st->percent = -2;
393 st->spare_group = NULL;
394 st->expected_spares = -1;
395 statelist = st;
396 alert("NewArray", st->devname, NULL, mailaddr, alert_cmd, dosyslog);
397 new_found = 1;
398 }
399 }
400 /* If an array has active < raid && spare == 0 && spare_group != NULL
401 * Look for another array with spare > 0 and active == raid and same spare_group
402 * if found, choose a device and hotremove/hotadd
403 */
404 for (st = statelist; st; st=st->next)
405 if (st->active < st->raid &&
406 st->spare == 0 &&
407 st->spare_group != NULL) {
408 struct state *st2;
409 for (st2=statelist ; st2 ; st2=st2->next)
410 if (st2 != st &&
411 st2->spare > 0 &&
412 st2->active == st2->raid &&
413 st2->spare_group != NULL &&
414 strcmp(st->spare_group, st2->spare_group) == 0) {
415 /* try to remove and add */
416 int fd1 = open(st->devname, O_RDONLY);
417 int fd2 = open(st2->devname, O_RDONLY);
418 int dev = -1;
419 int d;
420 if (fd1 < 0 || fd2 < 0) {
421 if (fd1>=0) close(fd1);
422 if (fd2>=0) close(fd2);
423 continue;
424 }
425 for (d=st2->raid; d < MaxDisks; d++) {
426 if (st2->devid[d] > 0 &&
427 st2->devstate[d] == 0) {
428 dev = st2->devid[d];
429 break;
430 }
431 }
432 if (dev > 0) {
433 if (ioctl(fd2, HOT_REMOVE_DISK,
434 (unsigned long)dev) == 0) {
435 if (ioctl(fd1, HOT_ADD_DISK,
436 (unsigned long)dev) == 0) {
437 alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd, dosyslog);
438 close(fd1);
439 close(fd2);
440 break;
441 }
442 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
443 }
444 }
445 close(fd1);
446 close(fd2);
447 }
448 }
449 if (!new_found) {
450 if (oneshot)
451 break;
452 else
453 mdstat_wait(period);
454 }
455 test = 0;
456 }
457 if (pidfile)
458 unlink(pidfile);
459 return 0;
460 }
461
462
463 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd,
464 int dosyslog)
465 {
466 int priority;
467
468 if (!cmd && !mailaddr) {
469 time_t now = time(0);
470
471 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
472 }
473 if (cmd) {
474 int pid = fork();
475 switch(pid) {
476 default:
477 waitpid(pid, NULL, 0);
478 break;
479 case -1:
480 break;
481 case 0:
482 execl(cmd, cmd, event, dev, disc, NULL);
483 exit(2);
484 }
485 }
486 if (mailaddr &&
487 (strncmp(event, "Fail", 4)==0 ||
488 strncmp(event, "Test", 4)==0 ||
489 strncmp(event, "Degrade", 7)==0)) {
490 FILE *mp = popen(Sendmail, "w");
491 if (mp) {
492 char hname[256];
493 gethostname(hname, sizeof(hname));
494 signal(SIGPIPE, SIG_IGN);
495 fprintf(mp, "From: " Name " monitoring <root>\n");
496 fprintf(mp, "To: %s\n", mailaddr);
497 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
498
499 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
500 fprintf(mp, "running on %s\n\n", hname);
501
502 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
503
504 if (disc)
505 fprintf(mp, "It could be related to component device %s.\n\n", disc);
506
507 fprintf(mp, "Faithfully yours, etc.\n");
508 fclose(mp);
509 }
510
511 }
512
513 /* log the event to syslog maybe */
514 if (dosyslog) {
515 /* Log at a different severity depending on the event.
516 *
517 * These are the critical events: */
518 if (strncmp(event, "Fail", 4)==0 ||
519 strncmp(event, "Degrade", 7)==0 ||
520 strncmp(event, "DeviceDisappeared", 17)==0)
521 priority = LOG_CRIT;
522 /* Good to know about, but are not failures: */
523 else if (strncmp(event, "Rebuild", 7)==0 ||
524 strncmp(event, "MoveSpare", 9)==0)
525 priority = LOG_WARNING;
526 /* Everything else: */
527 else
528 priority = LOG_INFO;
529
530 if (disc)
531 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
532 else
533 syslog(priority, "%s event detected on md device %s", event, dev);
534 }
535 }