plugins_d.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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. if(feof(fp))
  123. error("read failed: end of file");
  124. else if(ferror(fp))
  125. error("read failed: input error");
  126. else
  127. error("read failed: unknown error");
  128. break;
  129. }
  130. if(unlikely(netdata_exit)) break;
  131. line[PLUGINSD_LINE_MAX] = '\0';
  132. int w = pluginsd_split_words(line, words, PLUGINSD_MAX_WORDS);
  133. char *s = words[0];
  134. if(unlikely(!s || !*s || !w)) {
  135. continue;
  136. }
  137. // 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]);
  138. if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
  139. char *dimension = words[1];
  140. char *value = words[2];
  141. if(unlikely(!dimension || !*dimension)) {
  142. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", st->id, host->hostname);
  143. enabled = 0;
  144. break;
  145. }
  146. if(unlikely(!value || !*value)) value = NULL;
  147. if(unlikely(!st)) {
  148. error("requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension, value?value:"<nothing>", host->hostname);
  149. enabled = 0;
  150. break;
  151. }
  152. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  153. debug(D_PLUGINSD, "is setting dimension %s/%s to %s", st->id, dimension, value?value:"<nothing>");
  154. if(value) {
  155. RRDDIM *rd = rrddim_find(st, dimension);
  156. if(unlikely(!rd)) {
  157. 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);
  158. enabled = 0;
  159. break;
  160. }
  161. else
  162. rrddim_set_by_pointer(st, rd, strtoll(value, NULL, 0));
  163. }
  164. }
  165. else if(likely(hash == BEGIN_HASH && !strcmp(s, PLUGINSD_KEYWORD_BEGIN))) {
  166. char *id = words[1];
  167. char *microseconds_txt = words[2];
  168. if(unlikely(!id)) {
  169. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", host->hostname);
  170. enabled = 0;
  171. break;
  172. }
  173. st = rrdset_find(host, id);
  174. if(unlikely(!st)) {
  175. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, host->hostname);
  176. enabled = 0;
  177. break;
  178. }
  179. if(likely(st->counter_done)) {
  180. usec_t microseconds = 0;
  181. if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
  182. if(likely(microseconds)) {
  183. if(trust_durations)
  184. rrdset_next_usec_unfiltered(st, microseconds);
  185. else
  186. rrdset_next_usec(st, microseconds);
  187. }
  188. else rrdset_next(st);
  189. }
  190. }
  191. else if(likely(hash == END_HASH && !strcmp(s, PLUGINSD_KEYWORD_END))) {
  192. if(unlikely(!st)) {
  193. error("requested an END, without a BEGIN on host '%s'. Disabling it.", host->hostname);
  194. enabled = 0;
  195. break;
  196. }
  197. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  198. debug(D_PLUGINSD, "requested an END on chart %s", st->id);
  199. rrdset_done(st);
  200. st = NULL;
  201. count++;
  202. }
  203. else if(likely(hash == CHART_HASH && !strcmp(s, PLUGINSD_KEYWORD_CHART))) {
  204. st = NULL;
  205. char *type = words[1];
  206. char *name = words[2];
  207. char *title = words[3];
  208. char *units = words[4];
  209. char *family = words[5];
  210. char *context = words[6];
  211. char *chart = words[7];
  212. char *priority_s = words[8];
  213. char *update_every_s = words[9];
  214. char *options = words[10];
  215. char *plugin = words[11];
  216. char *module = words[12];
  217. // parse the id from type
  218. char *id = NULL;
  219. if(likely(type && (id = strchr(type, '.')))) {
  220. *id = '\0';
  221. id++;
  222. }
  223. // make sure we have the required variables
  224. if(unlikely(!type || !*type || !id || !*id)) {
  225. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", host->hostname);
  226. enabled = 0;
  227. break;
  228. }
  229. // parse the name, and make sure it does not include 'type.'
  230. if(unlikely(name && *name)) {
  231. // when data are coming from slaves
  232. // name will be type.name
  233. // so we have to remove 'type.' from name too
  234. size_t len = strlen(type);
  235. if(strncmp(type, name, len) == 0 && name[len] == '.')
  236. name = &name[len + 1];
  237. // if the name is the same with the id,
  238. // or is just 'NULL', clear it.
  239. if(unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  240. name = NULL;
  241. }
  242. int priority = 1000;
  243. if(likely(priority_s && *priority_s)) priority = str2i(priority_s);
  244. int update_every = cd->update_every;
  245. if(likely(update_every_s && *update_every_s)) update_every = str2i(update_every_s);
  246. if(unlikely(!update_every)) update_every = cd->update_every;
  247. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  248. if(unlikely(chart)) chart_type = rrdset_type_id(chart);
  249. if(unlikely(name && !*name)) name = NULL;
  250. if(unlikely(family && !*family)) family = NULL;
  251. if(unlikely(context && !*context)) context = NULL;
  252. if(unlikely(!title)) title = "";
  253. if(unlikely(!units)) units = "unknown";
  254. debug(D_PLUGINSD, "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
  255. , type, id
  256. , name?name:""
  257. , family?family:""
  258. , context?context:""
  259. , rrdset_type_name(chart_type)
  260. , priority
  261. , update_every
  262. );
  263. st = rrdset_create(
  264. host
  265. , type
  266. , id
  267. , name
  268. , family
  269. , context
  270. , title
  271. , units
  272. , (plugin && *plugin)?plugin:cd->filename
  273. , module
  274. , priority
  275. , update_every
  276. , chart_type
  277. );
  278. if(options && *options) {
  279. if(strstr(options, "obsolete"))
  280. rrdset_is_obsolete(st);
  281. else
  282. rrdset_isnot_obsolete(st);
  283. if(strstr(options, "detail"))
  284. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  285. else
  286. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  287. if(strstr(options, "hidden"))
  288. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  289. else
  290. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  291. if(strstr(options, "store_first"))
  292. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  293. else
  294. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  295. }
  296. else {
  297. rrdset_isnot_obsolete(st);
  298. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  299. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  300. }
  301. }
  302. else if(likely(hash == DIMENSION_HASH && !strcmp(s, PLUGINSD_KEYWORD_DIMENSION))) {
  303. char *id = words[1];
  304. char *name = words[2];
  305. char *algorithm = words[3];
  306. char *multiplier_s = words[4];
  307. char *divisor_s = words[5];
  308. char *options = words[6];
  309. if(unlikely(!id || !*id)) {
  310. error("requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", host->hostname, st?st->id:"UNSET");
  311. enabled = 0;
  312. break;
  313. }
  314. if(unlikely(!st)) {
  315. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", host->hostname);
  316. enabled = 0;
  317. break;
  318. }
  319. long multiplier = 1;
  320. if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
  321. if(unlikely(!multiplier)) multiplier = 1;
  322. long divisor = 1;
  323. if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
  324. if(unlikely(!divisor)) divisor = 1;
  325. if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
  326. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  327. debug(D_PLUGINSD, "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
  328. , st->id
  329. , id
  330. , name?name:""
  331. , rrd_algorithm_name(rrd_algorithm_id(algorithm))
  332. , multiplier
  333. , divisor
  334. , options?options:""
  335. );
  336. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
  337. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  338. rrddim_flag_clear(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  339. if(options && *options) {
  340. if(strstr(options, "obsolete") != NULL)
  341. rrddim_is_obsolete(st, rd);
  342. else
  343. rrddim_isnot_obsolete(st, rd);
  344. if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  345. if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  346. if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  347. }
  348. else {
  349. rrddim_isnot_obsolete(st, rd);
  350. }
  351. }
  352. else if(likely(hash == VARIABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_VARIABLE))) {
  353. char *name = words[1];
  354. char *value = words[2];
  355. int global = (st)?0:1;
  356. if(name && *name) {
  357. if((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  358. global = 1;
  359. name = words[2];
  360. value = words[3];
  361. }
  362. else if((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  363. global = 0;
  364. name = words[2];
  365. value = words[3];
  366. }
  367. }
  368. if(unlikely(!name || !*name)) {
  369. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", host->hostname);
  370. enabled = 0;
  371. break;
  372. }
  373. if(unlikely(!value || !*value))
  374. value = NULL;
  375. if(value) {
  376. char *endptr = NULL;
  377. calculated_number v = (calculated_number)str2ld(value, &endptr);
  378. if(unlikely(endptr && *endptr)) {
  379. if(endptr == value)
  380. error("the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name, host->hostname);
  381. else
  382. error("the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, host->hostname, endptr);
  383. }
  384. if(global) {
  385. RRDVAR *rv = rrdvar_custom_host_variable_create(host, name);
  386. if (rv) rrdvar_custom_host_variable_set(host, rv, v);
  387. else error("cannot find/create HOST VARIABLE '%s' on host '%s'", name, host->hostname);
  388. }
  389. else if(st) {
  390. RRDSETVAR *rs = rrdsetvar_custom_chart_variable_create(st, name);
  391. if (rs) rrdsetvar_custom_chart_variable_set(rs, v);
  392. else error("cannot find/create CHART VARIABLE '%s' on host '%s', chart '%s'", name, host->hostname, st->id);
  393. }
  394. else
  395. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, host->hostname);
  396. }
  397. else
  398. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global)?"HOST":"CHART", name, host->hostname);
  399. }
  400. else if(likely(hash == FLUSH_HASH && !strcmp(s, PLUGINSD_KEYWORD_FLUSH))) {
  401. debug(D_PLUGINSD, "requested a FLUSH");
  402. st = NULL;
  403. }
  404. else if(unlikely(hash == DISABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_DISABLE))) {
  405. info("called DISABLE. Disabling it.");
  406. enabled = 0;
  407. break;
  408. }
  409. else {
  410. error("sent command '%s' which is not known by netdata, for host '%s'. Disabling it.", s, host->hostname);
  411. enabled = 0;
  412. break;
  413. }
  414. }
  415. cleanup:
  416. cd->enabled = enabled;
  417. if(likely(count)) {
  418. cd->successful_collections += count;
  419. cd->serial_failures = 0;
  420. }
  421. else
  422. cd->serial_failures++;
  423. return count;
  424. }
  425. static void pluginsd_worker_thread_cleanup(void *arg) {
  426. struct plugind *cd = (struct plugind *)arg;
  427. if(cd->enabled && !cd->obsolete) {
  428. cd->obsolete = 1;
  429. info("data collection thread exiting");
  430. if (cd->pid) {
  431. siginfo_t info;
  432. info("killing child process pid %d", cd->pid);
  433. if (killpid(cd->pid, SIGTERM) != -1) {
  434. info("waiting for child process pid %d to exit...", cd->pid);
  435. waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
  436. }
  437. cd->pid = 0;
  438. }
  439. }
  440. }
  441. #define SERIAL_FAILURES_THRESHOLD 10
  442. static void pluginsd_worker_thread_handle_success(struct plugind *cd) {
  443. if (likely(cd->successful_collections)) {
  444. sleep((unsigned int) cd->update_every);
  445. return;
  446. }
  447. if(likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  448. info("'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  449. cd->fullfilename, cd->pid,
  450. cd->enabled ?
  451. "Waiting a bit before starting it again." :
  452. "Will not start it again - it is now disabled.");
  453. sleep((unsigned int) (cd->update_every * 10));
  454. return;
  455. }
  456. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  457. error("'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  458. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  459. cd->fullfilename, cd->pid, cd->serial_failures);
  460. cd->enabled = 0;
  461. return;
  462. }
  463. return;
  464. }
  465. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code) {
  466. if (worker_ret_code == -1) {
  467. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  468. cd->enabled = 0;
  469. return;
  470. }
  471. if (!cd->successful_collections) {
  472. error("'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.",
  473. cd->fullfilename, cd->pid, worker_ret_code);
  474. cd->enabled = 0;
  475. return;
  476. }
  477. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  478. error("'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  479. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  480. cd->enabled ?
  481. "Waiting a bit before starting it again." :
  482. "Will not start it again - it is disabled.");
  483. sleep((unsigned int) (cd->update_every * 10));
  484. return;
  485. }
  486. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  487. error("'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  488. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  489. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  490. cd->enabled = 0;
  491. return;
  492. }
  493. return;
  494. }
  495. #undef SERIAL_FAILURES_THRESHOLD
  496. void *pluginsd_worker_thread(void *arg) {
  497. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  498. struct plugind *cd = (struct plugind *)arg;
  499. cd->obsolete = 0;
  500. size_t count = 0;
  501. while(!netdata_exit) {
  502. FILE *fp = mypopen(cd->cmd, &cd->pid);
  503. if(unlikely(!fp)) {
  504. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  505. break;
  506. }
  507. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  508. count = pluginsd_process(localhost, cd, fp, 0);
  509. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  510. killpid(cd->pid, SIGTERM);
  511. int worker_ret_code = mypclose(fp, cd->pid);
  512. if (likely(worker_ret_code == 0))
  513. pluginsd_worker_thread_handle_success(cd);
  514. else
  515. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  516. cd->pid = 0;
  517. if(unlikely(!cd->enabled)) break;
  518. }
  519. netdata_thread_cleanup_pop(1);
  520. return NULL;
  521. }
  522. static void pluginsd_main_cleanup(void *data) {
  523. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  524. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  525. info("cleaning up...");
  526. struct plugind *cd;
  527. for (cd = pluginsd_root; cd; cd = cd->next) {
  528. if (cd->enabled && !cd->obsolete) {
  529. info("stopping plugin thread: %s", cd->id);
  530. netdata_thread_cancel(cd->thread);
  531. }
  532. }
  533. info("cleanup completed.");
  534. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  535. }
  536. void *pluginsd_main(void *ptr) {
  537. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  538. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  539. int scan_frequency = (int) config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  540. if(scan_frequency < 1) scan_frequency = 1;
  541. // store the errno for each plugins directory
  542. // so that we don't log broken directories on each loop
  543. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  544. while(!netdata_exit) {
  545. int idx;
  546. const char *directory_name;
  547. for( idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]) ; idx++ ) {
  548. if(unlikely(netdata_exit)) break;
  549. errno = 0;
  550. DIR *dir = opendir(directory_name);
  551. if(unlikely(!dir)) {
  552. if(directory_errors[idx] != errno) {
  553. directory_errors[idx] = errno;
  554. error("cannot open plugins directory '%s'", directory_name);
  555. }
  556. continue;
  557. }
  558. struct dirent *file = NULL;
  559. while(likely((file = readdir(dir)))) {
  560. if(unlikely(netdata_exit)) break;
  561. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  562. if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
  563. int len = (int) strlen(file->d_name);
  564. if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
  565. if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  566. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  567. continue;
  568. }
  569. char pluginname[CONFIG_MAX_NAME + 1];
  570. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  571. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  572. if(unlikely(!enabled)) {
  573. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  574. continue;
  575. }
  576. // check if it runs already
  577. struct plugind *cd;
  578. for(cd = pluginsd_root ; cd ; cd = cd->next)
  579. if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
  580. if(likely(cd && !cd->obsolete)) {
  581. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  582. continue;
  583. }
  584. // it is not running
  585. // allocate a new one, or use the obsolete one
  586. if(unlikely(!cd)) {
  587. cd = callocz(sizeof(struct plugind), 1);
  588. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  589. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  590. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  591. cd->enabled = enabled;
  592. cd->update_every = (int) config_get_number(cd->id, "update every", localhost->rrd_update_every);
  593. cd->started_t = now_realtime_sec();
  594. char *def = "";
  595. snprintfz(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
  596. // link it
  597. if(likely(pluginsd_root)) cd->next = pluginsd_root;
  598. pluginsd_root = cd;
  599. // it is not currently running
  600. cd->obsolete = 1;
  601. if(cd->enabled) {
  602. char tag[NETDATA_THREAD_TAG_MAX + 1];
  603. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  604. // spawn a new thread for it
  605. netdata_thread_create(&cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  606. }
  607. }
  608. }
  609. closedir(dir);
  610. }
  611. sleep((unsigned int) scan_frequency);
  612. }
  613. netdata_thread_cleanup_pop(1);
  614. return NULL;
  615. }