plugins_d.c 14 KB

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