plugins_d.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 size_t pluginsd_initialize_plugin_directories()
  7. {
  8. char plugins_dirs[(FILENAME_MAX * 2) + 1];
  9. static char *plugins_dir_list = NULL;
  10. // Get the configuration entry
  11. if (likely(!plugins_dir_list)) {
  12. snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
  13. plugins_dir_list = strdupz(config_get(CONFIG_SECTION_DIRECTORIES, "plugins", plugins_dirs));
  14. }
  15. // Parse it and store it to plugin directories
  16. return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace, NULL, NULL, 0);
  17. }
  18. static void pluginsd_worker_thread_cleanup(void *arg)
  19. {
  20. struct plugind *cd = (struct plugind *)arg;
  21. if (cd->enabled && !cd->obsolete) {
  22. cd->obsolete = 1;
  23. info("data collection thread exiting");
  24. if (cd->pid) {
  25. siginfo_t info;
  26. info("killing child process pid %d", cd->pid);
  27. if (killpid(cd->pid) != -1) {
  28. info("waiting for child process pid %d to exit...", cd->pid);
  29. waitid(P_PID, (id_t)cd->pid, &info, WEXITED);
  30. }
  31. cd->pid = 0;
  32. }
  33. }
  34. }
  35. #define SERIAL_FAILURES_THRESHOLD 10
  36. static void pluginsd_worker_thread_handle_success(struct plugind *cd)
  37. {
  38. if (likely(cd->successful_collections)) {
  39. sleep((unsigned int)cd->update_every);
  40. return;
  41. }
  42. if (likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  43. info(
  44. "'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  45. cd->fullfilename, cd->pid,
  46. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is now disabled.");
  47. sleep((unsigned int)(cd->update_every * 10));
  48. return;
  49. }
  50. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  51. error(
  52. "'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  53. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  54. cd->fullfilename, cd->pid, cd->serial_failures);
  55. cd->enabled = 0;
  56. return;
  57. }
  58. return;
  59. }
  60. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code)
  61. {
  62. if (worker_ret_code == -1) {
  63. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  64. cd->enabled = 0;
  65. return;
  66. }
  67. if (!cd->successful_collections) {
  68. error(
  69. "'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.", cd->fullfilename,
  70. cd->pid, worker_ret_code);
  71. cd->enabled = 0;
  72. return;
  73. }
  74. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  75. error(
  76. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  77. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  78. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is disabled.");
  79. sleep((unsigned int)(cd->update_every * 10));
  80. return;
  81. }
  82. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  83. error(
  84. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  85. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  86. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  87. cd->enabled = 0;
  88. return;
  89. }
  90. return;
  91. }
  92. #undef SERIAL_FAILURES_THRESHOLD
  93. void *pluginsd_worker_thread(void *arg)
  94. {
  95. worker_register("PLUGINSD");
  96. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  97. struct plugind *cd = (struct plugind *)arg;
  98. cd->obsolete = 0;
  99. size_t count = 0;
  100. while (!netdata_exit) {
  101. FILE *fp_child_input = NULL;
  102. FILE *fp_child_output = netdata_popen(cd->cmd, &cd->pid, &fp_child_input);
  103. if (unlikely(!fp_child_input || !fp_child_output)) {
  104. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  105. break;
  106. }
  107. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  108. count = pluginsd_process(localhost, cd, fp_child_input, fp_child_output, 0);
  109. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  110. killpid(cd->pid);
  111. int worker_ret_code = netdata_pclose(fp_child_input, fp_child_output, cd->pid);
  112. if (likely(worker_ret_code == 0))
  113. pluginsd_worker_thread_handle_success(cd);
  114. else
  115. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  116. cd->pid = 0;
  117. if (unlikely(!cd->enabled))
  118. break;
  119. }
  120. worker_unregister();
  121. netdata_thread_cleanup_pop(1);
  122. return NULL;
  123. }
  124. static void pluginsd_main_cleanup(void *data)
  125. {
  126. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  127. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  128. info("cleaning up...");
  129. struct plugind *cd;
  130. for (cd = pluginsd_root; cd; cd = cd->next) {
  131. if (cd->enabled && !cd->obsolete) {
  132. info("stopping plugin thread: %s", cd->id);
  133. netdata_thread_cancel(cd->thread);
  134. }
  135. }
  136. info("cleanup completed.");
  137. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  138. worker_unregister();
  139. }
  140. void *pluginsd_main(void *ptr)
  141. {
  142. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  143. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  144. int scan_frequency = (int)config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  145. if (scan_frequency < 1)
  146. scan_frequency = 1;
  147. // disable some plugins by default
  148. config_get_boolean(CONFIG_SECTION_PLUGINS, "slabinfo", CONFIG_BOOLEAN_NO);
  149. // store the errno for each plugins directory
  150. // so that we don't log broken directories on each loop
  151. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  152. while (!netdata_exit) {
  153. int idx;
  154. const char *directory_name;
  155. for (idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]); idx++) {
  156. if (unlikely(netdata_exit))
  157. break;
  158. errno = 0;
  159. DIR *dir = opendir(directory_name);
  160. if (unlikely(!dir)) {
  161. if (directory_errors[idx] != errno) {
  162. directory_errors[idx] = errno;
  163. error("cannot open plugins directory '%s'", directory_name);
  164. }
  165. continue;
  166. }
  167. struct dirent *file = NULL;
  168. while (likely((file = readdir(dir)))) {
  169. if (unlikely(netdata_exit))
  170. break;
  171. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  172. if (unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0))
  173. continue;
  174. int len = (int)strlen(file->d_name);
  175. if (unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN))
  176. continue;
  177. if (unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  178. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  179. continue;
  180. }
  181. char pluginname[CONFIG_MAX_NAME + 1];
  182. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  183. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  184. if (unlikely(!enabled)) {
  185. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  186. continue;
  187. }
  188. // check if it runs already
  189. struct plugind *cd;
  190. for (cd = pluginsd_root; cd; cd = cd->next)
  191. if (unlikely(strcmp(cd->filename, file->d_name) == 0))
  192. break;
  193. if (likely(cd && !cd->obsolete)) {
  194. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  195. continue;
  196. }
  197. // it is not running
  198. // allocate a new one, or use the obsolete one
  199. if (unlikely(!cd)) {
  200. cd = callocz(sizeof(struct plugind), 1);
  201. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  202. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  203. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  204. cd->enabled = enabled;
  205. cd->update_every = (int)config_get_number(cd->id, "update every", localhost->rrd_update_every);
  206. cd->started_t = now_realtime_sec();
  207. char *def = "";
  208. snprintfz(
  209. cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every,
  210. config_get(cd->id, "command options", def));
  211. // link it
  212. if (likely(pluginsd_root))
  213. cd->next = pluginsd_root;
  214. pluginsd_root = cd;
  215. // it is not currently running
  216. cd->obsolete = 1;
  217. if (cd->enabled) {
  218. char tag[NETDATA_THREAD_TAG_MAX + 1];
  219. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  220. // spawn a new thread for it
  221. netdata_thread_create(
  222. &cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  223. }
  224. }
  225. }
  226. closedir(dir);
  227. }
  228. sleep((unsigned int)scan_frequency);
  229. }
  230. netdata_thread_cleanup_pop(1);
  231. return NULL;
  232. }