]> arthur.barton.de Git - netdata.git/blob - src/plugins_d.c
disabled per cpu core interrupts charts due to high memory requirements #12
[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(pluginsd_space(*s)) s++;
41
42         // check for quote
43         if(*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(*s) {
53                 // if it is escape
54                 if(*s == '\\' && s[1]) {
55                         s += 2;
56                         continue;
57                 }
58
59                 // if it is quote
60                 else if(*s == quote) {
61                         quote = 0;
62                         *s = ' ';
63                         continue;
64                 }
65
66                 // if it is a space
67                 else if(quote == 0 && pluginsd_space(*s)) {
68
69                         // terminate the word
70                         *s++ = '\0';
71
72                         // skip all white space
73                         while(pluginsd_space(*s)) s++;
74
75                         // check for quote
76                         if(*s == '\'' || *s == '"') {
77                                 quote = *s;     // remember the quote
78                                 s++;            // skip the quote
79                         }
80
81                         // if we reached the end, stop
82                         if(!*s) break;
83
84                         // store the next word
85                         if(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(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(1) {
122                 FILE *fp = mypopen(cd->cmd, &cd->pid);
123                 if(!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(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(!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(hash == SET_HASH && !strcmp(s, "SET")) {
152                                 char *dimension = words[1];
153                                 char *value = words[2];
154
155                                 if(!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(!value || !*value) value = "0";
163
164                                 if(!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(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(hash == BEGIN_HASH && !strcmp(s, "BEGIN")) {
177                                 char *id = words[1];
178                                 char *microseconds_txt = words[2];
179
180                                 if(!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(!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(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(hash == END_HASH && !strcmp(s, "END")) {
203                                 if(!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(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(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(hash == CHART_HASH && !strcmp(s, "CHART")) {
220                                 st = NULL;
221
222                                 char *type = words[1];
223                                 char *id = NULL;
224                                 if(type) {
225                                         id = strchr(type, '.');
226                                         if(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(!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(priority_s) priority = atoi(priority_s);
246
247                                 int update_every = cd->update_every;
248                                 if(update_every_s) update_every = atoi(update_every_s);
249                                 if(!update_every) update_every = cd->update_every;
250
251                                 int chart_type = RRDSET_TYPE_LINE;
252                                 if(chart) chart_type = rrdset_type_id(chart);
253
254                                 if(!name || !*name) name = NULL;
255                                 if(!family || !*family) family = id;
256                                 if(!category || !*category) category = type;
257
258                                 st = rrdset_find_bytype(type, id);
259                                 if(!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(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(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(!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(!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(!multiplier) multiplier = 1;
302
303                                 long divisor = 1;
304                                 if(divisor_s && *divisor_s) divisor = atol(divisor_s);
305                                 if(!divisor) divisor = 1;
306
307                                 if(!algorithm || !*algorithm) algorithm = "absolute";
308
309                                 if(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(!rd) {
321                                         rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
322                                         if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
323                                 }
324                                 else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
325                         }
326                         else if(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(hash == MYPID_HASH && !strcmp(s, "MYPID")) {
334                                 char *pid_s = words[1];
335                                 pid_t pid = atol(pid_s);
336
337                                 if(pid) cd->pid = pid;
338                                 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
339                         }
340                         else if(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(!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(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(!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(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         if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
396                 error("Cannot set pthread cancel type to DEFERRED.");
397
398         if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
399                 error("Cannot set pthread cancel state to ENABLE.");
400
401         char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
402         int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
403         int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
404         DIR *dir = NULL;
405         struct dirent *file = NULL;
406         struct plugind *cd;
407
408         // enable the apps plugin by default
409         config_get_boolean("plugins", "apps", 1);
410
411         if(scan_frequency < 1) scan_frequency = 1;
412
413         while(1) {
414                 dir = opendir(dir_name);
415                 if(!dir) {
416                         error("Cannot open directory '%s'.", dir_name);
417                         return NULL;
418                 }
419
420                 while((file = readdir(dir))) {
421                         debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
422
423                         if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue;
424
425                         int len = strlen(file->d_name);
426                         if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue;
427                         if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0) {
428                                 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
429                                 continue;
430                         }
431
432                         char pluginname[CONFIG_MAX_NAME + 1];
433                         snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
434                         int enabled = config_get_boolean("plugins", pluginname, automatic_run);
435
436                         if(!enabled) {
437                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
438                                 continue;
439                         }
440
441                         // check if it runs already
442                         for(cd = pluginsd_root ; cd ; cd = cd->next) {
443                                 if(strcmp(cd->filename, file->d_name) == 0) break;
444                         }
445                         if(cd && !cd->obsolete) {
446                                 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
447                                 continue;
448                         }
449
450                         // it is not running
451                         // allocate a new one, or use the obsolete one
452                         if(!cd) {
453                                 cd = calloc(sizeof(struct plugind), 1);
454                                 if(!cd) fatal("Cannot allocate memory for plugin.");
455
456                                 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
457                                 
458                                 strncpy(cd->filename, file->d_name, FILENAME_MAX);
459                                 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
460
461                                 cd->enabled = enabled;
462                                 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
463                                 cd->started_t = time(NULL);
464
465                                 char *def = "";
466                                 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
467
468                                 // link it
469                                 if(pluginsd_root) cd->next = pluginsd_root;
470                                 pluginsd_root = cd;
471                         }
472                         cd->obsolete = 0;
473
474                         if(!cd->enabled) continue;
475
476                         // spawn a new thread for it
477                         if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) {
478                                 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
479                                 cd->obsolete = 1;
480                         }
481                         else if(pthread_detach(cd->thread) != 0)
482                                 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
483                 }
484
485                 closedir(dir);
486                 sleep(scan_frequency);
487         }
488
489         return NULL;
490 }
491
492