Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / mdadm / Monitor.c
blobe5d98fb0a89a5c2a99735d337468f6a8f1a94265
1 /*
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
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
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>
38 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
39 char *cmd, int dosyslog);
41 static char *percentalerts[] = {
42 "RebuildStarted",
43 "Rebuild20",
44 "Rebuild40",
45 "Rebuild60",
46 "Rebuild80",
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.
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, 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:
65 * Update time
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.
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
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
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 char *mailfrom = NULL;
118 if (!mailaddr) {
119 mailaddr = conf_get_mailaddr();
120 if (mailaddr && ! scan)
121 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
122 mailaddr);
124 mailfrom = conf_get_mailfrom();
126 if (!alert_cmd) {
127 alert_cmd = conf_get_program();
128 if (alert_cmd && ! scan)
129 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
130 alert_cmd);
132 if (scan && !mailaddr && !alert_cmd) {
133 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
134 return 1;
137 if (daemonise) {
138 int pid = fork();
139 if (pid > 0) {
140 if (!pidfile)
141 printf("%d\n", pid);
142 else {
143 FILE *pid_file;
144 pid_file=fopen(pidfile, "w");
145 if (!pid_file)
146 perror("cannot create pid file");
147 else {
148 fprintf(pid_file,"%d\n", pid);
149 fclose(pid_file);
152 return 0;
154 if (pid < 0) {
155 perror("daemonise");
156 return 1;
158 close(0);
159 open("/dev/null", 3);
160 dup2(0,1);
161 dup2(0,2);
162 setsid();
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);
169 if (st == NULL)
170 continue;
171 st->devname = strdup(mdlist->devname);
172 st->utime = 0;
173 st->next = statelist;
174 st->err = 0;
175 st->devnum = MAXINT;
176 st->percent = -2;
177 st->expected_spares = mdlist->spare_disks;
178 if (mdlist->spare_group)
179 st->spare_group = strdup(mdlist->spare_group);
180 else
181 st->spare_group = NULL;
182 statelist = st;
184 } else {
185 mddev_dev_t dv;
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);
189 if (st == NULL)
190 continue;
191 st->devname = strdup(dv->devname);
192 st->utime = 0;
193 st->next = statelist;
194 st->err = 0;
195 st->devnum = MAXINT;
196 st->percent = -2;
197 st->expected_spares = -1;
198 st->spare_group = NULL;
199 if (mdlist) {
200 st->expected_spares = mdlist->spare_disks;
201 if (mdlist->spare_group)
202 st->spare_group = strdup(mdlist->spare_group);
204 statelist = st;
209 while (! finished) {
210 int new_found = 0;
211 struct state *st;
213 if (mdstat)
214 free_mdstat(mdstat);
215 mdstat = mdstat_read(oneshot?0:1, 0);
217 for (st=statelist; st; st=st->next) {
218 struct { int state, major, minor; } info[MaxDisks];
219 mdu_array_info_t array;
220 struct mdstat_ent *mse = NULL, *mse2;
221 char *dev = st->devname;
222 int fd;
223 unsigned int i;
225 if (test)
226 alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
227 fd = open(dev, O_RDONLY);
228 if (fd < 0) {
229 if (!st->err)
230 alert("DeviceDisappeared", dev, NULL,
231 mailaddr, mailfrom, alert_cmd, dosyslog);
232 /* fprintf(stderr, Name ": cannot open %s: %s\n",
233 dev, strerror(errno));
234 */ st->err=1;
235 continue;
237 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
238 if (!st->err)
239 alert("DeviceDisappeared", dev, NULL,
240 mailaddr, mailfrom, alert_cmd, dosyslog);
241 /* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
242 dev, strerror(errno));
243 */ st->err=1;
244 close(fd);
245 continue;
247 if (array.level != 1 && array.level != 5 && array.level != -4 &&
248 array.level != 6 && array.level != 10) {
249 if (!st->err)
250 alert("DeviceDisappeared", dev, "Wrong-Level",
251 mailaddr, mailfrom, alert_cmd, dosyslog);
252 st->err = 1;
253 close(fd);
254 continue;
256 if (st->devnum == MAXINT) {
257 struct stat stb;
258 if (fstat(fd, &stb) == 0 &&
259 (S_IFMT&stb.st_mode)==S_IFBLK) {
260 if (major(stb.st_rdev) == MD_MAJOR)
261 st->devnum = minor(stb.st_rdev);
262 else
263 st->devnum = -1- (minor(stb.st_rdev)>>6);
267 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
268 if (mse2->devnum == st->devnum) {
269 mse2->devnum = MAXINT; /* flag it as "used" */
270 mse = mse2;
273 if (st->utime == array.utime &&
274 st->failed == array.failed_disks &&
275 st->working == array.working_disks &&
276 st->spare == array.spare_disks &&
277 (mse == NULL || (
278 mse->percent == st->percent
279 ))) {
280 close(fd);
281 st->err = 0;
282 continue;
284 if (st->utime == 0 && /* new array */
285 mse && /* is in /proc/mdstat */
286 mse->pattern && strchr(mse->pattern, '_') /* degraded */
288 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
290 if (st->utime == 0 && /* new array */
291 st->expected_spares > 0 &&
292 array.spare_disks < st->expected_spares)
293 alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
294 if (mse &&
295 st->percent == -1 &&
296 mse->percent >= 0)
297 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
298 if (mse &&
299 st->percent >= 0 &&
300 mse->percent >= 0 &&
301 (mse->percent / 20) > (st->percent / 20))
302 alert(percentalerts[mse->percent/20],
303 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
305 if (mse &&
306 mse->percent == -1 &&
307 st->percent >= 0) {
308 /* Rebuild/sync/whatever just finished.
309 * If there is a number in /mismatch_cnt,
310 * we should report that.
312 struct sysarray *sra =
313 sysfs_read(-1, st->devnum, GET_MISMATCH);
314 if (sra && sra->mismatch_cnt > 0) {
315 char cnt[40];
316 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
317 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
318 } else
319 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
320 if (sra)
321 free(sra);
324 if (mse)
325 st->percent = mse->percent;
328 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
329 i++) {
330 mdu_disk_info_t disc;
331 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
332 info[i].state = disc.state;
333 info[i].major = disc.major;
334 info[i].minor = disc.minor;
335 } else
336 info[i].major = info[i].minor = 0;
338 close(fd);
340 for (i=0; i<MaxDisks; i++) {
341 mdu_disk_info_t disc = {0};
342 int newstate=0;
343 int change;
344 char *dv = NULL;
345 disc.number = i;
346 if (i > array.raid_disks + array.nr_disks) {
347 newstate = 0;
348 disc.major = disc.minor = 0;
349 } else if (info[i].major || info[i].minor) {
350 newstate = info[i].state;
351 dv = map_dev(info[i].major, info[i].minor, 1);
352 disc.state = newstate;
353 disc.major = info[i].major;
354 disc.minor = info[i].minor;
355 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
356 switch(mse->pattern[i]) {
357 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
358 case '_': newstate = 0; break;
360 disc.major = disc.minor = 0;
362 if (dv == NULL && st->devid[i])
363 dv = map_dev(major(st->devid[i]),
364 minor(st->devid[i]), 1);
365 change = newstate ^ st->devstate[i];
366 if (st->utime && change && !st->err) {
367 if (i < (unsigned)array.raid_disks &&
368 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
369 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
370 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
372 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
373 else if (i >= (unsigned)array.raid_disks &&
374 (disc.major || disc.minor) &&
375 st->devid[i] == makedev(disc.major, disc.minor) &&
376 ((newstate&change)&(1<<MD_DISK_FAULTY))
378 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
379 else if (i < (unsigned)array.raid_disks &&
380 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
381 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
382 ((newstate&change)&(1<<MD_DISK_SYNC)))
384 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
386 st->devstate[i] = disc.state;
387 st->devid[i] = makedev(disc.major, disc.minor);
389 st->active = array.active_disks;
390 st->working = array.working_disks;
391 st->spare = array.spare_disks;
392 st->failed = array.failed_disks;
393 st->utime = array.utime;
394 st->raid = array.raid_disks;
395 st->err = 0;
397 /* now check if there are any new devices found in mdstat */
398 if (scan) {
399 struct mdstat_ent *mse;
400 for (mse=mdstat; mse; mse=mse->next)
401 if (mse->devnum != MAXINT &&
402 (strcmp(mse->level, "raid1")==0 ||
403 strcmp(mse->level, "raid5")==0 ||
404 strcmp(mse->level, "multipath")==0)
406 struct state *st = malloc(sizeof *st);
407 mdu_array_info_t array;
408 int fd;
409 if (st == NULL)
410 continue;
411 st->devname = strdup(get_md_name(mse->devnum));
412 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
413 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
414 /* no such array */
415 if (fd >=0) close(fd);
416 put_md_name(st->devname);
417 free(st->devname);
418 free(st);
419 continue;
421 close(fd);
422 st->utime = 0;
423 st->next = statelist;
424 st->err = 1;
425 st->devnum = mse->devnum;
426 st->percent = -2;
427 st->spare_group = NULL;
428 st->expected_spares = -1;
429 statelist = st;
430 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
431 new_found = 1;
434 /* If an array has active < raid && spare == 0 && spare_group != NULL
435 * Look for another array with spare > 0 and active == raid and same spare_group
436 * if found, choose a device and hotremove/hotadd
438 for (st = statelist; st; st=st->next)
439 if (st->active < st->raid &&
440 st->spare == 0 &&
441 st->spare_group != NULL) {
442 struct state *st2;
443 for (st2=statelist ; st2 ; st2=st2->next)
444 if (st2 != st &&
445 st2->spare > 0 &&
446 st2->active == st2->raid &&
447 st2->spare_group != NULL &&
448 strcmp(st->spare_group, st2->spare_group) == 0) {
449 /* try to remove and add */
450 int fd1 = open(st->devname, O_RDONLY);
451 int fd2 = open(st2->devname, O_RDONLY);
452 int dev = -1;
453 int d;
454 if (fd1 < 0 || fd2 < 0) {
455 if (fd1>=0) close(fd1);
456 if (fd2>=0) close(fd2);
457 continue;
459 for (d=st2->raid; d < MaxDisks; d++) {
460 if (st2->devid[d] > 0 &&
461 st2->devstate[d] == 0) {
462 dev = st2->devid[d];
463 break;
466 if (dev > 0) {
467 if (ioctl(fd2, HOT_REMOVE_DISK,
468 (unsigned long)dev) == 0) {
469 if (ioctl(fd1, HOT_ADD_DISK,
470 (unsigned long)dev) == 0) {
471 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
472 close(fd1);
473 close(fd2);
474 break;
476 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
479 close(fd1);
480 close(fd2);
483 if (!new_found) {
484 if (oneshot)
485 break;
486 else
487 mdstat_wait(period);
489 test = 0;
491 if (pidfile)
492 unlink(pidfile);
493 return 0;
497 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
498 int dosyslog)
500 int priority;
502 if (!cmd && !mailaddr) {
503 time_t now = time(0);
505 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
507 if (cmd) {
508 int pid = fork();
509 switch(pid) {
510 default:
511 waitpid(pid, NULL, 0);
512 break;
513 case -1:
514 break;
515 case 0:
516 execl(cmd, cmd, event, dev, disc, NULL);
517 exit(2);
520 if (mailaddr &&
521 (strncmp(event, "Fail", 4)==0 ||
522 strncmp(event, "Test", 4)==0 ||
523 strncmp(event, "Spares", 6)==0 ||
524 strncmp(event, "Degrade", 7)==0)) {
525 FILE *mp = popen(Sendmail, "w");
526 if (mp) {
527 FILE *mdstat;
528 char hname[256];
529 gethostname(hname, sizeof(hname));
530 signal(SIGPIPE, SIG_IGN);
531 if (mailfrom)
532 fprintf(mp, "From: %s\n", mailfrom);
533 else
534 fprintf(mp, "From: " Name " monitoring <root>\n");
535 fprintf(mp, "To: %s\n", mailaddr);
536 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
538 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
539 fprintf(mp, "running on %s\n\n", hname);
541 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
543 if (disc && disc[0] != ' ')
544 fprintf(mp, "It could be related to component device %s.\n\n", disc);
545 if (disc && disc[0] == ' ')
546 fprintf(mp, "Extra information:%s.\n\n", disc);
548 fprintf(mp, "Faithfully yours, etc.\n");
550 mdstat = fopen("/proc/mdstat", "r");
551 if (mdstat) {
552 char buf[8192];
553 int n;
554 fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
555 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
556 n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
557 fclose(mdstat);
559 fclose(mp);
564 /* log the event to syslog maybe */
565 if (dosyslog) {
566 /* Log at a different severity depending on the event.
568 * These are the critical events: */
569 if (strncmp(event, "Fail", 4)==0 ||
570 strncmp(event, "Degrade", 7)==0 ||
571 strncmp(event, "DeviceDisappeared", 17)==0)
572 priority = LOG_CRIT;
573 /* Good to know about, but are not failures: */
574 else if (strncmp(event, "Rebuild", 7)==0 ||
575 strncmp(event, "MoveSpare", 9)==0 ||
576 strncmp(event, "Spares", 6) != 0)
577 priority = LOG_WARNING;
578 /* Everything else: */
579 else
580 priority = LOG_INFO;
582 if (disc)
583 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
584 else
585 syslog(priority, "%s event detected on md device %s", event, dev);
589 /* Not really Monitor but ... */
590 int Wait(char *dev)
592 struct stat stb;
593 int devnum;
594 int rv = 1;
596 if (stat(dev, &stb) != 0) {
597 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
598 strerror(errno));
599 return 2;
601 if (major(stb.st_rdev) == MD_MAJOR)
602 devnum = minor(stb.st_rdev);
603 else
604 devnum = -1-(minor(stb.st_rdev)/64);
606 while(1) {
607 struct mdstat_ent *ms = mdstat_read(1, 0);
608 struct mdstat_ent *e;
610 for (e=ms ; e; e=e->next)
611 if (e->devnum == devnum)
612 break;
614 if (!e || e->percent < 0) {
615 free_mdstat(ms);
616 return rv;
618 free(ms);
619 rv = 0;
620 mdstat_wait(5);