]> arthur.barton.de Git - netdata.git/blob - src/plugins_d.c
Merge pull request #35 from alonbl/cleanups
[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                         line[PLUGINSD_LINE_MAX] = '\0';
143
144                         // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
145
146                         int w = pluginsd_split_words(line, words, MAX_WORDS);
147                         s = words[0];
148                         if(unlikely(!s || !*s || !w)) {
149                                 // debug(D_PLUGINSD, "PLUGINSD: empty line");
150                                 continue;
151                         }
152
153                         // 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]);
154
155                         hash = simple_hash(s);
156
157                         if(likely(hash == SET_HASH && !strcmp(s, "SET"))) {
158                                 char *dimension = words[1];
159                                 char *value = words[2];
160
161                                 if(unlikely(!dimension || !*dimension)) {
162                                         error("PLUGINSD: '%s' is requesting a SET on chart '%s', without a dimension. Disabling it.", cd->fullfilename, st->id);
163                                         cd->enabled = 0;
164                                         killpid(cd->pid, SIGTERM);
165                                         break;
166                                 }
167
168                                 if(unlikely(!value || !*value)) value = NULL;
169
170                                 if(unlikely(!st)) {
171                                         error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value?value:"<nothing>");
172                                         cd->enabled = 0;
173                                         killpid(cd->pid, SIGTERM);
174                                         break;
175                                 }
176
177                                 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value?value:"<nothing>");
178
179                                 if(value) rrddim_set(st, dimension, atoll(value));
180
181                                 count++;
182                         }
183                         else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
184                                 char *id = words[1];
185                                 char *microseconds_txt = words[2];
186
187                                 if(unlikely(!id)) {
188                                         error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
189                                         cd->enabled = 0;
190                                         killpid(cd->pid, SIGTERM);
191                                         break;
192                                 }
193
194                                 st = rrdset_find(id);
195                                 if(unlikely(!st)) {
196                                         error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
197                                         cd->enabled = 0;
198                                         killpid(cd->pid, SIGTERM);
199                                         break;
200                                 }
201
202                                 if(likely(st->counter_done)) {
203                                         unsigned long long microseconds = 0;
204                                         if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
205                                         if(microseconds) rrdset_next_usec(st, microseconds);
206                                         else rrdset_next_plugins(st);
207                                 }
208                         }
209                         else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
210                                 if(unlikely(!st)) {
211                                         error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
212                                         cd->enabled = 0;
213                                         killpid(cd->pid, SIGTERM);
214                                         break;
215                                 }
216
217                                 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
218
219                                 rrdset_done(st);
220                                 st = NULL;
221                         }
222                         else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
223                                 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
224                                 st = NULL;
225                         }
226                         else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
227                                 st = NULL;
228
229                                 char *type = words[1];
230                                 char *id = NULL;
231                                 if(likely(type)) {
232                                         id = strchr(type, '.');
233                                         if(likely(id)) { *id = '\0'; id++; }
234                                 }
235                                 char *name = words[2];
236                                 char *title = words[3];
237                                 char *units = words[4];
238                                 char *family = words[5];
239                                 char *category = words[6];
240                                 char *chart = words[7];
241                                 char *priority_s = words[8];
242                                 char *update_every_s = words[9];
243
244                                 if(unlikely(!type || !*type || !id || !*id)) {
245                                         error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
246                                         cd->enabled = 0;
247                                         killpid(cd->pid, SIGTERM);
248                                         break;
249                                 }
250
251                                 int priority = 1000;
252                                 if(likely(priority_s)) priority = atoi(priority_s);
253
254                                 int update_every = cd->update_every;
255                                 if(likely(update_every_s)) update_every = atoi(update_every_s);
256                                 if(unlikely(!update_every)) update_every = cd->update_every;
257
258                                 int chart_type = RRDSET_TYPE_LINE;
259                                 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
260
261                                 if(unlikely(!name || !*name)) name = NULL;
262                                 if(unlikely(!family || !*family)) family = id;
263                                 if(unlikely(!category || !*category)) category = type;
264
265                                 st = rrdset_find_bytype(type, id);
266                                 if(unlikely(!st)) {
267                                         debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
268                                                 , type, id
269                                                 , name?name:""
270                                                 , family?family:""
271                                                 , category?category:""
272                                                 , rrdset_type_name(chart_type)
273                                                 , priority
274                                                 , update_every
275                                                 );
276
277                                         st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type);
278                                         cd->update_every = update_every;
279
280                                         if(unlikely(strcmp(category, "none") == 0)) st->isdetail = 1;
281                                 }
282                                 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
283                         }
284                         else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
285                                 char *id = words[1];
286                                 char *name = words[2];
287                                 char *algorithm = words[3];
288                                 char *multiplier_s = words[4];
289                                 char *divisor_s = words[5];
290                                 char *options = words[6];
291
292                                 if(unlikely(!id || !*id)) {
293                                         error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
294                                         cd->enabled = 0;
295                                         killpid(cd->pid, SIGTERM);
296                                         break;
297                                 }
298
299                                 if(unlikely(!st)) {
300                                         error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
301                                         cd->enabled = 0;
302                                         killpid(cd->pid, SIGTERM);
303                                         break;
304                                 }
305
306                                 long multiplier = 1;
307                                 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
308                                 if(unlikely(!multiplier)) multiplier = 1;
309
310                                 long divisor = 1;
311                                 if(likely(divisor_s && *divisor_s)) divisor = atol(divisor_s);
312                                 if(unlikely(!divisor)) divisor = 1;
313
314                                 if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
315
316                                 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'"
317                                         , st->id
318                                         , id
319                                         , name?name:""
320                                         , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
321                                         , multiplier
322                                         , divisor
323                                         , options?options:""
324                                         );
325
326                                 RRDDIM *rd = rrddim_find(st, id);
327                                 if(unlikely(!rd)) {
328                                         rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
329                                         rd->flags = 0x00000000;
330                                         if(options && *options) {
331                                                 if(strstr(options, "hidden") != NULL) rd->flags |= RRDDIM_FLAG_HIDDEN;
332                                                 if(strstr(options, "noreset") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
333                                                 if(strstr(options, "nooverflow") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
334                                         }
335                                 }
336                                 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
337                         }
338                         else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
339                                 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
340                                 cd->enabled = 0;
341                                 killpid(cd->pid, SIGTERM);
342                                 break;
343                         }
344 #ifdef DETACH_PLUGINS_FROM_NETDATA
345                         else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) {
346                                 char *pid_s = words[1];
347                                 pid_t pid = atol(pid_s);
348
349                                 if(likely(pid)) cd->pid = pid;
350                                 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
351                         }
352                         else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) {
353                                 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
354
355                                 gettimeofday(&now, NULL);
356                                 if(unlikely(!usec && !susec)) {
357                                         // our first run
358                                         susec = cd->rrd_update_every * 1000000ULL;
359                                 }
360                                 else {
361                                         // second+ run
362                                         usec = usecdiff(&now, &last) - susec;
363                                         error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
364                                         if(unlikely(usec < (rrd_update_every * 1000000ULL / 2ULL))) susec = (rrd_update_every * 1000000ULL) - usec;
365                                         else susec = rrd_update_every * 1000000ULL / 2ULL;
366                                 }
367
368                                 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
369                                 usleep(susec);
370                                 killpid(cd->pid, SIGCONT);
371                                 bcopy(&now, &last, sizeof(struct timeval));
372                                 break;
373                         }
374 #endif
375                         else {
376                                 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
377                                 cd->enabled = 0;
378                                 killpid(cd->pid, SIGTERM);
379                                 break;
380                         }
381                 }
382
383                 info("PLUGINSD: '%s' on pid %d stopped.", cd->fullfilename, cd->pid);
384
385                 // fgets() failed or loop broke
386                 mypclose(fp, cd->pid);
387
388                 if(unlikely(!count && cd->enabled)) {
389                         error("PLUGINSD: '%s' (pid %d) does not generate usefull output. Waiting a bit before starting it again.", cd->fullfilename, cd->pid);
390                         sleep(cd->update_every * 10);
391                 }
392
393                 cd->pid = 0;
394
395                 if(likely(cd->enabled)) sleep(cd->update_every);
396                 else break;
397         }
398
399         cd->obsolete = 1;
400         return NULL;
401 }
402
403 void *pluginsd_main(void *ptr)
404 {
405         if(ptr) { ; }
406
407         info("PLUGINS.D thread created with task id %d", gettid());
408
409         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
410                 error("Cannot set pthread cancel type to DEFERRED.");
411
412         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
413                 error("Cannot set pthread cancel state to ENABLE.");
414
415         char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
416         int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
417         int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
418         DIR *dir = NULL;
419         struct dirent *file = NULL;
420         struct plugind *cd;
421
422         // enable the apps plugin by default
423         // config_get_boolean("plugins", "apps", 1);
424
425         if(scan_frequency < 1) scan_frequency = 1;
426
427         while(likely(1)) {
428                 dir = opendir(dir_name);
429                 if(unlikely(!dir)) {
430                         error("Cannot open directory '%s'.", dir_name);
431                         return NULL;
432                 }
433
434                 while(likely((file = readdir(dir)))) {
435                         debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
436
437                         if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
438
439                         int len = strlen(file->d_name);
440                         if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
441                         if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
442                                 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
443                                 continue;
444                         }
445
446                         char pluginname[CONFIG_MAX_NAME + 1];
447                         snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
448                         int enabled = config_get_boolean("plugins", pluginname, automatic_run);
449
450                         if(unlikely(!enabled)) {
451                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
452                                 continue;
453                         }
454
455                         // check if it runs already
456                         for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) {
457                                 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
458                         }
459                         if(likely(cd && !cd->obsolete)) {
460                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
461                                 continue;
462                         }
463
464                         // it is not running
465                         // allocate a new one, or use the obsolete one
466                         if(unlikely(!cd)) {
467                                 cd = calloc(sizeof(struct plugind), 1);
468                                 if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
469
470                                 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
471
472                                 strncpy(cd->filename, file->d_name, FILENAME_MAX);
473                                 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
474
475                                 cd->enabled = enabled;
476                                 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
477                                 cd->started_t = time(NULL);
478
479                                 char *def = "";
480                                 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
481
482                                 // link it
483                                 if(likely(pluginsd_root)) cd->next = pluginsd_root;
484                                 pluginsd_root = cd;
485                         }
486                         cd->obsolete = 0;
487
488                         if(unlikely(!cd->enabled)) continue;
489
490                         // spawn a new thread for it
491                         if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) {
492                                 error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
493                                 cd->obsolete = 1;
494                         }
495                         else if(unlikely(pthread_detach(cd->thread) != 0))
496                                 error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
497                 }
498
499                 closedir(dir);
500                 sleep(scan_frequency);
501         }
502
503         return NULL;
504 }
505
506