Break Monitor into smaller functions.

Monitor() has become way too big.  Break it up into multiple smaller
functions that are all called from the main loop.

Signed-off-by: NeilBrown <neilb@suse.de>
This commit is contained in:
NeilBrown 2010-11-22 20:58:07 +11:00
parent ca750d9830
commit 2e0172b110
1 changed files with 452 additions and 397 deletions

457
Monitor.c
View File

@ -30,14 +30,44 @@
#include <limits.h>
#include <syslog.h>
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
char *cmd, int dosyslog);
/* 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
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 expected_spares;
int devstate[MaxDisks];
unsigned devid[MaxDisks];
int percent;
int parent_dev; /* For subarray, devnum of parent.
* For others, NoMdDev
*/
struct supertype *metadata;
struct state *next;
};
static int make_daemon(char *pidfile);
static int check_one_sharer(int scan);
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
char *cmd, int dosyslog);
static void check_array(struct state *st, struct mdstat_ent *mdstat,
int test, char *mailaddr,
char *mailfrom, char *alert_cmd, int dosyslog,
int increments);
static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist,
int test, char *mailaddr, char *mailfrom,
char *alert_cmd, int dosyslog);
static void try_spare_migration(struct state *statelist,
char *mailaddr, char *mailfrom,
char *alert_cmd, int dosyslog);
int Monitor(struct mddev_dev *devlist,
char *mailaddr, char *alert_cmd,
int period, int daemonise, int scan, int oneshot,
@ -86,23 +116,7 @@ int Monitor(struct mddev_dev *devlist,
* 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 expected_spares;
int devstate[MaxDisks];
unsigned devid[MaxDisks];
int percent;
int parent_dev; /* For subarray, devnum of parent.
* For others, NoMdDev
*/
struct supertype *metadata;
struct state *next;
} *statelist = NULL;
struct state *statelist = NULL;
int finished = 0;
struct mdstat_ent *mdstat = NULL;
char *mailfrom = NULL;
@ -126,72 +140,13 @@ int Monitor(struct mddev_dev *devlist,
return 1;
}
if (daemonise) {
int pid = fork();
if (pid > 0) {
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);
}
}
return 0;
}
if (pid < 0) {
perror("daemonise");
if (daemonise)
if (make_daemon(pidfile))
return 1;
}
close(0);
open("/dev/null", O_RDWR);
dup2(0,1);
dup2(0,2);
setsid();
}
if (share) {
int pid, rv;
FILE *fp;
char dir[20];
struct stat buf;
fp = fopen("/var/run/mdadm/autorebuild.pid", "r");
if (fp) {
fscanf(fp, "%d", &pid);
sprintf(dir, "/proc/%d", pid);
rv = stat(dir, &buf);
if (rv != -1) {
if (scan) {
fprintf(stderr, Name ": Only one "
"autorebuild process allowed"
" in scan mode, aborting\n");
fclose(fp);
if (share)
if (check_one_sharer(scan))
return 1;
} else {
fprintf(stderr, Name ": Warning: One"
" autorebuild process already"
" running.");
}
}
fclose(fp);
}
if (scan) {
fp = fopen("/var/run/mdadm/autorebuild.pid", "w");
if (!fp)
fprintf(stderr, Name ": Cannot create"
" autorebuild.pid "
"file\n");
else {
pid = getpid();
fprintf(fp, "%d\n", pid);
fclose(fp);
}
}
}
if (devlist == NULL) {
struct mddev_ident *mdlist = conf_get_ident(NULL);
@ -256,7 +211,204 @@ int Monitor(struct mddev_dev *devlist,
free_mdstat(mdstat);
mdstat = mdstat_read(oneshot?0:1, 0);
for (st=statelist; st; st=st->next) {
for (st=statelist; st; st=st->next)
check_array(st, mdstat, test, mailaddr, mailfrom,
alert_cmd, dosyslog, increments);
/* now check if there are any new devices found in mdstat */
if (scan)
new_found = add_new_arrays(mdstat, statelist, test,
mailaddr, mailfrom, alert_cmd,
dosyslog);
/* 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
*/
if (share)
try_spare_migration(statelist, mailaddr, mailfrom,
alert_cmd, dosyslog);
if (!new_found) {
if (oneshot)
break;
else
mdstat_wait(period);
}
test = 0;
}
if (pidfile)
unlink(pidfile);
return 0;
}
static int make_daemon(char *pidfile)
{
int pid = fork();
if (pid > 0) {
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);
}
}
return 0;
}
if (pid < 0) {
perror("daemonise");
return 1;
}
close(0);
open("/dev/null", O_RDWR);
dup2(0,1);
dup2(0,2);
setsid();
return 0;
}
static int check_one_sharer(int scan)
{
int pid, rv;
FILE *fp;
char dir[20];
struct stat buf;
fp = fopen("/var/run/mdadm/autorebuild.pid", "r");
if (fp) {
fscanf(fp, "%d", &pid);
sprintf(dir, "/proc/%d", pid);
rv = stat(dir, &buf);
if (rv != -1) {
if (scan) {
fprintf(stderr, Name ": Only one "
"autorebuild process allowed"
" in scan mode, aborting\n");
fclose(fp);
return 1;
} else {
fprintf(stderr, Name ": Warning: One"
" autorebuild process already"
" running.");
}
}
fclose(fp);
}
if (scan) {
fp = fopen("/var/run/mdadm/autorebuild.pid", "w");
if (!fp)
fprintf(stderr, Name ": Cannot create"
" autorebuild.pid "
"file\n");
else {
pid = getpid();
fprintf(fp, "%d\n", pid);
fclose(fp);
}
}
return 0;
}
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
int dosyslog)
{
int priority;
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 ||
strncmp(event, "Test", 4)==0 ||
strncmp(event, "Spares", 6)==0 ||
strncmp(event, "Degrade", 7)==0)) {
FILE *mp = popen(Sendmail, "w");
if (mp) {
FILE *mdstat;
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");
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 && disc[0] != ' ')
fprintf(mp, "It could be related to component device %s.\n\n", disc);
if (disc && disc[0] == ' ')
fprintf(mp, "Extra information:%s.\n\n", disc);
fprintf(mp, "Faithfully yours, etc.\n");
mdstat = fopen("/proc/mdstat", "r");
if (mdstat) {
char buf[8192];
int n;
fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
fclose(mdstat);
}
pclose(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);
}
}
static void check_array(struct state *st, struct mdstat_ent *mdstat,
int test, char *mailaddr,
char *mailfrom, char *alert_cmd, int dosyslog,
int increments)
{
struct { int state, major, minor; } info[MaxDisks];
mdu_array_info_t array;
struct mdstat_ent *mse = NULL, *mse2;
@ -274,7 +426,7 @@ int Monitor(struct mddev_dev *devlist,
/* fprintf(stderr, Name ": cannot open %s: %s\n",
dev, strerror(errno));
*/ st->err=1;
continue;
return;
}
fcntl(fd, F_SETFD, FD_CLOEXEC);
if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
@ -285,7 +437,7 @@ int Monitor(struct mddev_dev *devlist,
dev, strerror(errno));
*/ st->err=1;
close(fd);
continue;
return;
}
/* It's much easier to list what array levels can't
* have a device disappear than all of them that can
@ -296,7 +448,7 @@ int Monitor(struct mddev_dev *devlist,
mailaddr, mailfrom, alert_cmd, dosyslog);
st->err = 1;
close(fd);
continue;
return;
}
if (st->devnum == INT_MAX) {
struct stat stb;
@ -320,7 +472,7 @@ int Monitor(struct mddev_dev *devlist,
* or re-created after reading mdstat*/
st->err = 1;
close(fd);
continue;
return;
}
/* this array is in /proc/mdstat */
if (array.utime == 0)
@ -337,7 +489,7 @@ int Monitor(struct mddev_dev *devlist,
))) {
close(fd);
st->err = 0;
continue;
return;
}
if (st->utime == 0 && /* new array */
mse->pattern && strchr(mse->pattern, '_') /* degraded */
@ -466,9 +618,14 @@ int Monitor(struct mddev_dev *devlist,
st->raid = array.raid_disks;
st->err = 0;
}
/* now check if there are any new devices found in mdstat */
if (scan) {
static int add_new_arrays(struct mdstat_ent *mdstat, struct state *statelist,
int test, char *mailaddr, char *mailfrom,
char *alert_cmd, int dosyslog)
{
struct mdstat_ent *mse;
int new_found = 0;
for (mse=mdstat; mse; mse=mse->next)
if (mse->devnum != INT_MAX &&
(!mse->level || /* retrieve containers */
@ -515,12 +672,15 @@ int Monitor(struct mddev_dev *devlist,
alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
new_found = 1;
}
return new_found;
}
/* 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
*/
if (share) for (st = statelist; st; st=st->next)
static void try_spare_migration(struct state *statelist,
char *mailaddr, char *mailfrom,
char *alert_cmd, int dosyslog)
{
struct state *st;
for (st = statelist; st; st=st->next)
if (st->active < st->raid &&
st->spare == 0 &&
st->spare_group != NULL) {
@ -574,112 +734,7 @@ int Monitor(struct mddev_dev *devlist,
close(fd2);
}
}
if (!new_found) {
if (oneshot)
break;
else
mdstat_wait(period);
}
test = 0;
}
if (pidfile)
unlink(pidfile);
return 0;
}
static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
int dosyslog)
{
int priority;
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 ||
strncmp(event, "Test", 4)==0 ||
strncmp(event, "Spares", 6)==0 ||
strncmp(event, "Degrade", 7)==0)) {
FILE *mp = popen(Sendmail, "w");
if (mp) {
FILE *mdstat;
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");
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 && disc[0] != ' ')
fprintf(mp, "It could be related to component device %s.\n\n", disc);
if (disc && disc[0] == ' ')
fprintf(mp, "Extra information:%s.\n\n", disc);
fprintf(mp, "Faithfully yours, etc.\n");
mdstat = fopen("/proc/mdstat", "r");
if (mdstat) {
char buf[8192];
int n;
fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
fclose(mdstat);
}
pclose(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);
}
}
/* Not really Monitor but ... */
int Wait(char *dev)
{