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:
parent
e71641b88f
commit
bb7bf0bcd7
@ -1,3 +1,4 @@
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
@ -10,12 +11,13 @@ int writeyes(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int writeno(const char *s) {
|
||||
if(s)
|
||||
printf("no (%s)\n", s);
|
||||
else
|
||||
puts("no");
|
||||
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;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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';
|
||||
|
@ -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);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <libgen.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -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))
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user