From 69b46e42f6f2757cc7442c5dcd4320494fd2e957 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 11:49:42 +0100 Subject: [PATCH 01/14] add gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..a01ee289 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.*.swp From e041ac213559661b430b054aad16aa1b3715cfce Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Fri, 8 Feb 2013 17:09:11 +0100 Subject: [PATCH 02/14] mnc: starting with adding a gitignore --- tools/munin-node-c/.gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 tools/munin-node-c/.gitignore diff --git a/tools/munin-node-c/.gitignore b/tools/munin-node-c/.gitignore new file mode 100644 index 00000000..16edcbc0 --- /dev/null +++ b/tools/munin-node-c/.gitignore @@ -0,0 +1,3 @@ +# output files +/*.o +/munin-node-c From 52369dbed12986398a77c7ed74d965a76647893f Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Fri, 8 Feb 2013 17:36:32 +0100 Subject: [PATCH 03/14] mnc: initial add --- tools/munin-node-c/Makefile | 14 +++++++++ tools/munin-node-c/README | 28 ++++++++++++++++++ tools/munin-node-c/main.c | 58 +++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 tools/munin-node-c/Makefile create mode 100644 tools/munin-node-c/README create mode 100644 tools/munin-node-c/main.c diff --git a/tools/munin-node-c/Makefile b/tools/munin-node-c/Makefile new file mode 100644 index 00000000..bec42767 --- /dev/null +++ b/tools/munin-node-c/Makefile @@ -0,0 +1,14 @@ +CC=gcc +CFLAGS=-W -Wall -pedantic -Wextra -g -O2 +OBJS=main.o +LINKS= + +%.o: %.c + ${CC} ${CFLAGS} -c $< -o $@ +all: munin-node-c + +munin-node-c: ${OBJS} + ${CC} ${CFLAGS} $^ -o $@ +clean: + rm -f munin-node-c ${OBJS} ${LINKS} +.PHONY: all clean diff --git a/tools/munin-node-c/README b/tools/munin-node-c/README new file mode 100644 index 00000000..67c1544f --- /dev/null +++ b/tools/munin-node-c/README @@ -0,0 +1,28 @@ +This is a rewrite of munin node in C. + +Pro: +---- + +The purpose is multiple: + + * reducing resource usage for embedded plateforms, specially when paired + with the C rewrite of the core plugins. + + * no need for Perl + + * Everything runs from inetd. + +Cons: +----- + + * You lose flexibility + + It is compiled code, so you have to create binaries. Even one for each + architecture. + + * Not all the features are implemented + + - root uid is not supported. All plugins are run with a single user, usually nobody. + - no socket is opened. Everything runs from inetd. + +GPLv2 - (C) 2013 Steve SCHNEPP diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c new file mode 100644 index 00000000..e173ee59 --- /dev/null +++ b/tools/munin-node-c/main.c @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include + + +char VERSION[] = "1.0.0"; + +int verbose; + +char* host = ""; +char* plugin_dir = "plugins"; +char* spoolfetch_dir = ""; + +int main(int argc, char *argv[]) { + + int optch; + extern int opterr; + int optarg_len; + + char format[] = "vd:h:s:"; + + opterr = 1; + + while ((optch = getopt(argc, argv, format)) != -1) + switch (optch) { + case 'v': + verbose ++; + break; + case 'd': + optarg_len = strlen(optarg); + plugin_dir = (char *) malloc(optarg_len + 1); + strcpy(plugin_dir, optarg); + break; + case 'h': + optarg_len = strlen(optarg); + host = (char *) malloc(optarg_len + 1); + strcpy(host, optarg); + break; + case 's': + optarg_len = strlen(optarg); + spoolfetch_dir = (char *) malloc(optarg_len + 1); + strcpy(spoolfetch_dir, optarg); + break; + } + + /* get default hostname if not precised */ + if (! strlen(host)) { + host = (char *) malloc(HOST_NAME_MAX + 1); + gethostname(host, HOST_NAME_MAX); + } + + printf("verbose: %d, host: %s, plugin_dir: %s, spoolfetch_dir: %s\n", verbose, host, plugin_dir, spoolfetch_dir); + + + return 0; +} From acea10b2030e6738eca947a83fc1f1bb4d183042 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Fri, 8 Feb 2013 21:00:58 +0100 Subject: [PATCH 04/14] mnc: initial arg handling --- tools/munin-node-c/main.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index e173ee59..ad54a8f4 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -21,6 +21,8 @@ int main(int argc, char *argv[]) { char format[] = "vd:h:s:"; + char line[LINE_MAX]; + opterr = 1; while ((optch = getopt(argc, argv, format)) != -1) @@ -51,8 +53,30 @@ int main(int argc, char *argv[]) { gethostname(host, HOST_NAME_MAX); } - printf("verbose: %d, host: %s, plugin_dir: %s, spoolfetch_dir: %s\n", verbose, host, plugin_dir, spoolfetch_dir); + fprintf(stderr, "verbose: %d, host: %s, plugin_dir: %s, spoolfetch_dir: %s\n", verbose, host, plugin_dir, spoolfetch_dir); + printf("# munin node at %s\n", host); + while (fgets(line, LINE_MAX, stdin) != NULL) { + char* cmd; + char* arg; + + line[LINE_MAX-1] = '\0'; + + cmd = strtok(line, " \t\n"); + arg = strtok(line, " \t\n"); + + if (strlen(cmd) == 0) continue; + + if (strcmp(cmd, "version") == 0) { + } else if (strcmp(cmd, "nodes") == 0) { + } else if (strcmp(cmd, "quit") == 0) { + } else if (strcmp(cmd, "list") == 0) { + } else if (strcmp(cmd, "config") == 0) { + } else if (strcmp(cmd, "fetch") == 0) { + } else if (strcmp(cmd, "cap") == 0) { + } else if (strcmp(cmd, "spoolfetch") == 0) { + } + } return 0; } From 1d56b5845d4147e0fd432e59e8997fd7ac1b3ff0 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 12:58:16 +0100 Subject: [PATCH 05/14] mnc: suppress debug output --- tools/munin-node-c/main.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index ad54a8f4..22706444 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -53,8 +53,6 @@ int main(int argc, char *argv[]) { gethostname(host, HOST_NAME_MAX); } - fprintf(stderr, "verbose: %d, host: %s, plugin_dir: %s, spoolfetch_dir: %s\n", verbose, host, plugin_dir, spoolfetch_dir); - printf("# munin node at %s\n", host); while (fgets(line, LINE_MAX, stdin) != NULL) { char* cmd; From 773d5ed4cf06e389ae4cab6acae0f613345808da Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Fri, 8 Feb 2013 21:12:30 +0100 Subject: [PATCH 06/14] mnc: implem first commands --- tools/munin-node-c/main.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 22706444..201a7f48 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -63,16 +63,26 @@ int main(int argc, char *argv[]) { cmd = strtok(line, " \t\n"); arg = strtok(line, " \t\n"); - if (strlen(cmd) == 0) continue; - - if (strcmp(cmd, "version") == 0) { + if (strlen(cmd) == 0) { + } else if (strcmp(cmd, "version") == 0) { + printf("munin c node version: %s\n", VERSION); } else if (strcmp(cmd, "nodes") == 0) { + printf("%s\n", host); + printf(".\n"); } else if (strcmp(cmd, "quit") == 0) { + return(0); } else if (strcmp(cmd, "list") == 0) { } else if (strcmp(cmd, "config") == 0) { } else if (strcmp(cmd, "fetch") == 0) { } else if (strcmp(cmd, "cap") == 0) { + printf("cap "); + if (strlen(spoolfetch_dir)) { + printf("spool "); + } + printf("\n"); } else if (strcmp(cmd, "spoolfetch") == 0) { + } else { + printf("# unknown cmd: %s\n", cmd); } } From 2cdb57aec2f8dae09a36e0b5113a668a67a1b671 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Fri, 8 Feb 2013 21:24:44 +0100 Subject: [PATCH 07/14] mnc: handle empty input lines --- tools/munin-node-c/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 201a7f48..180d0678 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) { cmd = strtok(line, " \t\n"); arg = strtok(line, " \t\n"); - if (strlen(cmd) == 0) { + if (!cmd || strlen(cmd) == 0) { } else if (strcmp(cmd, "version") == 0) { printf("munin c node version: %s\n", VERSION); } else if (strcmp(cmd, "nodes") == 0) { From f3ae51d938753768a289f0e9146f9ce2a5e2511b Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 11:27:05 +0100 Subject: [PATCH 08/14] mnc: add placeholder --- tools/munin-node-c/main.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 180d0678..8f2c0575 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -72,6 +72,7 @@ int main(int argc, char *argv[]) { } else if (strcmp(cmd, "quit") == 0) { return(0); } else if (strcmp(cmd, "list") == 0) { + printf("# not implem yet cmd: %s\n", cmd); } else if (strcmp(cmd, "config") == 0) { } else if (strcmp(cmd, "fetch") == 0) { } else if (strcmp(cmd, "cap") == 0) { @@ -81,6 +82,7 @@ int main(int argc, char *argv[]) { } printf("\n"); } else if (strcmp(cmd, "spoolfetch") == 0) { + printf("# not implem yet cmd: %s\n", cmd); } else { printf("# unknown cmd: %s\n", cmd); } From b95b1672c6d8ee6c3d389390ef355c46fb2ff3a0 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 11:29:04 +0100 Subject: [PATCH 09/14] mnc: add empty handler --- tools/munin-node-c/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 8f2c0575..e814bb49 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -64,6 +64,7 @@ int main(int argc, char *argv[]) { arg = strtok(line, " \t\n"); if (!cmd || strlen(cmd) == 0) { + printf("# empty cmd\n"); } else if (strcmp(cmd, "version") == 0) { printf("munin c node version: %s\n", VERSION); } else if (strcmp(cmd, "nodes") == 0) { From bf25714e3ae3956666b34a3e0d18434165f05b3e Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 11:30:00 +0100 Subject: [PATCH 10/14] mnc: add config/fetch handler --- tools/munin-node-c/main.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index e814bb49..4dbae5d2 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -3,6 +3,7 @@ #include #include #include +#include char VERSION[] = "1.0.0"; @@ -61,7 +62,7 @@ int main(int argc, char *argv[]) { line[LINE_MAX-1] = '\0'; cmd = strtok(line, " \t\n"); - arg = strtok(line, " \t\n"); + arg = strtok(NULL, " \t\n"); if (!cmd || strlen(cmd) == 0) { printf("# empty cmd\n"); @@ -74,8 +75,18 @@ int main(int argc, char *argv[]) { return(0); } else if (strcmp(cmd, "list") == 0) { printf("# not implem yet cmd: %s\n", cmd); - } else if (strcmp(cmd, "config") == 0) { - } else if (strcmp(cmd, "fetch") == 0) { + } else if ( + strcmp(cmd, "config") == 0 || + strcmp(cmd, "fetch") == 0 + ) { + char cmdline[LINE_MAX]; + if (! access(arg, X_OK)) { + printf("# unknown plugin: %s\n", arg); + continue; + } + sprintf(cmdline, "exec %s/%s %s", plugin_dir, arg, cmd); + system(cmdline); + printf(".\n"); } else if (strcmp(cmd, "cap") == 0) { printf("cap "); if (strlen(spoolfetch_dir)) { From c32620c6451ce976c48139d6cc18c341acbc6dde Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 13:28:59 +0100 Subject: [PATCH 11/14] mnc: interact with munin-plugins-busybox --- tools/munin-node-c/Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tools/munin-node-c/Makefile b/tools/munin-node-c/Makefile index bec42767..58b4b59f 100644 --- a/tools/munin-node-c/Makefile +++ b/tools/munin-node-c/Makefile @@ -11,4 +11,11 @@ munin-node-c: ${OBJS} ${CC} ${CFLAGS} $^ -o $@ clean: rm -f munin-node-c ${OBJS} ${LINKS} -.PHONY: all clean + rm -Rf plugins + +plugins: + mkdir -p plugins + cd ../munin-plugins-busybox && make + cd plugins && for i in $$(find ../../munin-plugins-busybox -type l); do ln -s $$i; done + +.PHONY: all clean plugins From aae73ff3b184cdf29ed6887b10e831f74683baab Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sat, 9 Feb 2013 14:11:56 +0100 Subject: [PATCH 12/14] mnc: conditional make of munin-plugins-busybox --- tools/munin-node-c/.gitignore | 1 + tools/munin-node-c/Makefile | 5 ++++- tools/munin-plugins-busybox/.gitignore | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) create mode 100644 tools/munin-plugins-busybox/.gitignore diff --git a/tools/munin-node-c/.gitignore b/tools/munin-node-c/.gitignore index 16edcbc0..91f33c2b 100644 --- a/tools/munin-node-c/.gitignore +++ b/tools/munin-node-c/.gitignore @@ -1,3 +1,4 @@ # output files /*.o /munin-node-c +/plugins/* diff --git a/tools/munin-node-c/Makefile b/tools/munin-node-c/Makefile index 58b4b59f..d4c79fdc 100644 --- a/tools/munin-node-c/Makefile +++ b/tools/munin-node-c/Makefile @@ -13,9 +13,12 @@ clean: rm -f munin-node-c ${OBJS} ${LINKS} rm -Rf plugins -plugins: +plugins: plugins/.munin-plugins-busybox.installed + +plugins/.munin-plugins-busybox.installed: mkdir -p plugins cd ../munin-plugins-busybox && make cd plugins && for i in $$(find ../../munin-plugins-busybox -type l); do ln -s $$i; done + touch plugins/.munin-plugins-busybox.installed .PHONY: all clean plugins diff --git a/tools/munin-plugins-busybox/.gitignore b/tools/munin-plugins-busybox/.gitignore new file mode 100644 index 00000000..5761abcf --- /dev/null +++ b/tools/munin-plugins-busybox/.gitignore @@ -0,0 +1 @@ +*.o From 90c41118998a8d59dbcd984413e9f0687203b6aa Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sun, 10 Feb 2013 00:49:31 +0100 Subject: [PATCH 13/14] mnc: implement the "list" cmd --- tools/munin-node-c/main.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 4dbae5d2..990374cb 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -4,6 +4,8 @@ #include #include #include +#include +#include char VERSION[] = "1.0.0"; @@ -74,7 +76,24 @@ int main(int argc, char *argv[]) { } else if (strcmp(cmd, "quit") == 0) { return(0); } else if (strcmp(cmd, "list") == 0) { - printf("# not implem yet cmd: %s\n", cmd); + DIR* dirp = opendir(plugin_dir); + struct dirent* dp; + while ((dp = readdir(dirp)) != NULL) { + char cmdline[LINE_MAX]; + char* plugin_filename = dp->d_name;; + + if (plugin_filename[0] == '.') { + /* No dotted plugin */ + continue; + } + + sprintf(cmdline, "%s/%s", plugin_dir, plugin_filename); + if (access(cmdline, X_OK) == 0) { + printf("%s ", plugin_filename); + } + } + printf("\n"); + closedir(dirp); } else if ( strcmp(cmd, "config") == 0 || strcmp(cmd, "fetch") == 0 From c2ea44730c16bcbe214678f426aa1767950ff7f7 Mon Sep 17 00:00:00 2001 From: Steve Schnepp Date: Sun, 10 Feb 2013 00:53:09 +0100 Subject: [PATCH 14/14] mnc: fix plugin existance control --- tools/munin-node-c/main.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tools/munin-node-c/main.c b/tools/munin-node-c/main.c index 990374cb..483ca810 100644 --- a/tools/munin-node-c/main.c +++ b/tools/munin-node-c/main.c @@ -99,7 +99,8 @@ int main(int argc, char *argv[]) { strcmp(cmd, "fetch") == 0 ) { char cmdline[LINE_MAX]; - if (! access(arg, X_OK)) { + sprintf(cmdline, "%s/%s", plugin_dir, arg); + if (access(cmdline, X_OK) == -1) { printf("# unknown plugin: %s\n", arg); continue; }