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