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 int 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
98 while(likely(j < max_words)) words[j++] = NULL;
104 void *pluginsd_worker_thread(void *arg)
106 struct plugind *cd = (struct plugind *)arg;
107 char line[PLUGINSD_LINE_MAX + 1];
109 #ifdef DETACH_PLUGINS_FROM_NETDATA
110 unsigned long long usec = 0, susec = 0;
111 struct timeval last = {0, 0} , now = {0, 0};
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");
128 if(unlikely(netdata_exit)) break;
130 FILE *fp = mypopen(cd->cmd, &cd->pid);
132 error("Cannot popen(\"%s\", \"r\").", cd->cmd);
136 info("PLUGINSD: '%s' running on pid %d", cd->fullfilename, cd->pid);
139 unsigned long long count = 0;
143 while(likely(fgets(line, PLUGINSD_LINE_MAX, fp) != NULL)) {
144 if(unlikely(netdata_exit)) break;
146 line[PLUGINSD_LINE_MAX] = '\0';
148 // debug(D_PLUGINSD, "PLUGINSD: %s: %s", cd->filename, line);
150 int w = pluginsd_split_words(line, words, MAX_WORDS);
152 if(unlikely(!s || !*s || !w)) {
153 // debug(D_PLUGINSD, "PLUGINSD: empty line");
157 // 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]);
159 hash = simple_hash(s);
161 if(likely(hash == SET_HASH && !strcmp(s, "SET"))) {
162 char *dimension = words[1];
163 char *value = words[2];
165 if(unlikely(!dimension || !*dimension)) {
166 error("PLUGINSD: '%s' is requesting a SET on chart '%s', without a dimension. Disabling it.", cd->fullfilename, st->id);
168 killpid(cd->pid, SIGTERM);
172 if(unlikely(!value || !*value)) value = NULL;
175 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value?value:"<nothing>");
177 killpid(cd->pid, SIGTERM);
181 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value?value:"<nothing>");
183 if(value) rrddim_set(st, dimension, atoll(value));
187 else if(likely(hash == BEGIN_HASH && !strcmp(s, "BEGIN"))) {
189 char *microseconds_txt = words[2];
192 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
194 killpid(cd->pid, SIGTERM);
198 st = rrdset_find(id);
200 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
202 killpid(cd->pid, SIGTERM);
206 if(likely(st->counter_done)) {
207 unsigned long long microseconds = 0;
208 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
209 if(microseconds) rrdset_next_usec(st, microseconds);
210 else rrdset_next_plugins(st);
213 else if(likely(hash == END_HASH && !strcmp(s, "END"))) {
215 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
217 killpid(cd->pid, SIGTERM);
221 if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
226 else if(likely(hash == FLUSH_HASH && !strcmp(s, "FLUSH"))) {
227 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
230 else if(likely(hash == CHART_HASH && !strcmp(s, "CHART"))) {
234 if((words[1]) != NULL && (words[2]) != NULL && strcmp(words[1], words[2]) == 0)
237 char *type = words[1];
240 id = strchr(type, '.');
241 if(likely(id)) { *id = '\0'; id++; }
243 char *name = words[2];
244 char *title = words[3];
245 char *units = words[4];
246 char *family = words[5];
247 char *category = words[6];
248 char *chart = words[7];
249 char *priority_s = words[8];
250 char *update_every_s = words[9];
252 if(unlikely(!type || !*type || !id || !*id)) {
253 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
255 killpid(cd->pid, SIGTERM);
260 if(likely(priority_s)) priority = atoi(priority_s);
262 int update_every = cd->update_every;
263 if(likely(update_every_s)) update_every = atoi(update_every_s);
264 if(unlikely(!update_every)) update_every = cd->update_every;
266 int chart_type = RRDSET_TYPE_LINE;
267 if(unlikely(chart)) chart_type = rrdset_type_id(chart);
269 if(unlikely(noname || !name || !*name || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0)) name = NULL;
270 if(unlikely(!family || !*family)) family = id;
271 if(unlikely(!category || !*category)) category = type;
273 st = rrdset_find_bytype(type, id);
275 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
279 , category?category:""
280 , rrdset_type_name(chart_type)
285 st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type);
286 cd->update_every = update_every;
288 if(unlikely(strcmp(category, "none") == 0)) st->isdetail = 1;
290 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
292 else if(likely(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION"))) {
294 char *name = words[2];
295 char *algorithm = words[3];
296 char *multiplier_s = words[4];
297 char *divisor_s = words[5];
298 char *options = words[6];
300 if(unlikely(!id || !*id)) {
301 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
303 killpid(cd->pid, SIGTERM);
308 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
310 killpid(cd->pid, SIGTERM);
315 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
316 if(unlikely(!multiplier)) multiplier = 1;
319 if(likely(divisor_s && *divisor_s)) divisor = atol(divisor_s);
320 if(unlikely(!divisor)) divisor = 1;
322 if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
324 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'"
328 , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
334 RRDDIM *rd = rrddim_find(st, id);
336 rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
337 rd->flags = 0x00000000;
338 if(options && *options) {
339 if(strstr(options, "hidden") != NULL) rd->flags |= RRDDIM_FLAG_HIDDEN;
340 if(strstr(options, "noreset") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
341 if(strstr(options, "nooverflow") != NULL) rd->flags |= RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS;
344 else if(unlikely(st->debug)) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
346 else if(unlikely(hash == DISABLE_HASH && !strcmp(s, "DISABLE"))) {
347 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
349 killpid(cd->pid, SIGTERM);
352 #ifdef DETACH_PLUGINS_FROM_NETDATA
353 else if(likely(hash == MYPID_HASH && !strcmp(s, "MYPID"))) {
354 char *pid_s = words[1];
355 pid_t pid = atol(pid_s);
357 if(likely(pid)) cd->pid = pid;
358 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
360 else if(likely(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE"))) {
361 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
363 gettimeofday(&now, NULL);
364 if(unlikely(!usec && !susec)) {
366 susec = cd->rrd_update_every * 1000000ULL;
370 usec = usecdiff(&now, &last) - susec;
371 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
372 if(unlikely(usec < (rrd_update_every * 1000000ULL / 2ULL))) susec = (rrd_update_every * 1000000ULL) - usec;
373 else susec = rrd_update_every * 1000000ULL / 2ULL;
376 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
378 killpid(cd->pid, SIGCONT);
379 bcopy(&now, &last, sizeof(struct timeval));
384 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
386 killpid(cd->pid, SIGTERM);
391 info("PLUGINSD: '%s' on pid %d stopped.", cd->fullfilename, cd->pid);
393 // fgets() failed or loop broke
394 int code = mypclose(fp, cd->pid);
395 if(code == 1 || code == 127) {
397 // 127 = cannot even run it
398 error("PLUGINSD: '%s' (pid %d) exited with code %d. Disabling it.", cd->fullfilename, cd->pid, code);
410 if(unlikely(!count && cd->enabled)) {
411 error("PLUGINSD: '%s' (pid %d) does not generate usefull output. Waiting a bit before starting it again.", cd->fullfilename, cd->pid);
412 sleep((unsigned int) (cd->update_every * 10));
416 if(likely(cd->enabled)) sleep((unsigned int) cd->update_every);
425 void *pluginsd_main(void *ptr)
429 info("PLUGINS.D thread created with task id %d", gettid());
431 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
432 error("Cannot set pthread cancel type to DEFERRED.");
434 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
435 error("Cannot set pthread cancel state to ENABLE.");
437 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
438 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 1);
439 int scan_frequency = (int) config_get_number("plugins", "check for new plugins every", 60);
441 struct dirent *file = NULL;
444 // enable the apps plugin by default
445 // config_get_boolean("plugins", "apps", 1);
447 if(scan_frequency < 1) scan_frequency = 1;
450 if(unlikely(netdata_exit)) break;
452 dir = opendir(dir_name);
454 error("Cannot open directory '%s'.", dir_name);
459 while(likely((file = readdir(dir)))) {
460 if(unlikely(netdata_exit)) break;
462 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
464 if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
466 int len = (int) strlen(file->d_name);
467 if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
468 if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
469 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
473 char pluginname[CONFIG_MAX_NAME + 1];
474 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
475 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
477 if(unlikely(!enabled)) {
478 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
482 // check if it runs already
483 for(cd = pluginsd_root ; likely(cd) ; cd = cd->next) {
484 if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
486 if(likely(cd && !cd->obsolete)) {
487 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
492 // allocate a new one, or use the obsolete one
494 cd = calloc(sizeof(struct plugind), 1);
495 if(unlikely(!cd)) fatal("Cannot allocate memory for plugin.");
497 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
499 strncpy(cd->filename, file->d_name, FILENAME_MAX);
500 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
502 cd->enabled = enabled;
503 cd->update_every = (int) config_get_number(cd->id, "update every", rrd_update_every);
504 cd->started_t = time(NULL);
507 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
510 if(likely(pluginsd_root)) cd->next = pluginsd_root;
515 if(unlikely(!cd->enabled)) continue;
517 // spawn a new thread for it
518 if(unlikely(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0)) {
519 error("PLUGINSD: failed to create new thread for plugin '%s'.", cd->filename);
522 else if(unlikely(pthread_detach(cd->thread) != 0))
523 error("PLUGINSD: Cannot request detach of newly created thread for plugin '%s'.", cd->filename);
527 sleep((unsigned int) scan_frequency);