functions_evloop.h 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT 10 // seconds
  40. typedef void (*functions_evloop_worker_execute_t)(const char *transaction, char *function, int timeout, bool *cancelled);
  41. struct functions_evloop_worker_job;
  42. struct functions_evloop_globals *functions_evloop_init(size_t worker_threads, const char *tag, netdata_mutex_t *stdout_mutex, bool *plugin_should_exit);
  43. void functions_evloop_add_function(struct functions_evloop_globals *wg, const char *function, functions_evloop_worker_execute_t cb, time_t default_timeout);
  44. #define pluginsd_function_result_begin_to_buffer(wb, transaction, code, content_type, expires) \
  45. buffer_sprintf(wb \
  46. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  47. , (transaction) ? (transaction) : "" \
  48. , (int)(code) \
  49. , (content_type) ? (content_type) : "" \
  50. , (long int)(expires) \
  51. )
  52. #define pluginsd_function_result_end_to_buffer(wb) \
  53. buffer_strcat(wb, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  54. #define pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires) \
  55. fprintf(stdout \
  56. , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \
  57. , (transaction) ? (transaction) : "" \
  58. , (int)(code) \
  59. , (content_type) ? (content_type) : "" \
  60. , (long int)(expires) \
  61. )
  62. #define pluginsd_function_result_end_to_stdout() \
  63. fprintf(stdout, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n")
  64. static inline void pluginsd_function_json_error_to_stdout(const char *transaction, int code, const char *msg) {
  65. char buffer[PLUGINSD_LINE_MAX + 1];
  66. json_escape_string(buffer, msg, PLUGINSD_LINE_MAX);
  67. pluginsd_function_result_begin_to_stdout(transaction, code, "application/json", now_realtime_sec());
  68. fprintf(stdout, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer);
  69. pluginsd_function_result_end_to_stdout();
  70. fflush(stdout);
  71. }
  72. static inline void pluginsd_function_result_to_stdout(const char *transaction, int code, const char *content_type, time_t expires, BUFFER *result) {
  73. pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires);
  74. fwrite(buffer_tostring(result), buffer_strlen(result), 1, stdout);
  75. pluginsd_function_result_end_to_stdout();
  76. fflush(stdout);
  77. }
  78. #endif //NETDATA_FUNCTIONS_EVLOOP_H