2
0
mirror of https://github.com/munin-monitoring/contrib.git synced 2018-11-08 00:59:34 +01:00

refactor code

Added common functions autoconf_check_readable and fail.
Added some TODOs for later completion.
Updated messages from upstream plugins.
This commit is contained in:
Helmut Grohne 2013-02-05 12:38:12 +01:00
parent e71641b88f
commit bb7bf0bcd7
15 changed files with 97 additions and 166 deletions

View File

@ -1,3 +1,4 @@
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -10,12 +11,13 @@ int writeyes(void) {
return 0; return 0;
} }
int writeno(const char *s) { int autoconf_check_readable(const char *path) {
if(s) if(0 == access(path, R_OK))
printf("no (%s)\n", s); return writeyes();
else else {
puts("no"); printf("no (%s is not readable, errno=%d)\n", path, errno);
return 1; return 1;
}
} }
int getenvint(const char *name, int defvalue) { int getenvint(const char *name, int defvalue) {
@ -64,3 +66,9 @@ void print_warncrit(const char *name) {
print_warning(name); print_warning(name);
print_critical(name); print_critical(name);
} }
int fail(const char *message) {
fputs(message, stderr);
fputc('\n', stderr);
return 1;
}

View File

@ -4,11 +4,12 @@
#define PROC_STAT "/proc/stat" #define PROC_STAT "/proc/stat"
int writeyes(void); int writeyes(void);
int writeno(const char *); int autoconf_check_readable(const char *);
int getenvint(const char *, int); int getenvint(const char *, int);
const char *getenv_composed(const char *, const char *); const char *getenv_composed(const char *, const char *);
void print_warning(const char *); void print_warning(const char *);
void print_critical(const char *); void print_critical(const char *);
void print_warncrit(const char *); void print_warncrit(const char *);
int fail(const char *);
#endif #endif

View File

@ -9,6 +9,8 @@
#define SYSCRITICAL 50 #define SYSCRITICAL 50
#define USRWARNING 80 #define USRWARNING 80
/* TODO: port support for env.foo_warning and env.foo_critical from mainline plugin */
int cpu(int argc, char **argv) { int cpu(int argc, char **argv) {
FILE *f; FILE *f;
char buff[256], *s; char buff[256], *s;
@ -19,10 +21,8 @@ int cpu(int argc, char **argv) {
if(s && !strcmp(s, "yes")) if(s && !strcmp(s, "yes"))
scaleto100=1; scaleto100=1;
if(!(f=fopen(PROC_STAT, "r"))) { if(!(f=fopen(PROC_STAT, "r")))
fputs("cannot open " PROC_STAT "\n", stderr); return fail("cannot open " PROC_STAT);
return 1;
}
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
if(!strncmp(buff, "cpu", 3)) { if(!strncmp(buff, "cpu", 3)) {
if(isdigit(buff[3])) if(isdigit(buff[3]))
@ -36,10 +36,8 @@ int cpu(int argc, char **argv) {
} }
fclose(f); fclose(f);
if(ncpu < 1 || extinfo < 4) { if(ncpu < 1 || extinfo < 4)
fputs("cannot parse " PROC_STAT "\n", stderr); return fail("cannot parse " PROC_STAT);
return 1;
}
puts("graph_title CPU usage"); puts("graph_title CPU usage");
if(extinfo >= 7) if(extinfo >= 7)
@ -133,17 +131,11 @@ int cpu(int argc, char **argv) {
} }
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(PROC_STAT, R_OK)) return autoconf_check_readable(PROC_STAT);
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(!(f=fopen(PROC_STAT, "r")))
return fail("cannot open " PROC_STAT);
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
hz = getenvint("HZ", 100); hz = getenvint("HZ", 100);
if(!strncmp(buff, "cpu ", 4)) { if(!strncmp(buff, "cpu ", 4)) {
@ -179,6 +171,5 @@ int cpu(int argc, char **argv) {
} }
} }
fclose(f); fclose(f);
fputs("no cpu line found in " PROC_STAT "\n", stderr); return fail("no cpu line found in " PROC_STAT);
return 1;
} }

View File

@ -1,5 +1,6 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include "common.h" #include "common.h"
#define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail" #define ENTROPY_AVAIL "/proc/sys/kernel/random/entropy_avail"
@ -21,16 +22,13 @@ int entropy(int argc, char **argv) {
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) if(!strcmp(argv[1], "autoconf"))
return writeyes(); return autoconf_check_readable(ENTROPY_AVAIL);
}
if(!(f=fopen(ENTROPY_AVAIL, "r"))) {
fputs("cannot open " ENTROPY_AVAIL "\n", stderr);
return 1;
} }
if(!(f=fopen(ENTROPY_AVAIL, "r")))
return fail("cannot open " ENTROPY_AVAIL);
if(1 != fscanf(f, "%d", &entropy)) { if(1 != fscanf(f, "%d", &entropy)) {
fputs("cannot read from " ENTROPY_AVAIL "\n", stderr);
fclose(f); fclose(f);
return 1; return fail("cannot read from " ENTROPY_AVAIL);
} }
fclose(f); fclose(f);
printf("entropy.value %d\n", entropy); printf("entropy.value %d\n", entropy);

View File

@ -21,17 +21,11 @@ int forks(int argc, char **argv) {
print_warncrit("forks"); print_warncrit("forks");
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(PROC_STAT, R_OK)) return autoconf_check_readable(PROC_STAT);
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(!(f=fopen(PROC_STAT, "r")))
return fail("cannot open " PROC_STAT);
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
if(!strncmp(buff, "processes ", 10)) { if(!strncmp(buff, "processes ", 10)) {
fclose(f); fclose(f);
@ -40,6 +34,5 @@ int forks(int argc, char **argv) {
} }
} }
fclose(f); fclose(f);
fputs("no processes line found in " PROC_STAT "\n", stderr); return fail("no processes line found in " PROC_STAT);
return 1;
} }

View File

@ -25,17 +25,11 @@ int fw_packets(int argc, char **argv) {
"forwarded.min 0"); "forwarded.min 0");
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(PROC_NET_SNMP, R_OK)) return autoconf_check_readable(PROC_NET_SNMP);
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(!(f=fopen(PROC_NET_SNMP, "r")))
return fail("cannot open " PROC_NET_SNMP);
while(fgets(buff, 1024, f)) { while(fgets(buff, 1024, f)) {
if(!strncmp(buff, "Ip: ", 4) && isdigit(buff[4])) { if(!strncmp(buff, "Ip: ", 4) && isdigit(buff[4])) {
fclose(f); fclose(f);
@ -57,6 +51,5 @@ int fw_packets(int argc, char **argv) {
} }
} }
fclose(f); fclose(f);
fputs("no ip line found in " PROC_NET_SNMP "\n", stderr); return fail("no ip line found in " PROC_NET_SNMP);
return 1;
} }

View File

@ -9,27 +9,20 @@
int if_err_(int argc, char **argv) { int if_err_(int argc, char **argv) {
char *interface; char *interface;
size_t interface_len;
FILE *f; FILE *f;
char buff[256], *s; char buff[256], *s;
int i; int i;
interface = basename(argv[0]); interface = basename(argv[0]);
if(strncmp(interface, "if_err_", 7) != 0) { if(strncmp(interface, "if_err_", 7) != 0)
fputs("if_err_ invoked with invalid basename\n", stderr); return fail("if_err_ invoked with invalid basename");
return 1;
}
interface += 7; interface += 7;
interface_len = strlen(interface);
if(argc > 1) { if(argc > 1) {
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(access(PROC_NET_DEV, R_OK) == 0) { return autoconf_check_readable(PROC_NET_DEV);
puts("yes");
return 0;
} else {
puts("no (/proc/net/dev not found)");
return 1;
}
}
if(!strcmp(argv[1], "suggest")) { if(!strcmp(argv[1], "suggest")) {
if(NULL == (f = fopen(PROC_NET_DEV, "r"))) if(NULL == (f = fopen(PROC_NET_DEV, "r")))
return 1; return 1;
@ -83,9 +76,9 @@ int if_err_(int argc, char **argv) {
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
for(s=buff;*s == ' ';++s) for(s=buff;*s == ' ';++s)
; ;
if(0 != strncmp(s, interface, strlen(interface))) if(0 != strncmp(s, interface, interface_len))
continue; continue;
s += strlen(interface); s += interface_len;
if(*s != ':') if(*s != ':')
continue; continue;
++s; ++s;

View File

@ -8,7 +8,7 @@ int interrupts(int argc, char **argv) {
char buff[256]; char buff[256];
if(argc > 1) { if(argc > 1) {
if(!strcmp(argv[1], "config")) { 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_args --base 1000 -l 0\n"
"graph_vlabel interrupts & ctx switches / ${graph_period}\n" "graph_vlabel interrupts & ctx switches / ${graph_period}\n"
"graph_category system\n" "graph_category system\n"
@ -27,17 +27,11 @@ int interrupts(int argc, char **argv) {
print_warncrit("ctx"); print_warncrit("ctx");
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(PROC_STAT, R_OK)) return autoconf_check_readable(PROC_STAT);
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(!(f=fopen(PROC_STAT, "r")))
return fail("cannot open " PROC_STAT);
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
if(!strncmp(buff, "intr ", 5)) { if(!strncmp(buff, "intr ", 5)) {
buff[5 + strcspn(buff + 5, " \t\n")] = '\0'; buff[5 + strcspn(buff + 5, " \t\n")] = '\0';

View File

@ -17,19 +17,18 @@ int load(int argc, char **argv) {
"graph_category system\n" "graph_category system\n"
"load.label load"); "load.label load");
print_warncrit("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; return 0;
} }
if(!strcmp(argv[1], "autoconf")) if(!strcmp(argv[1], "autoconf"))
return writeyes(); return writeyes();
} }
if(!(f=fopen(PROC_LOADAVG, "r"))) { if(!(f=fopen(PROC_LOADAVG, "r")))
fputs("cannot open " PROC_LOADAVG "\n", stderr); return fail("cannot open " PROC_LOADAVG);
return 1;
}
if(1 != fscanf(f, "%*f %f", &val)) { if(1 != fscanf(f, "%*f %f", &val)) {
fputs("cannot read from " PROC_LOADAVG "\n", stderr);
fclose(f); fclose(f);
return 1; return fail("cannot read from " PROC_LOADAVG);
} }
fclose(f); fclose(f);
printf("load.value %.2f\n", val); printf("load.value %.2f\n", val);

View File

@ -1,6 +1,7 @@
#include <libgen.h> #include <libgen.h>
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "common.h"
int cpu(int argc, char **argv); int cpu(int argc, char **argv);
int entropy(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 uptime(int argc, char **argv);
int busybox(int argc, char **argv) { int busybox(int argc, char **argv) {
if(argc < 2) { if(argc < 2)
fprintf(stderr, "missing parameter\n"); return fail("missing parameter");
return 1; if(0 != strcmp(argv[1], "listplugins"))
} return fail("unknown parameter");
if(0 != strcmp(argv[1], "listplugins")) {
fprintf(stderr, "unknown parameter\n");
return 1;
}
puts("cpu\nentropy\nforks\nfw_packets\ninterrupts\nload\n" puts("cpu\nentropy\nforks\nfw_packets\ninterrupts\nload\n"
"open_files\nopen_inodes\nprocesses\nswap\nuptime"); "open_files\nopen_inodes\nprocesses\nswap\nuptime");
return 0; return 0;
@ -80,6 +77,5 @@ int main(int argc, char **argv) {
return uptime(argc, argv); return uptime(argc, argv);
break; break;
} }
fprintf(stderr, "unknown basename\n"); return fail("unknown basename");
return 1;
} }

View File

@ -5,20 +5,18 @@
#define FS_FILE_NR "/proc/sys/fs/file-nr" #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) { int open_files(int argc, char **argv) {
FILE *f; FILE *f;
int alloc, freeh, avail; int alloc, freeh, avail;
if(argc > 1) { if(argc > 1) {
if(!strcmp(argv[1], "config")) { if(!strcmp(argv[1], "config")) {
if(!(f=fopen(FS_FILE_NR, "r"))) { if(!(f=fopen(FS_FILE_NR, "r")))
fprintf(stderr, "cannot open " FS_FILE_NR "\n"); return fail("cannot open " FS_FILE_NR);
return 1;
}
if(1 != fscanf(f, "%*d %*d %d", &avail)) { if(1 != fscanf(f, "%*d %*d %d", &avail)) {
fclose(f); fclose(f);
fprintf(stderr, "cannot read from " FS_FILE_NR return fail("cannot read from " FS_FILE_NR);
"\n");
return 1;
} }
fclose(f); fclose(f);
puts("graph_title File table usage\n" 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)); (int)(avail*0.92), (int)(avail*0.98));
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(FS_FILE_NR, R_OK)) return autoconf_check_readable(FS_FILE_NR);
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(!(f=fopen(FS_FILE_NR, "r")))
return fail("cannot open " FS_FILE_NR);
if(3 != fscanf(f, "%d %d %d", &alloc, &freeh, &avail)) { if(3 != fscanf(f, "%d %d %d", &alloc, &freeh, &avail)) {
fclose(f); fclose(f);
fputs("cannot read from " FS_FILE_NR "\n", stderr); return fail("cannot read from " FS_FILE_NR);
return 1;
} }
fclose(f); fclose(f);
printf("used.value %d\nmax.value %d\n", alloc-freeh, avail); printf("used.value %d\nmax.value %d\n", alloc-freeh, avail);

View File

@ -23,21 +23,14 @@ int open_inodes(int argc, char **argv) {
print_warncrit("max"); print_warncrit("max");
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(FS_INODE_NR, R_OK)) return autoconf_check_readable(FS_INODE_NR);
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(!(f=fopen(FS_INODE_NR, "r")))
return fail("cannot open " FS_INODE_NR);
if(2 != fscanf(f, "%d %d", &nr, &freen)) { if(2 != fscanf(f, "%d %d", &nr, &freen)) {
fclose(f); fclose(f);
fputs("cannot read from " FS_INODE_NR "\n", stderr); return fail("cannot read from " FS_INODE_NR);
return 1;
} }
fclose(f); fclose(f);
printf("used.value %d\nmax.value %d\n", nr-freen, nr); printf("used.value %d\nmax.value %d\n", nr-freen, nr);

View File

@ -25,10 +25,8 @@ int processes(int argc, char **argv) {
if(!strcmp(argv[1], "autoconf")) if(!strcmp(argv[1], "autoconf"))
return writeyes(); return writeyes();
} }
if(!(d = opendir("/proc"))) { if(!(d = opendir("/proc")))
fputs("cannot open /proc\n", stderr); return fail("cannot open /proc");
return 1;
}
while((e = readdir(d))) { while((e = readdir(d))) {
for(s=e->d_name;*s;++s) for(s=e->d_name;*s;++s)
if(!isdigit(*s)) if(!isdigit(*s))

View File

@ -27,19 +27,13 @@ int swap(int argc, char **argv) {
print_warncrit("swap_out"); print_warncrit("swap_out");
return 0; return 0;
} }
if(!strcmp(argv[1], "autoconf")) { if(!strcmp(argv[1], "autoconf"))
if(0 == access(PROC_STAT, R_OK)) return autoconf_check_readable(PROC_STAT);
return writeyes();
else
return writeno(PROC_STAT " not readable");
}
} }
if(!access("/proc/vmstat", F_OK)) { if(!access("/proc/vmstat", F_OK)) {
in=out=0; in=out=0;
if(!(f=fopen("/proc/vmstat", "r"))) { if(!(f=fopen("/proc/vmstat", "r")))
fputs("cannot open /proc/vmstat\n", stderr); return fail("cannot open /proc/vmstat");
return 1;
}
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
if(!in && !strncmp(buff, "pswpin ", 7)) { if(!in && !strncmp(buff, "pswpin ", 7)) {
++in; ++in;
@ -51,30 +45,22 @@ int swap(int argc, char **argv) {
} }
} }
fclose(f); fclose(f);
if(!(in*out)) { if(!(in*out))
fputs("no usable data on /proc/vmstat\n", stderr); return fail("no usable data on /proc/vmstat");
return 1;
}
return 0; return 0;
} else { } else {
if(!(f=fopen(PROC_STAT, "r"))) { if(!(f=fopen(PROC_STAT, "r")))
fputs("cannot open " PROC_STAT "\n", stderr); return fail("cannot open " PROC_STAT);
return 1;
}
while(fgets(buff, 256, f)) { while(fgets(buff, 256, f)) {
if(!strncmp(buff, "swap ", 5)) { if(!strncmp(buff, "swap ", 5)) {
fclose(f); fclose(f);
if(2 != sscanf(buff+5, "%d %d", &in, &out)) { if(2 != sscanf(buff+5, "%d %d", &in, &out))
fputs("bad data on " PROC_STAT "\n", return fail("bad data on " PROC_STAT);
stderr);
return 1;
}
printf("swap_in.value %d\nswap_out.value %d\n", in, out); printf("swap_in.value %d\nswap_out.value %d\n", in, out);
return 0; return 0;
} }
} }
fclose(f); fclose(f);
fputs("no swap line found in " PROC_STAT "\n", stderr); return fail("no swap line found in " PROC_STAT);
return 1;
} }
} }

View File

@ -18,14 +18,11 @@ int uptime(int argc, char **argv) {
if(!strcmp(argv[1], "autoconf")) if(!strcmp(argv[1], "autoconf"))
return writeyes(); return writeyes();
} }
if(!(f=fopen("/proc/uptime", "r"))) { if(!(f=fopen("/proc/uptime", "r")))
fputs("cannot open /proc/uptime\n", stderr); return fail("cannot open /proc/uptime");
return 1;
}
if(1 != fscanf(f, "%f", &uptime)) { if(1 != fscanf(f, "%f", &uptime)) {
fputs("cannot read from /proc/uptime\n", stderr);
fclose(f); fclose(f);
return 1; return fail("cannot read from /proc/uptime");
} }
fclose(f); fclose(f);
printf("uptime.value %.2f\n", uptime/86400); printf("uptime.value %.2f\n", uptime/86400);