diff --git a/src/core/store.c b/src/core/store.c
index 19faee43994bdde816a6a84f4c09061bfb25bb4f..ed5b7ceb5095c683bdb2a3c775c67ff389950ff7 100644 (file)
--- a/src/core/store.c
+++ b/src/core/store.c
/*
* SysDB - src/core/store.c
- * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
+ * Copyright (C) 2012-2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
#include "sysdb.h"
#include "core/store.h"
-#include "core/error.h"
#include "core/plugin.h"
+#include "utils/error.h"
#include "utils/llist.h"
#include <assert.h>
return sobj;
}
+ /* don't lookups non-host types from hierarchical hosts */
+ if ((type != SDB_HOST) && (sobj->type == SDB_HOST))
+ continue;
+
sobj = sdb_store_lookup_in_list(sobj->children, type, name);
if (sobj) {
sdb_llist_iter_destroy(iter);
parent_list = parent->children;
}
- /* TODO: only look into direct children? */
if (type == SDB_HOST)
/* make sure that each host is unique */
old = STORE_OBJ(sdb_store_lookup_in_list(obj_list, type, name));
else if (type == SDB_ATTRIBUTE)
+ /* look into attributes of this host */
old = STORE_OBJ(sdb_llist_search_by_name(parent_list, name));
else
+ /* look into services assigned to this host (sdb_store_lookup_in_list
+ * does not look up services from hierarchical hosts) */
old = STORE_OBJ(sdb_store_lookup_in_list(parent_list, type, name));
if (old) {
/* stored object = */ SDB_ATTRIBUTE, key, last_update,
&updated_attr);
- SDB_ATTR(updated_attr)->value = strdup(value);
- if (! SDB_ATTR(updated_attr)->value) {
- sdb_object_deref(SDB_OBJ(updated_attr));
- status = -1;
+ if (status >= 0) {
+ assert(updated_attr);
+ SDB_ATTR(updated_attr)->value = strdup(value);
+ if (! SDB_ATTR(updated_attr)->value) {
+ sdb_object_deref(SDB_OBJ(updated_attr));
+ status = -1;
+ }
}
+
pthread_rwlock_unlock(&obj_lock);
return status;
} /* sdb_store_attribute */
/* TODO: actually support hierarchical data */
int
-sdb_store_dump(FILE *fh)
+sdb_store_tojson(sdb_strbuf_t *buf)
{
sdb_llist_iter_t *host_iter;
- if (! fh)
+ if (! buf)
return -1;
pthread_rwlock_rdlock(&obj_lock);
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;
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];
- 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)));
- continue;
+ sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
+ "attributes: %s\"}", errbuf);
}
+ /* has_next returns false if the iterator is NULL */
while (sdb_llist_iter_has_next(attr_iter)) {
sdb_attribute_t *attr = SDB_ATTR(sdb_llist_iter_get_next(attr_iter));
assert(attr);
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_strbuf_append(buf, "], \"services\": [");
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)));
- continue;
+ sdb_strbuf_append(buf, "{\"error\": \"failed to retrieve "
+ "services: %s\"}", errbuf);
}
while (sdb_llist_iter_has_next(svc_iter)) {
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_strbuf_append(buf, "]},");
}
+ sdb_strbuf_append(buf, "]}");
+
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 : */