| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #ifndef NETDATA_FUNCTIONS_EVLOOP_H |
| 4 | #define NETDATA_FUNCTIONS_EVLOOP_H |
| 5 | |
| 6 | #include "../libnetdata.h" |
| 7 | |
| 8 | #define MAX_FUNCTION_PARAMETERS 1024 |
| 9 | #define PLUGINS_FUNCTIONS_TIMEOUT_DEFAULT 10 // seconds |
| 10 | |
| 11 | // plugins.d 1st version of the external plugins and streaming protocol |
| 12 | #define PLUGINSD_KEYWORD_CHART "CHART" |
| 13 | #define PLUGINSD_KEYWORD_CHART_DEFINITION_END "CHART_DEFINITION_END" |
| 14 | #define PLUGINSD_KEYWORD_DIMENSION "DIMENSION" |
| 15 | #define PLUGINSD_KEYWORD_BEGIN "BEGIN" |
| 16 | #define PLUGINSD_KEYWORD_SET "SET" |
| 17 | #define PLUGINSD_KEYWORD_END "END" |
| 18 | #define PLUGINSD_KEYWORD_FLUSH "FLUSH" |
| 19 | #define PLUGINSD_KEYWORD_DISABLE "DISABLE" |
| 20 | #define PLUGINSD_KEYWORD_VARIABLE "VARIABLE" |
| 21 | #define PLUGINSD_KEYWORD_LABEL "LABEL" |
| 22 | #define PLUGINSD_KEYWORD_OVERWRITE "OVERWRITE" |
| 23 | #define PLUGINSD_KEYWORD_CLABEL "CLABEL" |
| 24 | #define PLUGINSD_KEYWORD_CLABEL_COMMIT "CLABEL_COMMIT" |
| 25 | #define PLUGINSD_KEYWORD_EXIT "EXIT" |
| 26 | |
| 27 | // high-speed versions of BEGIN, SET, END |
| 28 | #define PLUGINSD_KEYWORD_BEGIN_V2 "BEGIN2" |
| 29 | #define PLUGINSD_KEYWORD_SET_V2 "SET2" |
| 30 | #define PLUGINSD_KEYWORD_END_V2 "END2" |
| 31 | |
| 32 | // super high-speed versions of BEGIN, SET, END have this as first parameter |
| 33 | // enabled with the streaming capability STREAM_CAP_SLOTS |
| 34 | #define PLUGINSD_KEYWORD_SLOT "SLOT" // to change the length of this, update pluginsd_extract_chart_slot() too |
| 35 | |
| 36 | // virtual hosts (only for external plugins - for streaming virtual hosts are like all other hosts) |
| 37 | #define PLUGINSD_KEYWORD_HOST_DEFINE "HOST_DEFINE" |
| 38 | #define PLUGINSD_KEYWORD_HOST_DEFINE_END "HOST_DEFINE_END" |
| 39 | #define PLUGINSD_KEYWORD_HOST_LABEL "HOST_LABEL" |
| 40 | #define PLUGINSD_KEYWORD_HOST "HOST" |
| 41 | |
| 42 | // replication |
| 43 | // enabled with STREAM_CAP_REPLICATION |
| 44 | #define PLUGINSD_KEYWORD_REPLAY_CHART "REPLAY_CHART" |
| 45 | #define PLUGINSD_KEYWORD_REPLAY_BEGIN "RBEGIN" |
| 46 | #define PLUGINSD_KEYWORD_REPLAY_SET "RSET" |
| 47 | #define PLUGINSD_KEYWORD_REPLAY_RRDDIM_STATE "RDSTATE" |
| 48 | #define PLUGINSD_KEYWORD_REPLAY_RRDSET_STATE "RSSTATE" |
| 49 | #define PLUGINSD_KEYWORD_REPLAY_END "REND" |
| 50 | |
| 51 | // plugins.d accepts these for functions (from external plugins or streaming children) |
| 52 | // related to STREAM_CAP_FUNCTIONS, STREAM_CAP_PROGRESS |
| 53 | #define PLUGINSD_KEYWORD_FUNCTION "FUNCTION" // define a function |
| 54 | #define PLUGINSD_KEYWORD_FUNCTION_PROGRESS "FUNCTION_PROGRESS" // send updates about function progress |
| 55 | #define PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN "FUNCTION_RESULT_BEGIN" // the result of a function transaction |
| 56 | #define PLUGINSD_KEYWORD_FUNCTION_RESULT_END "FUNCTION_RESULT_END" // the end of the result of a func. trans. |
| 57 | |
| 58 | // plugins.d sends these for functions (to external plugins or streaming children) |
| 59 | // related to STREAM_CAP_FUNCTIONS, STREAM_CAP_PROGRESS |
| 60 | #define PLUGINSD_CALL_FUNCTION "FUNCTION" // call a function to a plugin or remote host |
| 61 | #define PLUGINSD_CALL_FUNCTION_PAYLOAD_BEGIN "FUNCTION_PAYLOAD" // call a function with a payload |
| 62 | #define PLUGINSD_CALL_FUNCTION_PAYLOAD_END "FUNCTION_PAYLOAD_END" // function payload ends |
| 63 | #define PLUGINSD_CALL_FUNCTION_CANCEL "FUNCTION_CANCEL" // cancel a running function transaction |
| 64 | #define PLUGINSD_CALL_FUNCTION_PROGRESS "FUNCTION_PROGRESS" // let the function know the user is waiting |
| 65 | |
| 66 | #define PLUGINSD_CALL_QUIT "QUIT" // ask the plugin to quit |
| 67 | |
| 68 | // dyncfg |
| 69 | // enabled with STREAM_CAP_DYNCFG |
| 70 | #define PLUGINSD_KEYWORD_CONFIG "CONFIG" |
| 71 | #define PLUGINSD_KEYWORD_CONFIG_ACTION_CREATE "create" |
| 72 | #define PLUGINSD_KEYWORD_CONFIG_ACTION_DELETE "delete" |
| 73 | #define PLUGINSD_KEYWORD_CONFIG_ACTION_STATUS "status" |
| 74 | #define PLUGINSD_FUNCTION_CONFIG "config" |
| 75 | |
| 76 | // claiming |
| 77 | #define PLUGINSD_KEYWORD_NODE_ID "NODE_ID" |
| 78 | #define PLUGINSD_KEYWORD_CLAIMED_ID "CLAIMED_ID" |
| 79 | |
| 80 | #define PLUGINSD_KEYWORD_JSON "JSON" |
| 81 | #define PLUGINSD_KEYWORD_JSON_END "JSON_PAYLOAD_END" |
| 82 | #define PLUGINSD_KEYWORD_JSON_CMD_STREAM_PATH "STREAM_PATH" |
| 83 | #define PLUGINSD_KEYWORD_JSON_CMD_ML_MODEL "ML_MODEL" |
| 84 | |
| 85 | // trust BEGIN timestamps from the plugin |
| 86 | #define PLUGINSD_KEYWORD_TRUST_DURATIONS "TRUST_DURATIONS" |
| 87 | |
| 88 | typedef void (*functions_evloop_worker_execute_t)(const char *transaction, char *function, usec_t *stop_monotonic_ut, |
| 89 | bool *cancelled, BUFFER *payload, HTTP_ACCESS access, |
| 90 | const char *source, void *data); |
| 91 | |
| 92 | struct functions_evloop_worker_job; |
| 93 | struct functions_evloop_globals *functions_evloop_init(size_t worker_threads, const char *tag, netdata_mutex_t *stdout_mutex, bool *plugin_should_exit, int *status); |
| 94 | void functions_evloop_add_function(struct functions_evloop_globals *wg, const char *function, functions_evloop_worker_execute_t cb, time_t default_timeout, void *data); |
| 95 | void functions_evloop_cancel_threads(struct functions_evloop_globals *wg); |
| 96 | |
| 97 | #define FUNCTIONS_EXTENDED_TIME_ON_PROGRESS_UT (10 * USEC_PER_SEC) |
| 98 | static inline void functions_stop_monotonic_update_on_progress(usec_t *stop_monotonic_ut) { |
| 99 | usec_t now_ut = now_monotonic_usec(); |
| 100 | if(now_ut + FUNCTIONS_EXTENDED_TIME_ON_PROGRESS_UT > *stop_monotonic_ut) { |
| 101 | nd_log(NDLS_DAEMON, NDLP_DEBUG, "Extending function timeout due to PROGRESS update..."); |
| 102 | __atomic_store_n(stop_monotonic_ut, now_ut + FUNCTIONS_EXTENDED_TIME_ON_PROGRESS_UT, __ATOMIC_RELAXED); |
| 103 | } |
| 104 | else |
| 105 | nd_log(NDLS_DAEMON, NDLP_DEBUG, "Received PROGRESS update..."); |
| 106 | } |
| 107 | |
| 108 | #define pluginsd_function_result_begin_to_buffer(wb, transaction, code, content_type, expires) \ |
| 109 | buffer_sprintf(wb \ |
| 110 | , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \ |
| 111 | , (transaction) ? (transaction) : "" \ |
| 112 | , (int)(code) \ |
| 113 | , (content_type) ? (content_type) : "" \ |
| 114 | , (long int)(expires) \ |
| 115 | ) |
| 116 | |
| 117 | #define pluginsd_function_result_end_to_buffer(wb) \ |
| 118 | buffer_strcat(wb, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n") |
| 119 | |
| 120 | #define pluginsd_function_result_begin_to_stdout(transaction, code, content_type, expires) \ |
| 121 | fprintf(stdout \ |
| 122 | , PLUGINSD_KEYWORD_FUNCTION_RESULT_BEGIN " \"%s\" %d \"%s\" %ld\n" \ |
| 123 | , (transaction) ? (transaction) : "" \ |
| 124 | , (int)(code) \ |
| 125 | , (content_type) ? (content_type) : "" \ |
| 126 | , (long int)(expires) \ |
| 127 | ) |
| 128 | |
| 129 | #define pluginsd_function_result_end_to_stdout() \ |
| 130 | fprintf(stdout, "\n" PLUGINSD_KEYWORD_FUNCTION_RESULT_END "\n") |
| 131 | |
| 132 | static inline void pluginsd_function_json_error_to_stdout(const char *transaction, int code, const char *msg) { |
| 133 | char buffer[PLUGINSD_LINE_MAX + 1]; |
| 134 | json_escape_string(buffer, msg, PLUGINSD_LINE_MAX); |
| 135 | |
| 136 | pluginsd_function_result_begin_to_stdout(transaction, code, "application/json", now_realtime_sec()); |
| 137 | fprintf(stdout, "{\"status\":%d,\"error_message\":\"%s\"}", code, buffer); |
| 138 | pluginsd_function_result_end_to_stdout(); |
| 139 | fflush(stdout); |
| 140 | } |
| 141 | |
| 142 | static inline void pluginsd_function_result_to_stdout(const char *transaction, BUFFER *result) { |
| 143 | pluginsd_function_result_begin_to_stdout(transaction, result->response_code, |
| 144 | content_type_id2string(result->content_type), |
| 145 | result->expires); |
| 146 | |
| 147 | fwrite(buffer_tostring(result), buffer_strlen(result), 1, stdout); |
| 148 | |
| 149 | pluginsd_function_result_end_to_stdout(); |
| 150 | fflush(stdout); |
| 151 | } |
| 152 | |
| 153 | static inline void pluginsd_function_progress_to_stdout(const char *transaction, size_t done, size_t all) { |
| 154 | fprintf(stdout, PLUGINSD_KEYWORD_FUNCTION_PROGRESS " '%s' %zu %zu\n", |
| 155 | transaction, done, all); |
| 156 | fflush(stdout); |
| 157 | } |
| 158 | |
| 159 | static inline void send_newline_and_flush(netdata_mutex_t *mutex) { |
| 160 | netdata_mutex_lock(mutex); |
| 161 | fprintf(stdout, "\n"); |
| 162 | fflush(stdout); |
| 163 | netdata_mutex_unlock(mutex); |
| 164 | } |
| 165 | |
| 166 | void functions_evloop_dyncfg_add(struct functions_evloop_globals *wg, const char *id, const char *path, |
| 167 | DYNCFG_STATUS status, DYNCFG_TYPE type, DYNCFG_SOURCE_TYPE source_type, const char *source, DYNCFG_CMDS cmds, |
| 168 | HTTP_ACCESS view_access, HTTP_ACCESS edit_access, |
| 169 | dyncfg_cb_t cb, void *data); |
| 170 | |
| 171 | void functions_evloop_dyncfg_del(struct functions_evloop_globals *wg, const char *id); |
| 172 | void functions_evloop_dyncfg_status(struct functions_evloop_globals *wg, const char *id, DYNCFG_STATUS status); |
| 173 | |
| 174 | #endif //NETDATA_FUNCTIONS_EVLOOP_H |