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

124 lines
2.6 KiB
C
Raw Normal View History

2013-02-08 17:36:32 +01:00
#include <libgen.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>
2013-02-09 11:30:00 +01:00
#include <stdlib.h>
2013-02-10 00:49:31 +01:00
#include <sys/types.h>
#include <dirent.h>
2013-02-08 17:36:32 +01:00
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:";
2013-02-08 21:00:58 +01:00
char line[LINE_MAX];
2013-02-08 17:36:32 +01:00
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);
}
2013-02-08 21:00:58 +01:00
printf("# munin node at %s\n", host);
while (fgets(line, LINE_MAX, stdin) != NULL) {
char* cmd;
char* arg;
line[LINE_MAX-1] = '\0';
2013-02-08 17:36:32 +01:00
2013-02-08 21:00:58 +01:00
cmd = strtok(line, " \t\n");
2013-02-09 11:30:00 +01:00
arg = strtok(NULL, " \t\n");
2013-02-08 21:00:58 +01:00
2013-02-08 21:24:44 +01:00
if (!cmd || strlen(cmd) == 0) {
2013-02-09 11:29:04 +01:00
printf("# empty cmd\n");
2013-02-08 21:12:30 +01:00
} else if (strcmp(cmd, "version") == 0) {
printf("munin c node version: %s\n", VERSION);
2013-02-08 21:00:58 +01:00
} else if (strcmp(cmd, "nodes") == 0) {
2013-02-08 21:12:30 +01:00
printf("%s\n", host);
printf(".\n");
2013-02-08 21:00:58 +01:00
} else if (strcmp(cmd, "quit") == 0) {
2013-02-08 21:12:30 +01:00
return(0);
2013-02-08 21:00:58 +01:00
} else if (strcmp(cmd, "list") == 0) {
2013-02-10 00:49:31 +01:00
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);
2013-02-09 11:30:00 +01:00
} 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");
2013-02-08 21:00:58 +01:00
} else if (strcmp(cmd, "cap") == 0) {
2013-02-08 21:12:30 +01:00
printf("cap ");
if (strlen(spoolfetch_dir)) {
printf("spool ");
}
printf("\n");
2013-02-08 21:00:58 +01:00
} else if (strcmp(cmd, "spoolfetch") == 0) {
2013-02-09 11:27:05 +01:00
printf("# not implem yet cmd: %s\n", cmd);
2013-02-08 21:12:30 +01:00
} else {
printf("# unknown cmd: %s\n", cmd);
2013-02-08 21:00:58 +01:00
}
}
2013-02-08 17:36:32 +01:00
return 0;
}