]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Monitor.c
Add gpt pseudo-metadata
[thirdparty/mdadm.git] / Monitor.c
1 /*
2 * mdadm - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
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@suse.de>
23 */
24
25 #include "mdadm.h"
26 #include "md_p.h"
27 #include "md_u.h"
28 #include <sys/wait.h>
29 #include <signal.h>
30 #include <limits.h>
31 #include <syslog.h>
32
33 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
34 char *cmd, int dosyslog);
35
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.
39 */
40 #define MaxDisks 384
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)
45 {
46 /*
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
50 *
51 * For each array, we record:
52 * Update time
53 * active/working/failed/spare drives
54 * State of each device.
55 * %rebuilt if rebuilding
56 *
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.
62 *
63 * Events are:
64 * Fail
65 * An active device had Faulty set or Active/Sync removed
66 * FailSpare
67 * A spare device had Faulty set
68 * SpareActive
69 * An active device had a reverse transition
70 * RebuildStarted
71 * percent went from -1 to +ve
72 * RebuildNN
73 * percent went from below to not-below NN%
74 * DeviceDisappeared
75 * Couldn't access a device which was previously visible
76 *
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
82 *
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
86 */
87
88 struct state {
89 char *devname;
90 int devnum; /* to sync with mdstat info */
91 long utime;
92 int err;
93 char *spare_group;
94 int active, working, failed, spare, raid;
95 int expected_spares;
96 int devstate[MaxDisks];
97 unsigned devid[MaxDisks];
98 int percent;
99 struct state *next;
100 } *statelist = NULL;
101 int finished = 0;
102 struct mdstat_ent *mdstat = NULL;
103 char *mailfrom = NULL;
104
105 if (!mailaddr) {
106 mailaddr = conf_get_mailaddr();
107 if (mailaddr && ! scan)
108 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
109 mailaddr);
110 }
111 mailfrom = conf_get_mailfrom();
112
113 if (!alert_cmd) {
114 alert_cmd = conf_get_program();
115 if (alert_cmd && ! scan)
116 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
117 alert_cmd);
118 }
119 if (scan && !mailaddr && !alert_cmd) {
120 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
121 return 1;
122 }
123
124 if (daemonise) {
125 int pid = fork();
126 if (pid > 0) {
127 if (!pidfile)
128 printf("%d\n", pid);
129 else {
130 FILE *pid_file;
131 pid_file=fopen(pidfile, "w");
132 if (!pid_file)
133 perror("cannot create pid file");
134 else {
135 fprintf(pid_file,"%d\n", pid);
136 fclose(pid_file);
137 }
138 }
139 return 0;
140 }
141 if (pid < 0) {
142 perror("daemonise");
143 return 1;
144 }
145 close(0);
146 open("/dev/null", O_RDWR);
147 dup2(0,1);
148 dup2(0,2);
149 setsid();
150 }
151
152 if (devlist == NULL) {
153 mddev_ident_t mdlist = conf_get_ident(NULL);
154 for (; mdlist; mdlist=mdlist->next) {
155 struct state *st;
156 if (mdlist->devname == NULL)
157 continue;
158 if (strcasecmp(mdlist->devname, "<ignore>") == 0)
159 continue;
160 st = malloc(sizeof *st);
161 if (st == NULL)
162 continue;
163 if (mdlist->devname[0] == '/')
164 st->devname = strdup(mdlist->devname);
165 else {
166 st->devname = malloc(8+strlen(mdlist->devname)+1);
167 strcpy(strcpy(st->devname, "/dev/md/"),
168 mdlist->devname);
169 }
170 st->utime = 0;
171 st->next = statelist;
172 st->err = 0;
173 st->devnum = INT_MAX;
174 st->percent = -2;
175 st->expected_spares = mdlist->spare_disks;
176 if (mdlist->spare_group)
177 st->spare_group = strdup(mdlist->spare_group);
178 else
179 st->spare_group = NULL;
180 statelist = st;
181 }
182 } else {
183 mddev_dev_t dv;
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);
187 if (st == NULL)
188 continue;
189 st->devname = strdup(dv->devname);
190 st->utime = 0;
191 st->next = statelist;
192 st->err = 0;
193 st->devnum = INT_MAX;
194 st->percent = -2;
195 st->expected_spares = -1;
196 st->spare_group = NULL;
197 if (mdlist) {
198 st->expected_spares = mdlist->spare_disks;
199 if (mdlist->spare_group)
200 st->spare_group = strdup(mdlist->spare_group);
201 }
202 statelist = st;
203 }
204 }
205
206
207 while (! finished) {
208 int new_found = 0;
209 struct state *st;
210
211 if (mdstat)
212 free_mdstat(mdstat);
213 mdstat = mdstat_read(oneshot?0:1, 0);
214
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;
220 int fd;
221 int i;
222
223 if (test)
224 alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
225 fd = open(dev, O_RDONLY);
226 if (fd < 0) {
227 if (!st->err)
228 alert("DeviceDisappeared", dev, NULL,
229 mailaddr, mailfrom, alert_cmd, dosyslog);
230 /* fprintf(stderr, Name ": cannot open %s: %s\n",
231 dev, strerror(errno));
232 */ st->err=1;
233 continue;
234 }
235 fcntl(fd, F_SETFD, FD_CLOEXEC);
236 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
237 if (!st->err)
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));
242 */ st->err=1;
243 close(fd);
244 continue;
245 }
246 /* It's much easier to list what array levels can't
247 * have a device disappear than all of them that can
248 */
249 if (array.level == 0 || array.level == -1) {
250 if (!st->err)
251 alert("DeviceDisappeared", dev, "Wrong-Level",
252 mailaddr, mailfrom, alert_cmd, dosyslog);
253 st->err = 1;
254 close(fd);
255 continue;
256 }
257 if (st->devnum == INT_MAX) {
258 struct stat stb;
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);
263 else
264 st->devnum = -1- (minor(stb.st_rdev)>>6);
265 }
266 }
267
268 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
269 if (mse2->devnum == st->devnum) {
270 mse2->devnum = INT_MAX; /* flag it as "used" */
271 mse = mse2;
272 }
273
274 if (array.utime == 0)
275 /* external arrays don't update utime */
276 array.utime = time(0);
277
278 if (st->utime == array.utime &&
279 st->failed == array.failed_disks &&
280 st->working == array.working_disks &&
281 st->spare == array.spare_disks &&
282 (mse == NULL || (
283 mse->percent == st->percent
284 ))) {
285 close(fd);
286 st->err = 0;
287 continue;
288 }
289 if (st->utime == 0 && /* new array */
290 mse && /* is in /proc/mdstat */
291 mse->pattern && strchr(mse->pattern, '_') /* degraded */
292 )
293 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
294
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);
299 if (mse &&
300 st->percent == -1 &&
301 mse->percent >= 0)
302 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
303 if (mse &&
304 st->percent >= 0 &&
305 mse->percent >= 0 &&
306 (mse->percent / increments) > (st->percent / increments)) {
307 char percentalert[15]; // "RebuildNN" (10 chars) or "RebuildStarted" (15 chars)
308
309 if((mse->percent / increments) == 0)
310 snprintf(percentalert, sizeof(percentalert), "RebuildStarted");
311 else
312 snprintf(percentalert, sizeof(percentalert), "Rebuild%02d", mse->percent);
313
314 alert(percentalert,
315 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
316 }
317
318 if (mse &&
319 mse->percent == -1 &&
320 st->percent >= 0) {
321 /* Rebuild/sync/whatever just finished.
322 * If there is a number in /mismatch_cnt,
323 * we should report that.
324 */
325 struct mdinfo *sra =
326 sysfs_read(-1, st->devnum, GET_MISMATCH);
327 if (sra && sra->mismatch_cnt > 0) {
328 char cnt[40];
329 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
330 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
331 } else
332 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
333 if (sra)
334 free(sra);
335 }
336
337 if (mse)
338 st->percent = mse->percent;
339
340
341 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
342 i++) {
343 mdu_disk_info_t disc;
344 disc.number = i;
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;
349 } else
350 info[i].major = info[i].minor = 0;
351 }
352 close(fd);
353
354 for (i=0; i<MaxDisks; i++) {
355 mdu_disk_info_t disc = {0,0,0,0,0};
356 int newstate=0;
357 int change;
358 char *dv = NULL;
359 disc.number = i;
360 if (i > array.raid_disks + array.nr_disks) {
361 newstate = 0;
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 < (int)strlen(mse->pattern)) {
370 switch(mse->pattern[i]) {
371 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
372 case '_': newstate = 0; break;
373 }
374 disc.major = disc.minor = 0;
375 }
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 < 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)))
385 )
386 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
387 else if (i >= array.raid_disks &&
388 (disc.major || disc.minor) &&
389 st->devid[i] == makedev(disc.major, disc.minor) &&
390 ((newstate&change)&(1<<MD_DISK_FAULTY))
391 )
392 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
393 else if (i < 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)))
398 )
399 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
400 }
401 st->devstate[i] = newstate;
402 st->devid[i] = makedev(disc.major, disc.minor);
403 }
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;
410 st->err = 0;
411 }
412 /* now check if there are any new devices found in mdstat */
413 if (scan) {
414 struct mdstat_ent *mse;
415 for (mse=mdstat; mse; mse=mse->next)
416 if (mse->devnum != INT_MAX &&
417 mse->level &&
418 (strcmp(mse->level, "raid0")!=0 &&
419 strcmp(mse->level, "linear")!=0)
420 ) {
421 struct state *st = malloc(sizeof *st);
422 mdu_array_info_t array;
423 int fd;
424 if (st == NULL)
425 continue;
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) {
429 /* no such array */
430 if (fd >=0) close(fd);
431 put_md_name(st->devname);
432 free(st->devname);
433 free(st);
434 continue;
435 }
436 close(fd);
437 st->utime = 0;
438 st->next = statelist;
439 st->err = 1;
440 st->devnum = mse->devnum;
441 st->percent = -2;
442 st->spare_group = NULL;
443 st->expected_spares = -1;
444 statelist = st;
445 if (test)
446 alert("TestMessage", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
447 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
448 new_found = 1;
449 }
450 }
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
454 */
455 for (st = statelist; st; st=st->next)
456 if (st->active < st->raid &&
457 st->spare == 0 &&
458 st->spare_group != NULL) {
459 struct state *st2;
460 for (st2=statelist ; st2 ; st2=st2->next)
461 if (st2 != st &&
462 st2->spare > 0 &&
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);
469 int dev = -1;
470 int d;
471 if (fd1 < 0 || fd2 < 0) {
472 if (fd1>=0) close(fd1);
473 if (fd2>=0) close(fd2);
474 continue;
475 }
476 for (d=st2->raid; d < MaxDisks; d++) {
477 if (st2->devid[d] > 0 &&
478 st2->devstate[d] == 0) {
479 dev = st2->devid[d];
480 break;
481 }
482 }
483 if (dev > 0) {
484 struct mddev_dev_s devlist;
485 char devname[20];
486 devlist.next = NULL;
487 devlist.used = 0;
488 devlist.re_add = 0;
489 devlist.writemostly = 0;
490 devlist.devname = devname;
491 sprintf(devname, "%d:%d", major(dev), minor(dev));
492
493 devlist.disposition = 'r';
494 if (Manage_subdevs(st2->devname, fd2, &devlist, -1, 0) == 0) {
495 devlist.disposition = 'a';
496 if (Manage_subdevs(st->devname, fd1, &devlist, -1, 0) == 0) {
497 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
498 close(fd1);
499 close(fd2);
500 break;
501 }
502 else Manage_subdevs(st2->devname, fd2, &devlist, -1, 0);
503 }
504 }
505 close(fd1);
506 close(fd2);
507 }
508 }
509 if (!new_found) {
510 if (oneshot)
511 break;
512 else
513 mdstat_wait(period);
514 }
515 test = 0;
516 }
517 if (pidfile)
518 unlink(pidfile);
519 return 0;
520 }
521
522
523 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
524 int dosyslog)
525 {
526 int priority;
527
528 if (!cmd && !mailaddr) {
529 time_t now = time(0);
530
531 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
532 }
533 if (cmd) {
534 int pid = fork();
535 switch(pid) {
536 default:
537 waitpid(pid, NULL, 0);
538 break;
539 case -1:
540 break;
541 case 0:
542 execl(cmd, cmd, event, dev, disc, NULL);
543 exit(2);
544 }
545 }
546 if (mailaddr &&
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");
552 if (mp) {
553 FILE *mdstat;
554 char hname[256];
555 gethostname(hname, sizeof(hname));
556 signal(SIGPIPE, SIG_IGN);
557 if (mailfrom)
558 fprintf(mp, "From: %s\n", mailfrom);
559 else
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);
563
564 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
565 fprintf(mp, "running on %s\n\n", hname);
566
567 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
568
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);
573
574 fprintf(mp, "Faithfully yours, etc.\n");
575
576 mdstat = fopen("/proc/mdstat", "r");
577 if (mdstat) {
578 char buf[8192];
579 int n;
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 */
583 fclose(mdstat);
584 }
585 pclose(mp);
586 }
587
588 }
589
590 /* log the event to syslog maybe */
591 if (dosyslog) {
592 /* Log at a different severity depending on the event.
593 *
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)
598 priority = LOG_CRIT;
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: */
605 else
606 priority = LOG_INFO;
607
608 if (disc)
609 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
610 else
611 syslog(priority, "%s event detected on md device %s", event, dev);
612 }
613 }
614
615 /* Not really Monitor but ... */
616 int Wait(char *dev)
617 {
618 struct stat stb;
619 int devnum;
620 int rv = 1;
621
622 if (stat(dev, &stb) != 0) {
623 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
624 strerror(errno));
625 return 2;
626 }
627 devnum = stat2devnum(&stb);
628
629 while(1) {
630 struct mdstat_ent *ms = mdstat_read(1, 0);
631 struct mdstat_ent *e;
632
633 for (e=ms ; e; e=e->next)
634 if (e->devnum == devnum)
635 break;
636
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]);
642 else
643 ping_monitor(devnum2devname(devnum));
644 }
645 free_mdstat(ms);
646 return rv;
647 }
648 free_mdstat(ms);
649 rv = 0;
650 mdstat_wait(5);
651 }
652 }