plugins_d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugins_d.h"
  3. #include "pluginsd_parser.h"
  4. char *plugin_directories[PLUGINSD_MAX_DIRECTORIES] = { NULL };
  5. struct plugind *pluginsd_root = NULL;
  6. inline int pluginsd_space(char c) {
  7. switch(c) {
  8. case ' ':
  9. case '\t':
  10. case '\r':
  11. case '\n':
  12. case '=':
  13. return 1;
  14. default:
  15. return 0;
  16. }
  17. }
  18. inline int config_isspace(char c)
  19. {
  20. switch (c) {
  21. case ' ':
  22. case '\t':
  23. case '\r':
  24. case '\n':
  25. case ',':
  26. return 1;
  27. default:
  28. return 0;
  29. }
  30. }
  31. // split a text into words, respecting quotes
  32. inline int quoted_strings_splitter(char *str, char **words, int max_words, int (*custom_isspace)(char), char *recover_input, char **recover_location, int max_recover)
  33. {
  34. char *s = str, quote = 0;
  35. int i = 0, j, rec = 0;
  36. char *recover = recover_input;
  37. // skip all white space
  38. while (unlikely(custom_isspace(*s)))
  39. s++;
  40. // check for quote
  41. if (unlikely(*s == '\'' || *s == '"')) {
  42. quote = *s; // remember the quote
  43. s++; // skip the quote
  44. }
  45. // store the first word
  46. words[i++] = s;
  47. // while we have something
  48. while (likely(*s)) {
  49. // if it is escape
  50. if (unlikely(*s == '\\' && s[1])) {
  51. s += 2;
  52. continue;
  53. }
  54. // if it is quote
  55. else if (unlikely(*s == quote)) {
  56. quote = 0;
  57. if (recover && rec < max_recover) {
  58. recover_location[rec++] = s;
  59. *recover++ = *s;
  60. }
  61. *s = ' ';
  62. continue;
  63. }
  64. // if it is a space
  65. else if (unlikely(quote == 0 && custom_isspace(*s))) {
  66. // terminate the word
  67. if (recover && rec < max_recover) {
  68. if (!rec || (rec && recover_location[rec-1] != s)) {
  69. recover_location[rec++] = s;
  70. *recover++ = *s;
  71. }
  72. }
  73. *s++ = '\0';
  74. // skip all white space
  75. while (likely(custom_isspace(*s)))
  76. s++;
  77. // check for quote
  78. if (unlikely(*s == '\'' || *s == '"')) {
  79. quote = *s; // remember the quote
  80. s++; // skip the quote
  81. }
  82. // if we reached the end, stop
  83. if (unlikely(!*s))
  84. break;
  85. // store the next word
  86. if (likely(i < max_words))
  87. words[i++] = s;
  88. else
  89. break;
  90. }
  91. // anything else
  92. else
  93. s++;
  94. }
  95. // terminate the words
  96. j = i;
  97. while (likely(j < max_words))
  98. words[j++] = NULL;
  99. return i;
  100. }
  101. inline int pluginsd_initialize_plugin_directories()
  102. {
  103. char plugins_dirs[(FILENAME_MAX * 2) + 1];
  104. static char *plugins_dir_list = NULL;
  105. // Get the configuration entry
  106. if (likely(!plugins_dir_list)) {
  107. snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
  108. plugins_dir_list = strdupz(config_get(CONFIG_SECTION_DIRECTORIES, "plugins", plugins_dirs));
  109. }
  110. // Parse it and store it to plugin directories
  111. return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace, NULL, NULL, 0);
  112. }
  113. inline int pluginsd_split_words(char *str, char **words, int max_words, char *recover_input, char **recover_location, int max_recover)
  114. {
  115. return quoted_strings_splitter(str, words, max_words, pluginsd_space, recover_input, recover_location, max_recover);
  116. }
  117. static void pluginsd_worker_thread_cleanup(void *arg)
  118. {
  119. struct plugind *cd = (struct plugind *)arg;
  120. if (cd->enabled && !cd->obsolete) {
  121. cd->obsolete = 1;
  122. info("data collection thread exiting");
  123. if (cd->pid) {
  124. siginfo_t info;
  125. info("killing child process pid %d", cd->pid);
  126. if (killpid(cd->pid) != -1) {
  127. info("waiting for child process pid %d to exit...", cd->pid);
  128. waitid(P_PID, (id_t)cd->pid, &info, WEXITED);
  129. }
  130. cd->pid = 0;
  131. }
  132. }
  133. }
  134. #define SERIAL_FAILURES_THRESHOLD 10
  135. static void pluginsd_worker_thread_handle_success(struct plugind *cd)
  136. {
  137. if (likely(cd->successful_collections)) {
  138. sleep((unsigned int)cd->update_every);
  139. return;
  140. }
  141. if (likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  142. info(
  143. "'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  144. cd->fullfilename, cd->pid,
  145. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is now disabled.");
  146. sleep((unsigned int)(cd->update_every * 10));
  147. return;
  148. }
  149. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  150. error(
  151. "'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  152. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  153. cd->fullfilename, cd->pid, cd->serial_failures);
  154. cd->enabled = 0;
  155. return;
  156. }
  157. return;
  158. }
  159. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code)
  160. {
  161. if (worker_ret_code == -1) {
  162. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  163. cd->enabled = 0;
  164. return;
  165. }
  166. if (!cd->successful_collections) {
  167. error(
  168. "'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.", cd->fullfilename,
  169. cd->pid, worker_ret_code);
  170. cd->enabled = 0;
  171. return;
  172. }
  173. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  174. error(
  175. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  176. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  177. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is disabled.");
  178. sleep((unsigned int)(cd->update_every * 10));
  179. return;
  180. }
  181. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  182. error(
  183. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  184. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  185. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  186. cd->enabled = 0;
  187. return;
  188. }
  189. return;
  190. }
  191. #undef SERIAL_FAILURES_THRESHOLD
  192. void *pluginsd_worker_thread(void *arg)
  193. {
  194. worker_register("PLUGINSD");
  195. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  196. struct plugind *cd = (struct plugind *)arg;
  197. cd->obsolete = 0;
  198. size_t count = 0;
  199. while (!netdata_exit) {
  200. FILE *fp = mypopen(cd->cmd, &cd->pid);
  201. if (unlikely(!fp)) {
  202. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  203. break;
  204. }
  205. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  206. count = pluginsd_process(localhost, cd, fp, 0);
  207. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  208. killpid(cd->pid);
  209. int worker_ret_code = mypclose(fp, cd->pid);
  210. if (likely(worker_ret_code == 0))
  211. pluginsd_worker_thread_handle_success(cd);
  212. else
  213. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  214. cd->pid = 0;
  215. if (unlikely(!cd->enabled))
  216. break;
  217. }
  218. worker_unregister();
  219. netdata_thread_cleanup_pop(1);
  220. return NULL;
  221. }
  222. static void pluginsd_main_cleanup(void *data)
  223. {
  224. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  225. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  226. info("cleaning up...");
  227. struct plugind *cd;
  228. for (cd = pluginsd_root; cd; cd = cd->next) {
  229. if (cd->enabled && !cd->obsolete) {
  230. info("stopping plugin thread: %s", cd->id);
  231. netdata_thread_cancel(cd->thread);
  232. }
  233. }
  234. info("cleanup completed.");
  235. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  236. worker_unregister();
  237. }
  238. void *pluginsd_main(void *ptr)
  239. {
  240. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  241. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  242. int scan_frequency = (int)config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  243. if (scan_frequency < 1)
  244. scan_frequency = 1;
  245. // disable some plugins by default
  246. config_get_boolean(CONFIG_SECTION_PLUGINS, "slabinfo", CONFIG_BOOLEAN_NO);
  247. // store the errno for each plugins directory
  248. // so that we don't log broken directories on each loop
  249. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  250. while (!netdata_exit) {
  251. int idx;
  252. const char *directory_name;
  253. for (idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]); idx++) {
  254. if (unlikely(netdata_exit))
  255. break;
  256. errno = 0;
  257. DIR *dir = opendir(directory_name);
  258. if (unlikely(!dir)) {
  259. if (directory_errors[idx] != errno) {
  260. directory_errors[idx] = errno;
  261. error("cannot open plugins directory '%s'", directory_name);
  262. }
  263. continue;
  264. }
  265. struct dirent *file = NULL;
  266. while (likely((file = readdir(dir)))) {
  267. if (unlikely(netdata_exit))
  268. break;
  269. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  270. if (unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0))
  271. continue;
  272. int len = (int)strlen(file->d_name);
  273. if (unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN))
  274. continue;
  275. if (unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  276. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  277. continue;
  278. }
  279. char pluginname[CONFIG_MAX_NAME + 1];
  280. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  281. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  282. if (unlikely(!enabled)) {
  283. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  284. continue;
  285. }
  286. // check if it runs already
  287. struct plugind *cd;
  288. for (cd = pluginsd_root; cd; cd = cd->next)
  289. if (unlikely(strcmp(cd->filename, file->d_name) == 0))
  290. break;
  291. if (likely(cd && !cd->obsolete)) {
  292. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  293. continue;
  294. }
  295. // it is not running
  296. // allocate a new one, or use the obsolete one
  297. if (unlikely(!cd)) {
  298. cd = callocz(sizeof(struct plugind), 1);
  299. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  300. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  301. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  302. cd->enabled = enabled;
  303. cd->update_every = (int)config_get_number(cd->id, "update every", localhost->rrd_update_every);
  304. cd->started_t = now_realtime_sec();
  305. char *def = "";
  306. snprintfz(
  307. cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every,
  308. config_get(cd->id, "command options", def));
  309. // link it
  310. if (likely(pluginsd_root))
  311. cd->next = pluginsd_root;
  312. pluginsd_root = cd;
  313. // it is not currently running
  314. cd->obsolete = 1;
  315. if (cd->enabled) {
  316. char tag[NETDATA_THREAD_TAG_MAX + 1];
  317. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  318. // spawn a new thread for it
  319. netdata_thread_create(
  320. &cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  321. }
  322. }
  323. }
  324. closedir(dir);
  325. }
  326. sleep((unsigned int)scan_frequency);
  327. }
  328. netdata_thread_cleanup_pop(1);
  329. return NULL;
  330. }