| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "graphite.h" |
| 4 | |
| 5 | /** |
| 6 | * Initialize Graphite connector instance |
| 7 | * |
| 8 | * @param instance an instance data structure. |
| 9 | * @return Returns 0 on success, 1 on failure. |
| 10 | */ |
| 11 | int init_graphite_instance(struct instance *instance) |
| 12 | { |
| 13 | instance->worker = simple_connector_worker; |
| 14 | |
| 15 | struct simple_connector_config *connector_specific_config = callocz(1, sizeof(struct simple_connector_config)); |
| 16 | instance->config.connector_specific_config = (void *)connector_specific_config; |
| 17 | connector_specific_config->default_port = 2003; |
| 18 | |
| 19 | struct simple_connector_data *connector_specific_data = callocz(1, sizeof(struct simple_connector_data)); |
| 20 | instance->connector_specific_data = connector_specific_data; |
| 21 | |
| 22 | connector_specific_data->ssl = NETDATA_SSL_UNSET_CONNECTION; |
| 23 | if (instance->config.options & EXPORTING_OPTION_USE_TLS) { |
| 24 | netdata_ssl_initialize_ctx(NETDATA_SSL_EXPORTING_CTX); |
| 25 | } |
| 26 | |
| 27 | instance->start_batch_formatting = NULL; |
| 28 | instance->start_host_formatting = format_host_labels_graphite_plaintext; |
| 29 | instance->start_chart_formatting = NULL; |
| 30 | |
| 31 | if (EXPORTING_OPTIONS_DATA_SOURCE(instance->config.options) == EXPORTING_SOURCE_DATA_AS_COLLECTED) |
| 32 | instance->metric_formatting = format_dimension_collected_graphite_plaintext; |
| 33 | else |
| 34 | instance->metric_formatting = format_dimension_stored_graphite_plaintext; |
| 35 | |
| 36 | instance->end_chart_formatting = NULL; |
| 37 | instance->variables_formatting = NULL; |
| 38 | instance->end_host_formatting = flush_host_labels; |
| 39 | instance->end_batch_formatting = simple_connector_end_batch; |
| 40 | |
| 41 | if (instance->config.type == EXPORTING_CONNECTOR_TYPE_GRAPHITE_HTTP) |
| 42 | instance->prepare_header = graphite_http_prepare_header; |
| 43 | else |
| 44 | instance->prepare_header = NULL; |
| 45 | |
| 46 | instance->check_response = exporting_discard_response; |
| 47 | |
| 48 | instance->buffer = (void *)buffer_create(0, &netdata_buffers_statistics.buffers_exporters); |
| 49 | if (!instance->buffer) { |
| 50 | netdata_log_error("EXPORTING: cannot create buffer for graphite exporting connector instance %s", instance->config.name); |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | simple_connector_init(instance); |
| 55 | |
| 56 | if (netdata_mutex_init(&instance->mutex)) |
| 57 | return 1; |
| 58 | if (netdata_cond_init(&instance->cond_var)) |
| 59 | return 1; |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Copy a label value and substitute underscores in place of characters which can't be used in Graphite output |
| 66 | * |
| 67 | * @param dst a destination string. |
| 68 | * @param src a source string. |
| 69 | * @param len the maximum number of characters copied. |
| 70 | */ |
| 71 | |
| 72 | void sanitize_graphite_label_value(char *dst, const char *src, size_t len) |
| 73 | { |
| 74 | while (*src != '\0' && len) { |
| 75 | if (isspace((uint8_t)*src) || *src == ';' || *src == '~') |
| 76 | *dst++ = '_'; |
| 77 | else |
| 78 | *dst++ = *src; |
| 79 | src++; |
| 80 | len--; |
| 81 | } |
| 82 | *dst = '\0'; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Format host labels for JSON connector |
| 87 | * |
| 88 | * @param instance an instance data structure. |
| 89 | * @param host a data collecting host. |
| 90 | * @return Always returns 0. |
| 91 | */ |
| 92 | |
| 93 | int format_host_labels_graphite_plaintext(struct instance *instance, RRDHOST *host) |
| 94 | { |
| 95 | if (!instance->labels_buffer) |
| 96 | instance->labels_buffer = buffer_create(1024, &netdata_buffers_statistics.buffers_exporters); |
| 97 | |
| 98 | if (unlikely(!sending_labels_configured(instance))) |
| 99 | return 0; |
| 100 | |
| 101 | rrdlabels_to_buffer(host->rrdlabels, instance->labels_buffer, ";", "=", "", "", |
| 102 | exporting_labels_filter_callback, instance, |
| 103 | NULL, sanitize_graphite_label_value); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Format dimension using collected data for Graphite connector |
| 110 | * |
| 111 | * @param instance an instance data structure. |
| 112 | * @param rd a dimension. |
| 113 | * @return Always returns 0. |
| 114 | */ |
| 115 | int format_dimension_collected_graphite_plaintext(struct instance *instance, RRDDIM *rd) |
| 116 | { |
| 117 | RRDSET *st = rd->rrdset; |
| 118 | RRDHOST *host = st->rrdhost; |
| 119 | |
| 120 | char chart_name[RRD_ID_LENGTH_MAX + 1]; |
| 121 | exporting_name_copy( |
| 122 | chart_name, |
| 123 | (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st), |
| 124 | RRD_ID_LENGTH_MAX); |
| 125 | |
| 126 | char dimension_name[RRD_ID_LENGTH_MAX + 1]; |
| 127 | exporting_name_copy( |
| 128 | dimension_name, |
| 129 | (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd), |
| 130 | RRD_ID_LENGTH_MAX); |
| 131 | |
| 132 | if(rrddim_is_float(rd)) |
| 133 | buffer_sprintf( |
| 134 | instance->buffer, |
| 135 | "%s.%s.%s.%s%s " NETDATA_DOUBLE_FORMAT " %llu\n", |
| 136 | instance->config.prefix, |
| 137 | (host == localhost) ? instance->config.hostname : rrdhost_hostname(host), |
| 138 | chart_name, |
| 139 | dimension_name, |
| 140 | (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "", |
| 141 | rrddim_last_collected_as_double(rd), |
| 142 | (unsigned long long)rd->collector.last_collected_time.tv_sec); |
| 143 | else |
| 144 | buffer_sprintf( |
| 145 | instance->buffer, |
| 146 | "%s.%s.%s.%s%s " COLLECTED_NUMBER_FORMAT " %llu\n", |
| 147 | instance->config.prefix, |
| 148 | (host == localhost) ? instance->config.hostname : rrdhost_hostname(host), |
| 149 | chart_name, |
| 150 | dimension_name, |
| 151 | (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "", |
| 152 | (collected_number)rrddim_last_collected_raw_int(rd), |
| 153 | (unsigned long long)rd->collector.last_collected_time.tv_sec); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Format dimension using a calculated value from stored data for Graphite connector |
| 160 | * |
| 161 | * @param instance an instance data structure. |
| 162 | * @param rd a dimension. |
| 163 | * @return Always returns 0. |
| 164 | */ |
| 165 | int format_dimension_stored_graphite_plaintext(struct instance *instance, RRDDIM *rd) |
| 166 | { |
| 167 | RRDSET *st = rd->rrdset; |
| 168 | RRDHOST *host = st->rrdhost; |
| 169 | |
| 170 | char chart_name[RRD_ID_LENGTH_MAX + 1]; |
| 171 | exporting_name_copy( |
| 172 | chart_name, |
| 173 | (instance->config.options & EXPORTING_OPTION_SEND_NAMES && st->name) ? rrdset_name(st) : rrdset_id(st), |
| 174 | RRD_ID_LENGTH_MAX); |
| 175 | |
| 176 | char dimension_name[RRD_ID_LENGTH_MAX + 1]; |
| 177 | exporting_name_copy( |
| 178 | dimension_name, |
| 179 | (instance->config.options & EXPORTING_OPTION_SEND_NAMES && rd->name) ? rrddim_name(rd) : rrddim_id(rd), |
| 180 | RRD_ID_LENGTH_MAX); |
| 181 | |
| 182 | time_t last_t; |
| 183 | NETDATA_DOUBLE value = exporting_calculate_value_from_stored_data(instance, rd, &last_t); |
| 184 | |
| 185 | if(isnan(value)) |
| 186 | return 0; |
| 187 | |
| 188 | buffer_sprintf( |
| 189 | instance->buffer, |
| 190 | "%s.%s.%s.%s%s " NETDATA_DOUBLE_FORMAT " %llu\n", |
| 191 | instance->config.prefix, |
| 192 | (host == localhost) ? instance->config.hostname : rrdhost_hostname(host), |
| 193 | chart_name, |
| 194 | dimension_name, |
| 195 | (instance->labels_buffer) ? buffer_tostring(instance->labels_buffer) : "", |
| 196 | value, |
| 197 | (unsigned long long)last_t); |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Prepare HTTP header |
| 204 | * |
| 205 | * @param instance an instance data structure. |
| 206 | * @return Returns 0 on success, 1 on failure. |
| 207 | */ |
| 208 | void graphite_http_prepare_header(struct instance *instance) |
| 209 | { |
| 210 | struct simple_connector_data *simple_connector_data = instance->connector_specific_data; |
| 211 | |
| 212 | buffer_sprintf( |
| 213 | simple_connector_data->last_buffer->header, |
| 214 | "POST /api/put HTTP/1.1\r\n" |
| 215 | "Host: %s\r\n" |
| 216 | "%s" |
| 217 | "Content-Type: application/graphite\r\n" |
| 218 | "Content-Length: %lu\r\n" |
| 219 | "\r\n", |
| 220 | instance->config.destination, |
| 221 | simple_connector_data->auth_string ? simple_connector_data->auth_string : "", |
| 222 | (unsigned long int) buffer_strlen(simple_connector_data->last_buffer->buffer)); |
| 223 | |
| 224 | return; |
| 225 | } |