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