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 kill(cd->pid, SIGTERM);
159 if(!value || !*value) value = "0";
162 error("PLUGINSD: '%s' is requesting a SET on dimension %s with value %s, without a BEGIN. Disabling it.", cd->fullfilename, dimension, value);
164 kill(cd->pid, SIGTERM);
168 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is setting dimension %s/%s to %s", cd->fullfilename, st->id, dimension, value);
169 rrddim_set(st, dimension, atoll(value));
173 else if(hash == BEGIN_HASH && !strcmp(s, "BEGIN")) {
175 char *microseconds_txt = words[2];
178 error("PLUGINSD: '%s' is requesting a BEGIN without a chart id. Disabling it.", cd->fullfilename);
180 kill(cd->pid, SIGTERM);
184 st = rrdset_find(id);
186 error("PLUGINSD: '%s' is requesting a BEGIN on chart '%s', which does not exist. Disabling it.", cd->fullfilename, id);
188 kill(cd->pid, SIGTERM);
192 if(st->counter_done) {
193 unsigned long long microseconds = 0;
194 if(microseconds_txt && *microseconds_txt) microseconds = strtoull(microseconds_txt, NULL, 10);
195 if(microseconds) rrdset_next_usec(st, microseconds);
196 else rrdset_next_plugins(st);
199 else if(hash == END_HASH && !strcmp(s, "END")) {
201 error("PLUGINSD: '%s' is requesting an END, without a BEGIN. Disabling it.", cd->fullfilename);
203 kill(cd->pid, SIGTERM);
207 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a END on chart %s", cd->fullfilename, st->id);
212 else if(hash == FLUSH_HASH && !strcmp(s, "FLUSH")) {
213 debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
216 else if(hash == CHART_HASH && !strcmp(s, "CHART")) {
219 char *type = words[1];
222 id = strchr(type, '.');
223 if(id) { *id = '\0'; id++; }
225 char *name = words[2];
226 char *title = words[3];
227 char *units = words[4];
228 char *family = words[5];
229 char *category = words[6];
230 char *chart = words[7];
231 char *priority_s = words[8];
232 char *update_every_s = words[9];
234 if(!type || !*type || !id || !*id) {
235 error("PLUGINSD: '%s' is requesting a CHART, without a type.id. Disabling it.", cd->fullfilename);
237 kill(cd->pid, SIGTERM);
242 if(priority_s) priority = atoi(priority_s);
244 int update_every = cd->update_every;
245 if(update_every_s) update_every = atoi(update_every_s);
246 if(!update_every) update_every = cd->update_every;
248 int chart_type = RRDSET_TYPE_LINE;
249 if(chart) chart_type = rrdset_type_id(chart);
251 if(!name || !*name) name = NULL;
252 if(!family || !*family) family = id;
253 if(!category || !*category) category = type;
255 st = rrdset_find_bytype(type, id);
257 debug(D_PLUGINSD, "PLUGINSD: Creating chart type='%s', id='%s', name='%s', family='%s', category='%s', chart='%s', priority=%d, update_every=%d"
261 , category?category:""
262 , rrdset_type_name(chart_type)
267 st = rrdset_create(type, id, name, family, title, units, priority, update_every, chart_type);
268 cd->update_every = update_every;
270 if(strcmp(category, "none") == 0) st->isdetail = 1;
272 else debug(D_PLUGINSD, "PLUGINSD: Chart '%s' already exists. Not adding it again.", st->id);
274 else if(hash == DIMENSION_HASH && !strcmp(s, "DIMENSION")) {
276 char *name = words[2];
277 char *algorithm = words[3];
278 char *multiplier_s = words[4];
279 char *divisor_s = words[5];
280 char *hidden = words[6];
283 error("PLUGINSD: '%s' is requesting a DIMENSION, without an id. Disabling it.", cd->fullfilename);
285 kill(cd->pid, SIGTERM);
290 error("PLUGINSD: '%s' is requesting a DIMENSION, without a CHART. Disabling it.", cd->fullfilename);
292 kill(cd->pid, SIGTERM);
297 if(multiplier_s && *multiplier_s) multiplier = atol(multiplier_s);
298 if(!multiplier) multiplier = 1;
301 if(divisor_s && *divisor_s) divisor = atol(divisor_s);
302 if(!divisor) divisor = 1;
304 if(!algorithm || !*algorithm) algorithm = "absolute";
306 if(st->debug) debug(D_PLUGINSD, "PLUGINSD: Creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
310 , rrddim_algorithm_name(rrddim_algorithm_id(algorithm))
316 RRDDIM *rd = rrddim_find(st, id);
318 rd = rrddim_add(st, id, name, multiplier, divisor, rrddim_algorithm_id(algorithm));
319 if(hidden && strcmp(hidden, "hidden") == 0) rd->hidden = 1;
321 else if(st->debug) debug(D_PLUGINSD, "PLUGINSD: dimension %s/%s already exists. Not adding it again.", st->id, id);
323 else if(hash == DISABLE_HASH && !strcmp(s, "DISABLE")) {
324 error("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
326 kill(cd->pid, SIGTERM);
329 #ifdef DETACH_PLUGINS_FROM_NETDATA
330 else if(hash == MYPID_HASH && !strcmp(s, "MYPID")) {
331 char *pid_s = words[1];
332 pid_t pid = atol(pid_s);
334 if(pid) cd->pid = pid;
335 debug(D_PLUGINSD, "PLUGINSD: %s is on pid %d", cd->id, cd->pid);
337 else if(hash == STOPPING_WAKE_ME_UP_PLEASE_HASH && !strcmp(s, "STOPPING_WAKE_ME_UP_PLEASE")) {
338 error("PLUGINSD: '%s' (pid %d) called STOPPING_WAKE_ME_UP_PLEASE.", cd->fullfilename, cd->pid);
340 gettimeofday(&now, NULL);
341 if(!usec && !susec) {
343 susec = cd->rrd_update_every * 1000000ULL;
347 usec = usecdiff(&now, &last) - susec;
348 error("PLUGINSD: %s last loop took %llu usec (worked for %llu, sleeped for %llu).\n", cd->fullfilename, usec + susec, usec, susec);
349 if(usec < (rrd_update_every * 1000000ULL / 2ULL)) susec = (rrd_update_every * 1000000ULL) - usec;
350 else susec = rrd_update_every * 1000000ULL / 2ULL;
353 error("PLUGINSD: %s sleeping for %llu. Will kill with SIGCONT pid %d to wake it up.\n", cd->fullfilename, susec, cd->pid);
355 kill(cd->pid, SIGCONT);
356 bcopy(&now, &last, sizeof(struct timeval));
361 error("PLUGINSD: '%s' is sending command '%s' which is not known by netdata. Disabling it.", cd->fullfilename, s);
363 kill(cd->pid, SIGTERM);
368 // fgets() failed or loop broke
369 mypclose(fp, cd->pid);
372 if(!count && cd->enabled) {
373 error("PLUGINSD: '%s' does not generate usefull output. Disabling it.", cd->fullfilename);
375 kill(cd->pid, SIGTERM);
378 if(cd->enabled) sleep(cd->update_every);
386 void *pluginsd_main(void *ptr)
390 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
391 error("Cannot set pthread cancel type to DEFERRED.");
393 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
394 error("Cannot set pthread cancel state to ENABLE.");
396 char *dir_name = config_get("plugins", "plugins directory", PLUGINS_DIR);
397 int automatic_run = config_get_boolean("plugins", "enable running new plugins", 0);
398 int scan_frequency = config_get_number("plugins", "check for new plugins every", 60);
400 struct dirent *file = NULL;
403 // enable the apps plugin by default
404 config_get_boolean("plugins", "apps", 1);
406 if(scan_frequency < 1) scan_frequency = 1;
409 dir = opendir(dir_name);
411 error("Cannot open directory '%s'.", dir_name);
415 while((file = readdir(dir))) {
416 debug(D_PLUGINSD, "PLUGINSD: Examining file '%s'", file->d_name);
418 if(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0) continue;
420 int len = strlen(file->d_name);
421 if(len <= (int)PLUGINSD_FILE_SUFFIX_LEN) continue;
422 if(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0) {
423 debug(D_PLUGINSD, "PLUGINSD: File '%s' does not end in '%s'.", file->d_name, PLUGINSD_FILE_SUFFIX);
427 char pluginname[CONFIG_MAX_NAME + 1];
428 snprintf(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
429 int enabled = config_get_boolean("plugins", pluginname, automatic_run);
432 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is not enabled", file->d_name);
436 // check if it runs already
437 for(cd = pluginsd_root ; cd ; cd = cd->next) {
438 if(strcmp(cd->filename, file->d_name) == 0) break;
440 if(cd && !cd->obsolete) {
441 debug(D_PLUGINSD, "PLUGINSD: plugin '%s' is already running", cd->filename);
446 // allocate a new one, or use the obsolete one
448 cd = calloc(sizeof(struct plugind), 1);
449 if(!cd) fatal("Cannot allocate memory for plugin.");
451 snprintf(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
453 strncpy(cd->filename, file->d_name, FILENAME_MAX);
454 snprintf(cd->fullfilename, FILENAME_MAX, "%s/%s", dir_name, cd->filename);
456 cd->enabled = enabled;
457 cd->update_every = config_get_number(cd->id, "update every", rrd_update_every);
458 cd->started_t = time(NULL);
461 snprintf(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
464 if(pluginsd_root) cd->next = pluginsd_root;
469 if(!cd->enabled) continue;
471 // spawn a new thread for it
472 if(pthread_create(&cd->thread, NULL, pluginsd_worker_thread, cd) != 0) {
473 error("CHARTS.D: failed to create new thread for chart.d %s.", cd->filename);
476 else if(pthread_detach(cd->thread) != 0)
477 error("CHARTS.D: Cannot request detach of newly created thread for chart.d %s.", cd->filename);
481 sleep(scan_frequency);