Merge remote branch 'helmut/next'

This commit is contained in:
Steve Schnepp 2013-02-10 10:01:15 +01:00
commit d6bbd2409d
17 changed files with 179 additions and 169 deletions

View File

@ -1,9 +1,10 @@
CC=gcc
CFLAGS=-W -Wall -pedantic -Wextra -g -O2
OBJS=main.o common.o cpu.o entropy.o forks.o fw_packets.o interrupts.o \
if_err_.o load.o open_files.o open_inodes.o processes.o swap.o uptime.o
if_err_.o load.o open_files.o open_inodes.o processes.o swap.o threads.o \
uptime.o
LINKS=cpu entropy forks fw_packets interrupts if_err_eth0 load open_files \
open_inodes processes swap uptime
open_inodes processes swap threads uptime
%.o:%.c
${CC} ${CFLAGS} -c $< -o $@

View File

@ -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");
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 0;
}
}
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;
}

View File

@ -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

View File

@ -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);
}

View File

@ -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);

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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;
@ -120,5 +113,6 @@ int if_err_(int argc, char **argv) {
fwrite(s, 1, i, stdout);
putchar('\n');
}
fclose(f);
return 0;
}

View File

@ -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';

View File

@ -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);

View File

@ -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);
@ -13,19 +14,16 @@ int open_files(int argc, char **argv);
int open_inodes(int argc, char **argv);
int processes(int argc, char **argv);
int swap(int argc, char **argv);
int threads(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");
"open_files\nopen_inodes\nswap\nthreads\nuptime");
return 0;
}
@ -75,11 +73,14 @@ int main(int argc, char **argv) {
if(!strcmp(progname, "swap"))
return swap(argc, argv);
break;
case 't':
if(!strcmp(progname+1, "hreads"))
return threads(argc, argv);
break;
case 'u':
if(!strcmp(progname, "uptime"))
return uptime(argc, argv);
break;
}
fprintf(stderr, "unknown basename\n");
return 1;
return fail("unknown basename");
}

View File

@ -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);

View File

@ -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);

View File

@ -5,6 +5,8 @@
#include <ctype.h>
#include "common.h"
/* TODO: The upstream plugin does way more nowawdays. */
int processes(int argc, char **argv) {
DIR *d;
struct dirent *e;
@ -25,10 +27,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))

View File

@ -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);
}
}

View File

@ -0,0 +1,70 @@
#include <ctype.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "common.h"
int threads(int argc, char **argv) {
FILE *f;
char buff[256];
const char *s;
int i, sum;
DIR *d;
struct dirent *e;
if(argc > 1) {
if(!strcmp(argv[1], "autoconf")) {
i = getpid();
sprintf(buff, "/proc/%d/status", i);
if(NULL == (f = fopen(buff, "r")))
return fail("failed to open /proc/$$/status");
while(fgets(buff, 256, f))
if(!strncmp(buff, "Threads:", 8)) {
fclose(f);
return writeyes();
}
fclose(f);
puts("no");
return 0;
}
if(!strcmp(argv[1], "config")) {
puts("graph_title Number of threads\n"
"graph_vlabel number of threads\n"
"graph_category processes\n"
"graph_info This graph shows the number of threads.\n"
"threads.label threads\n"
"threads.info The current number of threads.");
return 0;
}
}
if(NULL == (d = opendir("/proc")))
return fail("cannot open /proc");
sum = 0;
while((e = readdir(d))) {
for(s=e->d_name;*s;++s)
if(!isdigit(*s))
break;
if(*s) /* non-digit found */
continue;
snprintf(buff, 256, "/proc/%s/status", e->d_name);
if(!(f = fopen(buff, "r")))
continue; /* process has vanished */
while(fgets(buff, 256, f)) {
if(strncmp(buff, "Threads:", 8))
continue;
if(1 != sscanf(buff+8, "%d", &i)) {
fclose(f);
closedir(d);
return fail("failed to parse "
"/proc/somepid/status");
}
sum += i;
}
fclose(f);
}
closedir(d);
printf("threads.value %d\n", sum);
return 0;
}

View File

@ -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);