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