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