]>
Commit | Line | Data |
---|---|---|
52826846 | 1 | /* |
9a9dab36 | 2 | * mdadm - manage Linux "md" devices aka RAID arrays. |
52826846 | 3 | * |
4f589ad0 | 4 | * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de> |
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> |
98127a6d | 34 | #include <signal.h> |
4450e59f | 35 | #include <limits.h> |
773135f5 | 36 | #include <syslog.h> |
52826846 | 37 | |
4948b8f7 NB |
38 | static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, |
39 | char *cmd, int dosyslog); | |
52826846 | 40 | |
aba69144 | 41 | static char *percentalerts[] = { |
e0d19036 NB |
42 | "RebuildStarted", |
43 | "Rebuild20", | |
44 | "Rebuild40", | |
45 | "Rebuild60", | |
46 | "Rebuild80", | |
47 | }; | |
48 | ||
e27d562b NB |
49 | /* The largest number of disks current arrays can manage is 384 |
50 | * This really should be dynamically, but that will have to wait | |
51 | * At least it isn't MD_SB_DISKS. | |
52 | */ | |
53 | #define MaxDisks 384 | |
cd29a5c8 | 54 | int Monitor(mddev_dev_t devlist, |
52826846 | 55 | char *mailaddr, char *alert_cmd, |
aa88f531 | 56 | int period, int daemonise, int scan, int oneshot, |
8aec876d | 57 | int dosyslog, int test, char* pidfile) |
52826846 NB |
58 | { |
59 | /* | |
60 | * Every few seconds, scan every md device looking for changes | |
61 | * When a change is found, log it, possibly run the alert command, | |
62 | * and possibly send Email | |
63 | * | |
64 | * For each array, we record: | |
65 | * Update time | |
66 | * active/working/failed/spare drives | |
67 | * State of each device. | |
e0d19036 | 68 | * %rebuilt if rebuilding |
52826846 NB |
69 | * |
70 | * If the update time changes, check out all the data again | |
71 | * It is possible that we cannot get the state of each device | |
72 | * due to bugs in the md kernel module. | |
e0d19036 NB |
73 | * We also read /proc/mdstat to get rebuild percent, |
74 | * and to get state on all active devices incase of kernel bug. | |
52826846 | 75 | * |
e0d19036 NB |
76 | * Events are: |
77 | * Fail | |
78 | * An active device had Faulty set or Active/Sync removed | |
79 | * FailSpare | |
80 | * A spare device had Faulty set | |
81 | * SpareActive | |
82 | * An active device had a reverse transition | |
83 | * RebuildStarted | |
84 | * percent went from -1 to +ve | |
85 | * Rebuild20 Rebuild40 Rebuild60 Rebuild80 | |
86 | * percent went from below to not-below that number | |
87 | * DeviceDisappeared | |
88 | * Couldn't access a device which was previously visible | |
52826846 NB |
89 | * |
90 | * if we detect an array with active<raid and spare==0 | |
91 | * we look at other arrays that have same spare-group | |
92 | * If we find one with active==raid and spare>0, | |
93 | * and if we can get_disk_info and find a name | |
94 | * Then we hot-remove and hot-add to the other array | |
95 | * | |
e0d19036 NB |
96 | * If devlist is NULL, then we can monitor everything because --scan |
97 | * was given. We get an initial list from config file and add anything | |
98 | * that appears in /proc/mdstat | |
52826846 NB |
99 | */ |
100 | ||
101 | struct state { | |
102 | char *devname; | |
e0d19036 | 103 | int devnum; /* to sync with mdstat info */ |
52826846 NB |
104 | long utime; |
105 | int err; | |
e0d19036 NB |
106 | char *spare_group; |
107 | int active, working, failed, spare, raid; | |
feb716e9 | 108 | int expected_spares; |
e27d562b NB |
109 | int devstate[MaxDisks]; |
110 | int devid[MaxDisks]; | |
e0d19036 | 111 | int percent; |
52826846 NB |
112 | struct state *next; |
113 | } *statelist = NULL; | |
114 | int finished = 0; | |
e0d19036 | 115 | struct mdstat_ent *mdstat = NULL; |
4948b8f7 | 116 | char *mailfrom = NULL; |
e0d19036 | 117 | |
d013a55e | 118 | if (!mailaddr) { |
8aec876d | 119 | mailaddr = conf_get_mailaddr(); |
d013a55e | 120 | if (mailaddr && ! scan) |
56eedc1a | 121 | fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n", |
d013a55e NB |
122 | mailaddr); |
123 | } | |
8aec876d | 124 | mailfrom = conf_get_mailfrom(); |
4948b8f7 | 125 | |
d013a55e | 126 | if (!alert_cmd) { |
8aec876d | 127 | alert_cmd = conf_get_program(); |
d013a55e | 128 | if (alert_cmd && ! scan) |
56eedc1a | 129 | fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n", |
d013a55e NB |
130 | alert_cmd); |
131 | } | |
56eedc1a NB |
132 | if (scan && !mailaddr && !alert_cmd) { |
133 | fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n"); | |
d013a55e | 134 | return 1; |
56eedc1a | 135 | } |
d013a55e NB |
136 | |
137 | if (daemonise) { | |
138 | int pid = fork(); | |
139 | if (pid > 0) { | |
b5e64645 NB |
140 | if (!pidfile) |
141 | printf("%d\n", pid); | |
142 | else { | |
143 | FILE *pid_file; | |
144 | pid_file=fopen(pidfile, "w"); | |
145 | if (!pid_file) | |
146 | perror("cannot create pid file"); | |
147 | else { | |
148 | fprintf(pid_file,"%d\n", pid); | |
149 | fclose(pid_file); | |
150 | } | |
151 | } | |
d013a55e NB |
152 | return 0; |
153 | } | |
154 | if (pid < 0) { | |
155 | perror("daemonise"); | |
156 | return 1; | |
157 | } | |
158 | close(0); | |
fb97b4d6 | 159 | open("/dev/null", O_RDWR); |
d013a55e NB |
160 | dup2(0,1); |
161 | dup2(0,2); | |
162 | setsid(); | |
163 | } | |
e0d19036 NB |
164 | |
165 | if (devlist == NULL) { | |
8aec876d | 166 | mddev_ident_t mdlist = conf_get_ident(NULL); |
e0d19036 | 167 | for (; mdlist; mdlist=mdlist->next) { |
fe056d1f N |
168 | struct state *st; |
169 | if (mdlist->devname == NULL) | |
170 | continue; | |
171 | st = malloc(sizeof *st); | |
e0d19036 NB |
172 | if (st == NULL) |
173 | continue; | |
174 | st->devname = strdup(mdlist->devname); | |
175 | st->utime = 0; | |
176 | st->next = statelist; | |
d013a55e | 177 | st->err = 0; |
4450e59f | 178 | st->devnum = INT_MAX; |
e0d19036 | 179 | st->percent = -2; |
feb716e9 | 180 | st->expected_spares = mdlist->spare_disks; |
e0d19036 NB |
181 | if (mdlist->spare_group) |
182 | st->spare_group = strdup(mdlist->spare_group); | |
183 | else | |
184 | st->spare_group = NULL; | |
185 | statelist = st; | |
186 | } | |
187 | } else { | |
cd29a5c8 | 188 | mddev_dev_t dv; |
e0d19036 | 189 | for (dv=devlist ; dv; dv=dv->next) { |
8aec876d | 190 | mddev_ident_t mdlist = conf_get_ident(dv->devname); |
e0d19036 NB |
191 | struct state *st = malloc(sizeof *st); |
192 | if (st == NULL) | |
193 | continue; | |
194 | st->devname = strdup(dv->devname); | |
195 | st->utime = 0; | |
196 | st->next = statelist; | |
d013a55e | 197 | st->err = 0; |
4450e59f | 198 | st->devnum = INT_MAX; |
e0d19036 | 199 | st->percent = -2; |
feb716e9 | 200 | st->expected_spares = -1; |
e0d19036 | 201 | st->spare_group = NULL; |
e5329c37 NB |
202 | if (mdlist) { |
203 | st->expected_spares = mdlist->spare_disks; | |
204 | if (mdlist->spare_group) | |
205 | st->spare_group = strdup(mdlist->spare_group); | |
206 | } | |
e0d19036 NB |
207 | statelist = st; |
208 | } | |
209 | } | |
210 | ||
211 | ||
212 | while (! finished) { | |
aa88f531 | 213 | int new_found = 0; |
e0d19036 NB |
214 | struct state *st; |
215 | ||
216 | if (mdstat) | |
217 | free_mdstat(mdstat); | |
22a88995 | 218 | mdstat = mdstat_read(oneshot?0:1, 0); |
e0d19036 NB |
219 | |
220 | for (st=statelist; st; st=st->next) { | |
ab5303d6 | 221 | struct { int state, major, minor; } info[MaxDisks]; |
52826846 | 222 | mdu_array_info_t array; |
dd0781e5 | 223 | struct mdstat_ent *mse = NULL, *mse2; |
e0d19036 | 224 | char *dev = st->devname; |
52826846 | 225 | int fd; |
98c6faba | 226 | unsigned int i; |
e0d19036 | 227 | |
98c6faba | 228 | if (test) |
4948b8f7 | 229 | alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
52826846 NB |
230 | fd = open(dev, O_RDONLY); |
231 | if (fd < 0) { | |
232 | if (!st->err) | |
e0d19036 | 233 | alert("DeviceDisappeared", dev, NULL, |
4948b8f7 | 234 | mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 | 235 | /* fprintf(stderr, Name ": cannot open %s: %s\n", |
52826846 | 236 | dev, strerror(errno)); |
e0d19036 | 237 | */ st->err=1; |
52826846 NB |
238 | continue; |
239 | } | |
e4dc5106 | 240 | fcntl(fd, F_SETFD, FD_CLOEXEC); |
52826846 NB |
241 | if (ioctl(fd, GET_ARRAY_INFO, &array)<0) { |
242 | if (!st->err) | |
e0d19036 | 243 | alert("DeviceDisappeared", dev, NULL, |
4948b8f7 | 244 | mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 | 245 | /* fprintf(stderr, Name ": cannot get array info for %s: %s\n", |
52826846 | 246 | dev, strerror(errno)); |
e0d19036 | 247 | */ st->err=1; |
52826846 NB |
248 | close(fd); |
249 | continue; | |
250 | } | |
66f8bbbe DL |
251 | /* It's much easier to list what array levels can't |
252 | * have a device disappear than all of them that can | |
253 | */ | |
254 | if (array.level == 0 || array.level == -1) { | |
5787fa49 NB |
255 | if (!st->err) |
256 | alert("DeviceDisappeared", dev, "Wrong-Level", | |
4948b8f7 | 257 | mailaddr, mailfrom, alert_cmd, dosyslog); |
5787fa49 NB |
258 | st->err = 1; |
259 | close(fd); | |
260 | continue; | |
261 | } | |
4450e59f | 262 | if (st->devnum == INT_MAX) { |
e0d19036 NB |
263 | struct stat stb; |
264 | if (fstat(fd, &stb) == 0 && | |
98c6faba | 265 | (S_IFMT&stb.st_mode)==S_IFBLK) { |
0df46c2a NB |
266 | if (major(stb.st_rdev) == MD_MAJOR) |
267 | st->devnum = minor(stb.st_rdev); | |
98c6faba | 268 | else |
0df46c2a | 269 | st->devnum = -1- (minor(stb.st_rdev)>>6); |
98c6faba | 270 | } |
e0d19036 NB |
271 | } |
272 | ||
dd0781e5 NB |
273 | for (mse2 = mdstat ; mse2 ; mse2=mse2->next) |
274 | if (mse2->devnum == st->devnum) { | |
4450e59f | 275 | mse2->devnum = INT_MAX; /* flag it as "used" */ |
dd0781e5 NB |
276 | mse = mse2; |
277 | } | |
e0d19036 | 278 | |
52826846 | 279 | if (st->utime == array.utime && |
e0d19036 NB |
280 | st->failed == array.failed_disks && |
281 | st->working == array.working_disks && | |
282 | st->spare == array.spare_disks && | |
283 | (mse == NULL || ( | |
284 | mse->percent == st->percent | |
285 | ))) { | |
52826846 | 286 | close(fd); |
e0d19036 | 287 | st->err = 0; |
52826846 NB |
288 | continue; |
289 | } | |
aa88f531 NB |
290 | if (st->utime == 0 && /* new array */ |
291 | mse && /* is in /proc/mdstat */ | |
292 | mse->pattern && strchr(mse->pattern, '_') /* degraded */ | |
293 | ) | |
4948b8f7 | 294 | alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
aa88f531 | 295 | |
feb716e9 | 296 | if (st->utime == 0 && /* new array */ |
aba69144 NB |
297 | st->expected_spares > 0 && |
298 | array.spare_disks < st->expected_spares) | |
4948b8f7 | 299 | alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 | 300 | if (mse && |
aba69144 | 301 | st->percent == -1 && |
e0d19036 | 302 | mse->percent >= 0) |
4948b8f7 | 303 | alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 NB |
304 | if (mse && |
305 | st->percent >= 0 && | |
306 | mse->percent >= 0 && | |
307 | (mse->percent / 20) > (st->percent / 20)) | |
308 | alert(percentalerts[mse->percent/20], | |
4948b8f7 | 309 | dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 | 310 | |
98c6faba NB |
311 | if (mse && |
312 | mse->percent == -1 && | |
37dfc3d6 NB |
313 | st->percent >= 0) { |
314 | /* Rebuild/sync/whatever just finished. | |
315 | * If there is a number in /mismatch_cnt, | |
316 | * we should report that. | |
317 | */ | |
7e0f6979 | 318 | struct mdinfo *sra = |
37dfc3d6 NB |
319 | sysfs_read(-1, st->devnum, GET_MISMATCH); |
320 | if (sra && sra->mismatch_cnt > 0) { | |
321 | char cnt[40]; | |
322 | sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt); | |
323 | alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog); | |
324 | } else | |
325 | alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); | |
326 | if (sra) | |
327 | free(sra); | |
328 | } | |
98c6faba | 329 | |
e0d19036 NB |
330 | if (mse) |
331 | st->percent = mse->percent; | |
e27d562b | 332 | |
ab5303d6 NB |
333 | |
334 | for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks; | |
335 | i++) { | |
336 | mdu_disk_info_t disc; | |
01d9299c | 337 | disc.number = i; |
ab5303d6 NB |
338 | if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) { |
339 | info[i].state = disc.state; | |
340 | info[i].major = disc.major; | |
341 | info[i].minor = disc.minor; | |
342 | } else | |
343 | info[i].major = info[i].minor = 0; | |
344 | } | |
345 | close(fd); | |
346 | ||
e27d562b | 347 | for (i=0; i<MaxDisks; i++) { |
f8409e54 | 348 | mdu_disk_info_t disc = {0}; |
e0d19036 NB |
349 | int newstate=0; |
350 | int change; | |
351 | char *dv = NULL; | |
52826846 | 352 | disc.number = i; |
e27d562b NB |
353 | if (i > array.raid_disks + array.nr_disks) { |
354 | newstate = 0; | |
355 | disc.major = disc.minor = 0; | |
ab5303d6 NB |
356 | } else if (info[i].major || info[i].minor) { |
357 | newstate = info[i].state; | |
358 | dv = map_dev(info[i].major, info[i].minor, 1); | |
359 | disc.state = newstate; | |
360 | disc.major = info[i].major; | |
361 | disc.minor = info[i].minor; | |
e27d562b | 362 | } else if (mse && mse->pattern && i < strlen(mse->pattern)) { |
e0d19036 NB |
363 | switch(mse->pattern[i]) { |
364 | case 'U': newstate = 6 /* ACTIVE/SYNC */; break; | |
365 | case '_': newstate = 0; break; | |
52826846 | 366 | } |
e27d562b NB |
367 | disc.major = disc.minor = 0; |
368 | } | |
fe394e5e NB |
369 | if (dv == NULL && st->devid[i]) |
370 | dv = map_dev(major(st->devid[i]), | |
16c6fa80 | 371 | minor(st->devid[i]), 1); |
e0d19036 NB |
372 | change = newstate ^ st->devstate[i]; |
373 | if (st->utime && change && !st->err) { | |
98c6faba | 374 | if (i < (unsigned)array.raid_disks && |
e0d19036 NB |
375 | (((newstate&change)&(1<<MD_DISK_FAULTY)) || |
376 | ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) || | |
377 | ((st->devstate[i]&change)&(1<<MD_DISK_SYNC))) | |
378 | ) | |
4948b8f7 | 379 | alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); |
98c6faba | 380 | else if (i >= (unsigned)array.raid_disks && |
e0d19036 | 381 | (disc.major || disc.minor) && |
0df46c2a | 382 | st->devid[i] == makedev(disc.major, disc.minor) && |
e0d19036 NB |
383 | ((newstate&change)&(1<<MD_DISK_FAULTY)) |
384 | ) | |
4948b8f7 | 385 | alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); |
98c6faba | 386 | else if (i < (unsigned)array.raid_disks && |
e0d19036 NB |
387 | (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) || |
388 | ((newstate&change)&(1<<MD_DISK_ACTIVE)) || | |
389 | ((newstate&change)&(1<<MD_DISK_SYNC))) | |
390 | ) | |
4948b8f7 | 391 | alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog); |
52826846 | 392 | } |
3e6944b2 | 393 | st->devstate[i] = newstate; |
0df46c2a | 394 | st->devid[i] = makedev(disc.major, disc.minor); |
52826846 | 395 | } |
52826846 NB |
396 | st->active = array.active_disks; |
397 | st->working = array.working_disks; | |
398 | st->spare = array.spare_disks; | |
399 | st->failed = array.failed_disks; | |
400 | st->utime = array.utime; | |
e0d19036 NB |
401 | st->raid = array.raid_disks; |
402 | st->err = 0; | |
52826846 | 403 | } |
e0d19036 NB |
404 | /* now check if there are any new devices found in mdstat */ |
405 | if (scan) { | |
406 | struct mdstat_ent *mse; | |
aba69144 | 407 | for (mse=mdstat; mse; mse=mse->next) |
4450e59f | 408 | if (mse->devnum != INT_MAX && |
2cdb6489 | 409 | mse->level && |
66f8bbbe DL |
410 | (strcmp(mse->level, "raid0")!=0 && |
411 | strcmp(mse->level, "linear")!=0) | |
5787fa49 | 412 | ) { |
e0d19036 | 413 | struct state *st = malloc(sizeof *st); |
98c6faba NB |
414 | mdu_array_info_t array; |
415 | int fd; | |
e0d19036 NB |
416 | if (st == NULL) |
417 | continue; | |
418 | st->devname = strdup(get_md_name(mse->devnum)); | |
98c6faba NB |
419 | if ((fd = open(st->devname, O_RDONLY)) < 0 || |
420 | ioctl(fd, GET_ARRAY_INFO, &array)< 0) { | |
421 | /* no such array */ | |
422 | if (fd >=0) close(fd); | |
8bd2e0c3 | 423 | put_md_name(st->devname); |
98c6faba NB |
424 | free(st->devname); |
425 | free(st); | |
426 | continue; | |
427 | } | |
dd0781e5 | 428 | close(fd); |
e0d19036 NB |
429 | st->utime = 0; |
430 | st->next = statelist; | |
431 | st->err = 1; | |
432 | st->devnum = mse->devnum; | |
433 | st->percent = -2; | |
434 | st->spare_group = NULL; | |
98c6faba | 435 | st->expected_spares = -1; |
e0d19036 | 436 | statelist = st; |
4948b8f7 | 437 | alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog); |
aa88f531 | 438 | new_found = 1; |
e0d19036 NB |
439 | } |
440 | } | |
441 | /* If an array has active < raid && spare == 0 && spare_group != NULL | |
442 | * Look for another array with spare > 0 and active == raid and same spare_group | |
443 | * if found, choose a device and hotremove/hotadd | |
444 | */ | |
445 | for (st = statelist; st; st=st->next) | |
446 | if (st->active < st->raid && | |
447 | st->spare == 0 && | |
448 | st->spare_group != NULL) { | |
449 | struct state *st2; | |
450 | for (st2=statelist ; st2 ; st2=st2->next) | |
451 | if (st2 != st && | |
452 | st2->spare > 0 && | |
453 | st2->active == st2->raid && | |
454 | st2->spare_group != NULL && | |
455 | strcmp(st->spare_group, st2->spare_group) == 0) { | |
456 | /* try to remove and add */ | |
457 | int fd1 = open(st->devname, O_RDONLY); | |
458 | int fd2 = open(st2->devname, O_RDONLY); | |
459 | int dev = -1; | |
460 | int d; | |
461 | if (fd1 < 0 || fd2 < 0) { | |
462 | if (fd1>=0) close(fd1); | |
463 | if (fd2>=0) close(fd2); | |
464 | continue; | |
465 | } | |
e27d562b | 466 | for (d=st2->raid; d < MaxDisks; d++) { |
e0d19036 NB |
467 | if (st2->devid[d] > 0 && |
468 | st2->devstate[d] == 0) { | |
469 | dev = st2->devid[d]; | |
470 | break; | |
471 | } | |
472 | } | |
473 | if (dev > 0) { | |
aba69144 | 474 | if (ioctl(fd2, HOT_REMOVE_DISK, |
e0d19036 NB |
475 | (unsigned long)dev) == 0) { |
476 | if (ioctl(fd1, HOT_ADD_DISK, | |
477 | (unsigned long)dev) == 0) { | |
4948b8f7 | 478 | alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog); |
e0d19036 NB |
479 | close(fd1); |
480 | close(fd2); | |
481 | break; | |
482 | } | |
483 | else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev); | |
484 | } | |
485 | } | |
486 | close(fd1); | |
487 | close(fd2); | |
488 | } | |
489 | } | |
aa88f531 NB |
490 | if (!new_found) { |
491 | if (oneshot) | |
492 | break; | |
493 | else | |
dd0781e5 | 494 | mdstat_wait(period); |
aa88f531 | 495 | } |
98c6faba | 496 | test = 0; |
52826846 | 497 | } |
b5e64645 NB |
498 | if (pidfile) |
499 | unlink(pidfile); | |
52826846 NB |
500 | return 0; |
501 | } | |
502 | ||
503 | ||
4948b8f7 | 504 | static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd, |
773135f5 | 505 | int dosyslog) |
52826846 | 506 | { |
773135f5 NB |
507 | int priority; |
508 | ||
cd29a5c8 NB |
509 | if (!cmd && !mailaddr) { |
510 | time_t now = time(0); | |
aba69144 | 511 | |
e0d19036 | 512 | printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device"); |
cd29a5c8 | 513 | } |
52826846 NB |
514 | if (cmd) { |
515 | int pid = fork(); | |
516 | switch(pid) { | |
517 | default: | |
518 | waitpid(pid, NULL, 0); | |
519 | break; | |
520 | case -1: | |
521 | break; | |
522 | case 0: | |
523 | execl(cmd, cmd, event, dev, disc, NULL); | |
524 | exit(2); | |
525 | } | |
526 | } | |
aba69144 NB |
527 | if (mailaddr && |
528 | (strncmp(event, "Fail", 4)==0 || | |
98c6faba | 529 | strncmp(event, "Test", 4)==0 || |
d1732eeb | 530 | strncmp(event, "Spares", 6)==0 || |
aa88f531 | 531 | strncmp(event, "Degrade", 7)==0)) { |
52826846 NB |
532 | FILE *mp = popen(Sendmail, "w"); |
533 | if (mp) { | |
008e1100 | 534 | FILE *mdstat; |
52826846 NB |
535 | char hname[256]; |
536 | gethostname(hname, sizeof(hname)); | |
537 | signal(SIGPIPE, SIG_IGN); | |
4948b8f7 NB |
538 | if (mailfrom) |
539 | fprintf(mp, "From: %s\n", mailfrom); | |
540 | else | |
541 | fprintf(mp, "From: " Name " monitoring <root>\n"); | |
52826846 NB |
542 | fprintf(mp, "To: %s\n", mailaddr); |
543 | fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname); | |
544 | ||
545 | fprintf(mp, "This is an automatically generated mail message from " Name "\n"); | |
546 | fprintf(mp, "running on %s\n\n", hname); | |
547 | ||
548 | fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev); | |
549 | ||
37dfc3d6 | 550 | if (disc && disc[0] != ' ') |
56eedc1a | 551 | fprintf(mp, "It could be related to component device %s.\n\n", disc); |
37dfc3d6 NB |
552 | if (disc && disc[0] == ' ') |
553 | fprintf(mp, "Extra information:%s.\n\n", disc); | |
52826846 NB |
554 | |
555 | fprintf(mp, "Faithfully yours, etc.\n"); | |
008e1100 NB |
556 | |
557 | mdstat = fopen("/proc/mdstat", "r"); | |
558 | if (mdstat) { | |
559 | char buf[8192]; | |
560 | int n; | |
a524a7ee | 561 | fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n"); |
008e1100 | 562 | while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0) |
9fca7d62 | 563 | n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */ |
008e1100 NB |
564 | fclose(mdstat); |
565 | } | |
52826846 NB |
566 | fclose(mp); |
567 | } | |
568 | ||
569 | } | |
773135f5 NB |
570 | |
571 | /* log the event to syslog maybe */ | |
572 | if (dosyslog) { | |
573 | /* Log at a different severity depending on the event. | |
574 | * | |
575 | * These are the critical events: */ | |
576 | if (strncmp(event, "Fail", 4)==0 || | |
577 | strncmp(event, "Degrade", 7)==0 || | |
578 | strncmp(event, "DeviceDisappeared", 17)==0) | |
579 | priority = LOG_CRIT; | |
580 | /* Good to know about, but are not failures: */ | |
581 | else if (strncmp(event, "Rebuild", 7)==0 || | |
d1732eeb NB |
582 | strncmp(event, "MoveSpare", 9)==0 || |
583 | strncmp(event, "Spares", 6) != 0) | |
773135f5 NB |
584 | priority = LOG_WARNING; |
585 | /* Everything else: */ | |
586 | else | |
587 | priority = LOG_INFO; | |
588 | ||
589 | if (disc) | |
590 | syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc); | |
591 | else | |
592 | syslog(priority, "%s event detected on md device %s", event, dev); | |
593 | } | |
52826846 | 594 | } |
b90c0e9a NB |
595 | |
596 | /* Not really Monitor but ... */ | |
597 | int Wait(char *dev) | |
598 | { | |
599 | struct stat stb; | |
600 | int devnum; | |
601 | int rv = 1; | |
602 | ||
603 | if (stat(dev, &stb) != 0) { | |
604 | fprintf(stderr, Name ": Cannot find %s: %s\n", dev, | |
605 | strerror(errno)); | |
606 | return 2; | |
607 | } | |
c94709e8 | 608 | devnum = stat2devnum(&stb); |
b90c0e9a NB |
609 | |
610 | while(1) { | |
611 | struct mdstat_ent *ms = mdstat_read(1, 0); | |
612 | struct mdstat_ent *e; | |
613 | ||
614 | for (e=ms ; e; e=e->next) | |
615 | if (e->devnum == devnum) | |
616 | break; | |
617 | ||
618 | if (!e || e->percent < 0) { | |
e7783ee6 | 619 | if (e && e->metadata_version && |
c94709e8 DW |
620 | strncmp(e->metadata_version, "external:", 9) == 0) { |
621 | if (is_subarray(&e->metadata_version[9])) | |
622 | ping_monitor(&e->metadata_version[9]); | |
623 | else | |
624 | ping_monitor(devnum2devname(devnum)); | |
625 | } | |
b90c0e9a NB |
626 | free_mdstat(ms); |
627 | return rv; | |
628 | } | |
89a10d84 | 629 | free_mdstat(ms); |
b90c0e9a NB |
630 | rv = 0; |
631 | mdstat_wait(5); | |
632 | } | |
633 | } | |
1770662b DW |
634 | |
635 | static char *clean_states[] = { | |
636 | "clear", "inactive", "readonly", "read-auto", "clean", NULL }; | |
637 | ||
27dec8fa | 638 | int WaitClean(char *dev, int verbose) |
1770662b DW |
639 | { |
640 | int fd; | |
641 | struct mdinfo *mdi; | |
642 | int rv = 1; | |
643 | int devnum; | |
644 | ||
645 | fd = open(dev, O_RDONLY); | |
646 | if (fd < 0) { | |
27dec8fa DW |
647 | if (verbose) |
648 | fprintf(stderr, Name ": Couldn't open %s: %s\n", dev, strerror(errno)); | |
1770662b DW |
649 | return 1; |
650 | } | |
651 | ||
652 | devnum = fd2devnum(fd); | |
653 | mdi = sysfs_read(fd, devnum, GET_VERSION|GET_LEVEL|GET_SAFEMODE); | |
654 | if (!mdi) { | |
27dec8fa DW |
655 | if (verbose) |
656 | fprintf(stderr, Name ": Failed to read sysfs attributes for " | |
657 | "%s\n", dev); | |
1770662b DW |
658 | close(fd); |
659 | return 0; | |
660 | } | |
661 | ||
662 | switch(mdi->array.level) { | |
663 | case LEVEL_LINEAR: | |
664 | case LEVEL_MULTIPATH: | |
665 | case 0: | |
666 | /* safemode delay is irrelevant for these levels */ | |
667 | rv = 0; | |
668 | ||
669 | } | |
670 | ||
671 | /* for internal metadata the kernel handles the final clean | |
672 | * transition, containers can never be dirty | |
673 | */ | |
674 | if (!is_subarray(mdi->text_version)) | |
675 | rv = 0; | |
676 | ||
677 | /* safemode disabled ? */ | |
678 | if (mdi->safe_mode_delay == 0) | |
679 | rv = 0; | |
680 | ||
681 | if (rv) { | |
682 | int state_fd = sysfs_open(fd2devnum(fd), NULL, "array_state"); | |
1770662b | 683 | char buf[20]; |
0dd3ba30 DW |
684 | fd_set fds; |
685 | struct timeval tm; | |
1770662b | 686 | |
0dd3ba30 DW |
687 | /* minimize the safe_mode_delay and prepare to wait up to 5s |
688 | * for writes to quiesce | |
689 | */ | |
690 | sysfs_set_safemode(mdi, 1); | |
691 | tm.tv_sec = 5; | |
692 | tm.tv_usec = 0; | |
693 | ||
7146ec6a DW |
694 | /* give mdmon a chance to checkpoint resync */ |
695 | sysfs_set_str(mdi, NULL, "sync_action", "idle"); | |
696 | ||
0dd3ba30 | 697 | FD_ZERO(&fds); |
1770662b | 698 | |
0dd3ba30 DW |
699 | /* wait for array_state to be clean */ |
700 | while (1) { | |
1770662b DW |
701 | rv = read(state_fd, buf, sizeof(buf)); |
702 | if (rv < 0) | |
703 | break; | |
704 | if (sysfs_match_word(buf, clean_states) <= 4) | |
705 | break; | |
0dd3ba30 DW |
706 | FD_SET(state_fd, &fds); |
707 | rv = select(state_fd + 1, &fds, NULL, NULL, &tm); | |
708 | if (rv < 0 && errno != EINTR) | |
709 | break; | |
1770662b DW |
710 | lseek(state_fd, 0, SEEK_SET); |
711 | } | |
712 | if (rv < 0) | |
713 | rv = 1; | |
0dd3ba30 | 714 | else if (ping_monitor(mdi->text_version) == 0) { |
1770662b DW |
715 | /* we need to ping to close the window between array |
716 | * state transitioning to clean and the metadata being | |
717 | * marked clean | |
718 | */ | |
0dd3ba30 DW |
719 | rv = 0; |
720 | } else | |
721 | rv = 1; | |
27dec8fa | 722 | if (rv && verbose) |
1770662b DW |
723 | fprintf(stderr, Name ": Error waiting for %s to be clean\n", |
724 | dev); | |
725 | ||
0dd3ba30 DW |
726 | /* restore the original safe_mode_delay */ |
727 | sysfs_set_safemode(mdi, mdi->safe_mode_delay); | |
1770662b DW |
728 | close(state_fd); |
729 | } | |
730 | ||
0dd3ba30 | 731 | sysfs_free(mdi); |
1770662b DW |
732 | close(fd); |
733 | ||
734 | return rv; | |
735 | } | |
736 | ||
737 |