Code

store: Replaced store_dump() with store_tojson().
authorSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 16:47:04 +0000 (17:47 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 28 Nov 2013 16:47:04 +0000 (17:47 +0100)
The new function serializes the store as JSON and writes the result to a
string buffer.

src/core/store.c
src/daemon/sysdbd.c
src/include/core/store.h

index 19faee43994bdde816a6a84f4c09061bfb25bb4f..be307c5829adb49f212a478f13cb4d34c82983e4 100644 (file)
@@ -446,11 +446,11 @@ sdb_store_service(const char *hostname, const char *name,
 
 /* TODO: actually support hierarchical data */
 int
 
 /* TODO: actually support hierarchical data */
 int
-sdb_store_dump(FILE *fh)
+sdb_store_tojson(sdb_strbuf_t *buf)
 {
        sdb_llist_iter_t *host_iter;
 
 {
        sdb_llist_iter_t *host_iter;
 
-       if (! fh)
+       if (! buf)
                return -1;
 
        pthread_rwlock_rdlock(&obj_lock);
                return -1;
 
        pthread_rwlock_rdlock(&obj_lock);
@@ -461,6 +461,8 @@ sdb_store_dump(FILE *fh)
                return -1;
        }
 
                return -1;
        }
 
+       sdb_strbuf_append(buf, "{\"hosts\":[");
+
        while (sdb_llist_iter_has_next(host_iter)) {
                sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
                sdb_llist_iter_t *svc_iter;
        while (sdb_llist_iter_has_next(host_iter)) {
                sdb_store_obj_t *host = SDB_STORE_OBJ(sdb_llist_iter_get_next(host_iter));
                sdb_llist_iter_t *svc_iter;
@@ -475,15 +477,17 @@ sdb_store_dump(FILE *fh)
                        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';
 
-               fprintf(fh, "Host '%s' (last updated: %s):\n",
+               sdb_strbuf_append(buf, "{\"name\": \"%s\", "
+                               "\"last_update\": \"%s\", "
+                               "\"attributes\": [",
                                SDB_OBJ(host)->name, time_str);
 
                attr_iter = sdb_llist_get_iter(host->attributes);
                if (! attr_iter) {
                        char errbuf[1024];
                                SDB_OBJ(host)->name, time_str);
 
                attr_iter = sdb_llist_get_iter(host->attributes);
                if (! attr_iter) {
                        char errbuf[1024];
-                       fprintf(fh, "Failed to retrieve attributes: %s\n",
+                       sdb_log(SDB_LOG_ERR, "store: Failed to retrieve attributes: %s\n",
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       continue;
+                       break;
                }
 
                while (sdb_llist_iter_has_next(attr_iter)) {
                }
 
                while (sdb_llist_iter_has_next(attr_iter)) {
@@ -495,18 +499,20 @@ sdb_store_dump(FILE *fh)
                                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';
 
-                       fprintf(fh, "\tAttribute '%s' -> '%s' (last updated: %s)\n",
+                       sdb_strbuf_append(buf, "{\"name\": \"%s\", "
+                                       "\"value\": \"%s\", \"last_update\": \"%s\"},",
                                        SDB_OBJ(attr)->name, attr->value, time_str);
                }
 
                sdb_llist_iter_destroy(attr_iter);
                                        SDB_OBJ(attr)->name, attr->value, time_str);
                }
 
                sdb_llist_iter_destroy(attr_iter);
+               sdb_strbuf_append(buf, "], \"services\": [");
 
                svc_iter = sdb_llist_get_iter(host->children);
                if (! svc_iter) {
                        char errbuf[1024];
 
                svc_iter = sdb_llist_get_iter(host->children);
                if (! svc_iter) {
                        char errbuf[1024];
-                       fprintf(fh, "Failed to retrieve services: %s\n",
+                       sdb_log(SDB_LOG_ERR, "store: Failed to retrieve services: %s\n",
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
                                        sdb_strerror(errno, errbuf, sizeof(errbuf)));
-                       continue;
+                       break;
                }
 
                while (sdb_llist_iter_has_next(svc_iter)) {
                }
 
                while (sdb_llist_iter_has_next(svc_iter)) {
@@ -518,17 +524,21 @@ sdb_store_dump(FILE *fh)
                                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';
 
-                       fprintf(fh, "\tService '%s' (last updated: %s)\n",
+                       sdb_strbuf_append(buf, "{\"name\": \"%s\", "
+                                       "\"last_update\": \"%s\"},",
                                        SDB_OBJ(svc)->name, time_str);
                }
 
                sdb_llist_iter_destroy(svc_iter);
                                        SDB_OBJ(svc)->name, time_str);
                }
 
                sdb_llist_iter_destroy(svc_iter);
+               sdb_strbuf_append(buf, "]}");
        }
 
        }
 
+       sdb_strbuf_append(buf, "]}");
+
        sdb_llist_iter_destroy(host_iter);
        pthread_rwlock_unlock(&obj_lock);
        return 0;
        sdb_llist_iter_destroy(host_iter);
        pthread_rwlock_unlock(&obj_lock);
        return 0;
-} /* sdb_store_dump */
+} /* sdb_store_tojson */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index e2309224d57243bd9dd7d7bc19f7fde1841c8ed9..123314d3dfc0aa0e2fa73434ae3f3fc5bcd9bfb7 100644 (file)
@@ -287,9 +287,6 @@ main(int argc, char **argv)
 
        sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
                        SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
 
        sdb_log(SDB_LOG_INFO, "Shutting down SysDB daemon "SDB_VERSION_STRING
                        SDB_VERSION_EXTRA" (pid %i)", (int)getpid());
-
-       fprintf(stderr, "Store dump:\n");
-       sdb_store_dump(stderr);
        return 0;
 } /* main */
 
        return 0;
 } /* main */
 
index 538390afb3d69d24aaf7cce07fa38c689a36840a..bef29ba97add3241950af85303c158a27fcc4272 100644 (file)
@@ -32,6 +32,7 @@
 #include "core/object.h"
 #include "core/time.h"
 #include "utils/llist.h"
 #include "core/object.h"
 #include "core/time.h"
 #include "utils/llist.h"
+#include "utils/strbuf.h"
 
 #include <stdio.h>
 
 
 #include <stdio.h>
 
@@ -98,8 +99,17 @@ int
 sdb_store_service(const char *hostname, const char *name,
                sdb_time_t last_update);
 
 sdb_store_service(const char *hostname, const char *name,
                sdb_time_t last_update);
 
+/*
+ * sdb_store_tojson:
+ * Serialize the entire store to JSON and write the result to the specified
+ * buffer.
+ *
+ * Returns:
+ *  - 0 on success
+ *  - a negative value on error
+ */
 int
 int
-sdb_store_dump(FILE *fh);
+sdb_store_tojson(sdb_strbuf_t *buf);
 
 #ifdef __cplusplus
 } /* extern "C" */
 
 #ifdef __cplusplus
 } /* extern "C" */