plugins_d.c 32 KB

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