@cryptotaxi247 / netdata-1 / commits / 462493d86

Added json as new backend with the structure:

{ "prefix": "netdata", "hostname": "example", "id": "apps.cpu", "subid": "media", "value": 0, "timestamp": 1487361156 }

Luis Felipe Dominguez Vega committed Feb 17, 2017 at 14:50 UTC 462493d864d80d7f9586cec8c74ce5499783d4cf
1 file changed +63
src/backends.c
+63
@@ -93,6 +93,37 @@ static inline int format_dimension_stored_opentsdb_telnet(BUFFER *b, const char
93 return 0;
94 }
95
96 +static inline int format_dimension_collected_json_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
97 + (void)host;
98 + (void)after;
99 + (void)before;
100 + (void)options;
101 + buffer_sprintf(b, "{"
102 + "\"prefix\":\"%s\","
103 + "\"hostname\":\"%s\","
104 + "\"id\":\"%s\","
105 + "\"subid\":\"%s\","
106 + "\"value\":" COLLECTED_NUMBER_FORMAT ","
107 + "\"timestamp\": %u}\n", prefix, hostname, st->id, rd->id, rd->last_collected_value, (uint32_t)rd->last_collected_time.tv_sec);
108 + return 1;
109 +}
110 +
111 +static inline int format_dimension_stored_json_plaintext(BUFFER *b, const char *prefix, RRDHOST *host, const char *hostname, RRDSET *st, RRDDIM *rd, time_t after, time_t before, uint32_t options) {
112 + (void)host;
113 + calculated_number value = backend_calculate_value_from_stored_data(st, rd, after, before, options);
114 + if(!isnan(value)) {
115 + buffer_sprintf(b, "{"
116 + "\"prefix\":\"%s\","
117 + "\"hostname\":\"%s\","
118 + "\"id\":\"%s\","
119 + "\"subid\":\"%s\","
120 + "\"value\":" CALCULATED_NUMBER_FORMAT ","
121 + "\"timestamp\": %u}\n", prefix, hostname, st->id, rd->id, value, (uint32_t) before);
122 + return 1;
123 + }
124 + return 0;
125 +}
126 +
127 static inline int process_graphite_response(BUFFER *b) {
128 char sample[1024];
129 const char *s = buffer_tostring(b);
@@ -110,6 +141,23 @@ static inline int process_graphite_response(BUFFER *b) {
141 return 0;
142 }
143
144 +static inline int process_json_response(BUFFER *b) {
145 + char sample[1024];
146 + const char *s = buffer_tostring(b);
147 + char *d = sample, *e = &sample[sizeof(sample) - 1];
148 +
149 + for(; *s && d < e ;s++) {
150 + char c = *s;
151 + if(unlikely(!isprint(c))) c = ' ';
152 + *d++ = c;
153 + }
154 + *d = '\0';
155 +
156 + info("Received %zu bytes from json backend. Ignoring them. Sample: '%s'", buffer_strlen(b), sample);
157 + buffer_flush(b);
158 + return 0;
159 +}
160 +
161 static inline int process_opentsdb_response(BUFFER *b) {
162 char sample[1024];
163 const char *s = buffer_tostring(b);
@@ -200,6 +248,21 @@ void *backends_main(void *ptr) {
248
249 backend_response_checker = process_opentsdb_response;
250 }
251 + else if (!strcmp(type, "json") || !strcmp(type, "json:plaintext"))
252 + {
253 + default_port = 5448;
254 +
255 + if (options == BACKEND_SOURCE_DATA_AS_COLLECTED)
256 + {
257 + backend_request_formatter = format_dimension_collected_json_plaintext;
258 + }
259 + else
260 + {
261 + backend_request_formatter = format_dimension_stored_json_plaintext;
262 + }
263 +
264 + backend_response_checker = process_json_response;
265 + }
266 else {
267 error("Unknown backend type '%s'", type);
268 goto cleanup;