plugins_d.c 13 KB

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