From 4337b550a01e9f18a2470c30fc96627528de7bef Mon Sep 17 00:00:00 2001 From: Helmut Grohne Date: Tue, 5 Feb 2013 13:12:43 +0100 Subject: [PATCH] mpb: add threads.c plugin --- tools/munin-plugins-busybox/Makefile | 5 +- tools/munin-plugins-busybox/main.c | 5 ++ tools/munin-plugins-busybox/threads.c | 70 +++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tools/munin-plugins-busybox/threads.c diff --git a/tools/munin-plugins-busybox/Makefile b/tools/munin-plugins-busybox/Makefile index fe465f64..74de3046 100644 --- a/tools/munin-plugins-busybox/Makefile +++ b/tools/munin-plugins-busybox/Makefile @@ -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 $@ diff --git a/tools/munin-plugins-busybox/main.c b/tools/munin-plugins-busybox/main.c index 5e5545bc..535b8a9d 100644 --- a/tools/munin-plugins-busybox/main.c +++ b/tools/munin-plugins-busybox/main.c @@ -14,6 +14,7 @@ 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) { @@ -72,6 +73,10 @@ int main(int argc, char **argv) { if(!strcmp(progname+1, "wap")) return swap(argc, argv); break; + case 't': + if(!strcmp(progname+1, "hreads")) + return threads(argc, argv); + break; case 'u': if(!strcmp(progname+1, "ptime")) return uptime(argc, argv); diff --git a/tools/munin-plugins-busybox/threads.c b/tools/munin-plugins-busybox/threads.c new file mode 100644 index 00000000..c3389e60 --- /dev/null +++ b/tools/munin-plugins-busybox/threads.c @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include +#include +#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; +}