plugins_d.c 12 KB

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