Code

store_json: Let the formatter implement the store-writer interface.
authorSebastian Harl <sh@tokkee.org>
Wed, 30 Sep 2015 09:17:07 +0000 (11:17 +0200)
committerSebastian Harl <sh@tokkee.org>
Wed, 30 Sep 2015 09:17:07 +0000 (11:17 +0200)
src/core/store_json.c
src/include/core/store.h

index 26a0854eeb4ef09957d7cd400bc5e699496cd7bc..5d0408a75fee33ad7b0a3718efcd081944ea2013 100644 (file)
@@ -260,10 +260,118 @@ json_emit(sdb_store_json_formatter_t *f, obj_t *obj)
        return 0;
 } /* json_emit */
 
        return 0;
 } /* json_emit */
 
+static int
+emit_host(sdb_store_host_t *host, sdb_object_t *user_data)
+{
+       sdb_store_json_formatter_t *f = F(user_data);
+
+       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)
+{
+       sdb_store_json_formatter_t *f = F(user_data);
+
+       if ((! service) || (! user_data))
+               return -1;
+
+       {
+               obj_t o = {
+                       SDB_SERVICE,
+                       service->name,
+
+                       /* value */ NULL,
+                       /* timeseries */ -1,
+
+                       service->last_update,
+                       service->interval,
+                       service->backends_num,
+                       (const char * const *)service->backends,
+               };
+
+               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);
+       }
+} /* emit_attribute */
+
 /*
  * public API
  */
 
 /*
  * 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)
 {
 sdb_store_json_formatter_t *
 sdb_store_json_formatter(sdb_strbuf_t *buf, int type, int flags)
 {
index ea0cd97e06ff4c5e198e93e3290ea1918e3b4456..ddd0400e7684027560b0705f291dc006b3477201 100644 (file)
@@ -775,6 +775,13 @@ sdb_store_json_emit_full(sdb_store_json_formatter_t *f, sdb_store_obj_t *obj,
 int
 sdb_store_json_finish(sdb_store_json_formatter_t *f);
 
 int
 sdb_store_json_finish(sdb_store_json_formatter_t *f);
 
+/*
+ * sdb_store_json_writer:
+ * A store writer implementation that generates JSON output. It expects a
+ * store JSON formatter as its user-data argument.
+ */
+extern sdb_store_writer_t sdb_store_json_writer;
+
 #ifdef __cplusplus
 } /* extern "C" */
 #endif
 #ifdef __cplusplus
 } /* extern "C" */
 #endif