plugins_d.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugins_d.h"
  3. char *plugin_directories[PLUGINSD_MAX_DIRECTORIES] = { NULL };
  4. struct plugind *pluginsd_root = NULL;
  5. static inline int pluginsd_space(char c) {
  6. switch(c) {
  7. case ' ':
  8. case '\t':
  9. case '\r':
  10. case '\n':
  11. case '=':
  12. return 1;
  13. default:
  14. return 0;
  15. }
  16. }
  17. inline int config_isspace(char c) {
  18. switch(c) {
  19. case ' ':
  20. case '\t':
  21. case '\r':
  22. case '\n':
  23. case ',':
  24. return 1;
  25. default:
  26. return 0;
  27. }
  28. }
  29. // split a text into words, respecting quotes
  30. static inline int quoted_strings_splitter(char *str, char **words, int max_words, int (*custom_isspace)(char)) {
  31. char *s = str, quote = 0;
  32. int i = 0, j;
  33. // skip all white space
  34. while(unlikely(custom_isspace(*s))) s++;
  35. // check for quote
  36. if(unlikely(*s == '\'' || *s == '"')) {
  37. quote = *s; // remember the quote
  38. s++; // skip the quote
  39. }
  40. // store the first word
  41. words[i++] = s;
  42. // while we have something
  43. while(likely(*s)) {
  44. // if it is escape
  45. if(unlikely(*s == '\\' && s[1])) {
  46. s += 2;
  47. continue;
  48. }
  49. // if it is quote
  50. else if(unlikely(*s == quote)) {
  51. quote = 0;
  52. *s = ' ';
  53. continue;
  54. }
  55. // if it is a space
  56. else if(unlikely(quote == 0 && custom_isspace(*s))) {
  57. // terminate the word
  58. *s++ = '\0';
  59. // skip all white space
  60. while(likely(custom_isspace(*s))) s++;
  61. // check for quote
  62. if(unlikely(*s == '\'' || *s == '"')) {
  63. quote = *s; // remember the quote
  64. s++; // skip the quote
  65. }
  66. // if we reached the end, stop
  67. if(unlikely(!*s)) break;
  68. // store the next word
  69. if(likely(i < max_words)) words[i++] = s;
  70. else break;
  71. }
  72. // anything else
  73. else s++;
  74. }
  75. // terminate the words
  76. j = i;
  77. while(likely(j < max_words)) words[j++] = NULL;
  78. return i;
  79. }
  80. inline int pluginsd_initialize_plugin_directories() {
  81. char plugins_dirs[(FILENAME_MAX * 2) + 1];
  82. static char *plugins_dir_list = NULL;
  83. // Get the configuration entry
  84. if(likely(!plugins_dir_list)) {
  85. snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
  86. plugins_dir_list = strdupz(config_get(CONFIG_SECTION_GLOBAL, "plugins directory", plugins_dirs));
  87. }
  88. // Parse it and store it to plugin directories
  89. return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace);
  90. }
  91. inline int pluginsd_split_words(char *str, char **words, int max_words) {
  92. return quoted_strings_splitter(str, words, max_words, pluginsd_space);
  93. }
  94. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int trust_durations) {
  95. int enabled = cd->enabled;
  96. if(!fp || !enabled) {
  97. cd->enabled = 0;
  98. return 0;
  99. }
  100. size_t count = 0;
  101. char line[PLUGINSD_LINE_MAX + 1];
  102. char *words[PLUGINSD_MAX_WORDS] = { NULL };
  103. uint32_t BEGIN_HASH = simple_hash(PLUGINSD_KEYWORD_BEGIN);
  104. uint32_t END_HASH = simple_hash(PLUGINSD_KEYWORD_END);
  105. uint32_t FLUSH_HASH = simple_hash(PLUGINSD_KEYWORD_FLUSH);
  106. uint32_t CHART_HASH = simple_hash(PLUGINSD_KEYWORD_CHART);
  107. uint32_t DIMENSION_HASH = simple_hash(PLUGINSD_KEYWORD_DIMENSION);
  108. uint32_t DISABLE_HASH = simple_hash(PLUGINSD_KEYWORD_DISABLE);
  109. uint32_t VARIABLE_HASH = simple_hash(PLUGINSD_KEYWORD_VARIABLE);
  110. RRDSET *st = NULL;
  111. uint32_t hash;
  112. errno = 0;
  113. clearerr(fp);
  114. if(unlikely(fileno(fp) == -1)) {
  115. error("file descriptor given is not a valid stream");
  116. goto cleanup;
  117. }
  118. while(!ferror(fp)) {
  119. if(unlikely(netdata_exit)) break;
  120. char *r = fgets(line, PLUGINSD_LINE_MAX, fp);
  121. if(unlikely(!r)) {
  122. error("read failed");
  123. break;
  124. }
  125. if(unlikely(netdata_exit)) break;
  126. line[PLUGINSD_LINE_MAX] = '\0';
  127. int w = pluginsd_split_words(line, words, PLUGINSD_MAX_WORDS);
  128. char *s = words[0];
  129. if(unlikely(!s || !*s || !w)) {
  130. continue;
  131. }
  132. // 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]);
  133. if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
  134. char *dimension = words[1];
  135. char *value = words[2];
  136. if(unlikely(!dimension || !*dimension)) {
  137. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", st->id, host->hostname);
  138. enabled = 0;
  139. break;
  140. }
  141. if(unlikely(!value || !*value)) value = NULL;
  142. if(unlikely(!st)) {
  143. error("requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension, value?value:"<nothing>", host->hostname);
  144. enabled = 0;
  145. break;
  146. }
  147. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  148. debug(D_PLUGINSD, "is setting dimension %s/%s to %s", st->id, dimension, value?value:"<nothing>");
  149. if(value) {
  150. RRDDIM *rd = rrddim_find(st, dimension);
  151. if(unlikely(!rd)) {
  152. error("requested a SET to dimension with id '%s' on stats '%s' (%s) on host '%s', which does not exist. Disabling it.", dimension, st->name, st->id, st->rrdhost->hostname);
  153. enabled = 0;
  154. break;
  155. }
  156. else
  157. rrddim_set_by_pointer(st, rd, strtoll(value, NULL, 0));
  158. }
  159. }
  160. else if(likely(hash == BEGIN_HASH && !strcmp(s, PLUGINSD_KEYWORD_BEGIN))) {
  161. char *id = words[1];
  162. char *microseconds_txt = words[2];
  163. if(unlikely(!id)) {
  164. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", host->hostname);
  165. enabled = 0;
  166. break;
  167. }
  168. st = rrdset_find(host, id);
  169. if(unlikely(!st)) {
  170. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, host->hostname);
  171. enabled = 0;
  172. break;
  173. }
  174. if(likely(st->counter_done)) {
  175. usec_t microseconds = 0;
  176. if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
  177. if(likely(microseconds)) {
  178. if(trust_durations)
  179. rrdset_next_usec_unfiltered(st, microseconds);
  180. else
  181. rrdset_next_usec(st, microseconds);
  182. }
  183. else rrdset_next(st);
  184. }
  185. }
  186. else if(likely(hash == END_HASH && !strcmp(s, PLUGINSD_KEYWORD_END))) {
  187. if(unlikely(!st)) {
  188. error("requested an END, without a BEGIN on host '%s'. Disabling it.", host->hostname);
  189. enabled = 0;
  190. break;
  191. }
  192. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  193. debug(D_PLUGINSD, "requested an END on chart %s", st->id);
  194. rrdset_done(st);
  195. st = NULL;
  196. count++;
  197. }
  198. else if(likely(hash == CHART_HASH && !strcmp(s, PLUGINSD_KEYWORD_CHART))) {
  199. st = NULL;
  200. char *type = words[1];
  201. char *name = words[2];
  202. char *title = words[3];
  203. char *units = words[4];
  204. char *family = words[5];
  205. char *context = words[6];
  206. char *chart = words[7];
  207. char *priority_s = words[8];
  208. char *update_every_s = words[9];
  209. char *options = words[10];
  210. char *plugin = words[11];
  211. char *module = words[12];
  212. // parse the id from type
  213. char *id = NULL;
  214. if(likely(type && (id = strchr(type, '.')))) {
  215. *id = '\0';
  216. id++;
  217. }
  218. // make sure we have the required variables
  219. if(unlikely(!type || !*type || !id || !*id)) {
  220. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", host->hostname);
  221. enabled = 0;
  222. break;
  223. }
  224. // parse the name, and make sure it does not include 'type.'
  225. if(unlikely(name && *name)) {
  226. // when data are coming from slaves
  227. // name will be type.name
  228. // so we have to remove 'type.' from name too
  229. size_t len = strlen(type);
  230. if(strncmp(type, name, len) == 0 && name[len] == '.')
  231. name = &name[len + 1];
  232. // if the name is the same with the id,
  233. // or is just 'NULL', clear it.
  234. if(unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  235. name = NULL;
  236. }
  237. int priority = 1000;
  238. if(likely(priority_s && *priority_s)) priority = str2i(priority_s);
  239. int update_every = cd->update_every;
  240. if(likely(update_every_s && *update_every_s)) update_every = str2i(update_every_s);
  241. if(unlikely(!update_every)) update_every = cd->update_every;
  242. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  243. if(unlikely(chart)) chart_type = rrdset_type_id(chart);
  244. if(unlikely(name && !*name)) name = NULL;
  245. if(unlikely(family && !*family)) family = NULL;
  246. if(unlikely(context && !*context)) context = NULL;
  247. if(unlikely(!title)) title = "";
  248. if(unlikely(!units)) units = "unknown";
  249. debug(D_PLUGINSD, "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
  250. , type, id
  251. , name?name:""
  252. , family?family:""
  253. , context?context:""
  254. , rrdset_type_name(chart_type)
  255. , priority
  256. , update_every
  257. );
  258. st = rrdset_create(
  259. host
  260. , type
  261. , id
  262. , name
  263. , family
  264. , context
  265. , title
  266. , units
  267. , (plugin && *plugin)?plugin:cd->filename
  268. , module
  269. , priority
  270. , update_every
  271. , chart_type
  272. );
  273. if(options && *options) {
  274. if(strstr(options, "obsolete"))
  275. rrdset_is_obsolete(st);
  276. else
  277. rrdset_isnot_obsolete(st);
  278. if(strstr(options, "detail"))
  279. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  280. else
  281. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  282. if(strstr(options, "hidden"))
  283. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  284. else
  285. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  286. if(strstr(options, "store_first"))
  287. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  288. else
  289. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  290. }
  291. else {
  292. rrdset_isnot_obsolete(st);
  293. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  294. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  295. }
  296. }
  297. else if(likely(hash == DIMENSION_HASH && !strcmp(s, PLUGINSD_KEYWORD_DIMENSION))) {
  298. char *id = words[1];
  299. char *name = words[2];
  300. char *algorithm = words[3];
  301. char *multiplier_s = words[4];
  302. char *divisor_s = words[5];
  303. char *options = words[6];
  304. if(unlikely(!id || !*id)) {
  305. error("requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", host->hostname, st?st->id:"UNSET");
  306. enabled = 0;
  307. break;
  308. }
  309. if(unlikely(!st)) {
  310. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", host->hostname);
  311. enabled = 0;
  312. break;
  313. }
  314. long multiplier = 1;
  315. if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
  316. if(unlikely(!multiplier)) multiplier = 1;
  317. long divisor = 1;
  318. if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
  319. if(unlikely(!divisor)) divisor = 1;
  320. if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
  321. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  322. debug(D_PLUGINSD, "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
  323. , st->id
  324. , id
  325. , name?name:""
  326. , rrd_algorithm_name(rrd_algorithm_id(algorithm))
  327. , multiplier
  328. , divisor
  329. , options?options:""
  330. );
  331. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
  332. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  333. rrddim_flag_clear(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  334. if(options && *options) {
  335. if(strstr(options, "obsolete") != NULL)
  336. rrddim_is_obsolete(st, rd);
  337. else
  338. rrddim_isnot_obsolete(st, rd);
  339. if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  340. if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  341. if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  342. }
  343. else {
  344. rrddim_isnot_obsolete(st, rd);
  345. }
  346. }
  347. else if(likely(hash == VARIABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_VARIABLE))) {
  348. char *name = words[1];
  349. char *value = words[2];
  350. int global = (st)?0:1;
  351. if(name && *name) {
  352. if((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  353. global = 1;
  354. name = words[2];
  355. value = words[3];
  356. }
  357. else if((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  358. global = 0;
  359. name = words[2];
  360. value = words[3];
  361. }
  362. }
  363. if(unlikely(!name || !*name)) {
  364. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", host->hostname);
  365. enabled = 0;
  366. break;
  367. }
  368. if(unlikely(!value || !*value))
  369. value = NULL;
  370. if(value) {
  371. char *endptr = NULL;
  372. calculated_number v = (calculated_number)str2ld(value, &endptr);
  373. if(unlikely(endptr && *endptr)) {
  374. if(endptr == value)
  375. error("the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name, host->hostname);
  376. else
  377. error("the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, host->hostname, endptr);
  378. }
  379. if(global) {
  380. RRDVAR *rv = rrdvar_custom_host_variable_create(host, name);
  381. if (rv) rrdvar_custom_host_variable_set(host, rv, v);
  382. else error("cannot find/create HOST VARIABLE '%s' on host '%s'", name, host->hostname);
  383. }
  384. else if(st) {
  385. RRDSETVAR *rs = rrdsetvar_custom_chart_variable_create(st, name);
  386. if (rs) rrdsetvar_custom_chart_variable_set(rs, v);
  387. else error("cannot find/create CHART VARIABLE '%s' on host '%s', chart '%s'", name, host->hostname, st->id);
  388. }
  389. else
  390. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, host->hostname);
  391. }
  392. else
  393. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global)?"HOST":"CHART", name, host->hostname);
  394. }
  395. else if(likely(hash == FLUSH_HASH && !strcmp(s, PLUGINSD_KEYWORD_FLUSH))) {
  396. debug(D_PLUGINSD, "requested a FLUSH");
  397. st = NULL;
  398. }
  399. else if(unlikely(hash == DISABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_DISABLE))) {
  400. info("called DISABLE. Disabling it.");
  401. enabled = 0;
  402. break;
  403. }
  404. else {
  405. error("sent command '%s' which is not known by netdata, for host '%s'. Disabling it.", s, host->hostname);
  406. enabled = 0;
  407. break;
  408. }
  409. }
  410. cleanup:
  411. cd->enabled = enabled;
  412. if(likely(count)) {
  413. cd->successful_collections += count;
  414. cd->serial_failures = 0;
  415. }
  416. else
  417. cd->serial_failures++;
  418. return count;
  419. }
  420. static void pluginsd_worker_thread_cleanup(void *arg) {
  421. struct plugind *cd = (struct plugind *)arg;
  422. if(cd->enabled && !cd->obsolete) {
  423. cd->obsolete = 1;
  424. info("data collection thread exiting");
  425. if (cd->pid) {
  426. siginfo_t info;
  427. info("killing child process pid %d", cd->pid);
  428. if (killpid(cd->pid, SIGTERM) != -1) {
  429. info("waiting for child process pid %d to exit...", cd->pid);
  430. waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
  431. }
  432. cd->pid = 0;
  433. }
  434. }
  435. }
  436. void *pluginsd_worker_thread(void *arg) {
  437. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  438. struct plugind *cd = (struct plugind *)arg;
  439. cd->obsolete = 0;
  440. size_t count = 0;
  441. while(!netdata_exit) {
  442. FILE *fp = mypopen(cd->cmd, &cd->pid);
  443. if(unlikely(!fp)) {
  444. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  445. break;
  446. }
  447. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  448. count = pluginsd_process(localhost, cd, fp, 0);
  449. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  450. killpid(cd->pid, SIGTERM);
  451. // get the return code
  452. int code = mypclose(fp, cd->pid);
  453. if(code != 0) {
  454. // the plugin reports failure
  455. if(likely(!cd->successful_collections)) {
  456. // nothing collected - disable it
  457. error("'%s' (pid %d) exited with error code %d. Disabling it.", cd->fullfilename, cd->pid, code);
  458. cd->enabled = 0;
  459. }
  460. else {
  461. // we have collected something
  462. if(likely(cd->serial_failures <= 10)) {
  463. error("'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s", cd->fullfilename, cd->pid, code, cd->successful_collections, cd->enabled?"Waiting a bit before starting it again.":"Will not start it again - it is disabled.");
  464. sleep((unsigned int) (cd->update_every * 10));
  465. }
  466. else {
  467. error("'%s' (pid %d) 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, cd->pid, code, cd->successful_collections, cd->serial_failures);
  468. cd->enabled = 0;
  469. }
  470. }
  471. }
  472. else {
  473. // the plugin reports success
  474. if(unlikely(!cd->successful_collections)) {
  475. // we have collected nothing so far
  476. if(likely(cd->serial_failures <= 10)) {
  477. error("'%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 now disabled.");
  478. sleep((unsigned int) (cd->update_every * 10));
  479. }
  480. else {
  481. error("'%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);
  482. cd->enabled = 0;
  483. }
  484. }
  485. else
  486. sleep((unsigned int) cd->update_every);
  487. }
  488. cd->pid = 0;
  489. if(unlikely(!cd->enabled)) break;
  490. }
  491. netdata_thread_cleanup_pop(1);
  492. return NULL;
  493. }
  494. static void pluginsd_main_cleanup(void *data) {
  495. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  496. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  497. info("cleaning up...");
  498. struct plugind *cd;
  499. for (cd = pluginsd_root; cd; cd = cd->next) {
  500. if (cd->enabled && !cd->obsolete) {
  501. info("stopping plugin thread: %s", cd->id);
  502. netdata_thread_cancel(cd->thread);
  503. }
  504. }
  505. info("cleanup completed.");
  506. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  507. }
  508. void *pluginsd_main(void *ptr) {
  509. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  510. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  511. int scan_frequency = (int) config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  512. if(scan_frequency < 1) scan_frequency = 1;
  513. // store the errno for each plugins directory
  514. // so that we don't log broken directories on each loop
  515. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  516. while(!netdata_exit) {
  517. int idx;
  518. const char *directory_name;
  519. for( idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]) ; idx++ ) {
  520. if(unlikely(netdata_exit)) break;
  521. errno = 0;
  522. DIR *dir = opendir(directory_name);
  523. if(unlikely(!dir)) {
  524. if(directory_errors[idx] != errno) {
  525. directory_errors[idx] = errno;
  526. error("cannot open plugins directory '%s'", directory_name);
  527. }
  528. continue;
  529. }
  530. struct dirent *file = NULL;
  531. while(likely((file = readdir(dir)))) {
  532. if(unlikely(netdata_exit)) break;
  533. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  534. if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
  535. int len = (int) strlen(file->d_name);
  536. if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
  537. if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  538. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  539. continue;
  540. }
  541. char pluginname[CONFIG_MAX_NAME + 1];
  542. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  543. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  544. if(unlikely(!enabled)) {
  545. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  546. continue;
  547. }
  548. // check if it runs already
  549. struct plugind *cd;
  550. for(cd = pluginsd_root ; cd ; cd = cd->next)
  551. if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
  552. if(likely(cd && !cd->obsolete)) {
  553. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  554. continue;
  555. }
  556. // it is not running
  557. // allocate a new one, or use the obsolete one
  558. if(unlikely(!cd)) {
  559. cd = callocz(sizeof(struct plugind), 1);
  560. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  561. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  562. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  563. cd->enabled = enabled;
  564. cd->update_every = (int) config_get_number(cd->id, "update every", localhost->rrd_update_every);
  565. cd->started_t = now_realtime_sec();
  566. char *def = "";
  567. snprintfz(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
  568. // link it
  569. if(likely(pluginsd_root)) cd->next = pluginsd_root;
  570. pluginsd_root = cd;
  571. // it is not currently running
  572. cd->obsolete = 1;
  573. if(cd->enabled) {
  574. char tag[NETDATA_THREAD_TAG_MAX + 1];
  575. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  576. // spawn a new thread for it
  577. netdata_thread_create(&cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  578. }
  579. }
  580. }
  581. closedir(dir);
  582. }
  583. sleep((unsigned int) scan_frequency);
  584. }
  585. netdata_thread_cleanup_pop(1);
  586. return NULL;
  587. }