plugins_d.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_PLUGINS_D_H
  3. #define NETDATA_PLUGINS_D_H 1
  4. #include "daemon/common.h"
  5. #define PLUGINSD_FILE_SUFFIX ".plugin"
  6. #define PLUGINSD_FILE_SUFFIX_LEN strlen(PLUGINSD_FILE_SUFFIX)
  7. #define PLUGINSD_CMD_MAX (FILENAME_MAX*2)
  8. #define PLUGINSD_STOCK_PLUGINS_DIRECTORY_PATH 0
  9. #define PLUGINSD_KEYWORD_CHART "CHART"
  10. #define PLUGINSD_KEYWORD_CHART_DEFINITION_END "CHART_DEFINITION_END"
  11. #define PLUGINSD_KEYWORD_DIMENSION "DIMENSION"
  12. #define PLUGINSD_KEYWORD_BEGIN "BEGIN"
  13. #define PLUGINSD_KEYWORD_SET "SET"
  14. #define PLUGINSD_KEYWORD_END "END"
  15. #define PLUGINSD_KEYWORD_FLUSH "FLUSH"
  16. #define PLUGINSD_KEYWORD_DISABLE "DISABLE"
  17. #define PLUGINSD_KEYWORD_VARIABLE "VARIABLE"
  18. #define PLUGINSD_KEYWORD_LABEL "LABEL"
  19. #define PLUGINSD_KEYWORD_OVERWRITE "OVERWRITE"
  20. #define PLUGINSD_KEYWORD_CLABEL "CLABEL"
  21. #define PLUGINSD_KEYWORD_CLABEL_COMMIT "CLABEL_COMMIT"
  22. #define PLUGINSD_KEYWORD_FUNCTION "FUNCTION"
  23. #define PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN "FUNCTION_RESULT_BEGIN"
  24. #define PLUGINSD_KEYWORD_FUNCTION_RESULT_END "FUNCTION_RESULT_END"
  25. #define PLUGINSD_KEYWORD_REPLAY_CHART "REPLAY_CHART"
  26. #define PLUGINSD_KEYWORD_REPLAY_BEGIN "RBEGIN"
  27. #define PLUGINSD_KEYWORD_REPLAY_SET "RSET"
  28. #define PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE "RDSTATE"
  29. #define PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE "RSSTATE"
  30. #define PLUGINSD_KEYWORD_REPLAY_END "REND"
  31. #define PLUGINSD_KEYWORD_BEGIN_V2 "BEGIN2"
  32. #define PLUGINSD_KEYWORD_SET_V2 "SET2"
  33. #define PLUGINSD_KEYWORD_END_V2 "END2"
  34. #define PLUGINSD_KEYWORD_HOST_DEFINE "HOST_DEFINE"
  35. #define PLUGINSD_KEYWORD_HOST_DEFINE_END "HOST_DEFINE_END"
  36. #define PLUGINSD_KEYWORD_HOST_LABEL "HOST_LABEL"
  37. #define PLUGINSD_KEYWORD_HOST "HOST"
  38. #define PLUGINSD_KEYWORD_DYNCFG_ENABLE "DYNCFG_ENABLE"
  39. #define PLUGINSD_KEYWORD_DYNCFG_REGISTER_MODULE "DYNCFG_REGISTER_MODULE"
  40. #define PLUGINSD_KEYWORD_REPORT_JOB_STATUS "REPORT_JOB_STATUS"
  41. #define PLUGINSD_KEYWORD_EXIT "EXIT"
  42. #define PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT 10 // seconds
  43. #define PLUGINSD_LINE_MAX_SSL_READ 512
  44. #define PLUGINSD_MAX_WORDS 20
  45. #define PLUGINSD_MAX_DIRECTORIES 20
  46. extern char *plugin_directories[PLUGINSD_MAX_DIRECTORIES];
  47. struct plugind {
  48. char id[CONFIG_MAX_NAME+1]; // config node id
  49. char filename[FILENAME_MAX+1]; // just the filename
  50. char fullfilename[FILENAME_MAX+1]; // with path
  51. char cmd[PLUGINSD_CMD_MAX+1]; // the command that it executes
  52. size_t successful_collections; // the number of times we have seen
  53. // values collected from this plugin
  54. size_t serial_failures; // the number of times the plugin started
  55. // without collecting values
  56. RRDHOST *host; // the host the plugin collects data for
  57. int update_every; // the plugin default data collection frequency
  58. struct {
  59. SPINLOCK spinlock;
  60. bool running; // do not touch this structure after setting this to 1
  61. bool enabled; // if this is enabled or not
  62. netdata_thread_t thread;
  63. pid_t pid;
  64. } unsafe;
  65. time_t started_t;
  66. const DICTIONARY_ITEM *cfg_dict_item;
  67. struct configurable_plugin *configuration;
  68. struct plugind *prev;
  69. struct plugind *next;
  70. };
  71. extern struct plugind *pluginsd_root;
  72. size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp_plugin_input, FILE *fp_plugin_output, int trust_durations);
  73. void pluginsd_process_thread_cleanup(void *ptr);
  74. size_t pluginsd_initialize_plugin_directories();
  75. #define pluginsd_function_result_begin_to_buffer(wb, transaction, code, content_type, expires) \
  76. buffer_sprintf(wb \
  77. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  78. , (transaction) ? (transaction) : "" \
  79. , (int)(code) \
  80. , (content_type) ? (content_type) : "" \
  81. , (long int)(expires) \
  82. )
  83. #define pluginsd_function_result_end_to_buffer(wb) \
  84. buffer_strcat(wb, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  85. #define pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires) \
  86. fprintf(stdout \
  87. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  88. , (transaction) ? (transaction) : "" \
  89. , (int)(code) \
  90. , (content_type) ? (content_type) : "" \
  91. , (long int)(expires) \
  92. )
  93. #define pluginsd_function_result_end_to_stdout() \
  94. fprintf(stdout, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  95. static inline void pluginsd_function_json_error(const char *transaction, int code, const char *msg) {
  96. char buffer[PLUGINSD_LINE_MAX + 1];
  97. json_escape_string(buffer, msg, PLUGINSD_LINE_MAX);
  98. pluginsd_function_result_begin_to_stdout(transaction, code, "application/json", now_realtime_sec());
  99. fprintf(stdout, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer);
  100. pluginsd_function_result_end_to_stdout();
  101. }
  102. #endif /* NETDATA_PLUGINS_D_H */