Code

store: Add the timeseries field to metrics.
[sysdb.git] / src / core / store.c
index cb3189d72245690941c6d010a41dfa6d5da7f927..bf12a9378eb228e811a87ea268eb8fa403991672 100644 (file)
@@ -640,6 +640,7 @@ sdb_store_service(const char *hostname, const char *name,
 {
        sdb_host_t *host;
        sdb_avltree_t *services;
+       sdb_data_t d;
 
        int status = 0;
 
@@ -667,6 +668,12 @@ sdb_store_service(const char *hostname, const char *name,
 
        if (sdb_plugin_store_service(hostname, name, last_update))
                status = -1;
+
+       /* record the hostname as an attribute */
+       d.type = SDB_TYPE_STRING;
+       d.data.string = SDB_OBJ(host)->name;
+       if (sdb_store_service_attr(hostname, name, "hostname", &d, last_update))
+               status = -1;
        return status;
 } /* sdb_store_service */
 
@@ -721,6 +728,7 @@ sdb_store_metric(const char *hostname, const char *name,
        sdb_store_obj_t *obj = NULL;
        sdb_host_t *host;
        sdb_metric_t *metric;
+       sdb_data_t d;
 
        sdb_avltree_t *metrics;
 
@@ -765,6 +773,12 @@ sdb_store_metric(const char *hostname, const char *name,
 
        if (sdb_plugin_store_metric(hostname, name, store, last_update))
                status = -1;
+
+       /* record the hostname as an attribute */
+       d.type = SDB_TYPE_STRING;
+       d.data.string = SDB_OBJ(host)->name;
+       if (sdb_store_metric_attr(hostname, name, "hostname", &d, last_update))
+               status = -1;
        return status;
 } /* sdb_store_metric */
 
@@ -836,6 +850,8 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric,
 
        sdb_timeseries_t *ts;
 
+       int status = 0;
+
        if ((! hostname) || (! metric) || (! opts) || (! buf))
                return -1;
 
@@ -862,6 +878,7 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric,
                sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
                                "- no data-store configured for the stored metric",
                                hostname, metric);
+               sdb_object_deref(SDB_OBJ(m));
                pthread_rwlock_unlock(&host_lock);
                return -1;
        }
@@ -879,13 +896,14 @@ sdb_store_fetch_timeseries(const char *hostname, const char *metric,
                        sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
                                        "- %s fetcher callback returned no data for '%s'",
                                        hostname, metric, type, id);
-                       return -1;
+                       status = -1;
                }
        }
 
        ts_tojson(ts, buf);
+       sdb_object_deref(SDB_OBJ(m));
        sdb_timeseries_destroy(ts);
-       return 0;
+       return status;
 } /* sdb_store_fetch_timeseries */
 
 int
@@ -928,6 +946,11 @@ sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
                        if (! res)
                                return 0;
                        return sdb_data_copy(res, &ATTR(obj)->value);
+               case SDB_FIELD_TIMESERIES:
+                       if (obj->type != SDB_METRIC)
+                               return -1;
+                       tmp.type = SDB_TYPE_BOOLEAN;
+                       tmp.data.boolean = METRIC(obj)->store.type != NULL;
                default:
                        return -1;
        }