]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Use O_DIRECT to read bitmap files.
[thirdparty/mdadm.git] / Monitor.c
CommitLineData
52826846 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
52826846 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
52826846
NB
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
9a9dab36 30#include "mdadm.h"
52826846
NB
31#include "md_p.h"
32#include "md_u.h"
e0d19036 33#include <sys/wait.h>
52826846 34#include <sys/signal.h>
98c6faba 35#include <values.h>
773135f5 36#include <syslog.h>
52826846 37
773135f5
NB
38static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd,
39 int dosyslog);
52826846 40
e0d19036
NB
41static char *percentalerts[] = {
42 "RebuildStarted",
43 "Rebuild20",
44 "Rebuild40",
45 "Rebuild60",
46 "Rebuild80",
47};
48
e27d562b
NB
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
cd29a5c8 54int Monitor(mddev_dev_t devlist,
52826846 55 char *mailaddr, char *alert_cmd,
aa88f531 56 int period, int daemonise, int scan, int oneshot,
773135f5 57 int dosyslog, char *config, int test, char* pidfile)
52826846
NB
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.
e0d19036 68 * %rebuilt if rebuilding
52826846
NB
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.
e0d19036
NB
73 * We also read /proc/mdstat to get rebuild percent,
74 * and to get state on all active devices incase of kernel bug.
52826846 75 *
e0d19036
NB
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
52826846
NB
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 *
e0d19036
NB
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
52826846
NB
99 */
100
101 struct state {
102 char *devname;
e0d19036 103 int devnum; /* to sync with mdstat info */
52826846
NB
104 long utime;
105 int err;
e0d19036
NB
106 char *spare_group;
107 int active, working, failed, spare, raid;
feb716e9 108 int expected_spares;
e27d562b
NB
109 int devstate[MaxDisks];
110 int devid[MaxDisks];
e0d19036 111 int percent;
52826846
NB
112 struct state *next;
113 } *statelist = NULL;
114 int finished = 0;
e0d19036
NB
115 struct mdstat_ent *mdstat = NULL;
116
d013a55e 117 if (!mailaddr) {
e0d19036 118 mailaddr = conf_get_mailaddr(config);
d013a55e 119 if (mailaddr && ! scan)
56eedc1a 120 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
d013a55e
NB
121 mailaddr);
122 }
123 if (!alert_cmd) {
e0d19036 124 alert_cmd = conf_get_program(config);
d013a55e 125 if (alert_cmd && ! scan)
56eedc1a 126 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
d013a55e
NB
127 alert_cmd);
128 }
56eedc1a
NB
129 if (scan && !mailaddr && !alert_cmd) {
130 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
d013a55e 131 return 1;
56eedc1a 132 }
d013a55e
NB
133
134 if (daemonise) {
135 int pid = fork();
136 if (pid > 0) {
b5e64645
NB
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 }
d013a55e
NB
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 }
e0d19036
NB
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;
d013a55e 171 st->err = 0;
98c6faba 172 st->devnum = MAXINT;
e0d19036 173 st->percent = -2;
feb716e9 174 st->expected_spares = mdlist->spare_disks;
e0d19036
NB
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 {
cd29a5c8 182 mddev_dev_t dv;
e0d19036 183 for (dv=devlist ; dv; dv=dv->next) {
e5329c37 184 mddev_ident_t mdlist = conf_get_ident(config, dv->devname);
e0d19036
NB
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;
d013a55e 191 st->err = 0;
98c6faba 192 st->devnum = MAXINT;
e0d19036 193 st->percent = -2;
feb716e9 194 st->expected_spares = -1;
e0d19036 195 st->spare_group = NULL;
e5329c37
NB
196 if (mdlist) {
197 st->expected_spares = mdlist->spare_disks;
198 if (mdlist->spare_group)
199 st->spare_group = strdup(mdlist->spare_group);
200 }
e0d19036
NB
201 statelist = st;
202 }
203 }
204
205
206 while (! finished) {
aa88f531 207 int new_found = 0;
e0d19036
NB
208 struct state *st;
209
210 if (mdstat)
211 free_mdstat(mdstat);
22a88995 212 mdstat = mdstat_read(oneshot?0:1, 0);
e0d19036
NB
213
214 for (st=statelist; st; st=st->next) {
52826846 215 mdu_array_info_t array;
dd0781e5 216 struct mdstat_ent *mse = NULL, *mse2;
e0d19036 217 char *dev = st->devname;
52826846 218 int fd;
98c6faba 219 unsigned int i;
e0d19036 220
98c6faba 221 if (test)
773135f5 222 alert("TestMessage", dev, NULL, mailaddr, alert_cmd, dosyslog);
52826846
NB
223 fd = open(dev, O_RDONLY);
224 if (fd < 0) {
225 if (!st->err)
e0d19036 226 alert("DeviceDisappeared", dev, NULL,
773135f5 227 mailaddr, alert_cmd, dosyslog);
e0d19036 228/* fprintf(stderr, Name ": cannot open %s: %s\n",
52826846 229 dev, strerror(errno));
e0d19036 230*/ st->err=1;
52826846
NB
231 continue;
232 }
233 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
234 if (!st->err)
e0d19036 235 alert("DeviceDisappeared", dev, NULL,
773135f5 236 mailaddr, alert_cmd, dosyslog);
e0d19036 237/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
52826846 238 dev, strerror(errno));
e0d19036 239*/ st->err=1;
52826846
NB
240 close(fd);
241 continue;
242 }
e5329c37
NB
243 if (array.level != 1 && array.level != 5 && array.level != -4 &&
244 array.level != 6 && array.level != 10) {
5787fa49
NB
245 if (!st->err)
246 alert("DeviceDisappeared", dev, "Wrong-Level",
773135f5 247 mailaddr, alert_cmd, dosyslog);
5787fa49
NB
248 st->err = 1;
249 close(fd);
250 continue;
251 }
98c6faba 252 if (st->devnum == MAXINT) {
e0d19036
NB
253 struct stat stb;
254 if (fstat(fd, &stb) == 0 &&
98c6faba 255 (S_IFMT&stb.st_mode)==S_IFBLK) {
0df46c2a
NB
256 if (major(stb.st_rdev) == MD_MAJOR)
257 st->devnum = minor(stb.st_rdev);
98c6faba 258 else
0df46c2a 259 st->devnum = -1- (minor(stb.st_rdev)>>6);
98c6faba 260 }
e0d19036
NB
261 }
262
dd0781e5
NB
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 }
e0d19036 268
52826846 269 if (st->utime == array.utime &&
e0d19036
NB
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 ))) {
52826846 276 close(fd);
e0d19036 277 st->err = 0;
52826846
NB
278 continue;
279 }
aa88f531
NB
280 if (st->utime == 0 && /* new array */
281 mse && /* is in /proc/mdstat */
282 mse->pattern && strchr(mse->pattern, '_') /* degraded */
283 )
773135f5 284 alert("DegradedArray", dev, NULL, mailaddr, alert_cmd, dosyslog);
aa88f531 285
feb716e9
NB
286 if (st->utime == 0 && /* new array */
287 st->expected_spares > 0 &&
288 array.spare_disks < st->expected_spares)
773135f5 289 alert("SparesMissing", dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
290 if (mse &&
291 st->percent == -1 &&
292 mse->percent >= 0)
773135f5 293 alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
294 if (mse &&
295 st->percent >= 0 &&
296 mse->percent >= 0 &&
297 (mse->percent / 20) > (st->percent / 20))
298 alert(percentalerts[mse->percent/20],
773135f5 299 dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036 300
98c6faba
NB
301 if (mse &&
302 mse->percent == -1 &&
303 st->percent >= 0)
773135f5 304 alert("RebuildFinished", dev, NULL, mailaddr, alert_cmd, dosyslog);
98c6faba 305
e0d19036
NB
306 if (mse)
307 st->percent = mse->percent;
e27d562b
NB
308
309 for (i=0; i<MaxDisks; i++) {
52826846 310 mdu_disk_info_t disc;
e0d19036
NB
311 int newstate=0;
312 int change;
313 char *dv = NULL;
52826846 314 disc.number = i;
e27d562b
NB
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) {
e0d19036 319 newstate = disc.state;
16c6fa80 320 dv = map_dev(disc.major, disc.minor, 1);
e27d562b 321 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
e0d19036
NB
322 switch(mse->pattern[i]) {
323 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
324 case '_': newstate = 0; break;
52826846 325 }
e27d562b
NB
326 disc.major = disc.minor = 0;
327 }
fe394e5e
NB
328 if (dv == NULL && st->devid[i])
329 dv = map_dev(major(st->devid[i]),
16c6fa80 330 minor(st->devid[i]), 1);
e0d19036
NB
331 change = newstate ^ st->devstate[i];
332 if (st->utime && change && !st->err) {
98c6faba 333 if (i < (unsigned)array.raid_disks &&
e0d19036
NB
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 )
773135f5 338 alert("Fail", dev, dv, mailaddr, alert_cmd, dosyslog);
98c6faba 339 else if (i >= (unsigned)array.raid_disks &&
e0d19036 340 (disc.major || disc.minor) &&
0df46c2a 341 st->devid[i] == makedev(disc.major, disc.minor) &&
e0d19036
NB
342 ((newstate&change)&(1<<MD_DISK_FAULTY))
343 )
773135f5 344 alert("FailSpare", dev, dv, mailaddr, alert_cmd, dosyslog);
98c6faba 345 else if (i < (unsigned)array.raid_disks &&
e0d19036
NB
346 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
347 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
348 ((newstate&change)&(1<<MD_DISK_SYNC)))
349 )
773135f5 350 alert("SpareActive", dev, dv, mailaddr, alert_cmd, dosyslog);
52826846 351 }
e0d19036 352 st->devstate[i] = disc.state;
0df46c2a 353 st->devid[i] = makedev(disc.major, disc.minor);
52826846
NB
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;
e0d19036
NB
361 st->raid = array.raid_disks;
362 st->err = 0;
52826846 363 }
e0d19036
NB
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)
98c6faba 368 if (mse->devnum != MAXINT &&
5787fa49
NB
369 (strcmp(mse->level, "raid1")==0 ||
370 strcmp(mse->level, "raid5")==0 ||
371 strcmp(mse->level, "multipath")==0)
372 ) {
e0d19036 373 struct state *st = malloc(sizeof *st);
98c6faba
NB
374 mdu_array_info_t array;
375 int fd;
e0d19036
NB
376 if (st == NULL)
377 continue;
378 st->devname = strdup(get_md_name(mse->devnum));
98c6faba
NB
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 }
dd0781e5 387 close(fd);
e0d19036
NB
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;
98c6faba 394 st->expected_spares = -1;
e0d19036 395 statelist = st;
773135f5 396 alert("NewArray", st->devname, NULL, mailaddr, alert_cmd, dosyslog);
aa88f531 397 new_found = 1;
e0d19036
NB
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 }
e27d562b 425 for (d=st2->raid; d < MaxDisks; d++) {
e0d19036
NB
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) {
773135f5 437 alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
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 }
aa88f531
NB
449 if (!new_found) {
450 if (oneshot)
451 break;
452 else
dd0781e5 453 mdstat_wait(period);
aa88f531 454 }
98c6faba 455 test = 0;
52826846 456 }
b5e64645
NB
457 if (pidfile)
458 unlink(pidfile);
52826846
NB
459 return 0;
460}
461
462
773135f5
NB
463static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd,
464 int dosyslog)
52826846 465{
773135f5
NB
466 int priority;
467
cd29a5c8
NB
468 if (!cmd && !mailaddr) {
469 time_t now = time(0);
470
e0d19036 471 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 472 }
52826846
NB
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 }
aa88f531
NB
486 if (mailaddr &&
487 (strncmp(event, "Fail", 4)==0 ||
98c6faba 488 strncmp(event, "Test", 4)==0 ||
aa88f531 489 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
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)
56eedc1a 505 fprintf(mp, "It could be related to component device %s.\n\n", disc);
52826846
NB
506
507 fprintf(mp, "Faithfully yours, etc.\n");
508 fclose(mp);
509 }
510
511 }
773135f5
NB
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 }
52826846 535}