3 struct plugind *pluginsd_root = NULL;
7 static inline int pluginsd_space(char c) {
21 static int pluginsd_split_words(char *str, char **words, int max_words) {
22 char *s = str, quote = 0;
25 // skip all white space
26 while(unlikely(pluginsd_space(*s))) s++;
29 if(unlikely(*s == '\'' || *s == '"')) {
30 quote = *s; // remember the quote
31 s++; // skip the quote
34 // store the first word
37 // while we have something
40 if(unlikely(*s == '\\' && s[1])) {
46 else if(unlikely(*s == quote)) {
53 else if(unlikely(quote == 0 && pluginsd_space(*s))) {
58 // skip all white space
59 while(likely(pluginsd_space(*s))) s++;
62 if(unlikely(*s == '\'' || *s == '"')) {
63 quote = *s; // remember the quote
64 s++; // skip the quote
67 // if we reached the end, stop
68 if(unlikely(!*s)) break;
70 // store the next word
71 if(likely(i < max_words)) words[i++] = s;
79 // terminate the words
81 while(likely(j < max_words)) words[j++] = NULL;
87 void *pluginsd_worker_thread(void *arg) {
88 struct plugind *cd = (struct plugind *)arg;
91 char line[PLUGINSD_LINE_MAX + 1];
93 #ifdef DETACH_PLUGINS_FROM_NETDATA
94 usec_t usec = 0, susec = 0;
95 struct timeval last = {0, 0} , now = {0, 0};
98 char *words[MAX_WORDS] = { NULL };
99 uint32_t HOST_HASH = simple_hash("HOST");
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");
112 RRDHOST *host = &localhost;
115 if(unlikely(netdata_exit)) break;
117 FILE *fp = mypopen(cd->cmd, &cd->pid);
119 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
123 info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
128 while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
129 if(unlikely(netdata_exit)) break;
131 line[PLUGINSD_LINE_MAX] = '\0';
133 // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
135 int w = pluginsd_split_words(line, words, MAX_WORDS);
137 if(unlikely(!s || !*s || !w)) {
138 // debug(D_PLUGINSD, "PLUGINSD: empty line");
142 // 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]);
144 if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
145 char *dimension = words[1];
146 char *value = words[2];
148 if(unlikely(!dimension || !*dimension)) {
149 error("PLUGINSD: '%s' is requesting a SET on chart '%s', without a dimension. Disabling it.", cd->fullfilename, st->id);
151 killpid(cd->pid, SIGTERM);
155 if(unlikely(!value || !*value)) value = NULL;
158 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value?value:"<nothing>");
160 killpid(cd->pid, SIGTERM);
164 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value?value:"<nothing>");
166 if(value) rrddim_set(st, dimension, strtoll(value, NULL, 0));
168 else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
170 char *microseconds_txt = words[2];
173 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
175 killpid(cd->pid, SIGTERM);
179 st = rrdset_find(host, id);
181 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
183 killpid(cd->pid, SIGTERM);
187 if(likely(st->counter_done)) {
188 usec_t microseconds = 0;
189 if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
190 if(microseconds) rrdset_next_usec(st, microseconds);
191 else rrdset_next(st);
194 else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
196 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
198 killpid(cd->pid, SIGTERM);
202 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting an END on chart %s", cd->fullfilename, st->id);
209 else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
210 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
213 else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
217 if((words[1]) != NULL && (words[2]) != NULL && strcmp(words[1], words[2]) == 0)
220 char *type = words[1];
223 id = strchr(type, '.');
224 if(likely(id)) { *id = '\0'; id++; }
226 char *name = words[2];
227 char *title = words[3];
228 char *units = words[4];
229 char *family = words[5];
230 char *context = words[6];
231 char *chart = words[7];
232 char *priority_s = words[8];
233 char *update_every_s = words[9];
235 if(unlikely(!type || !*type || !id || !*id)) {
236 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
238 killpid(cd->pid, SIGTERM);
243 if(likely(priority_s)) priority = str2i(priority_s);
245 int update_every = cd->update_every;
246 if(likely(update_every_s)) update_every = str2i(update_every_s);
247 if(unlikely(!update_every)) update_every = cd->update_every;
249 int chart_type = RRDSET_TYPE_LINE;
250 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
252 if(unlikely(noname || !name || !*name || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0)) name = NULL;
253 if(unlikely(!family || !*family)) family = NULL;
254 if(unlikely(!context || !*context)) context = NULL;
256 st = rrdset_find_bytype(host, type, id);
258 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
263 , rrdset_type_name(chart_type)
268 st = rrdset_create(type, id, name, family, context, title, units, priority, update_every, chart_type);
269 cd->update_every = update_every;
271 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
273 else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
275 char *name = words[2];
276 char *algorithm = words[3];
277 char *multiplier_s = words[4];
278 char *divisor_s = words[5];
279 char *options = words[6];
281 if(unlikely(!id || !*id)) {
282 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
284 killpid(cd->pid, SIGTERM);
289 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
291 killpid(cd->pid, SIGTERM);
296 if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
297 if(unlikely(!multiplier)) multiplier = 1;
300 if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
301 if(unlikely(!divisor)) divisor = 1;
303 if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
305 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'"
309 , rrd_algorithm_name(rrd_algorithm_id(algorithm))
315 RRDDIM *rd = rrddim_find(st, id);
317 rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
318 rd->flags = 0x00000000;
319 if(options && *options) {
320 if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
321 if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
322 if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
325 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
327 else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
328 info("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
330 killpid(cd->pid, SIGTERM);
333 #ifdef DETACH_PLUGINS_FROM_NETDATA
334 else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) {
335 char *pid_s = words[1];
336 pid_t pid = strtod(pid_s, NULL, 0);
338 if(likely(pid)) cd->pid = pid;
339 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
341 else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) {
342 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
344 now_realtime_timeval(&now);
345 if(unlikely(!usec && !susec)) {
347 susec = cd->rrd_update_every * USEC_PER_SEC;
351 usec = dt_usec(&now, &last) - susec;
352 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
353 if(unlikely(usec < (rrd_update_every * USEC_PER_SEC / 2ULL))) susec = (rrd_update_every * USEC_PER_SEC) - usec;
354 else susec = rrd_update_every * USEC_PER_SEC / 2ULL;
357 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
359 killpid(cd->pid, SIGCONT);
360 memmove(&last, &now, sizeof(struct timeval));
365 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
367 killpid(cd->pid, SIGTERM);
372 cd->successful_collections += count;
373 cd->serial_failures = 0;
376 cd->serial_failures++;
378 info("PLUGINSD: '%s' on pid %d stopped after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
380 // get the return code
381 int code = mypclose(fp, cd->pid);
383 if(unlikely(netdata_exit)) break;
385 // the plugin reports failure
387 if(likely(!cd->successful_collections)) {
388 // nothing collected - disable it
389 error("PLUGINSD: '%s' exited with error code %d. Disabling it.", cd->fullfilename, code);
393 // we have collected something
395 if(likely(cd->serial_failures <= 10)) {
396 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.");
397 sleep((unsigned int) (cd->update_every * 10));
400 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);
406 // the plugin reports success
408 if(unlikely(!cd->successful_collections)) {
409 // we have collected nothing so far
411 if(likely(cd->serial_failures <= 10)) {
412 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.");
413 sleep((unsigned int) (cd->update_every * 10));
416 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);
421 sleep((unsigned int) cd->update_every);
425 if(unlikely(!cd->enabled)) break;
428 info("PLUGINSD: '%s' thread exiting", cd->fullfilename);
435 void *pluginsd_main(void *ptr) {
436 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
438 info("PLUGINS.D thread created with task id %d", gettid());
440 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
441 error("Cannot set pthread cancel type to DEFERRED.");
443 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
444 error("Cannot set pthread cancel state to ENABLE.");
446 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
447 int scan_frequency = (int) config_get_number("plugins", "check for new plugins every", 60);
449 struct dirent *file = NULL;
452 // enable the apps plugin by default
453 // config_get_boolean("plugins", "apps", 1);
455 if(scan_frequency < 1) scan_frequency = 1;
458 if(unlikely(netdata_exit)) break;
460 dir = opendir(netdata_configured_plugins_dir);
462 error("Cannot open directory '%s'.", netdata_configured_plugins_dir);
466 while(likely((file = readdir(dir)))) {
467 if(unlikely(netdata_exit)) break;
469 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
471 if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
473 int len = (int) strlen(file->d_name);
474 if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
475 if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
476 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
480 char pluginname[CONFIG_MAX_NAME + 1];
481 snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
482 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
484 if(unlikely(!enabled)) {
485 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
489 // check if it runs already
490 for(cd = pluginsd_root ; cd ; cd = cd->next)
491 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
493 if(likely(cd && !cd->obsolete)) {
494 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
499 // allocate a new one, or use the obsolete one
501 cd = callocz(sizeof(struct plugind), 1);
503 snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
505 strncpyz(cd->filename, file->d_name, FILENAME_MAX);
506 snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", netdata_configured_plugins_dir, cd->filename);
508 cd->enabled = enabled;
509 cd->update_every = (int) config_get_number(cd->id, "update every", rrd_update_every);
510 cd->started_t = now_realtime_sec();
513 snprintfz(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
516 if(likely(pluginsd_root)) cd->next = pluginsd_root;
519 // it is not currently running
523 // spawn a new thread for it
524 if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0))
525 error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
527 else if(unlikely(pthread_detach(cd->thread) != 0))
528 error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
534 sleep((unsigned int) scan_frequency);
538 info("PLUGINS.D thread exiting");
540 static_thread->enabled = 0;