From bb7bf0bcd7930c89dcd1af60ff8ed767c1db425b Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 5 Feb 2013 12:38:12 +0100 Subject: [PATCH] refactor code Added common functions autoconf_check_readable and fail. Added some TODOs for later completion. Updated messages from upstream plugins. --- tools/munin-plugins-busybox/common.c | 20 +++++++++---- tools/munin-plugins-busybox/common.h | 3 +- tools/munin-plugins-busybox/cpu.c | 31 +++++++------------ tools/munin-plugins-busybox/entropy.c | 12 ++++---- tools/munin-plugins-busybox/forks.c | 17 ++++------- tools/munin-plugins-busybox/fw_packets.c | 17 ++++------- tools/munin-plugins-busybox/if_err_.c | 23 +++++---------- tools/munin-plugins-busybox/interrupts.c | 16 ++++------ tools/munin-plugins-busybox/load.c | 11 ++++--- tools/munin-plugins-busybox/main.c | 16 ++++------ tools/munin-plugins-busybox/open_files.c | 29 +++++++----------- tools/munin-plugins-busybox/open_inodes.c | 17 ++++------- tools/munin-plugins-busybox/processes.c | 6 ++-- tools/munin-plugins-busybox/swap.c | 36 +++++++---------------- tools/munin-plugins-busybox/uptime.c | 9 ++---- 15 files changed, 97 insertions(+), 166 deletions(-) diff --git a/tools/munin-plugins-busybox/common.c b/tools/munin-plugins-busybox/common.c index 3527ef78..83457c17 100644 --- a/tools/munin-plugins-busybox/common.c +++ b/tools/munin-plugins-busybox/common.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -10,12 +11,13 @@ int writeyes(void) { return 0; } -int writeno(const char *s) { - if(s) - printf("no (%s)\n", s); - else - puts("no"); - return 1; +int autoconf_check_readable(const char *path) { + if(0 == access(path, R_OK)) + return writeyes(); + else { + printf("no (%s is not readable, errno=%d)\n", path, errno); + return 1; + } } int getenvint(const char *name, int defvalue) { @@ -64,3 +66,9 @@ void print_warncrit(const char *name) { print_warning(name); print_critical(name); } + +int fail(const char *message) { + fputs(message, stderr); + fputc('\n', stderr); + return 1; +} diff --git a/tools/munin-plugins-busybox/common.h b/tools/munin-plugins-busybox/common.h index ac4d08c8..c35cac85 100644 --- a/tools/munin-plugins-busybox/common.h +++ b/tools/munin-plugins-busybox/common.h @@ -4,11 +4,12 @@ #define PROC_STAT "/proc/stat" int writeyes(void); -int writeno(const char *); +int autoconf_check_readable(const char *); int getenvint(const char *, int); const char *getenv_composed(const char *, const char *); void print_warning(const char *); void print_critical(const char *); void print_warncrit(const char *); +int fail(const char *); #endif diff --git a/tools/munin-plugins-busybox/cpu.c b/tools/munin-plugins-busybox/cpu.c index 2c68f889..0cfa1d45 100644 --- a/tools/munin-plugins-busybox/cpu.c +++ b/tools/munin-plugins-busybox/cpu.c @@ -9,6 +9,8 @@ #define SYSCRITICAL 50 #define USRWARNING 80 +/* TODO: port support for env.foo_warning and env.foo_critical from mainline plugin */ + int cpu(int argc, char **argv) { FILE *f; char buff[256], *s; @@ -19,10 +21,8 @@ int cpu(int argc, char **argv) { if(s && !strcmp(s, "yes")) scaleto100=1; - if(!(f=fopen(PROC_STAT, "r"))) { - fputs("cannot open " PROC_STAT "\n", stderr); - return 1; - } + if(!(f=fopen(PROC_STAT, "r"))) + return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "cpu", 3)) { if(isdigit(buff[3])) @@ -36,10 +36,8 @@ int cpu(int argc, char **argv) { } fclose(f); - if(ncpu < 1 || extinfo < 4) { - fputs("cannot parse " PROC_STAT "\n", stderr); - return 1; - } + if(ncpu < 1 || extinfo < 4) + return fail("cannot parse " PROC_STAT); puts("graph_title CPU usage"); if(extinfo >= 7) @@ -133,17 +131,11 @@ int cpu(int argc, char **argv) { } return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(PROC_STAT, R_OK)) - return writeyes(); - else - return writeno(PROC_STAT " not readable"); - } - } - if(!(f=fopen(PROC_STAT, "r"))) { - fputs("cannot open " PROC_STAT "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_STAT); } + if(!(f=fopen(PROC_STAT, "r"))) + return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { hz = getenvint("HZ", 100); if(!strncmp(buff, "cpu ", 4)) { @@ -179,6 +171,5 @@ int cpu(int argc, char **argv) { } } fclose(f); - fputs("no cpu line found in " PROC_STAT "\n", stderr); - return 1; + return fail("no cpu line found in " PROC_STAT); } diff --git a/tools/munin-plugins-busybox/entropy.c b/tools/munin-plugins-busybox/entropy.c index 57206d2a..2f7d998f 100644 --- a/tools/munin-plugins-busybox/entropy.c +++ b/tools/munin-plugins-busybox/entropy.c @@ -1,5 +1,6 @@ #include #include +#include #include "common.h" #define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail" @@ -21,16 +22,13 @@ int entropy(int argc, char **argv) { return 0; } if(!strcmp(argv[1], "autoconf")) - return writeyes(); - } - if(!(f=fopen(ENTROPY_AVAIL, "r"))) { - fputs("cannot open " ENTROPY_AVAIL "\n", stderr); - return 1; + return autoconf_check_readable(ENTROPY_AVAIL); } + if(!(f=fopen(ENTROPY_AVAIL, "r"))) + return fail("cannot open " ENTROPY_AVAIL); if(1 != fscanf(f, "%d", &entropy)) { - fputs("cannot read from " ENTROPY_AVAIL "\n", stderr); fclose(f); - return 1; + return fail("cannot read from " ENTROPY_AVAIL); } fclose(f); printf("entropy.value %d\n", entropy); diff --git a/tools/munin-plugins-busybox/forks.c b/tools/munin-plugins-busybox/forks.c index eac1ccc4..fbbc5e59 100644 --- a/tools/munin-plugins-busybox/forks.c +++ b/tools/munin-plugins-busybox/forks.c @@ -21,17 +21,11 @@ int forks(int argc, char **argv) { print_warncrit("forks"); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(PROC_STAT, R_OK)) - return writeyes(); - else - return writeno(PROC_STAT " not readable"); - } - } - if(!(f=fopen(PROC_STAT, "r"))) { - fputs("cannot open " PROC_STAT "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_STAT); } + if(!(f=fopen(PROC_STAT, "r"))) + return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "processes ", 10)) { fclose(f); @@ -40,6 +34,5 @@ int forks(int argc, char **argv) { } } fclose(f); - fputs("no processes line found in " PROC_STAT "\n", stderr); - return 1; + return fail("no processes line found in " PROC_STAT); } diff --git a/tools/munin-plugins-busybox/fw_packets.c b/tools/munin-plugins-busybox/fw_packets.c index 8e94fdea..1bb9f0f0 100644 --- a/tools/munin-plugins-busybox/fw_packets.c +++ b/tools/munin-plugins-busybox/fw_packets.c @@ -25,17 +25,11 @@ int fw_packets(int argc, char **argv) { "forwarded.min 0"); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(PROC_NET_SNMP, R_OK)) - return writeyes(); - else - return writeno(PROC_NET_SNMP " not readable"); - } - } - if(!(f=fopen(PROC_NET_SNMP, "r"))) { - fputs("cannot open " PROC_NET_SNMP "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_NET_SNMP); } + if(!(f=fopen(PROC_NET_SNMP, "r"))) + return fail("cannot open " PROC_NET_SNMP); while(fgets(buff, 1024, f)) { if(!strncmp(buff, "Ip: ", 4) && isdigit(buff[4])) { fclose(f); @@ -57,6 +51,5 @@ int fw_packets(int argc, char **argv) { } } fclose(f); - fputs("no ip line found in " PROC_NET_SNMP "\n", stderr); - return 1; + return fail("no ip line found in " PROC_NET_SNMP); } diff --git a/tools/munin-plugins-busybox/if_err_.c b/tools/munin-plugins-busybox/if_err_.c index f4fe8174..4ebfe961 100644 --- a/tools/munin-plugins-busybox/if_err_.c +++ b/tools/munin-plugins-busybox/if_err_.c @@ -9,27 +9,20 @@ int if_err_(int argc, char **argv) { char *interface; + size_t interface_len; FILE *f; char buff[256], *s; int i; interface = basename(argv[0]); - if(strncmp(interface, "if_err_", 7) != 0) { - fputs("if_err_ invoked with invalid basename\n", stderr); - return 1; - } + if(strncmp(interface, "if_err_", 7) != 0) + return fail("if_err_ invoked with invalid basename"); interface += 7; + interface_len = strlen(interface); if(argc > 1) { - if(!strcmp(argv[1], "autoconf")) { - if(access(PROC_NET_DEV, R_OK) == 0) { - puts("yes"); - return 0; - } else { - puts("no (/proc/net/dev not found)"); - return 1; - } - } + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_NET_DEV); if(!strcmp(argv[1], "suggest")) { if(NULL == (f = fopen(PROC_NET_DEV, "r"))) return 1; @@ -83,9 +76,9 @@ int if_err_(int argc, char **argv) { while(fgets(buff, 256, f)) { for(s=buff;*s == ' ';++s) ; - if(0 != strncmp(s, interface, strlen(interface))) + if(0 != strncmp(s, interface, interface_len)) continue; - s += strlen(interface); + s += interface_len; if(*s != ':') continue; ++s; diff --git a/tools/munin-plugins-busybox/interrupts.c b/tools/munin-plugins-busybox/interrupts.c index d38e05f2..7fcc6094 100644 --- a/tools/munin-plugins-busybox/interrupts.c +++ b/tools/munin-plugins-busybox/interrupts.c @@ -8,7 +8,7 @@ int interrupts(int argc, char **argv) { char buff[256]; if(argc > 1) { if(!strcmp(argv[1], "config")) { - puts("graph_title Interrupts & context switches\n" + puts("graph_title Interrupts and context switches\n" "graph_args --base 1000 -l 0\n" "graph_vlabel interrupts & ctx switches / ${graph_period}\n" "graph_category system\n" @@ -27,17 +27,11 @@ int interrupts(int argc, char **argv) { print_warncrit("ctx"); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(PROC_STAT, R_OK)) - return writeyes(); - else - return writeno(PROC_STAT " not readable"); - } - } - if(!(f=fopen(PROC_STAT, "r"))) { - fputs("cannot open " PROC_STAT "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_STAT); } + if(!(f=fopen(PROC_STAT, "r"))) + return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "intr ", 5)) { buff[5 + strcspn(buff + 5, " \t\n")] = '\0'; diff --git a/tools/munin-plugins-busybox/load.c b/tools/munin-plugins-busybox/load.c index 8161890f..3b608ca2 100644 --- a/tools/munin-plugins-busybox/load.c +++ b/tools/munin-plugins-busybox/load.c @@ -17,19 +17,18 @@ int load(int argc, char **argv) { "graph_category system\n" "load.label load"); print_warncrit("load"); + puts("graph_info The load average of the machine describes how many processes are in the run-queue (scheduled to run \"immediately\").\n" + "load.info 5 minute load average"); return 0; } if(!strcmp(argv[1], "autoconf")) return writeyes(); } - if(!(f=fopen(PROC_LOADAVG, "r"))) { - fputs("cannot open " PROC_LOADAVG "\n", stderr); - return 1; - } + if(!(f=fopen(PROC_LOADAVG, "r"))) + return fail("cannot open " PROC_LOADAVG); if(1 != fscanf(f, "%*f %f", &val)) { - fputs("cannot read from " PROC_LOADAVG "\n", stderr); fclose(f); - return 1; + return fail("cannot read from " PROC_LOADAVG); } fclose(f); printf("load.value %.2f\n", val); diff --git a/tools/munin-plugins-busybox/main.c b/tools/munin-plugins-busybox/main.c index 6dc67251..5e5545bc 100644 --- a/tools/munin-plugins-busybox/main.c +++ b/tools/munin-plugins-busybox/main.c @@ -1,6 +1,7 @@ #include #include #include +#include "common.h" int cpu(int argc, char **argv); int entropy(int argc, char **argv); @@ -16,14 +17,10 @@ int swap(int argc, char **argv); int uptime(int argc, char **argv); int busybox(int argc, char **argv) { - if(argc < 2) { - fprintf(stderr, "missing parameter\n"); - return 1; - } - if(0 != strcmp(argv[1], "listplugins")) { - fprintf(stderr, "unknown parameter\n"); - return 1; - } + if(argc < 2) + return fail("missing parameter"); + if(0 != strcmp(argv[1], "listplugins")) + return fail("unknown parameter"); puts("cpu\nentropy\nforks\nfw_packets\ninterrupts\nload\n" "open_files\nopen_inodes\nprocesses\nswap\nuptime"); return 0; @@ -80,6 +77,5 @@ int main(int argc, char **argv) { return uptime(argc, argv); break; } - fprintf(stderr, "unknown basename\n"); - return 1; + return fail("unknown basename"); } diff --git a/tools/munin-plugins-busybox/open_files.c b/tools/munin-plugins-busybox/open_files.c index 9eb5c3d7..f83a9f6c 100644 --- a/tools/munin-plugins-busybox/open_files.c +++ b/tools/munin-plugins-busybox/open_files.c @@ -5,20 +5,18 @@ #define FS_FILE_NR "/proc/sys/fs/file-nr" +/* TODO: support env.warning and friends after the upstream plugin is fixed */ + int open_files(int argc, char **argv) { FILE *f; int alloc, freeh, avail; if(argc > 1) { if(!strcmp(argv[1], "config")) { - if(!(f=fopen(FS_FILE_NR, "r"))) { - fprintf(stderr, "cannot open " FS_FILE_NR "\n"); - return 1; - } + if(!(f=fopen(FS_FILE_NR, "r"))) + return fail("cannot open " FS_FILE_NR); if(1 != fscanf(f, "%*d %*d %d", &avail)) { fclose(f); - fprintf(stderr, "cannot read from " FS_FILE_NR - "\n"); - return 1; + return fail("cannot read from " FS_FILE_NR); } fclose(f); puts("graph_title File table usage\n" @@ -36,21 +34,14 @@ int open_files(int argc, char **argv) { (int)(avail*0.92), (int)(avail*0.98)); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(FS_FILE_NR, R_OK)) - return writeyes(); - else - return writeno(FS_FILE_NR " not readable"); - } - } - if(!(f=fopen(FS_FILE_NR, "r"))) { - fputs("cannot open " FS_FILE_NR "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(FS_FILE_NR); } + if(!(f=fopen(FS_FILE_NR, "r"))) + return fail("cannot open " FS_FILE_NR); if(3 != fscanf(f, "%d %d %d", &alloc, &freeh, &avail)) { fclose(f); - fputs("cannot read from " FS_FILE_NR "\n", stderr); - return 1; + return fail("cannot read from " FS_FILE_NR); } fclose(f); printf("used.value %d\nmax.value %d\n", alloc-freeh, avail); diff --git a/tools/munin-plugins-busybox/open_inodes.c b/tools/munin-plugins-busybox/open_inodes.c index a8db64f6..1c33e4de 100644 --- a/tools/munin-plugins-busybox/open_inodes.c +++ b/tools/munin-plugins-busybox/open_inodes.c @@ -23,21 +23,14 @@ int open_inodes(int argc, char **argv) { print_warncrit("max"); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(FS_INODE_NR, R_OK)) - return writeyes(); - else - return writeno(FS_INODE_NR " not readable"); - } - } - if(!(f=fopen(FS_INODE_NR, "r"))) { - fputs("cannot open " FS_INODE_NR "\n", stderr); - return 1; + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(FS_INODE_NR); } + if(!(f=fopen(FS_INODE_NR, "r"))) + return fail("cannot open " FS_INODE_NR); if(2 != fscanf(f, "%d %d", &nr, &freen)) { fclose(f); - fputs("cannot read from " FS_INODE_NR "\n", stderr); - return 1; + return fail("cannot read from " FS_INODE_NR); } fclose(f); printf("used.value %d\nmax.value %d\n", nr-freen, nr); diff --git a/tools/munin-plugins-busybox/processes.c b/tools/munin-plugins-busybox/processes.c index b5227686..5f085f54 100644 --- a/tools/munin-plugins-busybox/processes.c +++ b/tools/munin-plugins-busybox/processes.c @@ -25,10 +25,8 @@ int processes(int argc, char **argv) { if(!strcmp(argv[1], "autoconf")) return writeyes(); } - if(!(d = opendir("/proc"))) { - fputs("cannot open /proc\n", stderr); - return 1; - } + if(!(d = opendir("/proc"))) + return fail("cannot open /proc"); while((e = readdir(d))) { for(s=e->d_name;*s;++s) if(!isdigit(*s)) diff --git a/tools/munin-plugins-busybox/swap.c b/tools/munin-plugins-busybox/swap.c index a1b67234..2c46fd49 100644 --- a/tools/munin-plugins-busybox/swap.c +++ b/tools/munin-plugins-busybox/swap.c @@ -27,19 +27,13 @@ int swap(int argc, char **argv) { print_warncrit("swap_out"); return 0; } - if(!strcmp(argv[1], "autoconf")) { - if(0 == access(PROC_STAT, R_OK)) - return writeyes(); - else - return writeno(PROC_STAT " not readable"); - } + if(!strcmp(argv[1], "autoconf")) + return autoconf_check_readable(PROC_STAT); } if(!access("/proc/vmstat", F_OK)) { in=out=0; - if(!(f=fopen("/proc/vmstat", "r"))) { - fputs("cannot open /proc/vmstat\n", stderr); - return 1; - } + if(!(f=fopen("/proc/vmstat", "r"))) + return fail("cannot open /proc/vmstat"); while(fgets(buff, 256, f)) { if(!in && !strncmp(buff, "pswpin ", 7)) { ++in; @@ -51,30 +45,22 @@ int swap(int argc, char **argv) { } } fclose(f); - if(!(in*out)) { - fputs("no usable data on /proc/vmstat\n", stderr); - return 1; - } + if(!(in*out)) + return fail("no usable data on /proc/vmstat"); return 0; } else { - if(!(f=fopen(PROC_STAT, "r"))) { - fputs("cannot open " PROC_STAT "\n", stderr); - return 1; - } + if(!(f=fopen(PROC_STAT, "r"))) + return fail("cannot open " PROC_STAT); while(fgets(buff, 256, f)) { if(!strncmp(buff, "swap ", 5)) { fclose(f); - if(2 != sscanf(buff+5, "%d %d", &in, &out)) { - fputs("bad data on " PROC_STAT "\n", - stderr); - return 1; - } + if(2 != sscanf(buff+5, "%d %d", &in, &out)) + return fail("bad data on " PROC_STAT); printf("swap_in.value %d\nswap_out.value %d\n", in, out); return 0; } } fclose(f); - fputs("no swap line found in " PROC_STAT "\n", stderr); - return 1; + return fail("no swap line found in " PROC_STAT); } } diff --git a/tools/munin-plugins-busybox/uptime.c b/tools/munin-plugins-busybox/uptime.c index bb3e02cd..6270acb6 100644 --- a/tools/munin-plugins-busybox/uptime.c +++ b/tools/munin-plugins-busybox/uptime.c @@ -18,14 +18,11 @@ int uptime(int argc, char **argv) { if(!strcmp(argv[1], "autoconf")) return writeyes(); } - if(!(f=fopen("/proc/uptime", "r"))) { - fputs("cannot open /proc/uptime\n", stderr); - return 1; - } + if(!(f=fopen("/proc/uptime", "r"))) + return fail("cannot open /proc/uptime"); if(1 != fscanf(f, "%f", &uptime)) { - fputs("cannot read from /proc/uptime\n", stderr); fclose(f); - return 1; + return fail("cannot read from /proc/uptime"); } fclose(f); printf("uptime.value %.2f\n", uptime/86400);