plugins_d.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. #ifdef ENABLE_HTTPS
  95. /**
  96. * Update Buffer
  97. *
  98. * Update the temporary buffer used to parse data received from slave
  99. *
  100. * @param output is a pointer to the vector where I will store the data
  101. * @param ssl is the connection pointer with the server
  102. *
  103. * @return it returns the total of bytes read on success and a negative number otherwise
  104. */
  105. int pluginsd_update_buffer(char *output, SSL *ssl) {
  106. ERR_clear_error();
  107. int bytesleft = SSL_read(ssl, output, PLUGINSD_LINE_MAX_SSL_READ);
  108. if(bytesleft <= 0) {
  109. int sslerrno = SSL_get_error(ssl, bytesleft);
  110. switch(sslerrno) {
  111. case SSL_ERROR_WANT_READ:
  112. case SSL_ERROR_WANT_WRITE:
  113. {
  114. break;
  115. }
  116. default:
  117. {
  118. u_long err;
  119. char buf[256];
  120. int counter = 0;
  121. while ((err = ERR_get_error()) != 0) {
  122. ERR_error_string_n(err, buf, sizeof(buf));
  123. info("%d SSL Handshake error (%s) on socket %d ", counter++, ERR_error_string((long)SSL_get_error(ssl, bytesleft), NULL), SSL_get_fd(ssl));
  124. }
  125. }
  126. }
  127. } else {
  128. output[bytesleft] = '\0';
  129. }
  130. return bytesleft;
  131. }
  132. /**
  133. * Get from Buffer
  134. *
  135. * Get data to process from buffer
  136. *
  137. * @param output is the output vector that will be used to parse the string.
  138. * @param bytesread the amount of bytes read in the previous iteration.
  139. * @param input the input vector where there are data to process
  140. * @param ssl a pointer to the connection with the server
  141. * @param src the first address of the input, because sometime will be necessary to restart the addr with it.
  142. *
  143. * @return It returns a pointer for the next iteration on success and NULL otherwise.
  144. */
  145. char * pluginsd_get_from_buffer(char *output, int *bytesread, char *input, SSL *ssl, char *src) {
  146. int copying = 1;
  147. char *endbuffer;
  148. size_t length;
  149. while(copying) {
  150. if(*bytesread > 0) {
  151. endbuffer = strchr(input, '\n');
  152. if(endbuffer) {
  153. copying = 0;
  154. endbuffer++; //Advance due the fact I wanna copy '\n'
  155. length = endbuffer - input;
  156. *bytesread -= length;
  157. memcpy(output, input, length);
  158. output += length;
  159. *output = '\0';
  160. input += length;
  161. }else {
  162. length = strlen(input);
  163. memcpy(output, input, length);
  164. output += length;
  165. input = src;
  166. *bytesread = pluginsd_update_buffer(input, ssl);
  167. if(*bytesread <= 0) {
  168. input = NULL;
  169. copying = 0;
  170. }
  171. }
  172. }else {
  173. //reduce sample of bytes read, print the length
  174. *bytesread = pluginsd_update_buffer(input, ssl);
  175. if(*bytesread <= 0) {
  176. input = NULL;
  177. copying = 0;
  178. }
  179. }
  180. }
  181. return input;
  182. }
  183. #endif
  184. inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int trust_durations) {
  185. int enabled = cd->enabled;
  186. if(!fp || !enabled) {
  187. cd->enabled = 0;
  188. return 0;
  189. }
  190. size_t count = 0;
  191. char line[PLUGINSD_LINE_MAX + 1];
  192. char *words[PLUGINSD_MAX_WORDS] = { NULL };
  193. uint32_t BEGIN_HASH = simple_hash(PLUGINSD_KEYWORD_BEGIN);
  194. uint32_t END_HASH = simple_hash(PLUGINSD_KEYWORD_END);
  195. uint32_t FLUSH_HASH = simple_hash(PLUGINSD_KEYWORD_FLUSH);
  196. uint32_t CHART_HASH = simple_hash(PLUGINSD_KEYWORD_CHART);
  197. uint32_t DIMENSION_HASH = simple_hash(PLUGINSD_KEYWORD_DIMENSION);
  198. uint32_t DISABLE_HASH = simple_hash(PLUGINSD_KEYWORD_DISABLE);
  199. uint32_t VARIABLE_HASH = simple_hash(PLUGINSD_KEYWORD_VARIABLE);
  200. RRDSET *st = NULL;
  201. uint32_t hash;
  202. errno = 0;
  203. clearerr(fp);
  204. if(unlikely(fileno(fp) == -1)) {
  205. error("file descriptor given is not a valid stream");
  206. goto cleanup;
  207. }
  208. #ifdef ENABLE_HTTPS
  209. int bytesleft = 0;
  210. char tmpbuffer[PLUGINSD_LINE_MAX];
  211. char *readfrom = NULL;
  212. #endif
  213. char *r = NULL;
  214. while(!ferror(fp)) {
  215. if(unlikely(netdata_exit)) break;
  216. #ifdef ENABLE_HTTPS
  217. int normalread = 1;
  218. if(netdata_srv_ctx) {
  219. if(host->stream_ssl.conn && !host->stream_ssl.flags) {
  220. if(!bytesleft) {
  221. r = line;
  222. readfrom = tmpbuffer;
  223. bytesleft = pluginsd_update_buffer(readfrom, host->stream_ssl.conn);
  224. if(bytesleft <= 0) {
  225. break;
  226. }
  227. }
  228. readfrom = pluginsd_get_from_buffer(line, &bytesleft, readfrom, host->stream_ssl.conn, tmpbuffer);
  229. if(!readfrom) {
  230. r = NULL;
  231. }
  232. normalread = 0;
  233. }
  234. }
  235. if(normalread) {
  236. r = fgets(line, PLUGINSD_LINE_MAX, fp);
  237. }
  238. #else
  239. r = fgets(line, PLUGINSD_LINE_MAX, fp);
  240. #endif
  241. if(unlikely(!r)) {
  242. if(feof(fp))
  243. error("read failed: end of file");
  244. else if(ferror(fp))
  245. error("read failed: input error");
  246. else
  247. error("read failed: unknown error");
  248. break;
  249. }
  250. if(unlikely(netdata_exit)) break;
  251. line[PLUGINSD_LINE_MAX] = '\0';
  252. int w = pluginsd_split_words(line, words, PLUGINSD_MAX_WORDS);
  253. char *s = words[0];
  254. if(unlikely(!s || !*s || !w)) {
  255. continue;
  256. }
  257. // 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]);
  258. if(likely(!simple_hash_strcmp(s, "SET", &hash))) {
  259. char *dimension = words[1];
  260. char *value = words[2];
  261. if(unlikely(!dimension || !*dimension)) {
  262. error("requested a SET on chart '%s' of host '%s', without a dimension. Disabling it.", st->id, host->hostname);
  263. enabled = 0;
  264. break;
  265. }
  266. if(unlikely(!value || !*value)) value = NULL;
  267. if(unlikely(!st)) {
  268. error("requested a SET on dimension %s with value %s on host '%s', without a BEGIN. Disabling it.", dimension, value?value:"<nothing>", host->hostname);
  269. enabled = 0;
  270. break;
  271. }
  272. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  273. debug(D_PLUGINSD, "is setting dimension %s/%s to %s", st->id, dimension, value?value:"<nothing>");
  274. if(value) {
  275. RRDDIM *rd = rrddim_find(st, dimension);
  276. if(unlikely(!rd)) {
  277. 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);
  278. enabled = 0;
  279. break;
  280. }
  281. else
  282. rrddim_set_by_pointer(st, rd, strtoll(value, NULL, 0));
  283. }
  284. }
  285. else if(likely(hash == BEGIN_HASH && !strcmp(s, PLUGINSD_KEYWORD_BEGIN))) {
  286. char *id = words[1];
  287. char *microseconds_txt = words[2];
  288. if(unlikely(!id)) {
  289. error("requested a BEGIN without a chart id for host '%s'. Disabling it.", host->hostname);
  290. enabled = 0;
  291. break;
  292. }
  293. st = rrdset_find(host, id);
  294. if(unlikely(!st)) {
  295. error("requested a BEGIN on chart '%s', which does not exist on host '%s'. Disabling it.", id, host->hostname);
  296. enabled = 0;
  297. break;
  298. }
  299. if(likely(st->counter_done)) {
  300. usec_t microseconds = 0;
  301. if(microseconds_txt && *microseconds_txt) microseconds = str2ull(microseconds_txt);
  302. if(likely(microseconds)) {
  303. if(trust_durations)
  304. rrdset_next_usec_unfiltered(st, microseconds);
  305. else
  306. rrdset_next_usec(st, microseconds);
  307. }
  308. else rrdset_next(st);
  309. }
  310. }
  311. else if(likely(hash == END_HASH && !strcmp(s, PLUGINSD_KEYWORD_END))) {
  312. if(unlikely(!st)) {
  313. error("requested an END, without a BEGIN on host '%s'. Disabling it.", host->hostname);
  314. enabled = 0;
  315. break;
  316. }
  317. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  318. debug(D_PLUGINSD, "requested an END on chart %s", st->id);
  319. rrdset_done(st);
  320. st = NULL;
  321. count++;
  322. }
  323. else if(likely(hash == CHART_HASH && !strcmp(s, PLUGINSD_KEYWORD_CHART))) {
  324. st = NULL;
  325. char *type = words[1];
  326. char *name = words[2];
  327. char *title = words[3];
  328. char *units = words[4];
  329. char *family = words[5];
  330. char *context = words[6];
  331. char *chart = words[7];
  332. char *priority_s = words[8];
  333. char *update_every_s = words[9];
  334. char *options = words[10];
  335. char *plugin = words[11];
  336. char *module = words[12];
  337. // parse the id from type
  338. char *id = NULL;
  339. if(likely(type && (id = strchr(type, '.')))) {
  340. *id = '\0';
  341. id++;
  342. }
  343. // make sure we have the required variables
  344. if(unlikely(!type || !*type || !id || !*id)) {
  345. error("requested a CHART, without a type.id, on host '%s'. Disabling it.", host->hostname);
  346. enabled = 0;
  347. break;
  348. }
  349. // parse the name, and make sure it does not include 'type.'
  350. if(unlikely(name && *name)) {
  351. // when data are coming from slaves
  352. // name will be type.name
  353. // so we have to remove 'type.' from name too
  354. size_t len = strlen(type);
  355. if(strncmp(type, name, len) == 0 && name[len] == '.')
  356. name = &name[len + 1];
  357. // if the name is the same with the id,
  358. // or is just 'NULL', clear it.
  359. if(unlikely(strcmp(name, id) == 0 || strcasecmp(name, "NULL") == 0 || strcasecmp(name, "(NULL)") == 0))
  360. name = NULL;
  361. }
  362. int priority = 1000;
  363. if(likely(priority_s && *priority_s)) priority = str2i(priority_s);
  364. int update_every = cd->update_every;
  365. if(likely(update_every_s && *update_every_s)) update_every = str2i(update_every_s);
  366. if(unlikely(!update_every)) update_every = cd->update_every;
  367. RRDSET_TYPE chart_type = RRDSET_TYPE_LINE;
  368. if(unlikely(chart)) chart_type = rrdset_type_id(chart);
  369. if(unlikely(name && !*name)) name = NULL;
  370. if(unlikely(family && !*family)) family = NULL;
  371. if(unlikely(context && !*context)) context = NULL;
  372. if(unlikely(!title)) title = "";
  373. if(unlikely(!units)) units = "unknown";
  374. debug(D_PLUGINSD, "creating chart type='%s', id='%s', name='%s', family='%s', context='%s', chart='%s', priority=%d, update_every=%d"
  375. , type, id
  376. , name?name:""
  377. , family?family:""
  378. , context?context:""
  379. , rrdset_type_name(chart_type)
  380. , priority
  381. , update_every
  382. );
  383. st = rrdset_create(
  384. host
  385. , type
  386. , id
  387. , name
  388. , family
  389. , context
  390. , title
  391. , units
  392. , (plugin && *plugin)?plugin:cd->filename
  393. , module
  394. , priority
  395. , update_every
  396. , chart_type
  397. );
  398. if(options && *options) {
  399. if(strstr(options, "obsolete"))
  400. rrdset_is_obsolete(st);
  401. else
  402. rrdset_isnot_obsolete(st);
  403. if(strstr(options, "detail"))
  404. rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
  405. else
  406. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  407. if(strstr(options, "hidden"))
  408. rrdset_flag_set(st, RRDSET_FLAG_HIDDEN);
  409. else
  410. rrdset_flag_clear(st, RRDSET_FLAG_HIDDEN);
  411. if(strstr(options, "store_first"))
  412. rrdset_flag_set(st, RRDSET_FLAG_STORE_FIRST);
  413. else
  414. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  415. }
  416. else {
  417. rrdset_isnot_obsolete(st);
  418. rrdset_flag_clear(st, RRDSET_FLAG_DETAIL);
  419. rrdset_flag_clear(st, RRDSET_FLAG_STORE_FIRST);
  420. }
  421. }
  422. else if(likely(hash == DIMENSION_HASH && !strcmp(s, PLUGINSD_KEYWORD_DIMENSION))) {
  423. char *id = words[1];
  424. char *name = words[2];
  425. char *algorithm = words[3];
  426. char *multiplier_s = words[4];
  427. char *divisor_s = words[5];
  428. char *options = words[6];
  429. if(unlikely(!id || !*id)) {
  430. error("requested a DIMENSION, without an id, host '%s' and chart '%s'. Disabling it.", host->hostname, st?st->id:"UNSET");
  431. enabled = 0;
  432. break;
  433. }
  434. if(unlikely(!st)) {
  435. error("requested a DIMENSION, without a CHART, on host '%s'. Disabling it.", host->hostname);
  436. enabled = 0;
  437. break;
  438. }
  439. long multiplier = 1;
  440. if(multiplier_s && *multiplier_s) multiplier = strtol(multiplier_s, NULL, 0);
  441. if(unlikely(!multiplier)) multiplier = 1;
  442. long divisor = 1;
  443. if(likely(divisor_s && *divisor_s)) divisor = strtol(divisor_s, NULL, 0);
  444. if(unlikely(!divisor)) divisor = 1;
  445. if(unlikely(!algorithm || !*algorithm)) algorithm = "absolute";
  446. if(unlikely(rrdset_flag_check(st, RRDSET_FLAG_DEBUG)))
  447. debug(D_PLUGINSD, "creating dimension in chart %s, id='%s', name='%s', algorithm='%s', multiplier=%ld, divisor=%ld, hidden='%s'"
  448. , st->id
  449. , id
  450. , name?name:""
  451. , rrd_algorithm_name(rrd_algorithm_id(algorithm))
  452. , multiplier
  453. , divisor
  454. , options?options:""
  455. );
  456. RRDDIM *rd = rrddim_add(st, id, name, multiplier, divisor, rrd_algorithm_id(algorithm));
  457. rrddim_flag_clear(rd, RRDDIM_FLAG_HIDDEN);
  458. rrddim_flag_clear(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  459. if(options && *options) {
  460. if(strstr(options, "obsolete") != NULL)
  461. rrddim_is_obsolete(st, rd);
  462. else
  463. rrddim_isnot_obsolete(st, rd);
  464. if(strstr(options, "hidden") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_HIDDEN);
  465. if(strstr(options, "noreset") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  466. if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
  467. }
  468. else {
  469. rrddim_isnot_obsolete(st, rd);
  470. }
  471. }
  472. else if(likely(hash == VARIABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_VARIABLE))) {
  473. char *name = words[1];
  474. char *value = words[2];
  475. int global = (st)?0:1;
  476. if(name && *name) {
  477. if((strcmp(name, "GLOBAL") == 0 || strcmp(name, "HOST") == 0)) {
  478. global = 1;
  479. name = words[2];
  480. value = words[3];
  481. }
  482. else if((strcmp(name, "LOCAL") == 0 || strcmp(name, "CHART") == 0)) {
  483. global = 0;
  484. name = words[2];
  485. value = words[3];
  486. }
  487. }
  488. if(unlikely(!name || !*name)) {
  489. error("requested a VARIABLE on host '%s', without a variable name. Disabling it.", host->hostname);
  490. enabled = 0;
  491. break;
  492. }
  493. if(unlikely(!value || !*value))
  494. value = NULL;
  495. if(value) {
  496. char *endptr = NULL;
  497. calculated_number v = (calculated_number)str2ld(value, &endptr);
  498. if(unlikely(endptr && *endptr)) {
  499. if(endptr == value)
  500. error("the value '%s' of VARIABLE '%s' on host '%s' cannot be parsed as a number", value, name, host->hostname);
  501. else
  502. error("the value '%s' of VARIABLE '%s' on host '%s' has leftovers: '%s'", value, name, host->hostname, endptr);
  503. }
  504. if(global) {
  505. RRDVAR *rv = rrdvar_custom_host_variable_create(host, name);
  506. if (rv) rrdvar_custom_host_variable_set(host, rv, v);
  507. else error("cannot find/create HOST VARIABLE '%s' on host '%s'", name, host->hostname);
  508. }
  509. else if(st) {
  510. RRDSETVAR *rs = rrdsetvar_custom_chart_variable_create(st, name);
  511. if (rs) rrdsetvar_custom_chart_variable_set(rs, v);
  512. else error("cannot find/create CHART VARIABLE '%s' on host '%s', chart '%s'", name, host->hostname, st->id);
  513. }
  514. else
  515. error("cannot find/create CHART VARIABLE '%s' on host '%s' without a chart", name, host->hostname);
  516. }
  517. else
  518. error("cannot set %s VARIABLE '%s' on host '%s' to an empty value", (global)?"HOST":"CHART", name, host->hostname);
  519. }
  520. else if(likely(hash == FLUSH_HASH && !strcmp(s, PLUGINSD_KEYWORD_FLUSH))) {
  521. debug(D_PLUGINSD, "requested a FLUSH");
  522. st = NULL;
  523. }
  524. else if(unlikely(hash == DISABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_DISABLE))) {
  525. info("called DISABLE. Disabling it.");
  526. enabled = 0;
  527. break;
  528. }
  529. else {
  530. error("sent command '%s' which is not known by netdata, for host '%s'. Disabling it.", s, host->hostname);
  531. enabled = 0;
  532. break;
  533. }
  534. }
  535. cleanup:
  536. cd->enabled = enabled;
  537. if(likely(count)) {
  538. cd->successful_collections += count;
  539. cd->serial_failures = 0;
  540. }
  541. else
  542. cd->serial_failures++;
  543. return count;
  544. }
  545. static void pluginsd_worker_thread_cleanup(void *arg) {
  546. struct plugind *cd = (struct plugind *)arg;
  547. if(cd->enabled && !cd->obsolete) {
  548. cd->obsolete = 1;
  549. info("data collection thread exiting");
  550. if (cd->pid) {
  551. siginfo_t info;
  552. info("killing child process pid %d", cd->pid);
  553. if (killpid(cd->pid) != -1) {
  554. info("waiting for child process pid %d to exit...", cd->pid);
  555. waitid(P_PID, (id_t) cd->pid, &info, WEXITED);
  556. }
  557. cd->pid = 0;
  558. }
  559. }
  560. }
  561. #define SERIAL_FAILURES_THRESHOLD 10
  562. static void pluginsd_worker_thread_handle_success(struct plugind *cd) {
  563. if (likely(cd->successful_collections)) {
  564. sleep((unsigned int) cd->update_every);
  565. return;
  566. }
  567. if(likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  568. info("'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  569. cd->fullfilename, cd->pid,
  570. cd->enabled ?
  571. "Waiting a bit before starting it again." :
  572. "Will not start it again - it is now disabled.");
  573. sleep((unsigned int) (cd->update_every * 10));
  574. return;
  575. }
  576. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  577. error("'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  578. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  579. cd->fullfilename, cd->pid, cd->serial_failures);
  580. cd->enabled = 0;
  581. return;
  582. }
  583. return;
  584. }
  585. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code) {
  586. if (worker_ret_code == -1) {
  587. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  588. cd->enabled = 0;
  589. return;
  590. }
  591. if (!cd->successful_collections) {
  592. error("'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.",
  593. cd->fullfilename, cd->pid, worker_ret_code);
  594. cd->enabled = 0;
  595. return;
  596. }
  597. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  598. error("'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  599. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  600. cd->enabled ?
  601. "Waiting a bit before starting it again." :
  602. "Will not start it again - it is disabled.");
  603. sleep((unsigned int) (cd->update_every * 10));
  604. return;
  605. }
  606. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  607. error("'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  608. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  609. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  610. cd->enabled = 0;
  611. return;
  612. }
  613. return;
  614. }
  615. #undef SERIAL_FAILURES_THRESHOLD
  616. void *pluginsd_worker_thread(void *arg) {
  617. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  618. struct plugind *cd = (struct plugind *)arg;
  619. cd->obsolete = 0;
  620. size_t count = 0;
  621. while(!netdata_exit) {
  622. FILE *fp = mypopen(cd->cmd, &cd->pid);
  623. if(unlikely(!fp)) {
  624. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  625. break;
  626. }
  627. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  628. count = pluginsd_process(localhost, cd, fp, 0);
  629. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  630. killpid(cd->pid);
  631. int worker_ret_code = mypclose(fp, cd->pid);
  632. if (likely(worker_ret_code == 0))
  633. pluginsd_worker_thread_handle_success(cd);
  634. else
  635. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  636. cd->pid = 0;
  637. if(unlikely(!cd->enabled)) break;
  638. }
  639. netdata_thread_cleanup_pop(1);
  640. return NULL;
  641. }
  642. static void pluginsd_main_cleanup(void *data) {
  643. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  644. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  645. info("cleaning up...");
  646. struct plugind *cd;
  647. for (cd = pluginsd_root; cd; cd = cd->next) {
  648. if (cd->enabled && !cd->obsolete) {
  649. info("stopping plugin thread: %s", cd->id);
  650. netdata_thread_cancel(cd->thread);
  651. }
  652. }
  653. info("cleanup completed.");
  654. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  655. }
  656. void *pluginsd_main(void *ptr) {
  657. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  658. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  659. int scan_frequency = (int) config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  660. if(scan_frequency < 1) scan_frequency = 1;
  661. // disable some plugins by default
  662. config_get_boolean(CONFIG_SECTION_PLUGINS, "slabinfo", CONFIG_BOOLEAN_NO);
  663. // store the errno for each plugins directory
  664. // so that we don't log broken directories on each loop
  665. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  666. while(!netdata_exit) {
  667. int idx;
  668. const char *directory_name;
  669. for( idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]) ; idx++ ) {
  670. if(unlikely(netdata_exit)) break;
  671. errno = 0;
  672. DIR *dir = opendir(directory_name);
  673. if(unlikely(!dir)) {
  674. if(directory_errors[idx] != errno) {
  675. directory_errors[idx] = errno;
  676. error("cannot open plugins directory '%s'", directory_name);
  677. }
  678. continue;
  679. }
  680. struct dirent *file = NULL;
  681. while(likely((file = readdir(dir)))) {
  682. if(unlikely(netdata_exit)) break;
  683. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  684. if(unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0)) continue;
  685. int len = (int) strlen(file->d_name);
  686. if(unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN)) continue;
  687. if(unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  688. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  689. continue;
  690. }
  691. char pluginname[CONFIG_MAX_NAME + 1];
  692. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  693. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  694. if(unlikely(!enabled)) {
  695. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  696. continue;
  697. }
  698. // check if it runs already
  699. struct plugind *cd;
  700. for(cd = pluginsd_root ; cd ; cd = cd->next)
  701. if(unlikely(strcmp(cd->filename, file->d_name) == 0)) break;
  702. if(likely(cd && !cd->obsolete)) {
  703. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  704. continue;
  705. }
  706. // it is not running
  707. // allocate a new one, or use the obsolete one
  708. if(unlikely(!cd)) {
  709. cd = callocz(sizeof(struct plugind), 1);
  710. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  711. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  712. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  713. cd->enabled = enabled;
  714. cd->update_every = (int) config_get_number(cd->id, "update every", localhost->rrd_update_every);
  715. cd->started_t = now_realtime_sec();
  716. char *def = "";
  717. snprintfz(cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every, config_get(cd->id, "command options", def));
  718. // link it
  719. if(likely(pluginsd_root)) cd->next = pluginsd_root;
  720. pluginsd_root = cd;
  721. // it is not currently running
  722. cd->obsolete = 1;
  723. if(cd->enabled) {
  724. char tag[NETDATA_THREAD_TAG_MAX + 1];
  725. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  726. // spawn a new thread for it
  727. netdata_thread_create(&cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  728. }
  729. }
  730. }
  731. closedir(dir);
  732. }
  733. sleep((unsigned int) scan_frequency);
  734. }
  735. netdata_thread_cleanup_pop(1);
  736. return NULL;
  737. }