diff --git a/src/core/store_json.c b/src/core/store_json.c
index d9fc37c4b30d498b3340fce1b238b177f57ad95d..b39ff210a2aedfaf0d8b0064bad354234d8109bc 100644 (file)
--- a/src/core/store_json.c
+++ b/src/core/store_json.c
/*
* SysDB - src/core/store_json.c
/*
* SysDB - src/core/store_json.c
- * Copyright (C) 2013-2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2013-2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include <assert.h>
#include <assert.h>
+#include <ctype.h>
#include <stdlib.h>
#include <stdlib.h>
+#include <string.h>
/*
* private data types
*/
struct sdb_store_json_formatter {
/*
* 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
* the path pointing to the current object */
int context[8];
size_t current;
sdb_strbuf_t *buf;
/* The context describes the state of the formatter through
* the path pointing to the current object */
int context[8];
size_t current;
+
+ int type;
+ int flags;
+};
+#define F(obj) ((sdb_store_json_formatter_t *)(obj))
+
+static int
+formatter_init(sdb_object_t *obj, va_list ap)
+{
+ sdb_store_json_formatter_t *f = F(obj);
+
+ f->buf = va_arg(ap, sdb_strbuf_t *);
+ if (! f->buf)
+ return -1;
+
+ f->type = va_arg(ap, int);
+ if ((f->type != SDB_HOST) && (f->type != SDB_SERVICE) && (f->type != SDB_METRIC))
+ return -1;
+
+ f->flags = va_arg(ap, int);
+
+ f->context[0] = 0;
+ f->current = 0;
+ return 0;
+} /* formatter_init */
+
+static sdb_type_t formatter_type = {
+ /* size = */ sizeof(sdb_store_json_formatter_t),
+ /* init = */ formatter_init,
+ /* destroy = */ NULL,
};
};
+/* A generic representation of a stored object. */
+typedef struct {
+ /* identifier */
+ int type;
+ const char *name;
+
+ /* attribute value (optional) */
+ sdb_data_t *value;
+
+ /* metric's timeseries (optional)
+ * -1: unset
+ * 0: false
+ * 1: true */
+ int timeseries;
+
+ /* generic meta-data */
+ sdb_time_t last_update;
+ sdb_time_t interval;
+ size_t backends_num;
+ const char * const *backends;
+} obj_t;
+
/*
* private helper functions
*/
/*
* private helper functions
*/
+static void
+escape_string(const char *src, char *dest)
+{
+ 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, int type)
+{
+ /* first top-level object */
+ if (! f->context[0]) {
+ if ((type != f->type) && (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(type),
+ SDB_STORE_TYPE_TO_NAME(f->type));
+ return -1;
+ }
+ if (f->flags & SDB_WANT_ARRAY)
+ sdb_strbuf_append(f->buf, "[");
+ assert(f->current == 0);
+ f->context[f->current] = type;
+ return 0;
+ }
+
+ if ((f->context[f->current] != SDB_HOST)
+ && (type != SDB_ATTRIBUTE)) {
+ /* new entry of the same type or a parent object;
+ * rewind to the right state */
+ while ((f->current > 0)
+ && (f->context[f->current] != type)) {
+ sdb_strbuf_append(f->buf, "}]");
+ --f->current;
+ }
+ }
+
+ if (type == f->context[f->current]) {
+ /* new entry of the same type */
+ sdb_strbuf_append(f->buf, "},");
+ }
+ else if ((f->context[f->current] == SDB_HOST)
+ || (type == SDB_ATTRIBUTE)) {
+ assert(type != SDB_HOST);
+ /* all object types may be children of a host;
+ * attributes may be children of any type */
+ sdb_strbuf_append(f->buf, ", \"%ss\": [",
+ SDB_STORE_TYPE_TO_NAME(type));
+ ++f->current;
+ }
+ else {
+ sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
+ "on level %zu during JSON serialization",
+ SDB_STORE_TYPE_TO_NAME(type), f->current);
+ return -1;
+ }
+
+ assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
+ f->context[f->current] = type;
+ return 0;
+} /* handle_new_object */
+
static int
static int
-json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
+json_emit(sdb_store_json_formatter_t *f, obj_t *obj)
{
char time_str[64];
char interval_str[64];
{
char time_str[64];
char interval_str[64];
+ char name[2 * strlen(obj->name) + 3];
size_t i;
assert(f && obj);
size_t i;
assert(f && obj);
- 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)
+ handle_new_object(f, obj->type);
+
+ escape_string(obj->name, name);
+ sdb_strbuf_append(f->buf, "{\"name\": %s, ", name);
+ if ((obj->type == SDB_ATTRIBUTE) && (obj->value)) {
+ char tmp[sdb_data_strlen(obj->value) + 1];
+ char val[2 * sizeof(tmp) + 3];
+ if (! sdb_data_format(obj->value, tmp, sizeof(tmp),
+ SDB_DOUBLE_QUOTED))
snprintf(tmp, sizeof(tmp), "<error>");
snprintf(tmp, sizeof(tmp), "<error>");
- sdb_strbuf_append(f->buf, "\"value\": %s, ", 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) && (obj->timeseries >= 0)) {
+ if (obj->timeseries)
+ sdb_strbuf_append(f->buf, "\"timeseries\": true, ");
+ else
+ sdb_strbuf_append(f->buf, "\"timeseries\": false, ");
}
/* TODO: make time and interval formats configurable */
}
/* TODO: make time and interval formats configurable */
- if (! sdb_strftime(time_str, sizeof(time_str),
- "%F %T %z", obj->last_update))
+ if (! sdb_strftime(time_str, sizeof(time_str), obj->last_update))
snprintf(time_str, sizeof(time_str), "<error>");
time_str[sizeof(time_str) - 1] = '\0';
snprintf(time_str, sizeof(time_str), "<error>");
time_str[sizeof(time_str) - 1] = '\0';
return 0;
} /* json_emit */
return 0;
} /* json_emit */
-/*
- * public API
- */
-
-sdb_store_json_formatter_t *
-sdb_store_json_formatter(sdb_strbuf_t *buf)
+static int
+emit_host(sdb_store_host_t *host, sdb_object_t *user_data)
{
{
- sdb_store_json_formatter_t *f;
+ sdb_store_json_formatter_t *f = F(user_data);
- if (! buf)
- return NULL;
+ if ((! host) || (! user_data))
+ return -1;
- f = calloc(1, sizeof(*f));
- if (! f)
- return NULL;
+ {
+ obj_t o = {
+ SDB_HOST,
+ host->name,
- f->buf = buf;
- f->context[0] = 0;
- f->current = 0;
- return f;
-} /* sdb_store_json_formatter */
+ /* value */ NULL,
+ /* timeseries */ -1,
-int
-sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj)
+ host->last_update,
+ host->interval,
+ host->backends_num,
+ (const char * const *)host->backends,
+ };
+
+ return json_emit(f, &o);
+ }
+} /* emit_host */
+
+static int
+emit_service(sdb_store_service_t *service, sdb_object_t *user_data)
{
{
- if ((! f) || (! obj))
+ sdb_store_json_formatter_t *f = F(user_data);
+
+ if ((! service) || (! user_data))
return -1;
return -1;
- /* first host */
- if (! f->context[0]) {
- assert(f->current == 0);
- f->context[0] = SDB_HOST;
- return json_emit(f, obj);
- }
+ {
+ obj_t o = {
+ SDB_SERVICE,
+ service->name,
- if (obj->type == f->context[f->current]) {
- /* new entry of the same type */
- sdb_strbuf_append(f->buf, "},");
+ /* value */ NULL,
+ /* timeseries */ -1,
+
+ service->last_update,
+ service->interval,
+ service->backends_num,
+ (const char * const *)service->backends,
+ };
+
+ return json_emit(f, &o);
}
}
- else if ((f->context[f->current] == SDB_HOST)
- || (obj->type == SDB_ATTRIBUTE)) {
- assert(obj->type != SDB_HOST);
- /* all object types may be children of a host;
- * attributes may be children of any type */
- sdb_strbuf_append(f->buf, ", \"%ss\": [",
- SDB_STORE_TYPE_TO_NAME(obj->type));
- ++f->current;
+} /* emit_service */
+
+static int
+emit_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
+{
+ sdb_store_json_formatter_t *f = F(user_data);
+
+ if ((! metric) || (! user_data))
+ return -1;
+
+ {
+ obj_t o = {
+ SDB_METRIC,
+ metric->name,
+
+ /* value */ NULL,
+ /* timeseries */ metric->store.type != NULL,
+
+ metric->last_update,
+ metric->interval,
+ metric->backends_num,
+ (const char * const *)metric->backends,
+ };
+
+ return json_emit(f, &o);
}
}
- else if (f->current >= 1) {
- /* new entry of a previous type or a new type on the same level
- * -> rewind to the right state and then handle the new object */
- assert(obj->type != SDB_ATTRIBUTE);
- while (f->current > 0) {
- if (f->context[f->current] == obj->type)
- break;
- assert(f->context[f->current] != SDB_HOST);
- sdb_strbuf_append(f->buf, "}]");
- --f->current;
- }
- return sdb_store_json_emit(f, obj);
+} /* emit_metric */
+
+static int
+emit_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
+{
+ sdb_store_json_formatter_t *f = F(user_data);
+
+ if ((! attr) || (! user_data))
+ return -1;
+
+ {
+ obj_t o = {
+ SDB_ATTRIBUTE,
+ attr->key,
+
+ /* value */ &attr->value,
+ /* timeseries */ -1,
+
+ attr->last_update,
+ attr->interval,
+ attr->backends_num,
+ (const char * const *)attr->backends,
+ };
+
+ return json_emit(f, &o);
}
}
- else {
- sdb_log(SDB_LOG_ERR, "store: Unexpected object of type %s "
- "on level %zu during JSON serialization",
- SDB_STORE_TYPE_TO_NAME(obj->type), f->current);
+} /* emit_attribute */
+
+/*
+ * public API
+ */
+
+sdb_store_writer_t sdb_store_json_writer = {
+ emit_host, emit_service, emit_metric, emit_attribute,
+};
+
+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 */
+
+/* TODO: Move sdb_store_emit* somewhere else. */
+
+int
+sdb_memstore_emit(sdb_memstore_obj_t *obj, sdb_store_writer_t *w, sdb_object_t *wd)
+{
+ if ((! obj) || (! w))
return -1;
return -1;
+
+ switch (obj->type) {
+ case SDB_HOST:
+ {
+ sdb_store_host_t host = {
+ obj->_name,
+ obj->last_update,
+ obj->interval,
+ (const char * const *)obj->backends,
+ obj->backends_num,
+ };
+ if (! w->store_host)
+ return -1;
+ return w->store_host(&host, wd);
+ }
+ case SDB_SERVICE:
+ {
+ sdb_store_service_t service = {
+ obj->parent ? obj->parent->_name : NULL,
+ obj->_name,
+ obj->last_update,
+ obj->interval,
+ (const char * const *)obj->backends,
+ obj->backends_num,
+ };
+ if (! w->store_service)
+ return -1;
+ return w->store_service(&service, wd);
+ }
+ case SDB_METRIC:
+ {
+ sdb_store_metric_t metric = {
+ obj->parent ? obj->parent->_name : NULL,
+ obj->_name,
+ {
+ METRIC(obj)->store.type,
+ METRIC(obj)->store.id,
+ },
+ obj->last_update,
+ obj->interval,
+ (const char * const *)obj->backends,
+ obj->backends_num,
+ };
+ if (! w->store_metric)
+ return -1;
+ return w->store_metric(&metric, wd);
+ }
+ case SDB_ATTRIBUTE:
+ {
+ sdb_store_attribute_t attr = {
+ NULL,
+ obj->parent ? obj->parent->type : 0,
+ obj->parent ? obj->parent->_name : NULL,
+ obj->_name,
+ ATTR(obj)->value,
+ obj->last_update,
+ obj->interval,
+ (const char * const *)obj->backends,
+ obj->backends_num,
+ };
+ if (obj->parent && (obj->parent->type != SDB_HOST)
+ && obj->parent->parent)
+ attr.hostname = obj->parent->parent->_name;
+ if (! w->store_attribute)
+ return -1;
+ return w->store_attribute(&attr, wd);
+ }
}
}
- json_emit(f, obj);
- assert(f->current < SDB_STATIC_ARRAY_LEN(f->context));
- f->context[f->current] = obj->type;
- return 0;
-} /* sdb_store_json_emit */
+ return -1;
+} /* sdb_memstore_emit */
int
int
-sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
- sdb_store_matcher_t *filter)
+sdb_memstore_emit_full(sdb_memstore_obj_t *obj, sdb_memstore_matcher_t *filter,
+ sdb_store_writer_t *w, sdb_object_t *wd)
{
sdb_avltree_t *trees[] = { NULL, NULL, NULL };
size_t i;
{
sdb_avltree_t *trees[] = { NULL, NULL, NULL };
size_t i;
- if (sdb_store_json_emit(f, obj))
+ if (sdb_memstore_emit(obj, w, wd))
return -1;
if (obj->type == SDB_HOST) {
return -1;
if (obj->type == SDB_HOST) {
iter = sdb_avltree_get_iter(trees[i]);
while (sdb_avltree_iter_has_next(iter)) {
iter = sdb_avltree_get_iter(trees[i]);
while (sdb_avltree_iter_has_next(iter)) {
- sdb_store_obj_t *child;
+ sdb_memstore_obj_t *child;
child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
child = STORE_OBJ(sdb_avltree_iter_get_next(iter));
- if (filter && (! sdb_store_matcher_matches(filter, child, NULL)))
+ if (filter && (! sdb_memstore_matcher_matches(filter, child, NULL)))
continue;
continue;
- if (sdb_store_json_emit_full(f, child, filter)) {
+ if (sdb_memstore_emit_full(child, filter, w, wd)) {
sdb_avltree_iter_destroy(iter);
return -1;
}
sdb_avltree_iter_destroy(iter);
return -1;
}
sdb_avltree_iter_destroy(iter);
}
return 0;
sdb_avltree_iter_destroy(iter);
}
return 0;
-} /* sdb_store_json_emit_full */
+} /* sdb_memstore_emit_full */
int
sdb_store_json_finish(sdb_store_json_formatter_t *f)
int
sdb_store_json_finish(sdb_store_json_formatter_t *f)
if (! f)
return -1;
if (! f)
return -1;
+ if (! f->context[0]) {
+ /* no content */
+ if (f->flags & SDB_WANT_ARRAY)
+ sdb_strbuf_append(f->buf, "[]");
+ return 0;
+ }
+
while (f->current > 0) {
sdb_strbuf_append(f->buf, "}]");
--f->current;
}
while (f->current > 0) {
sdb_strbuf_append(f->buf, "}]");
--f->current;
}
- if (f->context[0])
- sdb_strbuf_append(f->buf, "}");
+ sdb_strbuf_append(f->buf, "}");
+
+ if (f->flags & SDB_WANT_ARRAY)
+ sdb_strbuf_append(f->buf, "]");
return 0;
} /* sdb_store_json_finish */
return 0;
} /* sdb_store_json_finish */