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