1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
|
/*
* mdadm - manage Linux "md" devices aka RAID arrays.
*
* Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Neil Brown
* Email: <neilb@cse.unsw.edu.au>
* Paper: Neil Brown
* School of Computer Science and Engineering
* The University of New South Wales
* Sydney, 2052
* Australia
*/
#include "mdadm.h"
#include "md_p.h"
#include "md_u.h"
#include <sys/wait.h>
#include <sys/signal.h>
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd);
static char *percentalerts[] = {
"RebuildStarted",
"Rebuild20",
"Rebuild40",
"Rebuild60",
"Rebuild80",
};
int Monitor(mddev_dev_t devlist,
char *mailaddr, char *alert_cmd,
int period, int daemonise, int scan,
char *config)
{
/*
* Every few seconds, scan every md device looking for changes
* When a change is found, log it, possibly run the alert command,
* and possibly send Email
*
* For each array, we record:
* Update time
* active/working/failed/spare drives
* State of each device.
* %rebuilt if rebuilding
*
* If the update time changes, check out all the data again
* It is possible that we cannot get the state of each device
* due to bugs in the md kernel module.
* We also read /proc/mdstat to get rebuild percent,
* and to get state on all active devices incase of kernel bug.
*
* Events are:
* Fail
* An active device had Faulty set or Active/Sync removed
* FailSpare
* A spare device had Faulty set
* SpareActive
* An active device had a reverse transition
* RebuildStarted
* percent went from -1 to +ve
* Rebuild20 Rebuild40 Rebuild60 Rebuild80
* percent went from below to not-below that number
* DeviceDisappeared
* Couldn't access a device which was previously visible
*
* if we detect an array with active<raid and spare==0
* we look at other arrays that have same spare-group
* If we find one with active==raid and spare>0,
* and if we can get_disk_info and find a name
* Then we hot-remove and hot-add to the other array
*
* If devlist is NULL, then we can monitor everything because --scan
* was given. We get an initial list from config file and add anything
* that appears in /proc/mdstat
*/
struct state {
char *devname;
int devnum; /* to sync with mdstat info */
long utime;
int err;
char *spare_group;
int active, working, failed, spare, raid;
int devstate[MD_SB_DISKS];
int devid[MD_SB_DISKS];
int percent;
struct state *next;
} *statelist = NULL;
int finished = 0;
struct mdstat_ent *mdstat = NULL;
if (!mailaddr) {
mailaddr = conf_get_mailaddr(config);
if (mailaddr && ! scan)
fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
mailaddr);
}
if (!alert_cmd) {
alert_cmd = conf_get_program(config);
if (alert_cmd && ! scan)
fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
alert_cmd);
}
if (scan && !mailaddr && !alert_cmd) {
fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
return 1;
}
if (daemonise) {
int pid = fork();
if (pid > 0) {
printf("%d\n", pid);
return 0;
}
if (pid < 0) {
perror("daemonise");
return 1;
}
close(0);
open("/dev/null", 3);
dup2(0,1);
dup2(0,2);
setsid();
}
if (devlist == NULL) {
mddev_ident_t mdlist = conf_get_ident(config, NULL);
for (; mdlist; mdlist=mdlist->next) {
struct state *st = malloc(sizeof *st);
if (st == NULL)
continue;
st->devname = strdup(mdlist->devname);
st->utime = 0;
st->next = statelist;
st->err = 0;
st->devnum = -1;
st->percent = -2;
if (mdlist->spare_group)
st->spare_group = strdup(mdlist->spare_group);
else
st->spare_group = NULL;
statelist = st;
}
} else {
mddev_dev_t dv;
for (dv=devlist ; dv; dv=dv->next) {
struct state *st = malloc(sizeof *st);
if (st == NULL)
continue;
st->devname = strdup(dv->devname);
st->utime = 0;
st->next = statelist;
st->err = 0;
st->devnum = -1;
st->percent = -2;
st->spare_group = NULL;
statelist = st;
}
}
while (! finished) {
struct state *st;
if (mdstat)
free_mdstat(mdstat);
mdstat = mdstat_read();
for (st=statelist; st; st=st->next) {
mdu_array_info_t array;
struct mdstat_ent *mse;
char *dev = st->devname;
int fd;
int i;
fd = open(dev, O_RDONLY);
if (fd < 0) {
if (!st->err)
alert("DeviceDisappeared", dev, NULL,
mailaddr, alert_cmd);
/* fprintf(stderr, Name ": cannot open %s: %s\n",
dev, strerror(errno));
*/ st->err=1;
continue;
}
if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
if (!st->err)
alert("DeviceDisappeared", dev, NULL,
mailaddr, alert_cmd);
/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
dev, strerror(errno));
*/ st->err=1;
close(fd);
continue;
}
if (array.level != 1 && array.level != 5 && array.level != -4) {
if (!st->err)
alert("DeviceDisappeared", dev, "Wrong-Level",
mailaddr, alert_cmd);
st->err = 1;
close(fd);
continue;
}
if (st->devnum < 0) {
struct stat stb;
if (fstat(fd, &stb) == 0 &&
(S_IFMT&stb.st_mode)==S_IFBLK)
st->devnum = MINOR(stb.st_rdev);
}
for (mse = mdstat ; mse ; mse=mse->next)
if (mse->devnum == st->devnum) {
mse->devnum = -1; /* flag it as "used" */
break;
}
if (st->utime == array.utime &&
st->failed == array.failed_disks &&
st->working == array.working_disks &&
st->spare == array.spare_disks &&
(mse == NULL || (
mse->percent == st->percent
))) {
close(fd);
st->err = 0;
continue;
}
if (mse &&
st->percent == -1 &&
mse->percent >= 0)
alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd);
if (mse &&
st->percent >= 0 &&
mse->percent >= 0 &&
(mse->percent / 20) > (st->percent / 20))
alert(percentalerts[mse->percent/20],
dev, NULL, mailaddr, alert_cmd);
if (mse)
st->percent = mse->percent;
for (i=0; i<MD_SB_DISKS; i++) {
mdu_disk_info_t disc;
int newstate=0;
int change;
char *dv = NULL;
disc.number = i;
if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
newstate = disc.state;
dv = map_dev(disc.major, disc.minor);
} else if (mse && mse->pattern && i < strlen(mse->pattern))
switch(mse->pattern[i]) {
case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
case '_': newstate = 0; break;
}
change = newstate ^ st->devstate[i];
if (st->utime && change && !st->err) {
if (i < array.raid_disks &&
(((newstate&change)&(1<<MD_DISK_FAULTY)) ||
((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
)
alert("Fail", dev, dv, mailaddr, alert_cmd);
else if (i>=array.raid_disks &&
(disc.major || disc.minor) &&
st->devid[i] == MKDEV(disc.major, disc.minor) &&
((newstate&change)&(1<<MD_DISK_FAULTY))
)
alert("FailSpare", dev, dv, mailaddr, alert_cmd);
else if (i < array.raid_disks &&
(((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
((newstate&change)&(1<<MD_DISK_SYNC)))
)
alert("SpareActive", dev, dv, mailaddr, alert_cmd);
}
st->devstate[i] = disc.state;
st->devid[i] = MKDEV(disc.major, disc.minor);
}
close(fd);
st->active = array.active_disks;
st->working = array.working_disks;
st->spare = array.spare_disks;
st->failed = array.failed_disks;
st->utime = array.utime;
st->raid = array.raid_disks;
st->err = 0;
}
/* now check if there are any new devices found in mdstat */
if (scan) {
struct mdstat_ent *mse;
for (mse=mdstat; mse; mse=mse->next)
if (mse->devnum >= 0 &&
(strcmp(mse->level, "raid1")==0 ||
strcmp(mse->level, "raid5")==0 ||
strcmp(mse->level, "multipath")==0)
) {
struct state *st = malloc(sizeof *st);
if (st == NULL)
continue;
st->devname = strdup(get_md_name(mse->devnum));
st->utime = 0;
st->next = statelist;
st->err = 1;
st->devnum = mse->devnum;
st->percent = -2;
st->spare_group = NULL;
statelist = st;
alert("NewArray", st->devname, NULL, mailaddr, alert_cmd);
}
}
/* If an array has active < raid && spare == 0 && spare_group != NULL
* Look for another array with spare > 0 and active == raid and same spare_group
* if found, choose a device and hotremove/hotadd
*/
for (st = statelist; st; st=st->next)
if (st->active < st->raid &&
st->spare == 0 &&
st->spare_group != NULL) {
struct state *st2;
for (st2=statelist ; st2 ; st2=st2->next)
if (st2 != st &&
st2->spare > 0 &&
st2->active == st2->raid &&
st2->spare_group != NULL &&
strcmp(st->spare_group, st2->spare_group) == 0) {
/* try to remove and add */
int fd1 = open(st->devname, O_RDONLY);
int fd2 = open(st2->devname, O_RDONLY);
int dev = -1;
int d;
if (fd1 < 0 || fd2 < 0) {
if (fd1>=0) close(fd1);
if (fd2>=0) close(fd2);
continue;
}
for (d=st2->raid; d<MD_SB_DISKS; d++) {
if (st2->devid[d] > 0 &&
st2->devstate[d] == 0) {
dev = st2->devid[d];
break;
}
}
if (dev > 0) {
if (ioctl(fd2, HOT_REMOVE_DISK,
(unsigned long)dev) == 0) {
if (ioctl(fd1, HOT_ADD_DISK,
(unsigned long)dev) == 0) {
alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd);
close(fd1);
close(fd2);
break;
}
else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
}
}
close(fd1);
close(fd2);
}
}
sleep(period);
}
return 0;
}
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd)
{
if (!cmd && !mailaddr) {
time_t now = time(0);
printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
}
if (cmd) {
int pid = fork();
switch(pid) {
default:
waitpid(pid, NULL, 0);
break;
case -1:
break;
case 0:
execl(cmd, cmd, event, dev, disc, NULL);
exit(2);
}
}
if (mailaddr && strncmp(event, "Fail", 4)==0) {
FILE *mp = popen(Sendmail, "w");
if (mp) {
char hname[256];
gethostname(hname, sizeof(hname));
signal(SIGPIPE, SIG_IGN);
fprintf(mp, "From: " Name " monitoring <root>\n");
fprintf(mp, "To: %s\n", mailaddr);
fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
fprintf(mp, "This is an automatically generated mail message from " Name "\n");
fprintf(mp, "running on %s\n\n", hname);
fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
if (disc)
fprintf(mp, "It could be related to component device %s.\n\n", disc);
fprintf(mp, "Faithfully yours, etc.\n");
fclose(mp);
}
}
/* FIXME log the event to syslog maybe */
}
|