X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fcore%2Fstore_json.c;h=a2c117b1f10d07308e7db9afd698c32933342e49;hp=493ac9f377c9072d54fdf76488ab242e76cc6ec6;hb=79debed9dacc5d48386124a1ee9a3f7be655fdf6;hpb=914549be3bfffdd3ad4b2599ed1e771d857512da diff --git a/src/core/store_json.c b/src/core/store_json.c index 493ac9f..a2c117b 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -1,6 +1,6 @@ /* * SysDB - src/core/store_json.c - * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl + * Copyright (C) 2013-2015 Sebastian 'tokkee' Harl * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -39,13 +39,18 @@ #include +#include #include +#include /* * private data types */ struct sdb_store_json_formatter { + sdb_object_t super; + + /* The string buffer to write to */ sdb_strbuf_t *buf; /* The context describes the state of the formatter through @@ -53,116 +58,77 @@ struct sdb_store_json_formatter { int context[8]; size_t current; - /* The current host context when processing non-host objects */ - sdb_store_obj_t *current_host; - int type; int flags; }; - -/* - * private helper functions - */ +#define F(obj) ((sdb_store_json_formatter_t *)(obj)) static int -json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) -{ - char time_str[64]; - char interval_str[64]; - size_t i; - - assert(f && obj); - - if ((f->type != SDB_HOST) && (f->type == obj->type)) { - /* create the host for the current entry first */ - assert(obj->parent && (obj->parent->type == SDB_HOST)); - if (f->current_host != obj->parent) { - json_emit(f, obj->parent); - sdb_strbuf_append(f->buf, ", \"%ss\": [", - SDB_STORE_TYPE_TO_NAME(obj->type)); - f->current_host = obj->parent; - } - } - - sdb_strbuf_append(f->buf, "{\"name\": \"%s\", ", SDB_OBJ(obj)->name); - if (obj->type == SDB_ATTRIBUTE) { - char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1]; - if (sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), - SDB_DOUBLE_QUOTED) < 0) - snprintf(tmp, sizeof(tmp), ""); - sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp); - } - - /* TODO: make time and interval formats configurable */ - if (! sdb_strftime(time_str, sizeof(time_str), - "%F %T %z", obj->last_update)) - snprintf(time_str, sizeof(time_str), ""); - time_str[sizeof(time_str) - 1] = '\0'; - - if (! sdb_strfinterval(interval_str, sizeof(interval_str), - obj->interval)) - snprintf(interval_str, sizeof(interval_str), ""); - interval_str[sizeof(interval_str) - 1] = '\0'; - - sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", " - "\"update_interval\": \"%s\", \"backends\": [", - time_str, interval_str); - - for (i = 0; i < obj->backends_num; ++i) { - sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]); - if (i < obj->backends_num - 1) - sdb_strbuf_append(f->buf, ","); - } - sdb_strbuf_append(f->buf, "]"); - return 0; -} /* json_emit */ - -/* - * public API - */ - -sdb_store_json_formatter_t * -sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) +formatter_init(sdb_object_t *obj, va_list ap) { - sdb_store_json_formatter_t *f; + sdb_store_json_formatter_t *f = F(obj); - if (! buf) - return NULL; + f->buf = va_arg(ap, sdb_strbuf_t *); + if (! f->buf) + return -1; - if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) - return NULL; + f->type = va_arg(ap, int); + if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC)) + return -1; - f = calloc(1, sizeof(*f)); - if (! f) - return NULL; + f->flags = va_arg(ap, int); - f->buf = buf; f->context[0] = 0; f->current = 0; + return 0; +} /* formatter_init */ - f->current_host = NULL; +static sdb_type_t formatter_type = { + /* size = */ sizeof(sdb_store_json_formatter_t), + /* init = */ formatter_init, + /* destroy = */ NULL, +}; - f->type = type; - f->flags = flags; - return f; -} /* sdb_store_json_formatter */ +/* + * private helper functions + */ -int -sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) +static void +escape_string(const char *src, char *dest) { - if ((! f) || (! obj)) - return -1; - - if ((f->type != SDB_HOST) && (obj->type == SDB_HOST)) { - sdb_log(SDB_LOG_ERR, "store: Unexpected object of type host " - "during %s JSON serialization", - SDB_STORE_TYPE_TO_NAME(f->type)); - return -1; + size_t i = 1; + dest[0] = '"'; + for ( ; *src; ++src) { + char c = *src; + if ((c == '"') || (c == '\\') || iscntrl((int)c)) { + dest[i] = '\\'; + ++i; + } + switch (c) { + case '\a': dest[i] = 'a'; break; + case '\b': dest[i] = 'b'; break; + case '\t': dest[i] = 't'; break; + case '\n': dest[i] = 'n'; break; + case '\v': dest[i] = 'v'; break; + case '\f': dest[i] = 'f'; break; + case '\r': dest[i] = 'r'; break; + default: dest[i] = c; break; + } + ++i; } + dest[i] = '"'; + dest[i + 1] = '\0'; +} /* escape_string */ +/* handle_new_object takes care of all maintenance logic related to adding a + * new object. That is, it manages context information and emit the prefix and + * suffix of an object. */ +static int +handle_new_object(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) +{ /* first top-level object */ if (! f->context[0]) { - if (obj->type != f->type) { + if ((obj->type != f->type) && (obj->type != SDB_HOST)) { sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s " "as the first element during %s JSON serialization", SDB_STORE_TYPE_TO_NAME(obj->type), @@ -172,23 +138,20 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) if (f->flags & SDB_WANT_ARRAY) sdb_strbuf_append(f->buf, "["); assert(f->current == 0); - f->context[0] = obj->type; - return json_emit(f, obj); + f->context[f->current] = obj->type; + return 0; } - if ((f->current >= 1) && (obj->type != SDB_ATTRIBUTE)) { - /* new entry of a previous type or a new type on the same level; + if ((f->context[f->current] != SDB_HOST) + && (obj->type != SDB_ATTRIBUTE)) { + /* new entry of the same type or a parent object; * rewind to the right state */ - while (f->current > 0) { - if (f->context[f->current] == obj->type) - break; + while ((f->current > 0) + && (f->context[f->current] != obj->type)) { sdb_strbuf_append(f->buf, "}]"); --f->current; } } - if ((obj->type == f->type) && (f->type != SDB_HOST)) - if (obj->parent != f->current_host) - sdb_strbuf_append(f->buf, "}]"); if (obj->type == f->context[f->current]) { /* new entry of the same type */ @@ -210,10 +173,88 @@ sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) return -1; } - json_emit(f, obj); assert(f->current < SDB_STATIC_ARRAY_LEN(f->context)); f->context[f->current] = obj->type; return 0; +} /* handle_new_object */ + +static int +json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) +{ + char time_str[64]; + char interval_str[64]; + char name[2 * strlen(SDB_OBJ(obj)->name) + 3]; + size_t i; + + assert(f && obj); + + handle_new_object(f, obj); + + escape_string(SDB_OBJ(obj)->name, name); + sdb_strbuf_append(f->buf, "{\"name\": %s, ", name); + if (obj->type == SDB_ATTRIBUTE) { + char tmp[sdb_data_strlen(&ATTR(obj)->value) + 1]; + char val[2 * sizeof(tmp) + 3]; + if (! sdb_data_format(&ATTR(obj)->value, tmp, sizeof(tmp), + SDB_DOUBLE_QUOTED)) + snprintf(tmp, sizeof(tmp), ""); + + if (tmp[0] == '"') { + /* a string; escape_string handles quoting */ + tmp[strlen(tmp) - 1] = '\0'; + escape_string(tmp + 1, val); + sdb_strbuf_append(f->buf, "\"value\": %s, ", val); + } + else + sdb_strbuf_append(f->buf, "\"value\": %s, ", tmp); + } + else if (obj->type == SDB_METRIC) { + if (METRIC(obj)->store.type != NULL) + sdb_strbuf_append(f->buf, "\"timeseries\": true, "); + else + sdb_strbuf_append(f->buf, "\"timeseries\": false, "); + } + + /* TODO: make time and interval formats configurable */ + if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update)) + snprintf(time_str, sizeof(time_str), ""); + time_str[sizeof(time_str) - 1] = '\0'; + + if (! sdb_strfinterval(interval_str, sizeof(interval_str), + obj->interval)) + snprintf(interval_str, sizeof(interval_str), ""); + interval_str[sizeof(interval_str) - 1] = '\0'; + + sdb_strbuf_append(f->buf, "\"last_update\": \"%s\", " + "\"update_interval\": \"%s\", \"backends\": [", + time_str, interval_str); + + for (i = 0; i < obj->backends_num; ++i) { + sdb_strbuf_append(f->buf, "\"%s\"", obj->backends[i]); + if (i < obj->backends_num - 1) + sdb_strbuf_append(f->buf, ","); + } + sdb_strbuf_append(f->buf, "]"); + return 0; +} /* json_emit */ + +/* + * public API + */ + +sdb_store_json_formatter_t * +sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) +{ + return F(sdb_object_create("json-formatter", formatter_type, + buf, type, flags)); +} /* sdb_store_json_formatter */ + +int +sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) +{ + if ((! f) || (! obj)) + return -1; + return json_emit(f, obj); } /* sdb_store_json_emit */ int @@ -281,12 +322,7 @@ sdb_store_json_finish(sdb_store_json_formatter_t *f) sdb_strbuf_append(f->buf, "}]"); --f->current; } - if (f->context[0] != SDB_HOST) { - assert(f->type != SDB_HOST); - sdb_strbuf_append(f->buf, "}]}"); - } - else - sdb_strbuf_append(f->buf, "}"); + sdb_strbuf_append(f->buf, "}"); if (f->flags & SDB_WANT_ARRAY) sdb_strbuf_append(f->buf, "]");