Projet

Général

Profil

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

root / tools / munin-node-c / main.c @ 99a00144

Historique | Voir | Annoter | Télécharger (3,1 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 <sys/wait.h>
9
#include <dirent.h>
10

    
11

    
12
char VERSION[] = "1.0.0";
13

    
14
int verbose = 0;
15

    
16
char* host = "";
17
char* plugin_dir = "plugins";
18
char* spoolfetch_dir = "";
19

    
20
int main(int argc, char *argv[]) {
21

    
22
        int optch;
23
        extern int opterr;
24
        int optarg_len;
25

    
26
        char format[] = "vd:h:s:";
27

    
28
        char line[LINE_MAX];
29

    
30
        opterr = 1;
31

    
32
        while ((optch = getopt(argc, argv, format)) != -1)
33
        switch (optch) {
34
                case 'v':
35
                        verbose ++;
36
                        break;
37
                case 'd':
38
                        optarg_len = strlen(optarg);
39
                        plugin_dir = (char *) malloc(optarg_len + 1);
40
                        strcpy(plugin_dir, optarg);
41
                        break;
42
                case 'h':
43
                        optarg_len = strlen(optarg);
44
                        host = (char *) malloc(optarg_len + 1);
45
                        strcpy(host, optarg);
46
                        break;
47
                case 's':
48
                        optarg_len = strlen(optarg);
49
                        spoolfetch_dir = (char *) malloc(optarg_len + 1);
50
                        strcpy(spoolfetch_dir, optarg);
51
                        break;
52
        }
53

    
54
        /* get default hostname if not precised */
55
        if (! strlen(host)) {
56
                host = (char *) malloc(HOST_NAME_MAX + 1);
57
                gethostname(host, HOST_NAME_MAX);
58
        }
59

    
60
        printf("# munin node at %s\n", host);
61
        while (fgets(line, LINE_MAX, stdin) != NULL) {
62
                char* cmd;
63
                char* arg;
64

    
65
                line[LINE_MAX-1] = '\0';
66

    
67
                cmd = strtok(line, " \t\n");
68
                if(cmd == NULL)
69
                        arg = NULL;
70
                else
71
                        arg = strtok(NULL, " \t\n");
72

    
73
                if (!cmd || strlen(cmd) == 0) {
74
                        printf("# empty cmd\n");
75
                } else if (strcmp(cmd, "version") == 0) {
76
                        printf("munin c node version: %s\n", VERSION);
77
                } else if (strcmp(cmd, "nodes") == 0) {
78
                        printf("%s\n", host);
79
                        printf(".\n");
80
                } else if (strcmp(cmd, "quit") == 0) {
81
                        return(0);
82
                } else if (strcmp(cmd, "list") == 0) {
83
                        DIR* dirp = opendir(plugin_dir);
84
                        struct dirent* dp;
85
                        while ((dp = readdir(dirp)) != NULL) {
86
                                char cmdline[LINE_MAX];
87
                                char* plugin_filename = dp->d_name;;
88

    
89
                                if (plugin_filename[0] == '.') {
90
                                        /* No dotted plugin */
91
                                        continue;
92
                                }
93

    
94
                                snprintf(cmdline, LINE_MAX, "%s/%s", plugin_dir, plugin_filename);
95
                                if (access(cmdline, X_OK) == 0) {
96
                                        printf("%s ", plugin_filename);
97
                                }
98
                        }
99
                        printf("\n");
100
                        closedir(dirp);
101
                } else if (
102
                                strcmp(cmd, "config") == 0 ||
103
                                strcmp(cmd, "fetch") == 0
104
                        ) {
105
                        char cmdline[LINE_MAX];
106
                        pid_t pid;
107
                        if(arg == NULL) {
108
                                printf("# no plugin given\n");
109
                                continue;
110
                        }
111
                        if(arg[0] == '.' || strchr(arg, '/')) {
112
                                printf("# invalid plugin character");
113
                                continue;
114
                        }
115
                        snprintf(cmdline, LINE_MAX, "%s/%s", plugin_dir, arg);
116
                        if (access(cmdline, X_OK) == -1) {
117
                                printf("# unknown plugin: %s\n", arg);
118
                                continue;
119
                        }
120
                        if(0 == (pid = vfork())) {
121
                                execl(cmdline, arg, cmd, NULL);
122
                                /* according to vfork(2) we must use _exit */
123
                                _exit(1);
124
                        } else if(pid < 0) {
125
                                printf("# fork failed\n");
126
                                continue;
127
                        } else {
128
                                waitpid(pid, NULL, 0);
129
                        }
130
                        printf(".\n");
131
                } else if (strcmp(cmd, "cap") == 0) {
132
                        printf("cap ");
133
                        if (strlen(spoolfetch_dir)) {
134
                                printf("spool ");
135
                        }
136
                        printf("\n");
137
                } else if (strcmp(cmd, "spoolfetch") == 0) {
138
                        printf("# not implem yet cmd: %s\n", cmd);
139
                } else {
140
                        printf("# unknown cmd: %s\n", cmd);
141
                }
142
        }
143

    
144
        return 0;
145
}