X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcore%2Fstore_json.c;h=b39ff210a2aedfaf0d8b0064bad354234d8109bc;hb=b54d9fff4bc9e70efe83123403a4c9f935efb74e;hp=26a0854eeb4ef09957d7cd400bc5e699496cd7bc;hpb=eabf5255d29d62b4fa89492d40830b0213831f07;p=sysdb.git diff --git a/src/core/store_json.c b/src/core/store_json.c index 26a0854..b39ff21 100644 --- a/src/core/store_json.c +++ b/src/core/store_json.c @@ -260,54 +260,212 @@ json_emit(sdb_store_json_formatter_t *f, obj_t *obj) return 0; } /* json_emit */ -/* - * public API - */ - -sdb_store_json_formatter_t * -sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags) +static int +emit_host(sdb_store_host_t *host, sdb_object_t *user_data) { - return F(sdb_object_create("json-formatter", formatter_type, - buf, type, flags)); -} /* sdb_store_json_formatter */ + sdb_store_json_formatter_t *f = F(user_data); -int -sdb_store_json_emit(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj) + if ((! host) || (! user_data)) + return -1; + + { + obj_t o = { + SDB_HOST, + host->name, + + /* value */ NULL, + /* timeseries */ -1, + + 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; { obj_t o = { - obj->type, - obj->_name, + SDB_SERVICE, + service->name, /* value */ NULL, /* timeseries */ -1, - obj->last_update, - obj->interval, - obj->backends_num, - (const char * const *)obj->backends, + service->last_update, + service->interval, + service->backends_num, + (const char * const *)service->backends, }; - if (obj->type == SDB_ATTRIBUTE) - o.value = &ATTR(obj)->value; - if (obj->type == SDB_METRIC) - o.timeseries = METRIC(obj)->store.type != NULL; + return json_emit(f, &o); + } +} /* 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); + } +} /* 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); } -} /* sdb_store_json_emit */ +} /* 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; + + 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); + } + } + + return -1; +} /* sdb_memstore_emit */ 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; - if (sdb_store_json_emit(f, obj)) + if (sdb_memstore_emit(obj, w, wd)) return -1; if (obj->type == SDB_HOST) { @@ -332,13 +490,13 @@ sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, 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)); - if (filter && (! sdb_store_matcher_matches(filter, child, NULL))) + if (filter && (! sdb_memstore_matcher_matches(filter, child, NULL))) 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; } @@ -346,7 +504,7 @@ sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj, 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)