]> arthur.barton.de Git - netdata.git/blob - src/plugins_d.c
added /stream/ URL that redirects socket to plugins.d for processing - api key valida...
[netdata.git] / src / plugins_d.c
1 #include "common.h"
2
3 struct plugind *pluginsd_root = NULL;
4
5 #define MAX_WORDS 20
6
7 static inline int pluginsd_space(char c) {
8     switch(c) {
9     case ' ':
10     case '\t':
11     case '\r':
12     case '\n':
13     case '=':
14         return 1;
15
16     default:
17         return 0;
18     }
19 }
20
21 static int pluginsd_split_words(char *str, char **words, int max_words) {
22     char *s = str, quote = 0;
23     int i = 0, j;
24
25     // skip all white space
26     while(unlikely(pluginsd_space(*s))) s++;
27
28     // check for quote
29     if(unlikely(*s == '\'' || *s == '"')) {
30         quote = *s; // remember the quote
31         s++;        // skip the quote
32     }
33
34     // store the first word
35     words[i++] = s;
36
37     // while we have something
38     while(likely(*s)) {
39         // if it is escape
40         if(unlikely(*s == '\\' && s[1])) {
41             s += 2;
42             continue;
43         }
44
45         // if it is quote
46         else if(unlikely(*s == quote)) {
47             quote = 0;
48             *s = ' ';
49             continue;
50         }
51
52         // if it is a space
53         else if(unlikely(quote == 0 && pluginsd_space(*s))) {
54
55             // terminate the word
56             *s++ = '\0';
57
58             // skip all white space
59             while(likely(pluginsd_space(*s))) s++;
60
61             // check for quote
62             if(unlikely(*s == '\'' || *s == '"')) {
63                 quote = *s; // remember the quote
64                 s++;        // skip the quote
65             }
66
67             // if we reached the end, stop
68             if(unlikely(!*s)) break;
69
70             // store the next word
71             if(likely(i < max_words)) words[i++] = s;
72             else break;
73         }
74
75         // anything else
76         else s++;
77     }
78
79     // terminate the words
80     j = i;
81     while(likely(j < max_words)) words[j++] = NULL;
82
83     return i;
84 }
85
86 inline size_t pluginsd_process(struct plugind *cd, FILE *fp, int trust_durations) {
87     int enabled = cd->enabled;
88
89     if(!fp || !enabled) {
90         cd->enabled = 0;
91         return 0;
92     }
93
94     size_t count = 0;
95     RRDHOST *host = localhost;
96
97     char line[PLUGINSD_LINE_MAX + 1];
98
99     char *words[MAX_WORDS] = { NULL };
100     uint32_t HOST_HASH = simple_hash("HOST");
101     uint32_t BEGIN_HASH = simple_hash("BEGIN");
102     uint32_t END_HASH = simple_hash("END");
103     uint32_t FLUSH_HASH = simple_hash("FLUSH");
104     uint32_t CHART_HASH = simple_hash("CHART");
105     uint32_t DIMENSION_HASH = simple_hash("DIMENSION");
106     uint32_t DISABLE_HASH = simple_hash("DISABLE");
107
108     RRDSET *st = NULL;
109     uint32_t hash;
110
111     while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
112         if(unlikely(netdata_exit)) break;
113
114         line[PLUGINSD_LINE_MAX] = '\0';
115
116         // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
117
118         int w = pluginsd_split_words(line, words, MAX_WORDS);
119         char *s = words[0];
120         if(unlikely(!s || !*s || !w)) {
121             // debug(D_PLUGINSD, "PLUGINSD: empty line");
122             continue;
123         }
124
125         // 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]);
126
127         if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
128             char *dimension = words[1];
129             char *value = words[2];
130
131             if(unlikely(!dimension || !*dimension)) {
132                 error("PLUGINSD: '%s' is requesting a SET on chart '%s', without a dimension. Disabling it.", cd->fullfilename, st->id);
133                 enabled = 0;
134                 break;
135             }
136
137             if(unlikely(!value || !*value)) value = NULL;
138
139             if(unlikely(!st)) {
140                 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value?value:"<nothing>");
141                 enabled = 0;
142                 break;
143             }
144
145             if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value?value:"<nothing>");
146
147             if(value) rrddim_set(st, dimension, strtoll(value, NULL, 0));
148         }
149         else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
150             char *id = words[1];
151             char *microseconds_txt = words[2];
152
153             if(unlikely(!id)) {
154                 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
155                 enabled = 0;
156                 break;
157             }
158
159             st = rrdset_find(host, id);
160             if(unlikely(!st)) {
161                 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
162                 enabled = 0;
163                 break;
164             }
165
166             if(likely(st->counter_done)) {
167                 usec_t microseconds = 0;
168                 if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
169
170                 if(likely(microseconds)) {
171                     if(trust_durations)
172                         rrdset_next_usec_unfiltered(st, microseconds);
173                     else
174                         rrdset_next_usec(st, microseconds);
175                 }
176                 else rrdset_next(st);
177             }
178         }
179         else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
180             if(unlikely(!st)) {
181                 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
182                 enabled = 0;
183                 break;
184             }
185
186             if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG))) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting an END on chart %s", cd->fullfilename, st->id);
187
188             rrdset_done(st);
189             st = NULL;
190
191             count++;
192         }
193         else if(likely(hash == HOST_HASH && !strcmp(s, "HOST"))) {
194             char *guid = words[1];
195             char *hostname = words[2];
196
197             if(unlikely(!guid || !*guid)) {
198                 error("PLUGINSD: '%s' is requesting a HOST, without a guid. Disabling it.", cd->fullfilename);
199                 enabled = 0;
200                 break;
201             }
202             if(unlikely(!hostname || !*hostname)) {
203                 error("PLUGINSD: '%s' is requesting a HOST, without a hostname. Disabling it.", cd->fullfilename);
204                 enabled = 0;
205                 break;
206             }
207
208             host = rrdhost_find_or_create(hostname, guid);
209         }
210         else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
211             debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
212             st = NULL;
213         }
214         else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
215             int noname = 0;
216             st = NULL;
217
218             if((words[1]) != NULL && (words[2]) != NULL && strcmp(words[1], words[2]) == 0)
219                 noname = 1;
220
221             char *type = words[1];
222             char *id = NULL;
223             if(likely(type)) {
224                 id = strchr(type, '.');
225                 if(likely(id)) { *id = '\0'; id++; }
226             }
227             char *name = words[2];
228             char *title = words[3];
229             char *units = words[4];
230             char *family = words[5];
231             char *context = words[6];
232             char *chart = words[7];
233             char *priority_s = words[8];
234             char *update_every_s = words[9];
235
236             if(unlikely(!type || !*type || !id || !*id)) {
237                 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
238                 enabled = 0;
239                 break;
240             }
241
242             int priority = 1000;
243             if(likely(priority_s)) priority = str2i(priority_s);
244
245             int update_every = cd->update_every;
246             if(likely(update_every_s)) update_every = str2i(update_every_s);
247             if(unlikely(!update_every)) update_every = cd->update_every;
248
249             RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
250             if(unlikely(chart)) chart_type = rrdset_type_id(chart);
251
252             if(unlikely(noname || !name || !*name || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0)) name = NULL;
253             if(unlikely(!family || !*family)) family = NULL;
254             if(unlikely(!context || !*context)) context = NULL;
255
256             st = rrdset_find_bytype(host, type, id);
257             if(unlikely(!st)) {
258                 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
259                       , type, id
260                       , name?name:""
261                       , family?family:""
262                       , context?context:""
263                       , rrdset_type_name(chart_type)
264                       , priority
265                       , update_every
266                 );
267
268                 st = rrdset_create(host, type, id, name, family, context, title, units, priority, update_every, chart_type);
269                 cd->update_every = update_every;
270             }
271             else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
272         }
273         else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
274             char *id = words[1];
275             char *name = words[2];
276             char *algorithm = words[3];
277             char *multiplier_s = words[4];
278             char *divisor_s = words[5];
279             char *options = words[6];
280
281             if(unlikely(!id || !*id)) {
282                 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
283                 enabled = 0;
284                 break;
285             }
286
287             if(unlikely(!st)) {
288                 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
289                 enabled = 0;
290                 break;
291             }
292
293             long multiplier = 1;
294             if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
295             if(unlikely(!multiplier)) multiplier = 1;
296
297             long divisor = 1;
298             if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
299             if(unlikely(!divisor)) divisor = 1;
300
301             if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
302
303             if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
304                 debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
305                       , st->id
306                       , id
307                       , name?name:""
308                       , rrd_algorithm_name(rrd_algorithm_id(algorithm))
309                       , multiplier
310                       , divisor
311                       , options?options:""
312                 );
313
314             RRDDIM *rd = rrddim_find(st, id);
315             if(unlikely(!rd)) {
316                 rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
317                 rd->flags = 0x00000000;
318                 if(options && *options) {
319                     if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
320                     if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
321                     if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
322                 }
323             }
324             else if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
325                 debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
326         }
327         else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
328             info("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
329             enabled = 0;
330             break;
331         }
332         else {
333             error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
334             enabled = 0;
335             break;
336         }
337     }
338
339     cd->enabled = enabled;
340
341     if(likely(count)) {
342         cd->successful_collections += count;
343         cd->serial_failures = 0;
344     }
345     else
346         cd->serial_failures++;
347
348     return count;
349 }
350
351 void *pluginsd_worker_thread(void *arg) {
352     struct plugind *cd = (struct plugind *)arg;
353     cd->obsolete = 0;
354
355     size_t count = 0;
356
357     for(;;) {
358         if(unlikely(netdata_exit)) break;
359
360         FILE *fp = mypopen(cd->cmd, &cd->pid);
361         if(unlikely(!fp)) {
362             error("Cannot popen(\"%s\", \"r\").", cd->cmd);
363             break;
364         }
365
366         info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
367
368         count = pluginsd_process(cd, fp, 0);
369         error("PLUGINSD: plugin '%s' disconnected.", cd->fullfilename);
370
371         killpid(cd->pid, SIGTERM);
372
373         info("PLUGINSD: '%s' on pid %d stopped after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
374
375         // get the return code
376         int code = mypclose(fp, cd->pid);
377
378         if(unlikely(netdata_exit)) break;
379         else if(code != 0) {
380             // the plugin reports failure
381
382             if(likely(!cd->successful_collections)) {
383                 // nothing collected - disable it
384                 error("PLUGINSD: '%s' exited with error code %d. Disabling it.", cd->fullfilename, code);
385                 cd->enabled = 0;
386             }
387             else {
388                 // we have collected something
389
390                 if(likely(cd->serial_failures <= 10)) {
391                     error("PLUGINSD: '%s' exited with error code %d, but has given useful output in the past (%zu times). %s", cd->fullfilename, code, cd->successful_collections, cd->enabled?"Waiting a bit before starting it again.":"Will not start it again - it is disabled.");
392                     sleep((unsigned int) (cd->update_every * 10));
393                 }
394                 else {
395                     error("PLUGINSD: '%s' exited with error code %d, but has given useful output in the past (%zu times). We tried %zu times to restart it, but it failed to generate data. Disabling it.", cd->fullfilename, code, cd->successful_collections, cd->serial_failures);
396                     cd->enabled = 0;
397                 }
398             }
399         }
400         else {
401             // the plugin reports success
402
403             if(unlikely(!cd->successful_collections)) {
404                 // we have collected nothing so far
405
406                 if(likely(cd->serial_failures <= 10)) {
407                     error("PLUGINSD: '%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.", cd->fullfilename, cd->pid, cd->enabled?"Waiting a bit before starting it again.":"Will not start it again - it is disabled.");
408                     sleep((unsigned int) (cd->update_every * 10));
409                 }
410                 else {
411                     error("PLUGINSD: '%s' (pid %d) does not generate useful output, although it reports success (exits with 0), but we have tried %zu times to collect something. Disabling it.", cd->fullfilename, cd->pid, cd->serial_failures);
412                     cd->enabled = 0;
413                 }
414             }
415             else
416                 sleep((unsigned int) cd->update_every);
417         }
418         cd->pid = 0;
419
420         if(unlikely(!cd->enabled)) break;
421     }
422
423     info("PLUGINSD: '%s' thread exiting", cd->fullfilename);
424
425     cd->obsolete = 1;
426     pthread_exit(NULL);
427     return NULL;
428 }
429
430 void *pluginsd_main(void *ptr) {
431     struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
432
433     info("PLUGINS.D thread created with task id %d", gettid());
434
435     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
436         error("Cannot set pthread cancel type to DEFERRED.");
437
438     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
439         error("Cannot set pthread cancel state to ENABLE.");
440
441     int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
442     int scan_frequency = (int) config_get_number("plugins", "check for new plugins every", 60);
443     DIR *dir = NULL;
444     struct dirent *file = NULL;
445     struct plugind *cd;
446
447     // enable the apps plugin by default
448     // config_get_boolean("plugins", "apps", 1);
449
450     if(scan_frequency < 1) scan_frequency = 1;
451
452     for(;;) {
453         if(unlikely(netdata_exit)) break;
454
455         dir = opendir(netdata_configured_plugins_dir);
456         if(unlikely(!dir)) {
457             error("Cannot open directory '%s'.", netdata_configured_plugins_dir);
458             goto cleanup;
459         }
460
461         while(likely((file = readdir(dir)))) {
462             if(unlikely(netdata_exit)) break;
463
464             debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
465
466             if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
467
468             int len = (int) strlen(file->d_name);
469             if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
470             if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
471                 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
472                 continue;
473             }
474
475             char pluginname[CONFIG_MAX_NAME + 1];
476             snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
477             int enabled = config_get_boolean("plugins", pluginname, automatic_run);
478
479             if(unlikely(!enabled)) {
480                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
481                 continue;
482             }
483
484             // check if it runs already
485             for(cd = pluginsd_root ; cd ; cd = cd->next)
486                 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
487
488             if(likely(cd && !cd->obsolete)) {
489                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
490                 continue;
491             }
492
493             // it is not running
494             // allocate a new one, or use the obsolete one
495             if(unlikely(!cd)) {
496                 cd = callocz(sizeof(struct plugind), 1);
497
498                 snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
499
500                 strncpyz(cd->filename, file->d_name, FILENAME_MAX);
501                 snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", netdata_configured_plugins_dir, cd->filename);
502
503                 cd->enabled = enabled;
504                 cd->update_every = (int) config_get_number(cd->id, "update every", localhost->rrd_update_every);
505                 cd->started_t = now_realtime_sec();
506
507                 char *def = "";
508                 snprintfz(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
509
510                 // link it
511                 if(likely(pluginsd_root)) cd->next = pluginsd_root;
512                 pluginsd_root = cd;
513
514                 // it is not currently running
515                 cd->obsolete = 1;
516
517                 if(cd->enabled) {
518                     // spawn a new thread for it
519                     if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0))
520                         error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
521
522                     else if(unlikely(pthread_detach(cd->thread) != 0))
523                         error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
524                 }
525             }
526         }
527
528         closedir(dir);
529         sleep((unsigned int) scan_frequency);
530     }
531
532 cleanup:
533     info("PLUGINS.D thread exiting");
534
535     static_thread->enabled = 0;
536     pthread_exit(NULL);
537     return NULL;
538 }