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