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