mdadm/Monitor.c

544 lines
15 KiB
C
Raw Normal View History

2001-08-23 04:33:20 +02:00
/*
2002-03-08 01:03:52 +01:00
* mdadm - manage Linux "md" devices aka RAID arrays.
2001-08-23 04:33:20 +02:00
*
2002-03-07 00:17:40 +01:00
* Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
2001-08-23 04:33:20 +02:00
*
*
* 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
*/
2002-03-08 01:03:52 +01:00
#include "mdadm.h"
2001-08-23 04:33:20 +02:00
#include "md_p.h"
#include "md_u.h"
2002-04-04 03:58:32 +02:00
#include <sys/wait.h>
2001-08-23 04:33:20 +02:00
#include <sys/signal.h>
2004-01-22 03:10:29 +01:00
#include <values.h>
#include <syslog.h>
2001-08-23 04:33:20 +02:00
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
char *cmd, int dosyslog);
2001-08-23 04:33:20 +02:00
2002-04-04 03:58:32 +02:00
static char *percentalerts[] = {
"RebuildStarted",
"Rebuild20",
"Rebuild40",
"Rebuild60",
"Rebuild80",
};
/* The largest number of disks current arrays can manage is 384
* This really should be dynamically, but that will have to wait
* At least it isn't MD_SB_DISKS.
*/
#define MaxDisks 384
2002-03-07 00:17:40 +01:00
int Monitor(mddev_dev_t devlist,
2001-08-23 04:33:20 +02:00
char *mailaddr, char *alert_cmd,
2003-07-29 01:59:00 +02:00
int period, int daemonise, int scan, int oneshot,
int dosyslog, char *config, int test, char* pidfile)
2001-08-23 04:33:20 +02:00
{
/*
* 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.
2002-04-04 03:58:32 +02:00
* %rebuilt if rebuilding
2001-08-23 04:33:20 +02:00
*
* 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.
2002-04-04 03:58:32 +02:00
* We also read /proc/mdstat to get rebuild percent,
* and to get state on all active devices incase of kernel bug.
2001-08-23 04:33:20 +02:00
*
2002-04-04 03:58:32 +02:00
* 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
2001-08-23 04:33:20 +02:00
*
* 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
*
2002-04-04 03:58:32 +02:00
* 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
2001-08-23 04:33:20 +02:00
*/
struct state {
char *devname;
2002-04-04 03:58:32 +02:00
int devnum; /* to sync with mdstat info */
2001-08-23 04:33:20 +02:00
long utime;
int err;
2002-04-04 03:58:32 +02:00
char *spare_group;
int active, working, failed, spare, raid;
2003-10-29 00:20:01 +01:00
int expected_spares;
int devstate[MaxDisks];
int devid[MaxDisks];
2002-04-04 03:58:32 +02:00
int percent;
2001-08-23 04:33:20 +02:00
struct state *next;
} *statelist = NULL;
int finished = 0;
2002-04-04 03:58:32 +02:00
struct mdstat_ent *mdstat = NULL;
char *mailfrom = NULL;
2002-04-04 03:58:32 +02:00
2003-03-03 00:11:38 +01:00
if (!mailaddr) {
2002-04-04 03:58:32 +02:00
mailaddr = conf_get_mailaddr(config);
2003-03-03 00:11:38 +01:00
if (mailaddr && ! scan)
2003-03-12 23:24:39 +01:00
fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
2003-03-03 00:11:38 +01:00
mailaddr);
}
mailfrom = conf_get_mailfrom(config);
2003-03-03 00:11:38 +01:00
if (!alert_cmd) {
2002-04-04 03:58:32 +02:00
alert_cmd = conf_get_program(config);
2003-03-03 00:11:38 +01:00
if (alert_cmd && ! scan)
2003-03-12 23:24:39 +01:00
fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
2003-03-03 00:11:38 +01:00
alert_cmd);
}
2003-03-12 23:24:39 +01:00
if (scan && !mailaddr && !alert_cmd) {
fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
2003-03-03 00:11:38 +01:00
return 1;
2003-03-12 23:24:39 +01:00
}
2003-03-03 00:11:38 +01:00
if (daemonise) {
int pid = fork();
if (pid > 0) {
2004-11-01 05:49:34 +01:00
if (!pidfile)
printf("%d\n", pid);
else {
FILE *pid_file;
pid_file=fopen(pidfile, "w");
if (!pid_file)
perror("cannot create pid file");
else {
fprintf(pid_file,"%d\n", pid);
fclose(pid_file);
}
}
2003-03-03 00:11:38 +01:00
return 0;
}
if (pid < 0) {
perror("daemonise");
return 1;
}
close(0);
open("/dev/null", 3);
dup2(0,1);
dup2(0,2);
setsid();
}
2002-04-04 03:58:32 +02:00
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;
2003-03-03 00:11:38 +01:00
st->err = 0;
2004-01-22 03:10:29 +01:00
st->devnum = MAXINT;
2002-04-04 03:58:32 +02:00
st->percent = -2;
2003-10-29 00:20:01 +01:00
st->expected_spares = mdlist->spare_disks;
2002-04-04 03:58:32 +02:00
if (mdlist->spare_group)
st->spare_group = strdup(mdlist->spare_group);
else
st->spare_group = NULL;
statelist = st;
}
} else {
2002-03-07 00:17:40 +01:00
mddev_dev_t dv;
2002-04-04 03:58:32 +02:00
for (dv=devlist ; dv; dv=dv->next) {
2004-08-11 04:16:01 +02:00
mddev_ident_t mdlist = conf_get_ident(config, dv->devname);
2002-04-04 03:58:32 +02:00
struct state *st = malloc(sizeof *st);
if (st == NULL)
continue;
st->devname = strdup(dv->devname);
st->utime = 0;
st->next = statelist;
2003-03-03 00:11:38 +01:00
st->err = 0;
2004-01-22 03:10:29 +01:00
st->devnum = MAXINT;
2002-04-04 03:58:32 +02:00
st->percent = -2;
2003-10-29 00:20:01 +01:00
st->expected_spares = -1;
2002-04-04 03:58:32 +02:00
st->spare_group = NULL;
2004-08-11 04:16:01 +02:00
if (mdlist) {
st->expected_spares = mdlist->spare_disks;
if (mdlist->spare_group)
st->spare_group = strdup(mdlist->spare_group);
}
2002-04-04 03:58:32 +02:00
statelist = st;
}
}
while (! finished) {
2003-07-29 01:59:00 +02:00
int new_found = 0;
2002-04-04 03:58:32 +02:00
struct state *st;
if (mdstat)
free_mdstat(mdstat);
mdstat = mdstat_read(oneshot?0:1, 0);
2002-04-04 03:58:32 +02:00
for (st=statelist; st; st=st->next) {
2001-08-23 04:33:20 +02:00
mdu_array_info_t array;
2004-06-04 14:03:19 +02:00
struct mdstat_ent *mse = NULL, *mse2;
2002-04-04 03:58:32 +02:00
char *dev = st->devname;
2001-08-23 04:33:20 +02:00
int fd;
2004-01-22 03:10:29 +01:00
unsigned int i;
2002-04-04 03:58:32 +02:00
2004-01-22 03:10:29 +01:00
if (test)
alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2001-08-23 04:33:20 +02:00
fd = open(dev, O_RDONLY);
if (fd < 0) {
if (!st->err)
2002-04-04 03:58:32 +02:00
alert("DeviceDisappeared", dev, NULL,
mailaddr, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
/* fprintf(stderr, Name ": cannot open %s: %s\n",
2001-08-23 04:33:20 +02:00
dev, strerror(errno));
2002-04-04 03:58:32 +02:00
*/ st->err=1;
2001-08-23 04:33:20 +02:00
continue;
}
if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
if (!st->err)
2002-04-04 03:58:32 +02:00
alert("DeviceDisappeared", dev, NULL,
mailaddr, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
2001-08-23 04:33:20 +02:00
dev, strerror(errno));
2002-04-04 03:58:32 +02:00
*/ st->err=1;
2001-08-23 04:33:20 +02:00
close(fd);
continue;
}
2004-08-11 04:16:01 +02:00
if (array.level != 1 && array.level != 5 && array.level != -4 &&
array.level != 6 && array.level != 10) {
2003-02-12 01:17:26 +01:00
if (!st->err)
alert("DeviceDisappeared", dev, "Wrong-Level",
mailaddr, mailfrom, alert_cmd, dosyslog);
2003-02-12 01:17:26 +01:00
st->err = 1;
close(fd);
continue;
}
2004-01-22 03:10:29 +01:00
if (st->devnum == MAXINT) {
2002-04-04 03:58:32 +02:00
struct stat stb;
if (fstat(fd, &stb) == 0 &&
2004-01-22 03:10:29 +01:00
(S_IFMT&stb.st_mode)==S_IFBLK) {
if (major(stb.st_rdev) == MD_MAJOR)
st->devnum = minor(stb.st_rdev);
2004-01-22 03:10:29 +01:00
else
st->devnum = -1- (minor(stb.st_rdev)>>6);
2004-01-22 03:10:29 +01:00
}
2002-04-04 03:58:32 +02:00
}
2004-06-04 14:03:19 +02:00
for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
if (mse2->devnum == st->devnum) {
mse2->devnum = MAXINT; /* flag it as "used" */
mse = mse2;
}
2002-04-04 03:58:32 +02:00
2001-08-23 04:33:20 +02:00
if (st->utime == array.utime &&
2002-04-04 03:58:32 +02:00
st->failed == array.failed_disks &&
st->working == array.working_disks &&
st->spare == array.spare_disks &&
(mse == NULL || (
mse->percent == st->percent
))) {
2001-08-23 04:33:20 +02:00
close(fd);
2002-04-04 03:58:32 +02:00
st->err = 0;
2001-08-23 04:33:20 +02:00
continue;
}
2003-07-29 01:59:00 +02:00
if (st->utime == 0 && /* new array */
mse && /* is in /proc/mdstat */
mse->pattern && strchr(mse->pattern, '_') /* degraded */
)
alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2003-07-29 01:59:00 +02:00
2003-10-29 00:20:01 +01:00
if (st->utime == 0 && /* new array */
st->expected_spares > 0 &&
array.spare_disks < st->expected_spares)
alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
if (mse &&
st->percent == -1 &&
mse->percent >= 0)
alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
if (mse &&
st->percent >= 0 &&
mse->percent >= 0 &&
(mse->percent / 20) > (st->percent / 20))
alert(percentalerts[mse->percent/20],
dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
2004-01-22 03:10:29 +01:00
if (mse &&
mse->percent == -1 &&
st->percent >= 0)
alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2004-01-22 03:10:29 +01:00
2002-04-04 03:58:32 +02:00
if (mse)
st->percent = mse->percent;
for (i=0; i<MaxDisks; i++) {
2001-08-23 04:33:20 +02:00
mdu_disk_info_t disc;
2002-04-04 03:58:32 +02:00
int newstate=0;
int change;
char *dv = NULL;
2001-08-23 04:33:20 +02:00
disc.number = i;
if (i > array.raid_disks + array.nr_disks) {
newstate = 0;
disc.major = disc.minor = 0;
} else if (ioctl(fd, GET_DISK_INFO, &disc)>= 0) {
2002-04-04 03:58:32 +02:00
newstate = disc.state;
dv = map_dev(disc.major, disc.minor, 1);
} else if (mse && mse->pattern && i < strlen(mse->pattern)) {
2002-04-04 03:58:32 +02:00
switch(mse->pattern[i]) {
case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
case '_': newstate = 0; break;
2001-08-23 04:33:20 +02:00
}
disc.major = disc.minor = 0;
}
if (dv == NULL && st->devid[i])
dv = map_dev(major(st->devid[i]),
minor(st->devid[i]), 1);
2002-04-04 03:58:32 +02:00
change = newstate ^ st->devstate[i];
if (st->utime && change && !st->err) {
2004-01-22 03:10:29 +01:00
if (i < (unsigned)array.raid_disks &&
2002-04-04 03:58:32 +02:00
(((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, mailfrom, alert_cmd, dosyslog);
2004-01-22 03:10:29 +01:00
else if (i >= (unsigned)array.raid_disks &&
2002-04-04 03:58:32 +02:00
(disc.major || disc.minor) &&
st->devid[i] == makedev(disc.major, disc.minor) &&
2002-04-04 03:58:32 +02:00
((newstate&change)&(1<<MD_DISK_FAULTY))
)
alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
2004-01-22 03:10:29 +01:00
else if (i < (unsigned)array.raid_disks &&
2002-04-04 03:58:32 +02:00
(((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, mailfrom, alert_cmd, dosyslog);
2001-08-23 04:33:20 +02:00
}
2002-04-04 03:58:32 +02:00
st->devstate[i] = disc.state;
st->devid[i] = makedev(disc.major, disc.minor);
2001-08-23 04:33:20 +02:00
}
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;
2002-04-04 03:58:32 +02:00
st->raid = array.raid_disks;
st->err = 0;
2001-08-23 04:33:20 +02:00
}
2002-04-04 03:58:32 +02:00
/* now check if there are any new devices found in mdstat */
if (scan) {
struct mdstat_ent *mse;
for (mse=mdstat; mse; mse=mse->next)
2004-01-22 03:10:29 +01:00
if (mse->devnum != MAXINT &&
2003-02-12 01:17:26 +01:00
(strcmp(mse->level, "raid1")==0 ||
strcmp(mse->level, "raid5")==0 ||
strcmp(mse->level, "multipath")==0)
) {
2002-04-04 03:58:32 +02:00
struct state *st = malloc(sizeof *st);
2004-01-22 03:10:29 +01:00
mdu_array_info_t array;
int fd;
2002-04-04 03:58:32 +02:00
if (st == NULL)
continue;
st->devname = strdup(get_md_name(mse->devnum));
2004-01-22 03:10:29 +01:00
if ((fd = open(st->devname, O_RDONLY)) < 0 ||
ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
/* no such array */
if (fd >=0) close(fd);
free(st->devname);
free(st);
continue;
}
2004-06-04 14:03:19 +02:00
close(fd);
2002-04-04 03:58:32 +02:00
st->utime = 0;
st->next = statelist;
st->err = 1;
st->devnum = mse->devnum;
st->percent = -2;
st->spare_group = NULL;
2004-01-22 03:10:29 +01:00
st->expected_spares = -1;
2002-04-04 03:58:32 +02:00
statelist = st;
alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
2003-07-29 01:59:00 +02:00
new_found = 1;
2002-04-04 03:58:32 +02:00
}
}
/* 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 < MaxDisks; d++) {
2002-04-04 03:58:32 +02:00
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, mailfrom, alert_cmd, dosyslog);
2002-04-04 03:58:32 +02:00
close(fd1);
close(fd2);
break;
}
else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
}
}
close(fd1);
close(fd2);
}
}
2003-07-29 01:59:00 +02:00
if (!new_found) {
if (oneshot)
break;
else
2004-06-04 14:03:19 +02:00
mdstat_wait(period);
2003-07-29 01:59:00 +02:00
}
2004-01-22 03:10:29 +01:00
test = 0;
2001-08-23 04:33:20 +02:00
}
2004-11-01 05:49:34 +01:00
if (pidfile)
unlink(pidfile);
2001-08-23 04:33:20 +02:00
return 0;
}
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
int dosyslog)
2001-08-23 04:33:20 +02:00
{
int priority;
2002-03-07 00:17:40 +01:00
if (!cmd && !mailaddr) {
time_t now = time(0);
2002-04-04 03:58:32 +02:00
printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
2002-03-07 00:17:40 +01:00
}
2001-08-23 04:33:20 +02:00
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);
}
}
2003-07-29 01:59:00 +02:00
if (mailaddr &&
(strncmp(event, "Fail", 4)==0 ||
2004-01-22 03:10:29 +01:00
strncmp(event, "Test", 4)==0 ||
strncmp(event, "Spares", 6)==0 ||
2003-07-29 01:59:00 +02:00
strncmp(event, "Degrade", 7)==0)) {
2001-08-23 04:33:20 +02:00
FILE *mp = popen(Sendmail, "w");
if (mp) {
char hname[256];
gethostname(hname, sizeof(hname));
signal(SIGPIPE, SIG_IGN);
if (mailfrom)
fprintf(mp, "From: %s\n", mailfrom);
else
fprintf(mp, "From: " Name " monitoring <root>\n");
2001-08-23 04:33:20 +02:00
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)
2003-03-12 23:24:39 +01:00
fprintf(mp, "It could be related to component device %s.\n\n", disc);
2001-08-23 04:33:20 +02:00
fprintf(mp, "Faithfully yours, etc.\n");
fclose(mp);
}
}
/* log the event to syslog maybe */
if (dosyslog) {
/* Log at a different severity depending on the event.
*
* These are the critical events: */
if (strncmp(event, "Fail", 4)==0 ||
strncmp(event, "Degrade", 7)==0 ||
strncmp(event, "DeviceDisappeared", 17)==0)
priority = LOG_CRIT;
/* Good to know about, but are not failures: */
else if (strncmp(event, "Rebuild", 7)==0 ||
strncmp(event, "MoveSpare", 9)==0 ||
strncmp(event, "Spares", 6) != 0)
priority = LOG_WARNING;
/* Everything else: */
else
priority = LOG_INFO;
if (disc)
syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
else
syslog(priority, "%s event detected on md device %s", event, dev);
}
2001-08-23 04:33:20 +02:00
}