plugins_d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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, 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. memset(&words[i], 0, (max_words - i) * sizeof (char *));
  97. return i;
  98. }
  99. inline int pluginsd_initialize_plugin_directories()
  100. {
  101. char plugins_dirs[(FILENAME_MAX * 2) + 1];
  102. static char *plugins_dir_list = NULL;
  103. // Get the configuration entry
  104. if (likely(!plugins_dir_list)) {
  105. snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
  106. plugins_dir_list = strdupz(config_get(CONFIG_SECTION_DIRECTORIES, "plugins", plugins_dirs));
  107. }
  108. // Parse it and store it to plugin directories
  109. return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace, NULL, NULL, 0);
  110. }
  111. inline int pluginsd_split_words(char *str, char **words, int max_words, char *recover_input, char **recover_location, int max_recover)
  112. {
  113. return quoted_strings_splitter(str, words, max_words, pluginsd_space, recover_input, recover_location, max_recover);
  114. }
  115. static void pluginsd_worker_thread_cleanup(void *arg)
  116. {
  117. struct plugind *cd = (struct plugind *)arg;
  118. if (cd->enabled && !cd->obsolete) {
  119. cd->obsolete = 1;
  120. info("data collection thread exiting");
  121. if (cd->pid) {
  122. siginfo_t info;
  123. info("killing child process pid %d", cd->pid);
  124. if (killpid(cd->pid) != -1) {
  125. info("waiting for child process pid %d to exit...", cd->pid);
  126. waitid(P_PID, (id_t)cd->pid, &info, WEXITED);
  127. }
  128. cd->pid = 0;
  129. }
  130. }
  131. }
  132. #define SERIAL_FAILURES_THRESHOLD 10
  133. static void pluginsd_worker_thread_handle_success(struct plugind *cd)
  134. {
  135. if (likely(cd->successful_collections)) {
  136. sleep((unsigned int)cd->update_every);
  137. return;
  138. }
  139. if (likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  140. info(
  141. "'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  142. cd->fullfilename, cd->pid,
  143. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is now disabled.");
  144. sleep((unsigned int)(cd->update_every * 10));
  145. return;
  146. }
  147. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  148. error(
  149. "'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  150. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  151. cd->fullfilename, cd->pid, cd->serial_failures);
  152. cd->enabled = 0;
  153. return;
  154. }
  155. return;
  156. }
  157. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code)
  158. {
  159. if (worker_ret_code == -1) {
  160. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  161. cd->enabled = 0;
  162. return;
  163. }
  164. if (!cd->successful_collections) {
  165. error(
  166. "'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.", cd->fullfilename,
  167. cd->pid, worker_ret_code);
  168. cd->enabled = 0;
  169. return;
  170. }
  171. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  172. error(
  173. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  174. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  175. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is disabled.");
  176. sleep((unsigned int)(cd->update_every * 10));
  177. return;
  178. }
  179. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  180. error(
  181. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  182. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  183. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  184. cd->enabled = 0;
  185. return;
  186. }
  187. return;
  188. }
  189. #undef SERIAL_FAILURES_THRESHOLD
  190. void *pluginsd_worker_thread(void *arg)
  191. {
  192. worker_register("PLUGINSD");
  193. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  194. struct plugind *cd = (struct plugind *)arg;
  195. cd->obsolete = 0;
  196. size_t count = 0;
  197. while (!netdata_exit) {
  198. FILE *fp = mypopen(cd->cmd, &cd->pid);
  199. if (unlikely(!fp)) {
  200. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  201. break;
  202. }
  203. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  204. count = pluginsd_process(localhost, cd, fp, 0);
  205. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  206. killpid(cd->pid);
  207. int worker_ret_code = mypclose(fp, cd->pid);
  208. if (likely(worker_ret_code == 0))
  209. pluginsd_worker_thread_handle_success(cd);
  210. else
  211. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  212. cd->pid = 0;
  213. if (unlikely(!cd->enabled))
  214. break;
  215. }
  216. worker_unregister();
  217. netdata_thread_cleanup_pop(1);
  218. return NULL;
  219. }
  220. static void pluginsd_main_cleanup(void *data)
  221. {
  222. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  223. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  224. info("cleaning up...");
  225. struct plugind *cd;
  226. for (cd = pluginsd_root; cd; cd = cd->next) {
  227. if (cd->enabled && !cd->obsolete) {
  228. info("stopping plugin thread: %s", cd->id);
  229. netdata_thread_cancel(cd->thread);
  230. }
  231. }
  232. info("cleanup completed.");
  233. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  234. worker_unregister();
  235. }
  236. void *pluginsd_main(void *ptr)
  237. {
  238. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  239. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  240. int scan_frequency = (int)config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  241. if (scan_frequency < 1)
  242. scan_frequency = 1;
  243. // disable some plugins by default
  244. config_get_boolean(CONFIG_SECTION_PLUGINS, "slabinfo", CONFIG_BOOLEAN_NO);
  245. // store the errno for each plugins directory
  246. // so that we don't log broken directories on each loop
  247. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  248. while (!netdata_exit) {
  249. int idx;
  250. const char *directory_name;
  251. for (idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]); idx++) {
  252. if (unlikely(netdata_exit))
  253. break;
  254. errno = 0;
  255. DIR *dir = opendir(directory_name);
  256. if (unlikely(!dir)) {
  257. if (directory_errors[idx] != errno) {
  258. directory_errors[idx] = errno;
  259. error("cannot open plugins directory '%s'", directory_name);
  260. }
  261. continue;
  262. }
  263. struct dirent *file = NULL;
  264. while (likely((file = readdir(dir)))) {
  265. if (unlikely(netdata_exit))
  266. break;
  267. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  268. if (unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0))
  269. continue;
  270. int len = (int)strlen(file->d_name);
  271. if (unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN))
  272. continue;
  273. if (unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  274. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  275. continue;
  276. }
  277. char pluginname[CONFIG_MAX_NAME + 1];
  278. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  279. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  280. if (unlikely(!enabled)) {
  281. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  282. continue;
  283. }
  284. // check if it runs already
  285. struct plugind *cd;
  286. for (cd = pluginsd_root; cd; cd = cd->next)
  287. if (unlikely(strcmp(cd->filename, file->d_name) == 0))
  288. break;
  289. if (likely(cd && !cd->obsolete)) {
  290. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  291. continue;
  292. }
  293. // it is not running
  294. // allocate a new one, or use the obsolete one
  295. if (unlikely(!cd)) {
  296. cd = callocz(sizeof(struct plugind), 1);
  297. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  298. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  299. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  300. cd->enabled = enabled;
  301. cd->update_every = (int)config_get_number(cd->id, "update every", localhost->rrd_update_every);
  302. cd->started_t = now_realtime_sec();
  303. char *def = "";
  304. snprintfz(
  305. cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every,
  306. config_get(cd->id, "command options", def));
  307. // link it
  308. if (likely(pluginsd_root))
  309. cd->next = pluginsd_root;
  310. pluginsd_root = cd;
  311. // it is not currently running
  312. cd->obsolete = 1;
  313. if (cd->enabled) {
  314. char tag[NETDATA_THREAD_TAG_MAX + 1];
  315. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  316. // spawn a new thread for it
  317. netdata_thread_create(
  318. &cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  319. }
  320. }
  321. }
  322. closedir(dir);
  323. }
  324. sleep((unsigned int)scan_frequency);
  325. }
  326. netdata_thread_cleanup_pop(1);
  327. return NULL;
  328. }