root / tools / munin-node-c / main.c @ a07c3d55
Historique | Voir | Annoter | Télécharger (2,67 ko)
| 1 | 52369dbe | Steve Schnepp | #include <libgen.h> |
|---|---|---|---|
| 2 | #include <string.h> |
||
| 3 | #include <stdio.h> |
||
| 4 | #include <unistd.h> |
||
| 5 | #include <limits.h> |
||
| 6 | bf25714e | Steve Schnepp | #include <stdlib.h> |
| 7 | 90c41118 | Steve Schnepp | #include <sys/types.h> |
| 8 | #include <dirent.h> |
||
| 9 | 52369dbe | Steve Schnepp | |
| 10 | |||
| 11 | char VERSION[] = "1.0.0"; |
||
| 12 | |||
| 13 | a07c3d55 | Steve Schnepp | int verbose = 0; |
| 14 | 52369dbe | Steve Schnepp | |
| 15 | char* host = ""; |
||
| 16 | char* plugin_dir = "plugins"; |
||
| 17 | char* spoolfetch_dir = ""; |
||
| 18 | |||
| 19 | int main(int argc, char *argv[]) { |
||
| 20 | |||
| 21 | int optch;
|
||
| 22 | extern int opterr; |
||
| 23 | int optarg_len;
|
||
| 24 | |||
| 25 | char format[] = "vd:h:s:"; |
||
| 26 | |||
| 27 | acea10b2 | Steve Schnepp | char line[LINE_MAX];
|
| 28 | |||
| 29 | 52369dbe | Steve Schnepp | opterr = 1;
|
| 30 | |||
| 31 | while ((optch = getopt(argc, argv, format)) != -1) |
||
| 32 | switch (optch) {
|
||
| 33 | case 'v': |
||
| 34 | verbose ++; |
||
| 35 | break;
|
||
| 36 | case 'd': |
||
| 37 | optarg_len = strlen(optarg); |
||
| 38 | plugin_dir = (char *) malloc(optarg_len + 1); |
||
| 39 | strcpy(plugin_dir, optarg); |
||
| 40 | break;
|
||
| 41 | case 'h': |
||
| 42 | optarg_len = strlen(optarg); |
||
| 43 | host = (char *) malloc(optarg_len + 1); |
||
| 44 | strcpy(host, optarg); |
||
| 45 | break;
|
||
| 46 | case 's': |
||
| 47 | optarg_len = strlen(optarg); |
||
| 48 | spoolfetch_dir = (char *) malloc(optarg_len + 1); |
||
| 49 | strcpy(spoolfetch_dir, optarg); |
||
| 50 | break;
|
||
| 51 | } |
||
| 52 | |||
| 53 | /* get default hostname if not precised */
|
||
| 54 | if (! strlen(host)) {
|
||
| 55 | host = (char *) malloc(HOST_NAME_MAX + 1); |
||
| 56 | gethostname(host, HOST_NAME_MAX); |
||
| 57 | } |
||
| 58 | |||
| 59 | acea10b2 | Steve Schnepp | printf("# munin node at %s\n", host);
|
| 60 | while (fgets(line, LINE_MAX, stdin) != NULL) { |
||
| 61 | char* cmd;
|
||
| 62 | char* arg;
|
||
| 63 | |||
| 64 | line[LINE_MAX-1] = '\0'; |
||
| 65 | 52369dbe | Steve Schnepp | |
| 66 | acea10b2 | Steve Schnepp | cmd = strtok(line, " \t\n");
|
| 67 | bf25714e | Steve Schnepp | arg = strtok(NULL, " \t\n"); |
| 68 | acea10b2 | Steve Schnepp | |
| 69 | 2cdb57ae | Steve Schnepp | if (!cmd || strlen(cmd) == 0) { |
| 70 | b95b1672 | Steve Schnepp | printf("# empty cmd\n");
|
| 71 | 773d5ed4 | Steve Schnepp | } else if (strcmp(cmd, "version") == 0) { |
| 72 | printf("munin c node version: %s\n", VERSION);
|
||
| 73 | acea10b2 | Steve Schnepp | } else if (strcmp(cmd, "nodes") == 0) { |
| 74 | 773d5ed4 | Steve Schnepp | printf("%s\n", host);
|
| 75 | printf(".\n");
|
||
| 76 | acea10b2 | Steve Schnepp | } else if (strcmp(cmd, "quit") == 0) { |
| 77 | 773d5ed4 | Steve Schnepp | return(0); |
| 78 | acea10b2 | Steve Schnepp | } else if (strcmp(cmd, "list") == 0) { |
| 79 | 90c41118 | Steve Schnepp | DIR* dirp = opendir(plugin_dir); |
| 80 | struct dirent* dp;
|
||
| 81 | while ((dp = readdir(dirp)) != NULL) { |
||
| 82 | char cmdline[LINE_MAX];
|
||
| 83 | char* plugin_filename = dp->d_name;;
|
||
| 84 | |||
| 85 | if (plugin_filename[0] == '.') { |
||
| 86 | /* No dotted plugin */
|
||
| 87 | continue;
|
||
| 88 | } |
||
| 89 | |||
| 90 | sprintf(cmdline, "%s/%s", plugin_dir, plugin_filename);
|
||
| 91 | if (access(cmdline, X_OK) == 0) { |
||
| 92 | printf("%s ", plugin_filename);
|
||
| 93 | } |
||
| 94 | } |
||
| 95 | printf("\n");
|
||
| 96 | closedir(dirp); |
||
| 97 | bf25714e | Steve Schnepp | } else if ( |
| 98 | strcmp(cmd, "config") == 0 || |
||
| 99 | strcmp(cmd, "fetch") == 0 |
||
| 100 | ) {
|
||
| 101 | char cmdline[LINE_MAX];
|
||
| 102 | c2ea4473 | Steve Schnepp | sprintf(cmdline, "%s/%s", plugin_dir, arg);
|
| 103 | if (access(cmdline, X_OK) == -1) { |
||
| 104 | bf25714e | Steve Schnepp | printf("# unknown plugin: %s\n", arg);
|
| 105 | continue;
|
||
| 106 | } |
||
| 107 | sprintf(cmdline, "exec %s/%s %s", plugin_dir, arg, cmd);
|
||
| 108 | system(cmdline); |
||
| 109 | printf(".\n");
|
||
| 110 | acea10b2 | Steve Schnepp | } else if (strcmp(cmd, "cap") == 0) { |
| 111 | 773d5ed4 | Steve Schnepp | printf("cap ");
|
| 112 | if (strlen(spoolfetch_dir)) {
|
||
| 113 | printf("spool ");
|
||
| 114 | } |
||
| 115 | printf("\n");
|
||
| 116 | acea10b2 | Steve Schnepp | } else if (strcmp(cmd, "spoolfetch") == 0) { |
| 117 | f3ae51d9 | Steve Schnepp | printf("# not implem yet cmd: %s\n", cmd);
|
| 118 | 773d5ed4 | Steve Schnepp | } else {
|
| 119 | printf("# unknown cmd: %s\n", cmd);
|
||
| 120 | acea10b2 | Steve Schnepp | } |
| 121 | } |
||
| 122 | 52369dbe | Steve Schnepp | |
| 123 | return 0; |
||
| 124 | } |
