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