mdadm-1.6.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 #include        <values.h>
36
37 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd);
38
39 static char *percentalerts[] = { 
40         "RebuildStarted",
41         "Rebuild20",
42         "Rebuild40",
43         "Rebuild60",
44         "Rebuild80",
45 };
46
47 int Monitor(mddev_dev_t devlist,
48             char *mailaddr, char *alert_cmd,
49             int period, int daemonise, int scan, int oneshot,
50             char *config, int test)
51 {
52         /*
53          * Every few seconds, scan every md device looking for changes
54          * When a change is found, log it, possibly run the alert command,
55          * and possibly send Email
56          *
57          * For each array, we record:
58          *   Update time
59          *   active/working/failed/spare drives
60          *   State of each device.
61          *   %rebuilt if rebuilding
62          *
63          * If the update time changes, check out all the data again
64          * It is possible that we cannot get the state of each device
65          * due to bugs in the md kernel module.
66          * We also read /proc/mdstat to get rebuild percent,
67          * and to get state on all active devices incase of kernel bug.
68          *
69          * Events are:
70          *    Fail
71          *      An active device had Faulty set or Active/Sync removed
72          *    FailSpare
73          *      A spare device had Faulty set
74          *    SpareActive
75          *      An active device had a reverse transition
76          *    RebuildStarted
77          *      percent went from -1 to +ve
78          *    Rebuild20 Rebuild40 Rebuild60 Rebuild80
79          *      percent went from below to not-below that number
80          *    DeviceDisappeared
81          *      Couldn't access a device which was previously visible
82          *
83          * if we detect an array with active<raid and spare==0
84          * we look at other arrays that have same spare-group
85          * If we find one with active==raid and spare>0,
86          *  and if we can get_disk_info and find a name
87          *  Then we hot-remove and hot-add to the other array
88          *
89          * If devlist is NULL, then we can monitor everything because --scan
90          * was given.  We get an initial list from config file and add anything
91          * that appears in /proc/mdstat
92          */
93
94         struct state {
95                 char *devname;
96                 int devnum;     /* to sync with mdstat info */
97                 long utime;
98                 int err;
99                 char *spare_group;
100                 int active, working, failed, spare, raid;
101                 int expected_spares;
102                 int devstate[MD_SB_DISKS];
103                 int devid[MD_SB_DISKS];
104                 int percent;
105                 struct state *next;
106         } *statelist = NULL;
107         int finished = 0;
108         struct mdstat_ent *mdstat = NULL;
109
110         if (!mailaddr) {
111                 mailaddr = conf_get_mailaddr(config);
112                 if (mailaddr && ! scan)
113                         fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
114                                mailaddr);
115         }
116         if (!alert_cmd) {
117                 alert_cmd = conf_get_program(config);
118                 if (alert_cmd && ! scan)
119                         fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
120                                alert_cmd);
121         }
122         if (scan && !mailaddr && !alert_cmd) {
123                 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
124                 return 1;
125         }
126
127         if (daemonise) {
128                 int pid = fork();
129                 if (pid > 0) {
130                         printf("%d\n", pid);
131                         return 0;
132                 }
133                 if (pid < 0) {
134                         perror("daemonise");
135                         return 1;
136                 }
137                 close(0);
138                 open("/dev/null", 3);
139                 dup2(0,1);
140                 dup2(0,2);
141                 setsid();
142         }
143
144         if (devlist == NULL) {
145                 mddev_ident_t mdlist = conf_get_ident(config, NULL);
146                 for (; mdlist; mdlist=mdlist->next) {
147                         struct state *st = malloc(sizeof *st);
148                         if (st == NULL)
149                                 continue;
150                         st->devname = strdup(mdlist->devname);
151                         st->utime = 0;
152                         st->next = statelist;
153                         st->err = 0;
154                         st->devnum = MAXINT;
155                         st->percent = -2;
156                         st->expected_spares = mdlist->spare_disks;
157                         if (mdlist->spare_group)
158                                 st->spare_group = strdup(mdlist->spare_group);
159                         else
160                                 st->spare_group = NULL;
161                         statelist = st;
162                 }
163         } else {
164                 mddev_dev_t dv;
165                 for (dv=devlist ; dv; dv=dv->next) {
166                         struct state *st = malloc(sizeof *st);
167                         if (st == NULL)
168                                 continue;
169                         st->devname = strdup(dv->devname);
170                         st->utime = 0;
171                         st->next = statelist;
172                         st->err = 0;
173                         st->devnum = MAXINT;
174                         st->percent = -2;
175                         st->expected_spares = -1;
176                         st->spare_group = NULL;
177                         statelist = st;
178                 }
179         }
180
181
182         while (! finished) {
183                 int new_found = 0;
184                 struct state *st;
185
186                 if (mdstat)
187                         free_mdstat(mdstat);
188                 mdstat = mdstat_read(oneshot?0:1);
189
190                 for (st=statelist; st; st=st->next) {
191                         mdu_array_info_t array;
192                         struct mdstat_ent *mse = NULL, *mse2;
193                         char *dev = st->devname;
194                         int fd;
195                         unsigned int i;
196
197                         if (test)
198                                 alert("TestMessage", dev, NULL, mailaddr, alert_cmd);
199                         fd = open(dev, O_RDONLY);
200                         if (fd < 0) {
201                                 if (!st->err)
202                                         alert("DeviceDisappeared", dev, NULL,
203                                               mailaddr, alert_cmd);
204 /*                                      fprintf(stderr, Name ": cannot open %s: %s\n",
205                                                 dev, strerror(errno));
206 */                              st->err=1;
207                                 continue;
208                         }
209                         if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
210                                 if (!st->err)
211                                         alert("DeviceDisappeared", dev, NULL,
212                                               mailaddr, alert_cmd);
213 /*                                      fprintf(stderr, Name ": cannot get array info for %s: %s\n",
214                                                 dev, strerror(errno));
215 */                              st->err=1;
216                                 close(fd);
217                                 continue;
218                         }
219                         if (array.level != 1 && array.level != 5 && array.level != -4) {
220                                 if (!st->err)
221                                         alert("DeviceDisappeared", dev, "Wrong-Level",
222                                               mailaddr, alert_cmd);
223                                 st->err = 1;
224                                 close(fd);
225                                 continue;
226                         }
227                         if (st->devnum == MAXINT) {
228                                 struct stat stb;
229                                 if (fstat(fd, &stb) == 0 &&
230                                     (S_IFMT&stb.st_mode)==S_IFBLK) {
231                                         if (MAJOR(stb.st_rdev) == MD_MAJOR)
232                                                 st->devnum = MINOR(stb.st_rdev);
233                                         else
234                                                 st->devnum = -1- (MINOR(stb.st_rdev)>>6);
235                                 }
236                         }
237
238                         for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
239                                 if (mse2->devnum == st->devnum) {
240                                         mse2->devnum = MAXINT; /* flag it as "used" */
241                                         mse = mse2;
242                                 }
243
244                         if (st->utime == array.utime &&
245                             st->failed == array.failed_disks &&
246                             st->working == array.working_disks &&
247                             st->spare == array.spare_disks &&
248                             (mse == NULL  || (
249                                     mse->percent == st->percent
250                                     ))) {
251                                 close(fd);
252                                 st->err = 0;
253                                 continue;
254                         }
255                         if (st->utime == 0 && /* new array */
256                             mse &&      /* is in /proc/mdstat */
257                             mse->pattern && strchr(mse->pattern, '_') /* degraded */
258                                 )
259                                 alert("DegradedArray", dev, NULL, mailaddr, alert_cmd);
260
261                         if (st->utime == 0 && /* new array */
262                             st->expected_spares > 0 && 
263                             array.spare_disks < st->expected_spares) 
264                                 alert("SparesMissing", dev, NULL, mailaddr, alert_cmd);
265                         if (mse &&
266                             st->percent == -1 && 
267                             mse->percent >= 0)
268                                 alert("RebuildStarted", dev, NULL, mailaddr, alert_cmd);
269                         if (mse &&
270                             st->percent >= 0 &&
271                             mse->percent >= 0 &&
272                             (mse->percent / 20) > (st->percent / 20))
273                                 alert(percentalerts[mse->percent/20],
274                                       dev, NULL, mailaddr, alert_cmd);
275
276                         if (mse &&
277                             mse->percent == -1 &&
278                             st->percent >= 0)
279                                 alert("RebuildFinished", dev, NULL, mailaddr, alert_cmd);
280
281                         if (mse)
282                                 st->percent = mse->percent;
283                                         
284                         for (i=0; i<MD_SB_DISKS; i++) {
285                                 mdu_disk_info_t disc;
286                                 int newstate=0;
287                                 int change;
288                                 char *dv = NULL;
289                                 disc.number = i;
290                                 if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
291                                         newstate = disc.state;
292                                         dv = map_dev(disc.major, disc.minor);
293                                 } else if (mse &&  mse->pattern && i < strlen(mse->pattern))
294                                         switch(mse->pattern[i]) {
295                                         case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
296                                         case '_': newstate = 0; break;
297                                         }
298                                 change = newstate ^ st->devstate[i];
299                                 if (st->utime && change && !st->err) {
300                                         if (i < (unsigned)array.raid_disks &&
301                                             (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
302                                              ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
303                                              ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
304                                                 )
305                                                 alert("Fail", dev, dv, mailaddr, alert_cmd);
306                                         else if (i >= (unsigned)array.raid_disks &&
307                                                  (disc.major || disc.minor) &&
308                                                  st->devid[i] == MKDEV(disc.major, disc.minor) &&
309                                                  ((newstate&change)&(1<<MD_DISK_FAULTY))
310                                                 )
311                                                 alert("FailSpare", dev, dv, mailaddr, alert_cmd);
312                                         else if (i < (unsigned)array.raid_disks &&
313                                                  (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
314                                                   ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
315                                                   ((newstate&change)&(1<<MD_DISK_SYNC)))
316                                                 )
317                                                 alert("SpareActive", dev, dv, mailaddr, alert_cmd);
318                                 }
319                                 st->devstate[i] = disc.state;
320                                 st->devid[i] = MKDEV(disc.major, disc.minor);
321                         }
322                         close(fd);
323                         st->active = array.active_disks;
324                         st->working = array.working_disks;
325                         st->spare = array.spare_disks;
326                         st->failed = array.failed_disks;
327                         st->utime = array.utime;
328                         st->raid = array.raid_disks;
329                         st->err = 0;
330                 }
331                 /* now check if there are any new devices found in mdstat */
332                 if (scan) {
333                         struct mdstat_ent *mse;
334                         for (mse=mdstat; mse; mse=mse->next) 
335                                 if (mse->devnum != MAXINT &&
336                                     (strcmp(mse->level, "raid1")==0 ||
337                                      strcmp(mse->level, "raid5")==0 ||
338                                      strcmp(mse->level, "multipath")==0)
339                                         ) {
340                                         struct state *st = malloc(sizeof *st);
341                                         mdu_array_info_t array;
342                                         int fd;
343                                         if (st == NULL)
344                                                 continue;
345                                         st->devname = strdup(get_md_name(mse->devnum));
346                                         if ((fd = open(st->devname, O_RDONLY)) < 0 ||
347                                             ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
348                                                 /* no such array */
349                                                 if (fd >=0) close(fd);
350                                                 free(st->devname);
351                                                 free(st);
352                                                 continue;
353                                         }
354                                         close(fd);
355                                         st->utime = 0;
356                                         st->next = statelist;
357                                         st->err = 1;
358                                         st->devnum = mse->devnum;
359                                         st->percent = -2;
360                                         st->spare_group = NULL;
361                                         st->expected_spares = -1;
362                                         statelist = st;
363                                         alert("NewArray", st->devname, NULL, mailaddr, alert_cmd);
364                                         new_found = 1;
365                                 }
366                 }
367                 /* If an array has active < raid && spare == 0 && spare_group != NULL
368                  * Look for another array with spare > 0 and active == raid and same spare_group
369                  *  if found, choose a device and hotremove/hotadd
370                  */
371                 for (st = statelist; st; st=st->next)
372                         if (st->active < st->raid &&
373                             st->spare == 0 &&
374                             st->spare_group != NULL) {
375                                 struct state *st2;
376                                 for (st2=statelist ; st2 ; st2=st2->next)
377                                         if (st2 != st &&
378                                             st2->spare > 0 &&
379                                             st2->active == st2->raid &&
380                                             st2->spare_group != NULL &&
381                                             strcmp(st->spare_group, st2->spare_group) == 0) {
382                                                 /* try to remove and add */
383                                                 int fd1 = open(st->devname, O_RDONLY);
384                                                 int fd2 = open(st2->devname, O_RDONLY);
385                                                 int dev = -1;
386                                                 int d;
387                                                 if (fd1 < 0 || fd2 < 0) {
388                                                         if (fd1>=0) close(fd1);
389                                                         if (fd2>=0) close(fd2);
390                                                         continue;
391                                                 }
392                                                 for (d=st2->raid; d<MD_SB_DISKS; d++) {
393                                                         if (st2->devid[d] > 0 &&
394                                                             st2->devstate[d] == 0) {
395                                                                 dev = st2->devid[d];
396                                                                 break;
397                                                         }
398                                                 }
399                                                 if (dev > 0) {
400                                                         if (ioctl(fd2, HOT_REMOVE_DISK, 
401                                                                   (unsigned long)dev) == 0) {
402                                                                 if (ioctl(fd1, HOT_ADD_DISK,
403                                                                           (unsigned long)dev) == 0) {
404                                                                         alert("MoveSpare", st->devname, st2->devname, mailaddr, alert_cmd);
405                                                                         close(fd1);
406                                                                         close(fd2);
407                                                                         break;
408                                                                 }
409                                                                 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
410                                                         }
411                                                 }
412                                                 close(fd1);
413                                                 close(fd2);
414                                         }
415                         }
416                 if (!new_found) {
417                         if (oneshot)
418                                 break;
419                         else
420                                 mdstat_wait(period);
421                 }
422                 test = 0;
423         }
424         return 0;
425 }
426
427
428 static void alert(char *event, char *dev, char *disc, char *mailaddr, char *cmd)
429 {
430         if (!cmd && !mailaddr) {
431                 time_t now = time(0);
432                
433                 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
434         }
435         if (cmd) {
436                 int pid = fork();
437                 switch(pid) {
438                 default:
439                         waitpid(pid, NULL, 0);
440                         break;
441                 case -1:
442                         break;
443                 case 0:
444                         execl(cmd, cmd, event, dev, disc, NULL);
445                         exit(2);
446                 }
447         }
448         if (mailaddr && 
449             (strncmp(event, "Fail", 4)==0 || 
450              strncmp(event, "Test", 4)==0 ||
451              strncmp(event, "Degrade", 7)==0)) {
452                 FILE *mp = popen(Sendmail, "w");
453                 if (mp) {
454                         char hname[256];
455                         gethostname(hname, sizeof(hname));
456                         signal(SIGPIPE, SIG_IGN);
457                         fprintf(mp, "From: " Name " monitoring <root>\n");
458                         fprintf(mp, "To: %s\n", mailaddr);
459                         fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
460
461                         fprintf(mp, "This is an automatically generated mail message from " Name "\n");
462                         fprintf(mp, "running on %s\n\n", hname);
463
464                         fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
465
466                         if (disc)
467                                 fprintf(mp, "It could be related to component device %s.\n\n", disc);
468
469                         fprintf(mp, "Faithfully yours, etc.\n");
470                         fclose(mp);
471                 }
472
473         }
474         /* FIXME log the event to syslog maybe */
475 }