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