14 #include "plugins_d.h"
16 struct plugind *pluginsd_root = NULL;
20 static inline int pluginsd_space(char c) {
34 static void pluginsd_split_words(char *str, char **words, int max_words) {
35 char *s = str, quote = 0;
38 // skip all white space
39 while(pluginsd_space(*s)) s++;
42 if(*s == '\'' || *s == '"') {
43 quote = *s; // remember the quote
44 s++; // skip the quote
47 // store the first word
50 // while we have something
53 if(*s == '\\' && s[1]) {
59 else if(*s == quote) {
66 else if(quote == 0 && pluginsd_space(*s)) {
71 // skip all white space
72 while(pluginsd_space(*s)) s++;
75 if(*s == '\'' || *s == '"') {
76 quote = *s; // remember the quote
77 s++; // skip the quote
80 // if we reached the end, stop
83 // store the next word
84 if(i < max_words) words[i++] = s;
92 // terminate the words
93 while(i < max_words) words[i++] = NULL;
97 void *pluginsd_worker_thread(void *arg)
99 struct plugind *cd = (struct plugind *)arg;
100 char line[PLUGINSD_LINE_MAX + 1];
102 #ifdef DETACH_PLUGINS_FROM_NETDATA
103 unsigned long long usec = 0, susec = 0;
104 struct timeval last = {0, 0} , now = {0, 0};
107 char *words[MAX_WORDS] = { NULL };
108 uint32_t SET_HASH = simple_hash("SET");
109 uint32_t BEGIN_HASH = simple_hash("BEGIN");
110 uint32_t END_HASH = simple_hash("END");
111 uint32_t FLUSH_HASH = simple_hash("FLUSH");
112 uint32_t CHART_HASH = simple_hash("CHART");
113 uint32_t DIMENSION_HASH = simple_hash("DIMENSION");
114 uint32_t DISABLE_HASH = simple_hash("DISABLE");
115 #ifdef DETACH_PLUGINS_FROM_NETDATA
116 uint32_t MYPID_HASH = simple_hash("MYPID");
117 uint32_t STOPPING_WAKE_ME_UP_PLEASE_HASH = simple_hash("STOPPING_WAKE_ME_UP_PLEASE");
121 FILE *fp = mypopen(cd->cmd, &cd->pid);
123 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
128 unsigned long long count = 0;
132 while(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL) {
133 line[PLUGINSD_LINE_MAX] = '\0';
135 // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
137 pluginsd_split_words(line, words, MAX_WORDS);
140 // debug(D_PLUGINSD, "PLUGINSD: empty line");
144 // 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]);
146 hash = simple_hash(s);
148 if(hash == SET_HASH && !strcmp(s, "SET")) {
149 char *dimension = words[1];
150 char *value = words[2];
152 if(!dimension || !*dimension) {
153 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>");
155 // test: killing an exited child kills us - lets check it exists before killing it
156 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
160 if(!value || !*value) value = "0";
163 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value);
165 // test: killing an exited child kills us - lets check it exists before killing it
166 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
170 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
171 rrddim_set(st, dimension, atoll(value));
175 else if(hash == BEGIN_HASH && !strcmp(s, "BEGIN")) {
177 char *microseconds_txt = words[2];
180 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
182 // test: killing an exited child kills us - lets check it exists before killing it
183 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
187 st = rrdset_find(id);
189 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
191 // test: killing an exited child kills us - lets check it exists before killing it
192 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
196 if(st->counter_done) {
197 unsigned long long microseconds = 0;
198 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
199 if(microseconds) rrdset_next_usec(st, microseconds);
200 else rrdset_next_plugins(st);
203 else if(hash == END_HASH && !strcmp(s, "END")) {
205 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
207 // test: killing an exited child kills us - lets check it exists before killing it
208 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
212 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
217 else if(hash == FLUSH_HASH && !strcmp(s, "FLUSH")) {
218 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
221 else if(hash == CHART_HASH && !strcmp(s, "CHART")) {
224 char *type = words[1];
227 id = strchr(type, '.');
228 if(id) { *id = '\0'; id++; }
230 char *name = words[2];
231 char *title = words[3];
232 char *units = words[4];
233 char *family = words[5];
234 char *category = words[6];
235 char *chart = words[7];
236 char *priority_s = words[8];
237 char *update_every_s = words[9];
239 if(!type || !*type || !id || !*id) {
240 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
242 // test: killing an exited child kills us - lets check it exists before killing it
243 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
248 if(priority_s) priority = atoi(priority_s);
250 int update_every = cd->update_every;
251 if(update_every_s) update_every = atoi(update_every_s);
252 if(!update_every) update_every = cd->update_every;
254 int chart_type = RRDSET_TYPE_LINE;
255 if(chart) chart_type = rrdset_type_id(chart);
257 if(!name || !*name) name = NULL;
258 if(!family || !*family) family = id;
259 if(!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(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(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];
289 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
291 // test: killing an exited child kills us - lets check it exists before killing it
292 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
297 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
299 // test: killing an exited child kills us - lets check it exists before killing it
300 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
305 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
306 if(!multiplier) multiplier = 1;
309 if(divisor_s && *divisor_s) divisor = atol(divisor_s);
310 if(!divisor) divisor = 1;
312 if(!algorithm || !*algorithm) algorithm = "absolute";
314 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
318 , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
324 RRDDIM *rd = rrddim_find(st, id);
326 rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
327 if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
329 else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
331 else if(hash == DISABLE_HASH && !strcmp(s, "DISABLE")) {
332 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
334 // test: killing an exited child kills us - lets check it exists before killing it
335 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
338 #ifdef DETACH_PLUGINS_FROM_NETDATA
339 else if(hash == MYPID_HASH && !strcmp(s, "MYPID")) {
340 char *pid_s = words[1];
341 pid_t pid = atol(pid_s);
343 if(pid) cd->pid = pid;
344 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
346 else if(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE")) {
347 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
349 gettimeofday(&now, NULL);
350 if(!usec && !susec) {
352 susec = cd->rrd_update_every * 1000000ULL;
356 usec = usecdiff(&now, &last) - susec;
357 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
358 if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec;
359 else susec = rrd_update_every * 1000000ULL / 2ULL;
362 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
364 // test: killing an exited child kills us - lets check it exists before killing it
365 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGCONT);
366 bcopy(&now, &last, sizeof(struct timeval));
371 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
373 // test: killing an exited child kills us - lets check it exists before killing it
374 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
379 // fgets() failed or loop broke
380 mypclose(fp, cd->pid);
383 if(!count && cd->enabled) {
384 error("PLUGINSD: '%s' does not generate usefull output. Disabling it.", cd->fullfilename);
386 // test: killing an exited child kills us - lets check it exists before killing it
387 if(kill(cd->pid, 0) != -1) kill(cd->pid, SIGTERM);
390 if(cd->enabled) sleep(cd->update_every);
398 void *pluginsd_main(void *ptr)
402 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
403 error("Cannot set pthread cancel type to DEFERRED.");
405 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
406 error("Cannot set pthread cancel state to ENABLE.");
408 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
409 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
410 int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
412 struct dirent *file = NULL;
415 // enable the apps plugin by default
416 config_get_boolean("plugins", "apps", 1);
418 if(scan_frequency < 1) scan_frequency = 1;
421 dir = opendir(dir_name);
423 error("Cannot open directory '%s'.", dir_name);
427 while((file = readdir(dir))) {
428 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
430 if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue;
432 int len = strlen(file->d_name);
433 if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue;
434 if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0) {
435 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
439 char pluginname[CONFIG_MAX_NAME + 1];
440 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
441 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
444 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
448 // check if it runs already
449 for(cd = pluginsd_root ; cd ; cd = cd->next) {
450 if(strcmp(cd->filename, file->d_name) == 0) break;
452 if(cd && !cd->obsolete) {
453 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
458 // allocate a new one, or use the obsolete one
460 cd = calloc(sizeof(struct plugind), 1);
461 if(!cd) fatal("Cannot allocate memory for plugin.");
463 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
465 strncpy(cd->filename, file->d_name, FILENAME_MAX);
466 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
468 cd->enabled = enabled;
469 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
470 cd->started_t = time(NULL);
473 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
476 if(pluginsd_root) cd->next = pluginsd_root;
481 if(!cd->enabled) continue;
483 // spawn a new thread for it
484 if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) {
485 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
488 else if(pthread_detach(cd->thread) != 0)
489 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
493 sleep(scan_frequency);