2 * collectd - src/utils_format_json.c
3 * Copyright (C) 2009-2015 Florian octo Forster
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 * Florian octo Forster <octo at collectd.org>
29 #include "utils_format_json.h"
33 #include "utils_cache.h"
36 #include <yajl/yajl_common.h>
37 #include <yajl/yajl_gen.h>
38 #if HAVE_YAJL_YAJL_VERSION_H
39 #include <yajl/yajl_version.h>
41 #if defined(YAJL_MAJOR) && (YAJL_MAJOR > 1)
42 #define HAVE_YAJL_V2 1
46 static int json_escape_string(char *buffer, size_t buffer_size, /* {{{ */
50 if ((buffer == NULL) || (string == NULL))
58 #define BUFFER_ADD(c) \
60 if (dst_pos >= (buffer_size - 1)) { \
61 buffer[buffer_size - 1] = 0; \
64 buffer[dst_pos] = (c); \
68 /* Escape special characters */
70 for (size_t src_pos = 0; string[src_pos] != 0; src_pos++) {
71 if ((string[src_pos] == '"') || (string[src_pos] == '\\')) {
73 BUFFER_ADD(string[src_pos]);
74 } else if (string[src_pos] <= 0x001F)
77 BUFFER_ADD(string[src_pos]);
85 } /* }}} int json_escape_string */
87 static int values_to_json(char *buffer, size_t buffer_size, /* {{{ */
88 const data_set_t *ds, const value_list_t *vl,
91 gauge_t *rates = NULL;
93 memset(buffer, 0, buffer_size);
95 #define BUFFER_ADD(...) \
98 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
102 } else if (((size_t)status) >= (buffer_size - offset)) { \
106 offset += ((size_t)status); \
110 for (size_t i = 0; i < ds->ds_num; i++) {
114 if (ds->ds[i].type == DS_TYPE_GAUGE) {
115 if (isfinite(vl->values[i].gauge))
116 BUFFER_ADD(JSON_GAUGE_FORMAT, vl->values[i].gauge);
119 } else if (store_rates) {
121 rates = uc_get_rate(ds, vl);
123 WARNING("utils_format_json: uc_get_rate failed.");
128 if (isfinite(rates[i]))
129 BUFFER_ADD(JSON_GAUGE_FORMAT, rates[i]);
132 } else if (ds->ds[i].type == DS_TYPE_COUNTER)
133 BUFFER_ADD("%" PRIu64, (uint64_t)vl->values[i].counter);
134 else if (ds->ds[i].type == DS_TYPE_DERIVE)
135 BUFFER_ADD("%" PRIi64, vl->values[i].derive);
136 else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
137 BUFFER_ADD("%" PRIu64, vl->values[i].absolute);
139 ERROR("format_json: Unknown data source type: %i", ds->ds[i].type);
143 } /* for ds->ds_num */
148 DEBUG("format_json: values_to_json: buffer = %s;", buffer);
151 } /* }}} int values_to_json */
153 static int dstypes_to_json(char *buffer, size_t buffer_size, /* {{{ */
154 const data_set_t *ds) {
157 memset(buffer, 0, buffer_size);
159 #define BUFFER_ADD(...) \
162 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
165 else if (((size_t)status) >= (buffer_size - offset)) \
168 offset += ((size_t)status); \
172 for (size_t i = 0; i < ds->ds_num; i++) {
176 BUFFER_ADD("\"%s\"", DS_TYPE_TO_STRING(ds->ds[i].type));
177 } /* for ds->ds_num */
182 DEBUG("format_json: dstypes_to_json: buffer = %s;", buffer);
185 } /* }}} int dstypes_to_json */
187 static int dsnames_to_json(char *buffer, size_t buffer_size, /* {{{ */
188 const data_set_t *ds) {
191 memset(buffer, 0, buffer_size);
193 #define BUFFER_ADD(...) \
196 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
199 else if (((size_t)status) >= (buffer_size - offset)) \
202 offset += ((size_t)status); \
206 for (size_t i = 0; i < ds->ds_num; i++) {
210 BUFFER_ADD("\"%s\"", ds->ds[i].name);
211 } /* for ds->ds_num */
216 DEBUG("format_json: dsnames_to_json: buffer = %s;", buffer);
219 } /* }}} int dsnames_to_json */
221 static int meta_data_keys_to_json(char *buffer, size_t buffer_size, /* {{{ */
222 meta_data_t *meta, char **keys,
229 #define BUFFER_ADD(...) \
231 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
234 else if (((size_t)status) >= (buffer_size - offset)) \
237 offset += ((size_t)status); \
240 for (size_t i = 0; i < keys_num; ++i) {
244 type = meta_data_type(meta, key);
245 if (type == MD_TYPE_STRING) {
247 if (meta_data_get_string(meta, key, &value) == 0) {
250 status = json_escape_string(temp, sizeof(temp), value);
255 BUFFER_ADD(",\"%s\":%s", key, temp);
257 } else if (type == MD_TYPE_SIGNED_INT) {
259 if (meta_data_get_signed_int(meta, key, &value) == 0)
260 BUFFER_ADD(",\"%s\":%" PRIi64, key, value);
261 } else if (type == MD_TYPE_UNSIGNED_INT) {
263 if (meta_data_get_unsigned_int(meta, key, &value) == 0)
264 BUFFER_ADD(",\"%s\":%" PRIu64, key, value);
265 } else if (type == MD_TYPE_DOUBLE) {
267 if (meta_data_get_double(meta, key, &value) == 0)
268 BUFFER_ADD(",\"%s\":%f", key, value);
269 } else if (type == MD_TYPE_BOOLEAN) {
271 if (meta_data_get_boolean(meta, key, &value) == 0)
272 BUFFER_ADD(",\"%s\":%s", key, value ? "true" : "false");
279 buffer[0] = '{'; /* replace leading ',' */
285 } /* }}} int meta_data_keys_to_json */
287 static int meta_data_to_json(char *buffer, size_t buffer_size, /* {{{ */
293 if ((buffer == NULL) || (buffer_size == 0) || (meta == NULL))
296 status = meta_data_toc(meta, &keys);
299 keys_num = (size_t)status;
301 status = meta_data_keys_to_json(buffer, buffer_size, meta, keys, keys_num);
303 for (size_t i = 0; i < keys_num; ++i)
308 } /* }}} int meta_data_to_json */
310 static int value_list_to_json(char *buffer, size_t buffer_size, /* {{{ */
311 const data_set_t *ds, const value_list_t *vl,
317 memset(buffer, 0, buffer_size);
319 #define BUFFER_ADD(...) \
321 status = snprintf(buffer + offset, buffer_size - offset, __VA_ARGS__); \
324 else if (((size_t)status) >= (buffer_size - offset)) \
327 offset += ((size_t)status); \
330 /* All value lists have a leading comma. The first one will be replaced with
331 * a square bracket in `format_json_finalize'. */
334 status = values_to_json(temp, sizeof(temp), ds, vl, store_rates);
337 BUFFER_ADD("\"values\":%s", temp);
339 status = dstypes_to_json(temp, sizeof(temp), ds);
342 BUFFER_ADD(",\"dstypes\":%s", temp);
344 status = dsnames_to_json(temp, sizeof(temp), ds);
347 BUFFER_ADD(",\"dsnames\":%s", temp);
349 BUFFER_ADD(",\"time\":%.3f", CDTIME_T_TO_DOUBLE(vl->time));
350 BUFFER_ADD(",\"interval\":%.3f", CDTIME_T_TO_DOUBLE(vl->interval));
352 #define BUFFER_ADD_KEYVAL(key, value) \
354 status = json_escape_string(temp, sizeof(temp), (value)); \
357 BUFFER_ADD(",\"%s\":%s", (key), temp); \
360 BUFFER_ADD_KEYVAL("host", vl->host);
361 BUFFER_ADD_KEYVAL("plugin", vl->plugin);
362 BUFFER_ADD_KEYVAL("plugin_instance", vl->plugin_instance);
363 BUFFER_ADD_KEYVAL("type", vl->type);
364 BUFFER_ADD_KEYVAL("type_instance", vl->type_instance);
366 if (vl->meta != NULL) {
367 char meta_buffer[buffer_size];
368 memset(meta_buffer, 0, sizeof(meta_buffer));
369 status = meta_data_to_json(meta_buffer, sizeof(meta_buffer), vl->meta);
373 BUFFER_ADD(",\"meta\":%s", meta_buffer);
374 } /* if (vl->meta != NULL) */
378 #undef BUFFER_ADD_KEYVAL
381 DEBUG("format_json: value_list_to_json: buffer = %s;", buffer);
384 } /* }}} int value_list_to_json */
386 static int format_json_value_list_nocheck(char *buffer, /* {{{ */
387 size_t *ret_buffer_fill,
388 size_t *ret_buffer_free,
389 const data_set_t *ds,
390 const value_list_t *vl,
391 int store_rates, size_t temp_size) {
392 char temp[temp_size];
395 status = value_list_to_json(temp, sizeof(temp), ds, vl, store_rates);
398 temp_size = strlen(temp);
400 memcpy(buffer + (*ret_buffer_fill), temp, temp_size + 1);
401 (*ret_buffer_fill) += temp_size;
402 (*ret_buffer_free) -= temp_size;
405 } /* }}} int format_json_value_list_nocheck */
407 int format_json_initialize(char *buffer, /* {{{ */
408 size_t *ret_buffer_fill, size_t *ret_buffer_free) {
412 if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
413 (ret_buffer_free == NULL))
416 buffer_fill = *ret_buffer_fill;
417 buffer_free = *ret_buffer_free;
419 buffer_free = buffer_fill + buffer_free;
425 memset(buffer, 0, buffer_free);
426 *ret_buffer_fill = buffer_fill;
427 *ret_buffer_free = buffer_free;
430 } /* }}} int format_json_initialize */
432 int format_json_finalize(char *buffer, /* {{{ */
433 size_t *ret_buffer_fill, size_t *ret_buffer_free) {
436 if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
437 (ret_buffer_free == NULL))
440 if (*ret_buffer_free < 2)
443 /* Replace the leading comma added in `value_list_to_json' with a square
445 if (buffer[0] != ',')
449 pos = *ret_buffer_fill;
453 (*ret_buffer_fill)++;
454 (*ret_buffer_free)--;
457 } /* }}} int format_json_finalize */
459 int format_json_value_list(char *buffer, /* {{{ */
460 size_t *ret_buffer_fill, size_t *ret_buffer_free,
461 const data_set_t *ds, const value_list_t *vl,
463 if ((buffer == NULL) || (ret_buffer_fill == NULL) ||
464 (ret_buffer_free == NULL) || (ds == NULL) || (vl == NULL))
467 if (*ret_buffer_free < 3)
470 return format_json_value_list_nocheck(buffer, ret_buffer_fill,
471 ret_buffer_free, ds, vl, store_rates,
472 (*ret_buffer_free) - 2);
473 } /* }}} int format_json_value_list */
476 static int json_add_string(yajl_gen g, char const *str) /* {{{ */
479 return (int)yajl_gen_null(g);
481 return (int)yajl_gen_string(g, (const unsigned char *)str,
482 (unsigned int)strlen(str));
483 } /* }}} int json_add_string */
485 #define JSON_ADD(g, str) \
487 yajl_gen_status status = json_add_string(g, str); \
488 if (status != yajl_gen_status_ok) { \
493 #define JSON_ADDF(g, format, ...) \
495 char *str = ssnprintf_alloc(format, __VA_ARGS__); \
496 yajl_gen_status status = json_add_string(g, str); \
498 if (status != yajl_gen_status_ok) { \
503 #define CHECK_SUCCESS(cmd) \
505 yajl_gen_status s = (cmd); \
506 if (s != yajl_gen_status_ok) { \
511 static int format_json_meta(yajl_gen g, notification_meta_t *meta) /* {{{ */
516 JSON_ADD(g, meta->name);
517 switch (meta->type) {
519 JSON_ADD(g, meta->nm_value.nm_string);
521 case NM_TYPE_SIGNED_INT:
522 JSON_ADDF(g, "%" PRIi64, meta->nm_value.nm_signed_int);
524 case NM_TYPE_UNSIGNED_INT:
525 JSON_ADDF(g, "%" PRIu64, meta->nm_value.nm_unsigned_int);
528 JSON_ADDF(g, JSON_GAUGE_FORMAT, meta->nm_value.nm_double);
530 case NM_TYPE_BOOLEAN:
531 JSON_ADD(g, meta->nm_value.nm_boolean ? "true" : "false");
534 ERROR("format_json_meta: unknown meta data type %d (name \"%s\")",
535 meta->type, meta->name);
536 CHECK_SUCCESS(yajl_gen_null(g));
539 return format_json_meta(g, meta->next);
540 } /* }}} int format_json_meta */
542 static int format_time(yajl_gen g, cdtime_t t) /* {{{ */
544 char buffer[RFC3339NANO_SIZE] = "";
546 if (rfc3339nano(buffer, sizeof(buffer), t) != 0)
551 } /* }}} int format_time */
553 static int format_alert(yajl_gen g, notification_t const *n) /* {{{ */
555 CHECK_SUCCESS(yajl_gen_array_open(g)); /* BEGIN array */
556 CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN alert */
561 JSON_ADD(g, "labels");
562 CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN labels */
564 JSON_ADD(g, "alertname");
565 if (strncmp(n->plugin, n->type, strlen(n->plugin)) == 0)
566 JSON_ADDF(g, "collectd_%s", n->type);
568 JSON_ADDF(g, "collectd_%s_%s", n->plugin, n->type);
570 JSON_ADD(g, "instance");
571 JSON_ADD(g, n->host);
573 /* mangling of plugin instance and type instance into labels is copied from
574 * the Prometheus collectd exporter. */
575 if (strlen(n->plugin_instance) > 0) {
576 JSON_ADD(g, n->plugin);
577 JSON_ADD(g, n->plugin_instance);
579 if (strlen(n->type_instance) > 0) {
580 if (strlen(n->plugin_instance) > 0)
583 JSON_ADD(g, n->plugin);
584 JSON_ADD(g, n->type_instance);
587 JSON_ADD(g, "severity");
589 (n->severity == NOTIF_FAILURE)
591 : (n->severity == NOTIF_WARNING)
593 : (n->severity == NOTIF_OKAY) ? "OKAY" : "UNKNOWN");
595 JSON_ADD(g, "service");
596 JSON_ADD(g, "collectd");
598 CHECK_SUCCESS(yajl_gen_map_close(g)); /* END labels */
603 JSON_ADD(g, "annotations");
604 CHECK_SUCCESS(yajl_gen_map_open(g)); /* BEGIN annotations */
606 JSON_ADD(g, "summary");
607 JSON_ADD(g, n->message);
609 if (format_json_meta(g, n->meta) != 0) {
613 CHECK_SUCCESS(yajl_gen_map_close(g)); /* END annotations */
615 JSON_ADD(g, "startsAt");
616 if (format_time(g, n->time) != 0) {
620 CHECK_SUCCESS(yajl_gen_map_close(g)); /* END alert */
621 CHECK_SUCCESS(yajl_gen_array_close(g)); /* END array */
624 } /* }}} format_alert */
627 * Format (prometheus/alertmanager v1):
631 * "alertname": "collectd_cpu",
632 * "instance": "host.example.com",
633 * "severity": "FAILURE",
634 * "service": "collectd",
642 * "startsAt": <rfc3339 time>,
643 * "endsAt": <rfc3339 time>, // not used
646 int format_json_notification(char *buffer, size_t buffer_size, /* {{{ */
647 notification_t const *n) {
649 unsigned char const *out;
651 size_t unused_out_len;
653 unsigned int unused_out_len;
656 if ((buffer == NULL) || (n == NULL))
660 g = yajl_gen_alloc(NULL);
664 yajl_gen_config(g, yajl_gen_beautify, 1);
665 yajl_gen_config(g, yajl_gen_validate_utf8, 1);
668 #else /* !HAVE_YAJL_V2 */
669 yajl_gen_config conf = {0};
672 conf.indentString = " ";
674 g = yajl_gen_alloc(&conf, NULL);
679 if (format_alert(g, n) != 0) {
685 /* copy to output buffer */
686 if (yajl_gen_get_buf(g, &out, &unused_out_len) != yajl_gen_status_ok) {
691 sstrncpy(buffer, (void *)out, buffer_size);
696 } /* }}} format_json_notification */
698 int format_json_notification(char *buffer, size_t buffer_size, /* {{{ */
699 notification_t const *n) {
700 ERROR("format_json_notification: Not available (requires libyajl).");
702 } /* }}} int format_json_notification */