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