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