14 #include "appconfig.h"
18 #include "plugins_d.h"
19 #include "../config.h"
21 struct plugind *pluginsd_root = NULL;
25 static inline int pluginsd_space(char c) {
39 static int pluginsd_split_words(char *str, char **words, int max_words) {
40 char *s = str, quote = 0;
43 // skip all white space
44 while(unlikely(pluginsd_space(*s))) s++;
47 if(unlikely(*s == '\'' || *s == '"')) {
48 quote = *s; // remember the quote
49 s++; // skip the quote
52 // store the first word
55 // while we have something
58 if(unlikely(*s == '\\' && s[1])) {
64 else if(unlikely(*s == quote)) {
71 else if(unlikely(quote == 0 && pluginsd_space(*s))) {
76 // skip all white space
77 while(likely(pluginsd_space(*s))) s++;
80 if(unlikely(*s == '\'' || *s == '"')) {
81 quote = *s; // remember the quote
82 s++; // skip the quote
85 // if we reached the end, stop
86 if(unlikely(!*s)) break;
88 // store the next word
89 if(likely(i < max_words)) words[i++] = s;
97 // terminate the words
99 while(likely(j < max_words)) words[j++] = NULL;
105 void *pluginsd_worker_thread(void *arg)
107 struct plugind *cd = (struct plugind *)arg;
108 char line[PLUGINSD_LINE_MAX + 1];
110 #ifdef DETACH_PLUGINS_FROM_NETDATA
111 unsigned long long usec = 0, susec = 0;
112 struct timeval last = {0, 0} , now = {0, 0};
115 char *words[MAX_WORDS] = { NULL };
116 uint32_t SET_HASH = simple_hash("SET");
117 uint32_t BEGIN_HASH = simple_hash("BEGIN");
118 uint32_t END_HASH = simple_hash("END");
119 uint32_t FLUSH_HASH = simple_hash("FLUSH");
120 uint32_t CHART_HASH = simple_hash("CHART");
121 uint32_t DIMENSION_HASH = simple_hash("DIMENSION");
122 uint32_t DISABLE_HASH = simple_hash("DISABLE");
123 #ifdef DETACH_PLUGINS_FROM_NETDATA
124 uint32_t MYPID_HASH = simple_hash("MYPID");
125 uint32_t STOPPING_WAKE_ME_UP_PLEASE_HASH = simple_hash("STOPPING_WAKE_ME_UP_PLEASE");
129 if(unlikely(netdata_exit)) break;
131 FILE *fp = mypopen(cd->cmd, &cd->pid);
133 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
137 info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
140 unsigned long long count = 0;
144 while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
145 if(unlikely(netdata_exit)) break;
147 line[PLUGINSD_LINE_MAX] = '\0';
149 // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
151 int w = pluginsd_split_words(line, words, MAX_WORDS);
153 if(unlikely(!s || !*s || !w)) {
154 // debug(D_PLUGINSD, "PLUGINSD: empty line");
158 // debug(D_PLUGINSD, "PLUGINSD: words 0='%s' 1='%s' 2='%s' 3='%s' 4='%s' 5='%s' 6='%s' 7='%s' 8='%s' 9='%s'", words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7], words[8], words[9]);
160 hash = simple_hash(s);
162 if(likely(hash == SET_HASH && !strcmp(s, "SET"))) {
163 char *dimension = words[1];
164 char *value = words[2];
166 if(unlikely(!dimension || !*dimension)) {
167 error("PLUGINSD: '%s' is requesting a SET on chart '%s', without a dimension. Disabling it.", cd->fullfilename, st->id);
169 killpid(cd->pid, SIGTERM);
173 if(unlikely(!value || !*value)) value = NULL;
176 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value?value:"<nothing>");
178 killpid(cd->pid, SIGTERM);
182 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value?value:"<nothing>");
184 if(value) rrddim_set(st, dimension, strtoll(value, NULL, 0));
188 else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
190 char *microseconds_txt = words[2];
193 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
195 killpid(cd->pid, SIGTERM);
199 st = rrdset_find(id);
201 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
203 killpid(cd->pid, SIGTERM);
207 if(likely(st->counter_done)) {
208 unsigned long long microseconds = 0;
209 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
210 if(microseconds) rrdset_next_usec(st, microseconds);
211 else rrdset_next_plugins(st);
214 else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
216 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
218 killpid(cd->pid, SIGTERM);
222 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting an END on chart %s", cd->fullfilename, st->id);
227 else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
228 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
231 else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
235 if((words[1]) != NULL && (words[2]) != NULL && strcmp(words[1], words[2]) == 0)
238 char *type = words[1];
241 id = strchr(type, '.');
242 if(likely(id)) { *id = '\0'; id++; }
244 char *name = words[2];
245 char *title = words[3];
246 char *units = words[4];
247 char *family = words[5];
248 char *context = words[6];
249 char *chart = words[7];
250 char *priority_s = words[8];
251 char *update_every_s = words[9];
253 if(unlikely(!type || !*type || !id || !*id)) {
254 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
256 killpid(cd->pid, SIGTERM);
261 if(likely(priority_s)) priority = atoi(priority_s);
263 int update_every = cd->update_every;
264 if(likely(update_every_s)) update_every = atoi(update_every_s);
265 if(unlikely(!update_every)) update_every = cd->update_every;
267 int chart_type = RRDSET_TYPE_LINE;
268 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
270 if(unlikely(noname || !name || !*name || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0)) name = NULL;
271 if(unlikely(!family || !*family)) family = NULL;
272 if(unlikely(!context || !*context)) context = NULL;
274 st = rrdset_find_bytype(type, id);
276 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
281 , rrdset_type_name(chart_type)
286 st = rrdset_create(type, id, name, family, context, title, units, priority, update_every, chart_type);
287 cd->update_every = update_every;
289 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
291 else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
293 char *name = words[2];
294 char *algorithm = words[3];
295 char *multiplier_s = words[4];
296 char *divisor_s = words[5];
297 char *options = words[6];
299 if(unlikely(!id || !*id)) {
300 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
302 killpid(cd->pid, SIGTERM);
307 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
309 killpid(cd->pid, SIGTERM);
314 if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
315 if(unlikely(!multiplier)) multiplier = 1;
318 if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
319 if(unlikely(!divisor)) divisor = 1;
321 if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
323 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
327 , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
333 RRDDIM *rd = rrddim_find(st, id);
335 rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
336 rd->flags = 0x00000000;
337 if(options && *options) {
338 if(strstr(options, "hidden") != NULL) rd->flags |= RRDDIM_FLAG_HIDDEN;
339 if(strstr(options, "noreset") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
340 if(strstr(options, "nooverflow") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
343 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
345 else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
346 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
348 killpid(cd->pid, SIGTERM);
351 #ifdef DETACH_PLUGINS_FROM_NETDATA
352 else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) {
353 char *pid_s = words[1];
354 pid_t pid = strtod(pid_s, NULL, 0);
356 if(likely(pid)) cd->pid = pid;
357 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
359 else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) {
360 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
362 gettimeofday(&now, NULL);
363 if(unlikely(!usec && !susec)) {
365 susec = cd->rrd_update_every * 1000000ULL;
369 usec = usecdiff(&now, &last) - susec;
370 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
371 if(unlikely(usec < (rrd_update_every * 1000000ULL / 2ULL))) susec = (rrd_update_every * 1000000ULL) - usec;
372 else susec = rrd_update_every * 1000000ULL / 2ULL;
375 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
377 killpid(cd->pid, SIGCONT);
378 bcopy(&now, &last, sizeof(struct timeval));
383 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
385 killpid(cd->pid, SIGTERM);
390 info("PLUGINSD: '%s' on pid %d stopped.", cd->fullfilename, cd->pid);
392 // fgets() failed or loop broke
393 int code = mypclose(fp, cd->pid);
394 if(code == 1 || code == 127) {
396 // 127 = cannot even run it
397 error("PLUGINSD: '%s' (pid %d) exited with code %d. Disabling it.", cd->fullfilename, cd->pid, code);
409 if(unlikely(!count && cd->enabled)) {
410 error("PLUGINSD: '%s' (pid %d) does not generate useful output. Waiting a bit before starting it again.", cd->fullfilename, cd->pid);
411 sleep((unsigned int) (cd->update_every * 10));
415 if(likely(cd->enabled)) sleep((unsigned int) cd->update_every);
424 void *pluginsd_main(void *ptr)
428 info("PLUGINS.D thread created with task id %d", gettid());
430 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
431 error("Cannot set pthread cancel type to DEFERRED.");
433 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
434 error("Cannot set pthread cancel state to ENABLE.");
436 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
437 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
438 int scan_frequency = (int) config_get_number("plugins", "check for new plugins every", 60);
440 struct dirent *file = NULL;
443 // enable the apps plugin by default
444 // config_get_boolean("plugins", "apps", 1);
446 if(scan_frequency < 1) scan_frequency = 1;
449 if(unlikely(netdata_exit)) break;
451 dir = opendir(dir_name);
453 error("Cannot open directory '%s'.", dir_name);
458 while(likely((file = readdir(dir)))) {
459 if(unlikely(netdata_exit)) break;
461 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
463 if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
465 int len = (int) strlen(file->d_name);
466 if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
467 if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
468 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
472 char pluginname[CONFIG_MAX_NAME + 1];
473 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
474 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
476 if(unlikely(!enabled)) {
477 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
481 // check if it runs already
482 for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) {
483 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
485 if(likely(cd && !cd->obsolete)) {
486 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
491 // allocate a new one, or use the obsolete one
493 cd = calloc(sizeof(struct plugind), 1);
494 if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
496 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
498 strncpy(cd->filename, file->d_name, FILENAME_MAX);
499 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
501 cd->enabled = enabled;
502 cd->update_every = (int) config_get_number(cd->id, "update every", rrd_update_every);
503 cd->started_t = time(NULL);
506 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
509 if(likely(pluginsd_root)) cd->next = pluginsd_root;
514 if(unlikely(!cd->enabled)) continue;
516 // spawn a new thread for it
517 if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) {
518 error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
521 else if(unlikely(pthread_detach(cd->thread) != 0))
522 error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
526 sleep((unsigned int) scan_frequency);