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