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