]> arthur.barton.de Git - netdata.git/blob - src/plugins_d.c
1bc567752b488cc765206bc95429bd4ea36a846f
[netdata.git] / src / plugins_d.c
1 #include <sys/types.h>
2 #include <dirent.h>
3 #include <pthread.h>
4 #include <string.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <signal.h>
8
9 #include "common.h"
10 #include "config.h"
11 #include "log.h"
12 #include "rrd.h"
13 #include "popen.h"
14 #include "plugins_d.h"
15
16 struct plugind *pluginsd_root = NULL;
17
18 #define MAX_WORDS 20
19
20 static inline int pluginsd_space(char c) {
21         switch(c) {
22         case ' ':
23         case '\t':
24         case '\r':
25         case '\n':
26         case '=':
27                 return 1;
28
29         default:
30                 return 0;
31         }
32 }
33
34 static void pluginsd_split_words(char *str, char **words, int max_words) {
35         char *s = str, quote = 0;
36         int i = 0;
37
38         // skip all white space
39         while(pluginsd_space(*s)) s++;
40
41         // check for quote
42         if(*s == '\'' || *s == '"') {
43                 quote = *s;     // remember the quote
44                 s++;            // skip the quote
45         }
46
47         // store the first word
48         words[i++] = s;
49
50         // while we have something
51         while(*s) {
52                 // if it is escape
53                 if(*s == '\\' && s[1]) {
54                         s += 2;
55                         continue;
56                 }
57
58                 // if it is quote
59                 else if(*s == quote) {
60                         quote = 0;
61                         *s = ' ';
62                         continue;
63                 }
64
65                 // if it is a space
66                 else if(quote == 0 && pluginsd_space(*s)) {
67
68                         // terminate the word
69                         *s++ = '\0';
70
71                         // skip all white space
72                         while(pluginsd_space(*s)) s++;
73
74                         // check for quote
75                         if(*s == '\'' || *s == '"') {
76                                 quote = *s;     // remember the quote
77                                 s++;            // skip the quote
78                         }
79
80                         // if we reached the end, stop
81                         if(!*s) break;
82
83                         // store the next word
84                         if(i < max_words) words[i++] = s;
85                         else break;
86                 }
87
88                 // anything else
89                 else s++;
90         }
91
92         // terminate the words
93         while(i < max_words) words[i++] = NULL;
94 }
95
96
97 void *pluginsd_worker_thread(void *arg)
98 {
99         struct plugind *cd = (struct plugind *)arg;
100         char line[PLUGINSD_LINE_MAX + 1];
101
102 #ifdef DETACH_PLUGINS_FROM_NETDATA
103         unsigned long long usec = 0, susec = 0;
104         struct timeval last = {0, 0} , now = {0, 0};
105 #endif
106
107         char *words[MAX_WORDS] = { NULL };
108         uint32_t SET_HASH = simple_hash("SET");
109         uint32_t BEGIN_HASH = simple_hash("BEGIN");
110         uint32_t END_HASH = simple_hash("END");
111         uint32_t FLUSH_HASH = simple_hash("FLUSH");
112         uint32_t CHART_HASH = simple_hash("CHART");
113         uint32_t DIMENSION_HASH = simple_hash("DIMENSION");
114         uint32_t DISABLE_HASH = simple_hash("DISABLE");
115 #ifdef DETACH_PLUGINS_FROM_NETDATA
116         uint32_t MYPID_HASH = simple_hash("MYPID");
117         uint32_t STOPPING_WAKE_ME_UP_PLEASE_HASH = simple_hash("STOPPING_WAKE_ME_UP_PLEASE");
118 #endif
119
120         while(1) {
121                 FILE *fp = mypopen(cd->cmd, &cd->pid);
122                 if(!fp) {
123                         error("Cannot popen(\"%s\", \"r\").", cd->cmd);
124                         break;
125                 }
126
127                 RRDSET *st = NULL;
128                 unsigned long long count = 0;
129                 char *s;
130                 uint32_t hash;
131
132                 while(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL) {
133                         line[PLUGINSD_LINE_MAX] = '\0';
134
135                         // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
136
137                         pluginsd_split_words(line, words, MAX_WORDS);
138                         s = words[0];
139                         if(!s || !*s) {
140                                 // debug(D_PLUGINSD, "PLUGINSD: empty line");
141                                 continue;
142                         }
143
144                         // debug(D_PLUGINSD, "PLUGINSD: words 0='%s' 1='%s' 2='%s' 3='%s' 4='%s' 5='%s' 6='%s' 7='%s' 8='%s' 9='%s'", words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7], words[8], words[9]);
145
146                         hash = simple_hash(s);
147
148                         if(hash == SET_HASH && !strcmp(s, "SET")) {
149                                 char *dimension = words[1];
150                                 char *value = words[2];
151
152                                 if(!dimension || !*dimension) {
153                                         error("PLUGINSD: '%s' is requesting a SET on chart '%s', like this: 'SET %s = %s'. Disabling it.", cd->fullfilename, st->id, dimension?dimension:"<nothing>", value?value:"<nothing>");
154                                         cd->enabled = 0;
155                                         kill(cd->pid, SIGTERM);
156                                         break;
157                                 }
158
159                                 if(!value || !*value) value = "0";
160
161                                 if(!st) {
162                                         error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value);
163                                         cd->enabled = 0;
164                                         kill(cd->pid, SIGTERM);
165                                         break;
166                                 }
167
168                                 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
169                                 rrddim_set(st, dimension, atoll(value));
170
171                                 count++;
172                         }
173                         else if(hash == BEGIN_HASH && !strcmp(s, "BEGIN")) {
174                                 char *id = words[1];
175                                 char *microseconds_txt = words[2];
176
177                                 if(!id) {
178                                         error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
179                                         cd->enabled = 0;
180                                         kill(cd->pid, SIGTERM);
181                                         break;
182                                 }
183
184                                 st = rrdset_find(id);
185                                 if(!st) {
186                                         error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
187                                         cd->enabled = 0;
188                                         kill(cd->pid, SIGTERM);
189                                         break;
190                                 }
191
192                                 if(st->counter_done) {
193                                         unsigned long long microseconds = 0;
194                                         if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
195                                         if(microseconds) rrdset_next_usec(st, microseconds);
196                                         else rrdset_next_plugins(st);
197                                 }
198                         }
199                         else if(hash == END_HASH && !strcmp(s, "END")) {
200                                 if(!st) {
201                                         error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
202                                         cd->enabled = 0;
203                                         kill(cd->pid, SIGTERM);
204                                         break;
205                                 }
206
207                                 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
208
209                                 rrdset_done(st);
210                                 st = NULL;
211                         }
212                         else if(hash == FLUSH_HASH && !strcmp(s, "FLUSH")) {
213                                 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
214                                 st = NULL;
215                         }
216                         else if(hash == CHART_HASH && !strcmp(s, "CHART")) {
217                                 st = NULL;
218
219                                 char *type = words[1];
220                                 char *id = NULL;
221                                 if(type) {
222                                         id = strchr(type, '.');
223                                         if(id) { *id = '\0'; id++; }
224                                 }
225                                 char *name = words[2];
226                                 char *title = words[3];
227                                 char *units = words[4];
228                                 char *family = words[5];
229                                 char *category = words[6];
230                                 char *chart = words[7];
231                                 char *priority_s = words[8];
232                                 char *update_every_s = words[9];
233
234                                 if(!type || !*type || !id || !*id) {
235                                         error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
236                                         cd->enabled = 0;
237                                         kill(cd->pid, SIGTERM);
238                                         break;
239                                 }
240
241                                 int priority = 1000;
242                                 if(priority_s) priority = atoi(priority_s);
243
244                                 int update_every = cd->update_every;
245                                 if(update_every_s) update_every = atoi(update_every_s);
246                                 if(!update_every) update_every = cd->update_every;
247
248                                 int chart_type = RRDSET_TYPE_LINE;
249                                 if(chart) chart_type = rrdset_type_id(chart);
250
251                                 if(!name || !*name) name = NULL;
252                                 if(!family || !*family) family = id;
253                                 if(!category || !*category) category = type;
254
255                                 st = rrdset_find_bytype(type, id);
256                                 if(!st) {
257                                         debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
258                                                 , type, id
259                                                 , name?name:""
260                                                 , family?family:""
261                                                 , category?category:""
262                                                 , rrdset_type_name(chart_type)
263                                                 , priority
264                                                 , update_every
265                                                 );
266
267                                         st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type);
268                                         cd->update_every = update_every;
269
270                                         if(strcmp(category, "none") == 0) st->isdetail = 1;
271                                 }
272                                 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
273                         }
274                         else if(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION")) {
275                                 char *id = words[1];
276                                 char *name = words[2];
277                                 char *algorithm = words[3];
278                                 char *multiplier_s = words[4];
279                                 char *divisor_s = words[5];
280                                 char *hidden = words[6];
281
282                                 if(!id || !*id) {
283                                         error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
284                                         cd->enabled = 0;
285                                         kill(cd->pid, SIGTERM);
286                                         break;
287                                 }
288
289                                 if(!st) {
290                                         error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
291                                         cd->enabled = 0;
292                                         kill(cd->pid, SIGTERM);
293                                         break;
294                                 }
295
296                                 long multiplier = 1;
297                                 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
298                                 if(!multiplier) multiplier = 1;
299
300                                 long divisor = 1;
301                                 if(divisor_s && *divisor_s) divisor = atol(divisor_s);
302                                 if(!divisor) divisor = 1;
303
304                                 if(!algorithm || !*algorithm) algorithm = "absolute";
305
306                                 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
307                                         , st->id
308                                         , id
309                                         , name?name:""
310                                         , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
311                                         , multiplier
312                                         , divisor
313                                         , hidden?hidden:""
314                                         );
315
316                                 RRDDIM *rd = rrddim_find(st, id);
317                                 if(!rd) {
318                                         rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
319                                         if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
320                                 }
321                                 else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
322                         }
323                         else if(hash == DISABLE_HASH && !strcmp(s, "DISABLE")) {
324                                 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
325                                 cd->enabled = 0;
326                                 kill(cd->pid, SIGTERM);
327                                 break;
328                         }
329 #ifdef DETACH_PLUGINS_FROM_NETDATA
330                         else if(hash == MYPID_HASH && !strcmp(s, "MYPID")) {
331                                 char *pid_s = words[1];
332                                 pid_t pid = atol(pid_s);
333
334                                 if(pid) cd->pid = pid;
335                                 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
336                         }
337                         else if(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE")) {
338                                 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
339
340                                 gettimeofday(&now, NULL);
341                                 if(!usec && !susec) {
342                                         // our first run
343                                         susec = cd->rrd_update_every * 1000000ULL;
344                                 }
345                                 else {
346                                         // second+ run
347                                         usec = usecdiff(&now, &last) - susec;
348                                         error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
349                                         if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec;
350                                         else susec = rrd_update_every * 1000000ULL / 2ULL;
351                                 }
352
353                                 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
354                                 usleep(susec);
355                                 kill(cd->pid, SIGCONT);
356                                 bcopy(&now, &last, sizeof(struct timeval));
357                                 break;
358                         }
359 #endif
360                         else {
361                                 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
362                                 cd->enabled = 0;
363                                 kill(cd->pid, SIGTERM);
364                                 break;
365                         }
366                 }
367
368                 // fgets() failed or loop broke
369                 mypclose(fp, cd->pid);
370                 cd->pid = 0;
371
372                 if(!count && cd->enabled) {
373                         error("PLUGINSD: '%s' does not generate usefull output. Disabling it.", cd->fullfilename);
374                         cd->enabled = 0;
375                         kill(cd->pid, SIGTERM);
376                 }
377
378                 if(cd->enabled) sleep(cd->update_every);
379                 else break;
380         }
381
382         cd->obsolete = 1;
383         return NULL;
384 }
385
386 void *pluginsd_main(void *ptr)
387 {
388         if(ptr) { ; }
389
390         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
391                 error("Cannot set pthread cancel type to DEFERRED.");
392
393         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
394                 error("Cannot set pthread cancel state to ENABLE.");
395
396         char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
397         int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
398         int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
399         DIR *dir = NULL;
400         struct dirent *file = NULL;
401         struct plugind *cd;
402
403         // enable the apps plugin by default
404         config_get_boolean("plugins", "apps", 1);
405
406         if(scan_frequency < 1) scan_frequency = 1;
407
408         while(1) {
409                 dir = opendir(dir_name);
410                 if(!dir) {
411                         error("Cannot open directory '%s'.", dir_name);
412                         return NULL;
413                 }
414
415                 while((file = readdir(dir))) {
416                         debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
417
418                         if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue;
419
420                         int len = strlen(file->d_name);
421                         if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue;
422                         if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0) {
423                                 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
424                                 continue;
425                         }
426
427                         char pluginname[CONFIG_MAX_NAME + 1];
428                         snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
429                         int enabled = config_get_boolean("plugins", pluginname, automatic_run);
430
431                         if(!enabled) {
432                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
433                                 continue;
434                         }
435
436                         // check if it runs already
437                         for(cd = pluginsd_root ; cd ; cd = cd->next) {
438                                 if(strcmp(cd->filename, file->d_name) == 0) break;
439                         }
440                         if(cd && !cd->obsolete) {
441                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
442                                 continue;
443                         }
444
445                         // it is not running
446                         // allocate a new one, or use the obsolete one
447                         if(!cd) {
448                                 cd = calloc(sizeof(struct plugind), 1);
449                                 if(!cd) fatal("Cannot allocate memory for plugin.");
450
451                                 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
452                                 
453                                 strncpy(cd->filename, file->d_name, FILENAME_MAX);
454                                 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
455
456                                 cd->enabled = enabled;
457                                 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
458                                 cd->started_t = time(NULL);
459
460                                 char *def = "";
461                                 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
462
463                                 // link it
464                                 if(pluginsd_root) cd->next = pluginsd_root;
465                                 pluginsd_root = cd;
466                         }
467                         cd->obsolete = 0;
468
469                         if(!cd->enabled) continue;
470
471                         // spawn a new thread for it
472                         if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) {
473                                 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
474                                 cd->obsolete = 1;
475                         }
476                         else if(pthread_detach(cd->thread) != 0)
477                                 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
478                 }
479
480                 closedir(dir);
481                 sleep(scan_frequency);
482         }
483
484         return NULL;
485 }
486
487