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