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