]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Create missing /dev files where needed.
[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
cd29a5c8 49int Monitor(mddev_dev_t devlist,
52826846 50 char *mailaddr, char *alert_cmd,
aa88f531 51 int period, int daemonise, int scan, int oneshot,
773135f5 52 int dosyslog, char *config, int test, char* pidfile)
52826846
NB
53{
54 /*
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
58 *
59 * For each array, we record:
60 * Update time
61 * active/working/failed/spare drives
62 * State of each device.
e0d19036 63 * %rebuilt if rebuilding
52826846
NB
64 *
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.
e0d19036
NB
68 * We also read /proc/mdstat to get rebuild percent,
69 * and to get state on all active devices incase of kernel bug.
52826846 70 *
e0d19036
NB
71 * Events are:
72 * Fail
73 * An active device had Faulty set or Active/Sync removed
74 * FailSpare
75 * A spare device had Faulty set
76 * SpareActive
77 * An active device had a reverse transition
78 * RebuildStarted
79 * percent went from -1 to +ve
80 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
81 * percent went from below to not-below that number
82 * DeviceDisappeared
83 * Couldn't access a device which was previously visible
52826846
NB
84 *
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
90 *
e0d19036
NB
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
52826846
NB
94 */
95
96 struct state {
97 char *devname;
e0d19036 98 int devnum; /* to sync with mdstat info */
52826846
NB
99 long utime;
100 int err;
e0d19036
NB
101 char *spare_group;
102 int active, working, failed, spare, raid;
feb716e9 103 int expected_spares;
52826846 104 int devstate[MD_SB_DISKS];
e0d19036
NB
105 int devid[MD_SB_DISKS];
106 int percent;
52826846
NB
107 struct state *next;
108 } *statelist = NULL;
109 int finished = 0;
e0d19036
NB
110 struct mdstat_ent *mdstat = NULL;
111
d013a55e 112 if (!mailaddr) {
e0d19036 113 mailaddr = conf_get_mailaddr(config);
d013a55e 114 if (mailaddr && ! scan)
56eedc1a 115 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
d013a55e
NB
116 mailaddr);
117 }
118 if (!alert_cmd) {
e0d19036 119 alert_cmd = conf_get_program(config);
d013a55e 120 if (alert_cmd && ! scan)
56eedc1a 121 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
d013a55e
NB
122 alert_cmd);
123 }
56eedc1a
NB
124 if (scan && !mailaddr && !alert_cmd) {
125 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
d013a55e 126 return 1;
56eedc1a 127 }
d013a55e
NB
128
129 if (daemonise) {
130 int pid = fork();
131 if (pid > 0) {
b5e64645
NB
132 if (!pidfile)
133 printf("%d\n", pid);
134 else {
135 FILE *pid_file;
136 pid_file=fopen(pidfile, "w");
137 if (!pid_file)
138 perror("cannot create pid file");
139 else {
140 fprintf(pid_file,"%d\n", pid);
141 fclose(pid_file);
142 }
143 }
d013a55e
NB
144 return 0;
145 }
146 if (pid < 0) {
147 perror("daemonise");
148 return 1;
149 }
150 close(0);
151 open("/dev/null", 3);
152 dup2(0,1);
153 dup2(0,2);
154 setsid();
155 }
e0d19036
NB
156
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);
161 if (st == NULL)
162 continue;
163 st->devname = strdup(mdlist->devname);
164 st->utime = 0;
165 st->next = statelist;
d013a55e 166 st->err = 0;
98c6faba 167 st->devnum = MAXINT;
e0d19036 168 st->percent = -2;
feb716e9 169 st->expected_spares = mdlist->spare_disks;
e0d19036
NB
170 if (mdlist->spare_group)
171 st->spare_group = strdup(mdlist->spare_group);
172 else
173 st->spare_group = NULL;
174 statelist = st;
175 }
176 } else {
cd29a5c8 177 mddev_dev_t dv;
e0d19036 178 for (dv=devlist ; dv; dv=dv->next) {
e5329c37 179 mddev_ident_t mdlist = conf_get_ident(config, dv->devname);
e0d19036
NB
180 struct state *st = malloc(sizeof *st);
181 if (st == NULL)
182 continue;
183 st->devname = strdup(dv->devname);
184 st->utime = 0;
185 st->next = statelist;
d013a55e 186 st->err = 0;
98c6faba 187 st->devnum = MAXINT;
e0d19036 188 st->percent = -2;
feb716e9 189 st->expected_spares = -1;
e0d19036 190 st->spare_group = NULL;
e5329c37
NB
191 if (mdlist) {
192 st->expected_spares = mdlist->spare_disks;
193 if (mdlist->spare_group)
194 st->spare_group = strdup(mdlist->spare_group);
195 }
e0d19036
NB
196 statelist = st;
197 }
198 }
199
200
201 while (! finished) {
aa88f531 202 int new_found = 0;
e0d19036
NB
203 struct state *st;
204
205 if (mdstat)
206 free_mdstat(mdstat);
22a88995 207 mdstat = mdstat_read(oneshot?0:1, 0);
e0d19036
NB
208
209 for (st=statelist; st; st=st->next) {
52826846 210 mdu_array_info_t array;
dd0781e5 211 struct mdstat_ent *mse = NULL, *mse2;
e0d19036 212 char *dev = st->devname;
52826846 213 int fd;
98c6faba 214 unsigned int i;
e0d19036 215
98c6faba 216 if (test)
773135f5 217 alert("TestMessage", dev, NULL, mailaddr, alert_cmd, dosyslog);
52826846
NB
218 fd = open(dev, O_RDONLY);
219 if (fd < 0) {
220 if (!st->err)
e0d19036 221 alert("DeviceDisappeared", dev, NULL,
773135f5 222 mailaddr, alert_cmd, dosyslog);
e0d19036 223/* fprintf(stderr, Name ": cannot open %s: %s\n",
52826846 224 dev, strerror(errno));
e0d19036 225*/ st->err=1;
52826846
NB
226 continue;
227 }
228 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
229 if (!st->err)
e0d19036 230 alert("DeviceDisappeared", dev, NULL,
773135f5 231 mailaddr, alert_cmd, dosyslog);
e0d19036 232/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
52826846 233 dev, strerror(errno));
e0d19036 234*/ st->err=1;
52826846
NB
235 close(fd);
236 continue;
237 }
e5329c37
NB
238 if (array.level != 1 && array.level != 5 && array.level != -4 &&
239 array.level != 6 && array.level != 10) {
5787fa49
NB
240 if (!st->err)
241 alert("DeviceDisappeared", dev, "Wrong-Level",
773135f5 242 mailaddr, alert_cmd, dosyslog);
5787fa49
NB
243 st->err = 1;
244 close(fd);
245 continue;
246 }
98c6faba 247 if (st->devnum == MAXINT) {
e0d19036
NB
248 struct stat stb;
249 if (fstat(fd, &stb) == 0 &&
98c6faba 250 (S_IFMT&stb.st_mode)==S_IFBLK) {
0df46c2a
NB
251 if (major(stb.st_rdev) == MD_MAJOR)
252 st->devnum = minor(stb.st_rdev);
98c6faba 253 else
0df46c2a 254 st->devnum = -1- (minor(stb.st_rdev)>>6);
98c6faba 255 }
e0d19036
NB
256 }
257
dd0781e5
NB
258 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
259 if (mse2->devnum == st->devnum) {
260 mse2->devnum = MAXINT; /* flag it as "used" */
261 mse = mse2;
262 }
e0d19036 263
52826846 264 if (st->utime == array.utime &&
e0d19036
NB
265 st->failed == array.failed_disks &&
266 st->working == array.working_disks &&
267 st->spare == array.spare_disks &&
268 (mse == NULL || (
269 mse->percent == st->percent
270 ))) {
52826846 271 close(fd);
e0d19036 272 st->err = 0;
52826846
NB
273 continue;
274 }
aa88f531
NB
275 if (st->utime == 0 && /* new array */
276 mse && /* is in /proc/mdstat */
277 mse->pattern && strchr(mse->pattern, '_') /* degraded */
278 )
773135f5 279 alert("DegradedArray", dev, NULL, mailaddr, alert_cmd, dosyslog);
aa88f531 280
feb716e9
NB
281 if (st->utime == 0 && /* new array */
282 st->expected_spares > 0 &&
283 array.spare_disks < st->expected_spares)
773135f5 284 alert("SparesMissing", dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
285 if (mse &&
286 st->percent == -1 &&
287 mse->percent >= 0)
773135f5 288 alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
289 if (mse &&
290 st->percent >= 0 &&
291 mse->percent >= 0 &&
292 (mse->percent / 20) > (st->percent / 20))
293 alert(percentalerts[mse->percent/20],
773135f5 294 dev, NULL, mailaddr, alert_cmd, dosyslog);
e0d19036 295
98c6faba
NB
296 if (mse &&
297 mse->percent == -1 &&
298 st->percent >= 0)
773135f5 299 alert("RebuildFinished", dev, NULL, mailaddr, alert_cmd, dosyslog);
98c6faba 300
e0d19036
NB
301 if (mse)
302 st->percent = mse->percent;
303
304 for (i=0; i<MD_SB_DISKS; i++) {
52826846 305 mdu_disk_info_t disc;
e0d19036
NB
306 int newstate=0;
307 int change;
308 char *dv = NULL;
52826846
NB
309 disc.number = i;
310 if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
e0d19036 311 newstate = disc.state;
16c6fa80 312 dv = map_dev(disc.major, disc.minor, 1);
5787fa49 313 } else if (mse && mse->pattern && i < strlen(mse->pattern))
e0d19036
NB
314 switch(mse->pattern[i]) {
315 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
316 case '_': newstate = 0; break;
52826846 317 }
fe394e5e
NB
318 if (dv == NULL && st->devid[i])
319 dv = map_dev(major(st->devid[i]),
16c6fa80 320 minor(st->devid[i]), 1);
e0d19036
NB
321 change = newstate ^ st->devstate[i];
322 if (st->utime && change && !st->err) {
98c6faba 323 if (i < (unsigned)array.raid_disks &&
e0d19036
NB
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)))
327 )
773135f5 328 alert("Fail", dev, dv, mailaddr, alert_cmd, dosyslog);
98c6faba 329 else if (i >= (unsigned)array.raid_disks &&
e0d19036 330 (disc.major || disc.minor) &&
0df46c2a 331 st->devid[i] == makedev(disc.major, disc.minor) &&
e0d19036
NB
332 ((newstate&change)&(1<<MD_DISK_FAULTY))
333 )
773135f5 334 alert("FailSpare", dev, dv, mailaddr, alert_cmd, dosyslog);
98c6faba 335 else if (i < (unsigned)array.raid_disks &&
e0d19036
NB
336 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
337 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
338 ((newstate&change)&(1<<MD_DISK_SYNC)))
339 )
773135f5 340 alert("SpareActive", dev, dv, mailaddr, alert_cmd, dosyslog);
52826846 341 }
e0d19036 342 st->devstate[i] = disc.state;
0df46c2a 343 st->devid[i] = makedev(disc.major, disc.minor);
52826846
NB
344 }
345 close(fd);
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;
e0d19036
NB
351 st->raid = array.raid_disks;
352 st->err = 0;
52826846 353 }
e0d19036
NB
354 /* now check if there are any new devices found in mdstat */
355 if (scan) {
356 struct mdstat_ent *mse;
357 for (mse=mdstat; mse; mse=mse->next)
98c6faba 358 if (mse->devnum != MAXINT &&
5787fa49
NB
359 (strcmp(mse->level, "raid1")==0 ||
360 strcmp(mse->level, "raid5")==0 ||
361 strcmp(mse->level, "multipath")==0)
362 ) {
e0d19036 363 struct state *st = malloc(sizeof *st);
98c6faba
NB
364 mdu_array_info_t array;
365 int fd;
e0d19036
NB
366 if (st == NULL)
367 continue;
368 st->devname = strdup(get_md_name(mse->devnum));
98c6faba
NB
369 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
370 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
371 /* no such array */
372 if (fd >=0) close(fd);
373 free(st->devname);
374 free(st);
375 continue;
376 }
dd0781e5 377 close(fd);
e0d19036
NB
378 st->utime = 0;
379 st->next = statelist;
380 st->err = 1;
381 st->devnum = mse->devnum;
382 st->percent = -2;
383 st->spare_group = NULL;
98c6faba 384 st->expected_spares = -1;
e0d19036 385 statelist = st;
773135f5 386 alert("NewArray", st->devname, NULL, mailaddr, alert_cmd, dosyslog);
aa88f531 387 new_found = 1;
e0d19036
NB
388 }
389 }
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
393 */
394 for (st = statelist; st; st=st->next)
395 if (st->active < st->raid &&
396 st->spare == 0 &&
397 st->spare_group != NULL) {
398 struct state *st2;
399 for (st2=statelist ; st2 ; st2=st2->next)
400 if (st2 != st &&
401 st2->spare > 0 &&
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);
408 int dev = -1;
409 int d;
410 if (fd1 < 0 || fd2 < 0) {
411 if (fd1>=0) close(fd1);
412 if (fd2>=0) close(fd2);
413 continue;
414 }
415 for (d=st2->raid; d<MD_SB_DISKS; d++) {
416 if (st2->devid[d] > 0 &&
417 st2->devstate[d] == 0) {
418 dev = st2->devid[d];
419 break;
420 }
421 }
422 if (dev > 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) {
773135f5 427 alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd, dosyslog);
e0d19036
NB
428 close(fd1);
429 close(fd2);
430 break;
431 }
432 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
433 }
434 }
435 close(fd1);
436 close(fd2);
437 }
438 }
aa88f531
NB
439 if (!new_found) {
440 if (oneshot)
441 break;
442 else
dd0781e5 443 mdstat_wait(period);
aa88f531 444 }
98c6faba 445 test = 0;
52826846 446 }
b5e64645
NB
447 if (pidfile)
448 unlink(pidfile);
52826846
NB
449 return 0;
450}
451
452
773135f5
NB
453static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd,
454 int dosyslog)
52826846 455{
773135f5
NB
456 int priority;
457
cd29a5c8
NB
458 if (!cmd && !mailaddr) {
459 time_t now = time(0);
460
e0d19036 461 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 462 }
52826846
NB
463 if (cmd) {
464 int pid = fork();
465 switch(pid) {
466 default:
467 waitpid(pid, NULL, 0);
468 break;
469 case -1:
470 break;
471 case 0:
472 execl(cmd, cmd, event, dev, disc, NULL);
473 exit(2);
474 }
475 }
aa88f531
NB
476 if (mailaddr &&
477 (strncmp(event, "Fail", 4)==0 ||
98c6faba 478 strncmp(event, "Test", 4)==0 ||
aa88f531 479 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
480 FILE *mp = popen(Sendmail, "w");
481 if (mp) {
482 char hname[256];
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);
488
489 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
490 fprintf(mp, "running on %s\n\n", hname);
491
492 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
493
494 if (disc)
56eedc1a 495 fprintf(mp, "It could be related to component device %s.\n\n", disc);
52826846
NB
496
497 fprintf(mp, "Faithfully yours, etc.\n");
498 fclose(mp);
499 }
500
501 }
773135f5
NB
502
503 /* log the event to syslog maybe */
504 if (dosyslog) {
505 /* Log at a different severity depending on the event.
506 *
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)
511 priority = LOG_CRIT;
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: */
517 else
518 priority = LOG_INFO;
519
520 if (disc)
521 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
522 else
523 syslog(priority, "%s event detected on md device %s", event, dev);
524 }
52826846 525}