plugins_d.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 int pluginsd_space(char c) {
  7. switch(c) {
  8. case ' ':
  9. case '\t':
  10. case '\r':
  11. case '\n':
  12. case '=':
  13. return 1;
  14. default:
  15. return 0;
  16. }
  17. }
  18. inline int config_isspace(char c)
  19. {
  20. switch (c) {
  21. case ' ':
  22. case '\t':
  23. case '\r':
  24. case '\n':
  25. case ',':
  26. return 1;
  27. default:
  28. return 0;
  29. }
  30. }
  31. // split a text into words, respecting quotes
  32. inline int quoted_strings_splitter(char *str, char **words, int max_words, int (*custom_isspace)(char), char *recover_input, char **recover_location, int max_recover)
  33. {
  34. char *s = str, quote = 0;
  35. int i = 0, j, rec = 0;
  36. char *recover = recover_input;
  37. // skip all white space
  38. while (unlikely(custom_isspace(*s)))
  39. s++;
  40. // check for quote
  41. if (unlikely(*s == '\'' || *s == '"')) {
  42. quote = *s; // remember the quote
  43. s++; // skip the quote
  44. }
  45. // store the first word
  46. words[i++] = s;
  47. // while we have something
  48. while (likely(*s)) {
  49. // if it is escape
  50. if (unlikely(*s == '\\' && s[1])) {
  51. s += 2;
  52. continue;
  53. }
  54. // if it is quote
  55. else if (unlikely(*s == quote)) {
  56. quote = 0;
  57. if (recover && rec < max_recover) {
  58. recover_location[rec++] = s;
  59. *recover++ = *s;
  60. }
  61. *s = ' ';
  62. continue;
  63. }
  64. // if it is a space
  65. else if (unlikely(quote == 0 && custom_isspace(*s))) {
  66. // terminate the word
  67. if (recover && rec < max_recover) {
  68. if (!rec || (rec && recover_location[rec-1] != s)) {
  69. recover_location[rec++] = s;
  70. *recover++ = *s;
  71. }
  72. }
  73. *s++ = '\0';
  74. // skip all white space
  75. while (likely(custom_isspace(*s)))
  76. s++;
  77. // check for quote
  78. if (unlikely(*s == '\'' || *s == '"')) {
  79. quote = *s; // remember the quote
  80. s++; // skip the quote
  81. }
  82. // if we reached the end, stop
  83. if (unlikely(!*s))
  84. break;
  85. // store the next word
  86. if (likely(i < max_words))
  87. words[i++] = s;
  88. else
  89. break;
  90. }
  91. // anything else
  92. else
  93. s++;
  94. }
  95. // terminate the words
  96. j = i;
  97. while (likely(j < max_words))
  98. words[j++] = NULL;
  99. return i;
  100. }
  101. inline int pluginsd_initialize_plugin_directories()
  102. {
  103. char plugins_dirs[(FILENAME_MAX * 2) + 1];
  104. static char *plugins_dir_list = NULL;
  105. // Get the configuration entry
  106. if (likely(!plugins_dir_list)) {
  107. snprintfz(plugins_dirs, FILENAME_MAX * 2, "\"%s\" \"%s/custom-plugins.d\"", PLUGINS_DIR, CONFIG_DIR);
  108. plugins_dir_list = strdupz(config_get(CONFIG_SECTION_GLOBAL, "plugins directory", plugins_dirs));
  109. }
  110. // Parse it and store it to plugin directories
  111. return quoted_strings_splitter(plugins_dir_list, plugin_directories, PLUGINSD_MAX_DIRECTORIES, config_isspace, NULL, NULL, 0);
  112. }
  113. inline int pluginsd_split_words(char *str, char **words, int max_words, char *recover_input, char **recover_location, int max_recover)
  114. {
  115. return quoted_strings_splitter(str, words, max_words, pluginsd_space, recover_input, recover_location, max_recover);
  116. }
  117. static void pluginsd_worker_thread_cleanup(void *arg)
  118. {
  119. struct plugind *cd = (struct plugind *)arg;
  120. if (cd->enabled && !cd->obsolete) {
  121. cd->obsolete = 1;
  122. info("data collection thread exiting");
  123. if (cd->pid) {
  124. siginfo_t info;
  125. info("killing child process pid %d", cd->pid);
  126. if (killpid(cd->pid) != -1) {
  127. info("waiting for child process pid %d to exit...", cd->pid);
  128. waitid(P_PID, (id_t)cd->pid, &info, WEXITED);
  129. }
  130. cd->pid = 0;
  131. }
  132. }
  133. }
  134. #define SERIAL_FAILURES_THRESHOLD 10
  135. static void pluginsd_worker_thread_handle_success(struct plugind *cd)
  136. {
  137. if (likely(cd->successful_collections)) {
  138. sleep((unsigned int)cd->update_every);
  139. return;
  140. }
  141. if (likely(cd->serial_failures <= SERIAL_FAILURES_THRESHOLD)) {
  142. info(
  143. "'%s' (pid %d) does not generate useful output but it reports success (exits with 0). %s.",
  144. cd->fullfilename, cd->pid,
  145. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is now disabled.");
  146. sleep((unsigned int)(cd->update_every * 10));
  147. return;
  148. }
  149. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  150. error(
  151. "'%s' (pid %d) does not generate useful output, although it reports success (exits with 0)."
  152. "We have tried to collect something %zu times - unsuccessfully. Disabling it.",
  153. cd->fullfilename, cd->pid, cd->serial_failures);
  154. cd->enabled = 0;
  155. return;
  156. }
  157. return;
  158. }
  159. static void pluginsd_worker_thread_handle_error(struct plugind *cd, int worker_ret_code)
  160. {
  161. if (worker_ret_code == -1) {
  162. info("'%s' (pid %d) was killed with SIGTERM. Disabling it.", cd->fullfilename, cd->pid);
  163. cd->enabled = 0;
  164. return;
  165. }
  166. if (!cd->successful_collections) {
  167. error(
  168. "'%s' (pid %d) exited with error code %d and haven't collected any data. Disabling it.", cd->fullfilename,
  169. cd->pid, worker_ret_code);
  170. cd->enabled = 0;
  171. return;
  172. }
  173. if (cd->serial_failures <= SERIAL_FAILURES_THRESHOLD) {
  174. error(
  175. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times). %s",
  176. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections,
  177. cd->enabled ? "Waiting a bit before starting it again." : "Will not start it again - it is disabled.");
  178. sleep((unsigned int)(cd->update_every * 10));
  179. return;
  180. }
  181. if (cd->serial_failures > SERIAL_FAILURES_THRESHOLD) {
  182. error(
  183. "'%s' (pid %d) exited with error code %d, but has given useful output in the past (%zu times)."
  184. "We tried to restart it %zu times, but it failed to generate data. Disabling it.",
  185. cd->fullfilename, cd->pid, worker_ret_code, cd->successful_collections, cd->serial_failures);
  186. cd->enabled = 0;
  187. return;
  188. }
  189. return;
  190. }
  191. #undef SERIAL_FAILURES_THRESHOLD
  192. void *pluginsd_worker_thread(void *arg)
  193. {
  194. netdata_thread_cleanup_push(pluginsd_worker_thread_cleanup, arg);
  195. struct plugind *cd = (struct plugind *)arg;
  196. cd->obsolete = 0;
  197. size_t count = 0;
  198. while (!netdata_exit) {
  199. FILE *fp = mypopen(cd->cmd, &cd->pid);
  200. if (unlikely(!fp)) {
  201. error("Cannot popen(\"%s\", \"r\").", cd->cmd);
  202. break;
  203. }
  204. info("connected to '%s' running on pid %d", cd->fullfilename, cd->pid);
  205. count = pluginsd_process(localhost, cd, fp, 0);
  206. error("'%s' (pid %d) disconnected after %zu successful data collections (ENDs).", cd->fullfilename, cd->pid, count);
  207. killpid(cd->pid);
  208. int worker_ret_code = mypclose(fp, cd->pid);
  209. if (likely(worker_ret_code == 0))
  210. pluginsd_worker_thread_handle_success(cd);
  211. else
  212. pluginsd_worker_thread_handle_error(cd, worker_ret_code);
  213. cd->pid = 0;
  214. if (unlikely(!cd->enabled))
  215. break;
  216. }
  217. netdata_thread_cleanup_pop(1);
  218. return NULL;
  219. }
  220. static void pluginsd_main_cleanup(void *data)
  221. {
  222. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)data;
  223. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  224. info("cleaning up...");
  225. struct plugind *cd;
  226. for (cd = pluginsd_root; cd; cd = cd->next) {
  227. if (cd->enabled && !cd->obsolete) {
  228. info("stopping plugin thread: %s", cd->id);
  229. netdata_thread_cancel(cd->thread);
  230. }
  231. }
  232. info("cleanup completed.");
  233. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  234. }
  235. void *pluginsd_main(void *ptr)
  236. {
  237. netdata_thread_cleanup_push(pluginsd_main_cleanup, ptr);
  238. int automatic_run = config_get_boolean(CONFIG_SECTION_PLUGINS, "enable running new plugins", 1);
  239. int scan_frequency = (int)config_get_number(CONFIG_SECTION_PLUGINS, "check for new plugins every", 60);
  240. if (scan_frequency < 1)
  241. scan_frequency = 1;
  242. // disable some plugins by default
  243. config_get_boolean(CONFIG_SECTION_PLUGINS, "slabinfo", CONFIG_BOOLEAN_NO);
  244. // store the errno for each plugins directory
  245. // so that we don't log broken directories on each loop
  246. int directory_errors[PLUGINSD_MAX_DIRECTORIES] = { 0 };
  247. while (!netdata_exit) {
  248. int idx;
  249. const char *directory_name;
  250. for (idx = 0; idx < PLUGINSD_MAX_DIRECTORIES && (directory_name = plugin_directories[idx]); idx++) {
  251. if (unlikely(netdata_exit))
  252. break;
  253. errno = 0;
  254. DIR *dir = opendir(directory_name);
  255. if (unlikely(!dir)) {
  256. if (directory_errors[idx] != errno) {
  257. directory_errors[idx] = errno;
  258. error("cannot open plugins directory '%s'", directory_name);
  259. }
  260. continue;
  261. }
  262. struct dirent *file = NULL;
  263. while (likely((file = readdir(dir)))) {
  264. if (unlikely(netdata_exit))
  265. break;
  266. debug(D_PLUGINSD, "examining file '%s'", file->d_name);
  267. if (unlikely(strcmp(file->d_name, ".") == 0 || strcmp(file->d_name, "..") == 0))
  268. continue;
  269. int len = (int)strlen(file->d_name);
  270. if (unlikely(len <= (int)PLUGINSD_FILE_SUFFIX_LEN))
  271. continue;
  272. if (unlikely(strcmp(PLUGINSD_FILE_SUFFIX, &file->d_name[len - (int)PLUGINSD_FILE_SUFFIX_LEN]) != 0)) {
  273. debug(D_PLUGINSD, "file '%s' does not end in '%s'", file->d_name, PLUGINSD_FILE_SUFFIX);
  274. continue;
  275. }
  276. char pluginname[CONFIG_MAX_NAME + 1];
  277. snprintfz(pluginname, CONFIG_MAX_NAME, "%.*s", (int)(len - PLUGINSD_FILE_SUFFIX_LEN), file->d_name);
  278. int enabled = config_get_boolean(CONFIG_SECTION_PLUGINS, pluginname, automatic_run);
  279. if (unlikely(!enabled)) {
  280. debug(D_PLUGINSD, "plugin '%s' is not enabled", file->d_name);
  281. continue;
  282. }
  283. // check if it runs already
  284. struct plugind *cd;
  285. for (cd = pluginsd_root; cd; cd = cd->next)
  286. if (unlikely(strcmp(cd->filename, file->d_name) == 0))
  287. break;
  288. if (likely(cd && !cd->obsolete)) {
  289. debug(D_PLUGINSD, "plugin '%s' is already running", cd->filename);
  290. continue;
  291. }
  292. // it is not running
  293. // allocate a new one, or use the obsolete one
  294. if (unlikely(!cd)) {
  295. cd = callocz(sizeof(struct plugind), 1);
  296. snprintfz(cd->id, CONFIG_MAX_NAME, "plugin:%s", pluginname);
  297. strncpyz(cd->filename, file->d_name, FILENAME_MAX);
  298. snprintfz(cd->fullfilename, FILENAME_MAX, "%s/%s", directory_name, cd->filename);
  299. cd->enabled = enabled;
  300. cd->update_every = (int)config_get_number(cd->id, "update every", localhost->rrd_update_every);
  301. cd->started_t = now_realtime_sec();
  302. char *def = "";
  303. snprintfz(
  304. cd->cmd, PLUGINSD_CMD_MAX, "exec %s %d %s", cd->fullfilename, cd->update_every,
  305. config_get(cd->id, "command options", def));
  306. // link it
  307. if (likely(pluginsd_root))
  308. cd->next = pluginsd_root;
  309. pluginsd_root = cd;
  310. // it is not currently running
  311. cd->obsolete = 1;
  312. if (cd->enabled) {
  313. char tag[NETDATA_THREAD_TAG_MAX + 1];
  314. snprintfz(tag, NETDATA_THREAD_TAG_MAX, "PLUGINSD[%s]", pluginname);
  315. // spawn a new thread for it
  316. netdata_thread_create(
  317. &cd->thread, tag, NETDATA_THREAD_OPTION_DEFAULT, pluginsd_worker_thread, cd);
  318. }
  319. }
  320. }
  321. closedir(dir);
  322. }
  323. sleep((unsigned int)scan_frequency);
  324. }
  325. netdata_thread_cleanup_pop(1);
  326. return NULL;
  327. }