Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / tools / munin-node-c / main.c @ a07c3d55

Historique | Voir | Annoter | Télécharger (2,67 ko)

1
#include <libgen.h>
2
#include <string.h>
3
#include <stdio.h>
4
#include <unistd.h>
5
#include <limits.h>
6
#include <stdlib.h>
7
#include <sys/types.h>
8
#include <dirent.h>
9

    
10

    
11
char VERSION[] = "1.0.0";
12

    
13
int verbose = 0;
14

    
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
        char line[LINE_MAX];
28

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

    
66
                cmd = strtok(line, " \t\n");
67
                arg = strtok(NULL, " \t\n");
68

    
69
                if (!cmd || strlen(cmd) == 0) {
70
                        printf("# empty cmd\n");
71
                } else if (strcmp(cmd, "version") == 0) {
72
                        printf("munin c node version: %s\n", VERSION);
73
                } else if (strcmp(cmd, "nodes") == 0) {
74
                        printf("%s\n", host);
75
                        printf(".\n");
76
                } else if (strcmp(cmd, "quit") == 0) {
77
                        return(0);
78
                } else if (strcmp(cmd, "list") == 0) {
79
                        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
                } else if (
98
                                strcmp(cmd, "config") == 0 ||
99
                                strcmp(cmd, "fetch") == 0
100
                        ) {
101
                        char cmdline[LINE_MAX];
102
                        sprintf(cmdline, "%s/%s", plugin_dir, arg);
103
                        if (access(cmdline, X_OK) == -1) {
104
                                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
                } else if (strcmp(cmd, "cap") == 0) {
111
                        printf("cap ");
112
                        if (strlen(spoolfetch_dir)) {
113
                                printf("spool ");
114
                        }
115
                        printf("\n");
116
                } else if (strcmp(cmd, "spoolfetch") == 0) {
117
                        printf("# not implem yet cmd: %s\n", cmd);
118
                } else {
119
                        printf("# unknown cmd: %s\n", cmd);
120
                }
121
        }
122

    
123
        return 0;
124
}