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