plugins_d.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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);
  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. worker_unregister();
  44. netdata_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. netdata_spinlock_unlock(&cd->unsafe.spinlock);
  50. if (pid) {
  51. siginfo_t info;
  52. info("PLUGINSD: 'host:%s', killing data collection child process with pid %d",
  53. rrdhost_hostname(cd->host), pid);
  54. if (killpid(pid) != -1) {
  55. 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. 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. 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. 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. 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. 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. 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. error("PLUGINSD: 'host:%s', cannot popen(\"%s\", \"r\").", rrdhost_hostname(cd->host), cd->cmd);
  124. break;
  125. }
  126. 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. 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. info("PLUGINSD: cleaning up...");
  148. struct plugind *cd;
  149. for (cd = pluginsd_root; cd; cd = cd->next) {
  150. netdata_spinlock_lock(&cd->unsafe.spinlock);
  151. if (cd->unsafe.enabled && cd->unsafe.running && cd->unsafe.thread != 0) {
  152. info("PLUGINSD: 'host:%s', stopping plugin thread: %s",
  153. rrdhost_hostname(cd->host), cd->id);
  154. netdata_thread_cancel(cd->unsafe.thread);
  155. }
  156. netdata_spinlock_unlock(&cd->unsafe.spinlock);
  157. }
  158. 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. // store the errno for each plugins directory
  172. // so that we don't log broken directories on each loop
  173. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  174. while (service_running(SERVICE_COLLECTORS)) {
  175. int idx;
  176. const char *directory_name;
  177. for (idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]); idx++) {
  178. if (unlikely(!service_running(SERVICE_COLLECTORS)))
  179. break;
  180. errno = 0;
  181. DIR *dir = opendir(directory_name);
  182. if (unlikely(!dir)) {
  183. if (directory_errors[idx] != errno) {
  184. directory_errors[idx] = errno;
  185. error("cannot open plugins directory '%s'", directory_name);
  186. }
  187. continue;
  188. }
  189. struct dirent *file = NULL;
  190. while (likely((file = readdir(dir)))) {
  191. if (unlikely(!service_running(SERVICE_COLLECTORS)))
  192. break;
  193. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  194. if (unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0))
  195. continue;
  196. int len = (int)strlen(file->d_name);
  197. if (unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN))
  198. continue;
  199. if (unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  200. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  201. continue;
  202. }
  203. char pluginname[CONFIG_MAX_NAME + 1];
  204. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  205. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  206. if (unlikely(!enabled)) {
  207. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  208. continue;
  209. }
  210. // check if it runs already
  211. struct plugind *cd;
  212. for (cd = pluginsd_root; cd; cd = cd->next)
  213. if (unlikely(strcmp(cd->filename, file->d_name) == 0))
  214. break;
  215. if (likely(cd && plugin_is_running(cd))) {
  216. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  217. continue;
  218. }
  219. // it is not running
  220. // allocate a new one, or use the obsolete one
  221. if (unlikely(!cd)) {
  222. cd = callocz(sizeof(struct plugind), 1);
  223. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  224. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  225. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  226. cd->host = localhost;
  227. cd->unsafe.enabled = enabled;
  228. cd->unsafe.running = false;
  229. cd->update_every = (int)config_get_number(cd->id, "update every", localhost->rrd_update_every);
  230. cd->started_t = now_realtime_sec();
  231. char *def = "";
  232. snprintfz(
  233. cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every,
  234. config_get(cd->id, "command options", def));
  235. // link it
  236. DOUBLE_LINKED_LIST_PREPEND_ITEM_UNSAFE(pluginsd_root, cd, prev, next);
  237. if (plugin_is_enabled(cd)) {
  238. char tag[NETDATA_THREAD_TAG_MAX + 1];
  239. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PD[%s]", pluginname);
  240. // spawn a new thread for it
  241. netdata_thread_create(&cd->unsafe.thread,
  242. tag,
  243. NETDATA_THREAD_OPTION_DEFAULT,
  244. pluginsd_worker_thread,
  245. cd);
  246. }
  247. }
  248. }
  249. closedir(dir);
  250. }
  251. sleep((unsigned int)scan_frequency);
  252. }
  253. netdata_thread_cleanup_pop(1);
  254. return NULL;
  255. }