14 #include "appconfig.h"
18 #include "plugins_d.h"
20 struct plugind *pluginsd_root = NULL;
24 static inline int pluginsd_space(char c) {
38 static void pluginsd_split_words(char *str, char **words, int max_words) {
39 char *s = str, quote = 0;
42 // skip all white space
43 while(unlikely(pluginsd_space(*s))) s++;
46 if(unlikely(*s == '\'' || *s == '"')) {
47 quote = *s; // remember the quote
48 s++; // skip the quote
51 // store the first word
54 // while we have something
57 if(unlikely(*s == '\\' && s[1])) {
63 else if(unlikely(*s == quote)) {
70 else if(unlikely(quote == 0 && pluginsd_space(*s))) {
75 // skip all white space
76 while(likely(pluginsd_space(*s))) s++;
79 if(unlikely(*s == '\'' || *s == '"')) {
80 quote = *s; // remember the quote
81 s++; // skip the quote
84 // if we reached the end, stop
85 if(unlikely(!*s)) break;
87 // store the next word
88 if(likely(i < max_words)) words[i++] = s;
96 // terminate the words
97 while(likely(i < max_words)) words[i++] = NULL;
101 void *pluginsd_worker_thread(void *arg)
103 struct plugind *cd = (struct plugind *)arg;
104 char line[PLUGINSD_LINE_MAX + 1];
106 #ifdef DETACH_PLUGINS_FROM_NETDATA
107 unsigned long long usec = 0, susec = 0;
108 struct timeval last = {0, 0} , now = {0, 0};
111 char *words[MAX_WORDS] = { NULL };
112 uint32_t SET_HASH = simple_hash("SET");
113 uint32_t BEGIN_HASH = simple_hash("BEGIN");
114 uint32_t END_HASH = simple_hash("END");
115 uint32_t FLUSH_HASH = simple_hash("FLUSH");
116 uint32_t CHART_HASH = simple_hash("CHART");
117 uint32_t DIMENSION_HASH = simple_hash("DIMENSION");
118 uint32_t DISABLE_HASH = simple_hash("DISABLE");
119 #ifdef DETACH_PLUGINS_FROM_NETDATA
120 uint32_t MYPID_HASH = simple_hash("MYPID");
121 uint32_t STOPPING_WAKE_ME_UP_PLEASE_HASH = simple_hash("STOPPING_WAKE_ME_UP_PLEASE");
125 FILE *fp = mypopen(cd->cmd, &cd->pid);
127 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
131 info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
134 unsigned long long count = 0;
138 while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
139 line[PLUGINSD_LINE_MAX] = '\0';
141 // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
143 pluginsd_split_words(line, words, MAX_WORDS);
145 if(unlikely(!s || !*s)) {
146 // debug(D_PLUGINSD, "PLUGINSD: empty line");
150 // 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]);
152 hash = simple_hash(s);
154 if(likely(hash == SET_HASH && !strcmp(s, "SET"))) {
155 char *dimension = words[1];
156 char *value = words[2];
158 if(unlikely(!dimension || !*dimension)) {
159 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>");
161 killpid(cd->pid, SIGTERM);
165 if(unlikely(!value || !*value)) value = "0";
168 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value);
170 killpid(cd->pid, SIGTERM);
174 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
175 rrddim_set(st, dimension, atoll(value));
179 else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
181 char *microseconds_txt = words[2];
184 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
186 killpid(cd->pid, SIGTERM);
190 st = rrdset_find(id);
192 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
194 killpid(cd->pid, SIGTERM);
198 if(likely(st->counter_done)) {
199 unsigned long long microseconds = 0;
200 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
201 if(microseconds) rrdset_next_usec(st, microseconds);
202 else rrdset_next_plugins(st);
205 else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
207 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
209 killpid(cd->pid, SIGTERM);
213 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
218 else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
219 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
222 else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
225 char *type = words[1];
228 id = strchr(type, '.');
229 if(likely(id)) { *id = '\0'; id++; }
231 char *name = words[2];
232 char *title = words[3];
233 char *units = words[4];
234 char *family = words[5];
235 char *category = words[6];
236 char *chart = words[7];
237 char *priority_s = words[8];
238 char *update_every_s = words[9];
240 if(unlikely(!type || !*type || !id || !*id)) {
241 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
243 killpid(cd->pid, SIGTERM);
248 if(likely(priority_s)) priority = atoi(priority_s);
250 int update_every = cd->update_every;
251 if(likely(update_every_s)) update_every = atoi(update_every_s);
252 if(unlikely(!update_every)) update_every = cd->update_every;
254 int chart_type = RRDSET_TYPE_LINE;
255 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
257 if(unlikely(!name || !*name)) name = NULL;
258 if(unlikely(!family || !*family)) family = id;
259 if(unlikely(!category || !*category)) category = type;
261 st = rrdset_find_bytype(type, id);
263 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
267 , category?category:""
268 , rrdset_type_name(chart_type)
273 st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type);
274 cd->update_every = update_every;
276 if(unlikely(strcmp(category, "none") == 0)) st->isdetail = 1;
278 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
280 else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
282 char *name = words[2];
283 char *algorithm = words[3];
284 char *multiplier_s = words[4];
285 char *divisor_s = words[5];
286 char *hidden = words[6];
288 if(unlikely(!id || !*id)) {
289 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
291 killpid(cd->pid, SIGTERM);
296 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
298 killpid(cd->pid, SIGTERM);
303 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
304 if(unlikely(!multiplier)) multiplier = 1;
307 if(likely(divisor_s && *divisor_s)) divisor = atol(divisor_s);
308 if(unlikely(!divisor)) divisor = 1;
310 if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
312 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'"
316 , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
322 RRDDIM *rd = rrddim_find(st, id);
324 rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
325 if(unlikely(hidden && strcmp(hidden, "hidden") == 0)) rd->hidden = 1;
327 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
329 else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
330 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
332 killpid(cd->pid, SIGTERM);
335 #ifdef DETACH_PLUGINS_FROM_NETDATA
336 else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) {
337 char *pid_s = words[1];
338 pid_t pid = atol(pid_s);
340 if(likely(pid)) cd->pid = pid;
341 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
343 else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) {
344 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
346 gettimeofday(&now, NULL);
347 if(unlikely(!usec && !susec)) {
349 susec = cd->rrd_update_every * 1000000ULL;
353 usec = usecdiff(&now, &last) - susec;
354 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
355 if(unlikely(usec < (rrd_update_every * 1000000ULL / 2ULL))) susec = (rrd_update_every * 1000000ULL) - usec;
356 else susec = rrd_update_every * 1000000ULL / 2ULL;
359 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
361 killpid(cd->pid, SIGCONT);
362 bcopy(&now, &last, sizeof(struct timeval));
367 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
369 killpid(cd->pid, SIGTERM);
374 info("PLUGINSD: '%s' on pid %d stopped.", cd->fullfilename, cd->pid);
376 // fgets() failed or loop broke
377 mypclose(fp, cd->pid);
379 if(unlikely(!count && cd->enabled)) {
380 error("PLUGINSD: '%s' (pid %d) does not generate usefull output. Waiting a bit before starting it again.", cd->fullfilename, cd->pid);
381 sleep(cd->update_every * 10);
386 if(likely(cd->enabled)) sleep(cd->update_every);
394 void *pluginsd_main(void *ptr)
398 info("PLUGINS.D thread created with task id %d", gettid());
400 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
401 error("Cannot set pthread cancel type to DEFERRED.");
403 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
404 error("Cannot set pthread cancel state to ENABLE.");
406 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
407 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
408 int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
410 struct dirent *file = NULL;
413 // enable the apps plugin by default
414 config_get_boolean("plugins", "apps", 1);
416 if(scan_frequency < 1) scan_frequency = 1;
419 dir = opendir(dir_name);
421 error("Cannot open directory '%s'.", dir_name);
425 while(likely((file = readdir(dir)))) {
426 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
428 if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
430 int len = strlen(file->d_name);
431 if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
432 if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
433 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
437 char pluginname[CONFIG_MAX_NAME + 1];
438 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
439 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
441 if(unlikely(!enabled)) {
442 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
446 // check if it runs already
447 for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) {
448 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
450 if(likely(cd && !cd->obsolete)) {
451 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
456 // allocate a new one, or use the obsolete one
458 cd = calloc(sizeof(struct plugind), 1);
459 if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
461 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
463 strncpy(cd->filename, file->d_name, FILENAME_MAX);
464 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
466 cd->enabled = enabled;
467 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
468 cd->started_t = time(NULL);
471 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
474 if(likely(pluginsd_root)) cd->next = pluginsd_root;
479 if(unlikely(!cd->enabled)) continue;
481 // spawn a new thread for it
482 if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) {
483 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
486 else if(unlikely(pthread_detach(cd->thread) != 0))
487 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
491 sleep(scan_frequency);