functions_evloop.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_FUNCTIONS_EVLOOP_H
  3. #define NETDATA_FUNCTIONS_EVLOOP_H
  4. #include "../libnetdata.h"
  5. #define PLUGINSD_KEYWORD_CHART "CHART"
  6. #define PLUGINSD_KEYWORD_CHART_DEFINITION_END "CHART_DEFINITION_END"
  7. #define PLUGINSD_KEYWORD_DIMENSION "DIMENSION"
  8. #define PLUGINSD_KEYWORD_BEGIN "BEGIN"
  9. #define PLUGINSD_KEYWORD_SET "SET"
  10. #define PLUGINSD_KEYWORD_END "END"
  11. #define PLUGINSD_KEYWORD_FLUSH "FLUSH"
  12. #define PLUGINSD_KEYWORD_DISABLE "DISABLE"
  13. #define PLUGINSD_KEYWORD_VARIABLE "VARIABLE"
  14. #define PLUGINSD_KEYWORD_LABEL "LABEL"
  15. #define PLUGINSD_KEYWORD_OVERWRITE "OVERWRITE"
  16. #define PLUGINSD_KEYWORD_CLABEL "CLABEL"
  17. #define PLUGINSD_KEYWORD_CLABEL_COMMIT "CLABEL_COMMIT"
  18. #define PLUGINSD_KEYWORD_FUNCTION "FUNCTION"
  19. #define PLUGINSD_KEYWORD_FUNCTION_CANCEL "FUNCTION_CANCEL"
  20. #define PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN "FUNCTION_RESULT_BEGIN"
  21. #define PLUGINSD_KEYWORD_FUNCTION_RESULT_END "FUNCTION_RESULT_END"
  22. #define PLUGINSD_KEYWORD_REPLAY_CHART "REPLAY_CHART"
  23. #define PLUGINSD_KEYWORD_REPLAY_BEGIN "RBEGIN"
  24. #define PLUGINSD_KEYWORD_REPLAY_SET "RSET"
  25. #define PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE "RDSTATE"
  26. #define PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE "RSSTATE"
  27. #define PLUGINSD_KEYWORD_REPLAY_END "REND"
  28. #define PLUGINSD_KEYWORD_BEGIN_V2 "BEGIN2"
  29. #define PLUGINSD_KEYWORD_SET_V2 "SET2"
  30. #define PLUGINSD_KEYWORD_END_V2 "END2"
  31. #define PLUGINSD_KEYWORD_HOST_DEFINE "HOST_DEFINE"
  32. #define PLUGINSD_KEYWORD_HOST_DEFINE_END "HOST_DEFINE_END"
  33. #define PLUGINSD_KEYWORD_HOST_LABEL "HOST_LABEL"
  34. #define PLUGINSD_KEYWORD_HOST "HOST"
  35. #define PLUGINSD_KEYWORD_DYNCFG_ENABLE "DYNCFG_ENABLE"
  36. #define PLUGINSD_KEYWORD_DYNCFG_REGISTER_MODULE "DYNCFG_REGISTER_MODULE"
  37. #define PLUGINSD_KEYWORD_REPORT_JOB_STATUS "REPORT_JOB_STATUS"
  38. #define PLUGINSD_KEYWORD_EXIT "EXIT"
  39. #define PLUGINSD_KEYWORD_SLOT "SLOT" // to change the length of this, update pluginsd_extract_chart_slot() too
  40. #define PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT 10 // seconds
  41. typedef void (*functions_evloop_worker_execute_t)(const char *transaction, char *function, int timeout, bool *cancelled);
  42. struct functions_evloop_worker_job;
  43. struct functions_evloop_globals *functions_evloop_init(size_t worker_threads, const char *tag, netdata_mutex_t *stdout_mutex, bool *plugin_should_exit);
  44. void functions_evloop_add_function(struct functions_evloop_globals *wg, const char *function, functions_evloop_worker_execute_t cb, time_t default_timeout);
  45. void functions_evloop_cancel_threads(struct functions_evloop_globals *wg);
  46. #define pluginsd_function_result_begin_to_buffer(wb, transaction, code, content_type, expires) \
  47. buffer_sprintf(wb \
  48. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  49. , (transaction) ? (transaction) : "" \
  50. , (int)(code) \
  51. , (content_type) ? (content_type) : "" \
  52. , (long int)(expires) \
  53. )
  54. #define pluginsd_function_result_end_to_buffer(wb) \
  55. buffer_strcat(wb, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  56. #define pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires) \
  57. fprintf(stdout \
  58. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  59. , (transaction) ? (transaction) : "" \
  60. , (int)(code) \
  61. , (content_type) ? (content_type) : "" \
  62. , (long int)(expires) \
  63. )
  64. #define pluginsd_function_result_end_to_stdout() \
  65. fprintf(stdout, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  66. static inline void pluginsd_function_json_error_to_stdout(const char *transaction, int code, const char *msg) {
  67. char buffer[PLUGINSD_LINE_MAX + 1];
  68. json_escape_string(buffer, msg, PLUGINSD_LINE_MAX);
  69. pluginsd_function_result_begin_to_stdout(transaction, code, "application/json", now_realtime_sec());
  70. fprintf(stdout, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer);
  71. pluginsd_function_result_end_to_stdout();
  72. fflush(stdout);
  73. }
  74. static inline void pluginsd_function_result_to_stdout(const char *transaction, int code, const char *content_type, time_t expires, BUFFER *result) {
  75. pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires);
  76. fwrite(buffer_tostring(result), buffer_strlen(result), 1, stdout);
  77. pluginsd_function_result_end_to_stdout();
  78. fflush(stdout);
  79. }
  80. #endif //NETDATA_FUNCTIONS_EVLOOP_H