Code

store: Simplified store-attribute callback.
[sysdb.git] / src / core / store.c
1 /*
2  * SysDB - src/core/store.c
3  * Copyright (C) 2012-2013 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/store-private.h"
34 #include "core/plugin.h"
35 #include "utils/avltree.h"
36 #include "utils/error.h"
38 #include <assert.h>
40 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
46 #include <math.h>
47 #include <pthread.h>
49 /*
50  * private variables
51  */
53 struct sdb_store {
54         sdb_object_t super;
56         /* hosts are the top-level entries and
57          * reference everything else */
58         sdb_avltree_t *hosts;
59         pthread_rwlock_t host_lock;
60 };
62 /*
63  * private types
64  */
66 static sdb_type_t host_type;
67 static sdb_type_t service_type;
68 static sdb_type_t metric_type;
69 static sdb_type_t attribute_type;
71 static int
72 store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
73 {
74         int err;
75         if (! (SDB_STORE(obj)->hosts = sdb_avltree_create()))
76                 return -1;
77         if ((err = pthread_rwlock_init(&SDB_STORE(obj)->host_lock,
78                                         /* attr = */ NULL))) {
79                 char errbuf[128];
80                 sdb_log(SDB_LOG_ERR, "store: Failed to initialize lock: %s",
81                                 sdb_strerror(err, errbuf, sizeof(errbuf)));
82                 return -1;
83         }
84         return 0;
85 } /* store_init */
87 static void
88 store_destroy(sdb_object_t *obj)
89 {
90         int err;
91         if ((err = pthread_rwlock_destroy(&SDB_STORE(obj)->host_lock))) {
92                 char errbuf[128];
93                 sdb_log(SDB_LOG_ERR, "store: Failed to destroy lock: %s",
94                                 sdb_strerror(err, errbuf, sizeof(errbuf)));
95                 return;
96         }
97         sdb_avltree_destroy(SDB_STORE(obj)->hosts);
98         SDB_STORE(obj)->hosts = NULL;
99 } /* store_destroy */
101 static int
102 store_obj_init(sdb_object_t *obj, va_list ap)
104         sdb_store_obj_t *sobj = STORE_OBJ(obj);
106         sobj->type = va_arg(ap, int);
108         sobj->last_update = va_arg(ap, sdb_time_t);
109         sobj->interval = 0;
110         sobj->backends = NULL;
111         sobj->backends_num = 0;
112         sobj->parent = NULL;
113         return 0;
114 } /* store_obj_init */
116 static void
117 store_obj_destroy(sdb_object_t *obj)
119         sdb_store_obj_t *sobj = STORE_OBJ(obj);
120         size_t i;
122         for (i = 0; i < sobj->backends_num; ++i)
123                 free(sobj->backends[i]);
124         free(sobj->backends);
125         sobj->backends = NULL;
126         sobj->backends_num = 0;
128         // We don't currently keep an extra reference for parent objects to
129         // avoid circular self-references which are not handled correctly by
130         // the ref-count base management layer.
131         //sdb_object_deref(SDB_OBJ(sobj->parent));
132 } /* store_obj_destroy */
134 static int
135 host_init(sdb_object_t *obj, va_list ap)
137         host_t *sobj = HOST(obj);
138         int ret;
140         /* this will consume the first argument (type) of ap */
141         ret = store_obj_init(obj, ap);
142         if (ret)
143                 return ret;
145         sobj->services = sdb_avltree_create();
146         if (! sobj->services)
147                 return -1;
148         sobj->metrics = sdb_avltree_create();
149         if (! sobj->metrics)
150                 return -1;
151         sobj->attributes = sdb_avltree_create();
152         if (! sobj->attributes)
153                 return -1;
154         return 0;
155 } /* host_init */
157 static void
158 host_destroy(sdb_object_t *obj)
160         host_t *sobj = HOST(obj);
161         assert(obj);
163         store_obj_destroy(obj);
165         if (sobj->services)
166                 sdb_avltree_destroy(sobj->services);
167         if (sobj->metrics)
168                 sdb_avltree_destroy(sobj->metrics);
169         if (sobj->attributes)
170                 sdb_avltree_destroy(sobj->attributes);
171 } /* host_destroy */
173 static int
174 service_init(sdb_object_t *obj, va_list ap)
176         service_t *sobj = SVC(obj);
177         int ret;
179         /* this will consume the first argument (type) of ap */
180         ret = store_obj_init(obj, ap);
181         if (ret)
182                 return ret;
184         sobj->attributes = sdb_avltree_create();
185         if (! sobj->attributes)
186                 return -1;
187         return 0;
188 } /* service_init */
190 static void
191 service_destroy(sdb_object_t *obj)
193         service_t *sobj = SVC(obj);
194         assert(obj);
196         store_obj_destroy(obj);
198         if (sobj->attributes)
199                 sdb_avltree_destroy(sobj->attributes);
200 } /* service_destroy */
202 static int
203 metric_init(sdb_object_t *obj, va_list ap)
205         sdb_metric_t *sobj = METRIC(obj);
206         int ret;
208         /* this will consume the first argument (type) of ap */
209         ret = store_obj_init(obj, ap);
210         if (ret)
211                 return ret;
213         sobj->attributes = sdb_avltree_create();
214         if (! sobj->attributes)
215                 return -1;
217         sobj->store.type = sobj->store.id = NULL;
218         return 0;
219 } /* metric_init */
221 static void
222 metric_destroy(sdb_object_t *obj)
224         sdb_metric_t *sobj = METRIC(obj);
225         assert(obj);
227         store_obj_destroy(obj);
229         if (sobj->attributes)
230                 sdb_avltree_destroy(sobj->attributes);
232         if (sobj->store.type)
233                 free(sobj->store.type);
234         if (sobj->store.id)
235                 free(sobj->store.id);
236 } /* metric_destroy */
238 static int
239 attr_init(sdb_object_t *obj, va_list ap)
241         const sdb_data_t *value;
242         int ret;
244         /* this will consume the first two arguments
245          * (type and last_update) of ap */
246         ret = store_obj_init(obj, ap);
247         if (ret)
248                 return ret;
249         value = va_arg(ap, const sdb_data_t *);
251         if (value)
252                 if (sdb_data_copy(&ATTR(obj)->value, value))
253                         return -1;
254         return 0;
255 } /* attr_init */
257 static void
258 attr_destroy(sdb_object_t *obj)
260         assert(obj);
262         store_obj_destroy(obj);
263         sdb_data_free_datum(&ATTR(obj)->value);
264 } /* attr_destroy */
266 static sdb_type_t store_type = {
267         /* size = */ sizeof(sdb_store_t),
268         /* init = */ store_init,
269         /* destroy = */ store_destroy,
270 };
272 static sdb_type_t host_type = {
273         /* size = */ sizeof(host_t),
274         /* init = */ host_init,
275         /* destroy = */ host_destroy
276 };
278 static sdb_type_t service_type = {
279         /* size = */ sizeof(service_t),
280         /* init = */ service_init,
281         /* destroy = */ service_destroy
282 };
284 static sdb_type_t metric_type = {
285         /* size = */ sizeof(sdb_metric_t),
286         /* init = */ metric_init,
287         /* destroy = */ metric_destroy
288 };
290 static sdb_type_t attribute_type = {
291         /* size = */ sizeof(attr_t),
292         /* init = */ attr_init,
293         /* destroy = */ attr_destroy
294 };
296 /*
297  * private helper functions
298  */
300 static int
301 record_backend(sdb_store_obj_t *obj)
303         const sdb_plugin_info_t *info;
304         char **tmp;
305         size_t i;
307         info = sdb_plugin_current();
308         if (! info)
309                 return 0;
311         for (i = 0; i < obj->backends_num; ++i)
312                 if (!strcasecmp(obj->backends[i], info->plugin_name))
313                         return 0;
315         tmp = realloc(obj->backends,
316                         (obj->backends_num + 1) * sizeof(*obj->backends));
317         if (! tmp)
318                 return -1;
320         obj->backends = tmp;
321         obj->backends[obj->backends_num] = strdup(info->plugin_name);
322         if (! obj->backends[obj->backends_num])
323                 return -1;
325         ++obj->backends_num;
326         return 0;
327 } /* record_backend */
329 static int
330 store_obj(sdb_store_obj_t *parent, sdb_avltree_t *parent_tree,
331                 int type, const char *name, sdb_time_t last_update,
332                 sdb_store_obj_t **updated_obj)
334         sdb_store_obj_t *old, *new;
335         int status = 0;
337         assert(parent_tree);
339         if (last_update <= 0)
340                 last_update = sdb_gettime();
342         old = STORE_OBJ(sdb_avltree_lookup(parent_tree, name));
343         if (old) {
344                 if (old->last_update > last_update) {
345                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
346                                         "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
347                                         SDB_STORE_TYPE_TO_NAME(type), name,
348                                         last_update, old->last_update);
349                         /* don't report an error; the object may be updated by multiple
350                          * backends */
351                         status = 1;
352                 }
353                 else if (old->last_update == last_update) {
354                         /* don't report an error and also don't even log this to avoid
355                          * excessive noise on high sampling frequencies */
356                         status = 1;
357                 }
358                 else {
359                         sdb_time_t interval = last_update - old->last_update;
360                         old->last_update = last_update;
361                         if (interval) {
362                                 if (old->interval)
363                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
364                                                         + (0.1 * (double)interval));
365                                 else
366                                         old->interval = interval;
367                         }
368                 }
370                 new = old;
371                 sdb_object_deref(SDB_OBJ(old));
372         }
373         else {
374                 if (type == SDB_ATTRIBUTE) {
375                         /* the value will be updated by the caller */
376                         new = STORE_OBJ(sdb_object_create(name, attribute_type,
377                                                 type, last_update, NULL));
378                 }
379                 else {
380                         sdb_type_t t;
381                         t = type == SDB_HOST
382                                 ? host_type
383                                 : type == SDB_SERVICE
384                                         ? service_type
385                                         : metric_type;
386                         new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
387                 }
389                 if (new) {
390                         status = sdb_avltree_insert(parent_tree, SDB_OBJ(new));
392                         /* pass control to the tree or destroy in case of an error */
393                         sdb_object_deref(SDB_OBJ(new));
394                 }
395                 else {
396                         char errbuf[1024];
397                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
398                                         SDB_STORE_TYPE_TO_NAME(type), name,
399                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
400                         status = -1;
401                 }
402         }
404         if (status < 0)
405                 return status;
406         assert(new);
408         if (new->parent != parent) {
409                 // Avoid circular self-references which are not handled
410                 // correctly by the ref-count based management layer.
411                 //sdb_object_deref(SDB_OBJ(new->parent));
412                 //sdb_object_ref(SDB_OBJ(parent));
413                 new->parent = parent;
414         }
416         if (updated_obj)
417                 *updated_obj = new;
419         if (record_backend(new))
420                 return -1;
421         return status;
422 } /* store_obj */
424 static int
425 store_metric_store(sdb_metric_t *metric, sdb_store_metric_t *m)
427         char *type = metric->store.type;
428         char *id = metric->store.id;
430         if ((! metric->store.type) || strcasecmp(metric->store.type, m->store.type)) {
431                 if (! (type = strdup(m->store.type)))
432                         return -1;
433         }
434         if ((! metric->store.id) || strcasecmp(metric->store.id, m->store.id)) {
435                 if (! (id = strdup(m->store.id))) {
436                         if (type != metric->store.type)
437                                 free(type);
438                         return -1;
439                 }
440         }
442         if (type != metric->store.type) {
443                 if (metric->store.type)
444                         free(metric->store.type);
445                 metric->store.type = type;
446         }
447         if (id != metric->store.id) {
448                 if (metric->store.id)
449                         free(metric->store.id);
450                 metric->store.id = id;
451         }
452         return 0;
453 } /* store_metric_store */
455 /* The store's host_lock has to be acquired before calling this function. */
456 static sdb_avltree_t *
457 get_host_children(host_t *host, int type)
459         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
460                         && (type != SDB_ATTRIBUTE))
461                 return NULL;
463         if (! host)
464                 return NULL;
466         if (type == SDB_ATTRIBUTE)
467                 return host->attributes;
468         else if (type == SDB_METRIC)
469                 return host->metrics;
470         else
471                 return host->services;
472 } /* get_host_children */
474 /*
475  * ts_tojson serializes a time-series to JSON.
476  *
477  * The function never returns an error. Rather, an error message will be part
478  * of the serialized data.
479  */
480 static void
481 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
483         char start_str[64];
484         char end_str[64];
486         size_t i;
488         /* TODO: make time format configurable */
489         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
490                 snprintf(start_str, sizeof(start_str), "<error>");
491         start_str[sizeof(start_str) - 1] = '\0';
492         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
493                 snprintf(end_str, sizeof(end_str), "<error>");
494         end_str[sizeof(end_str) - 1] = '\0';
496         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
497                         start_str, end_str);
499         for (i = 0; i < ts->data_names_len; ++i) {
500                 size_t j;
501                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
503                 for (j = 0; j < ts->data_len; ++j) {
504                         char time_str[64];
506                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
507                                 snprintf(time_str, sizeof(time_str), "<error>");
508                         time_str[sizeof(time_str) - 1] = '\0';
510                         /* Some GNU libc versions may print '-nan' which we dont' want */
511                         if (isnan(ts->data[i][j].value))
512                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
513                                                 "\"value\": \"nan\"}", time_str);
514                         else
515                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
516                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
518                         if (j < ts->data_len - 1)
519                                 sdb_strbuf_append(buf, ",");
520                 }
522                 if (i < ts->data_names_len - 1)
523                         sdb_strbuf_append(buf, "],");
524                 else
525                         sdb_strbuf_append(buf, "]");
526         }
527         sdb_strbuf_append(buf, "}}");
528 } /* ts_tojson */
530 /*
531  * store writer API
532  */
534 static int
535 store_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
537         sdb_store_t *st = SDB_STORE(user_data);
538         sdb_store_obj_t *parent = NULL;
539         sdb_store_obj_t *new = NULL;
540         const char *hostname;
541         host_t *host;
543         sdb_avltree_t *children = NULL;
544         sdb_avltree_t *attributes;
545         int status = 0;
547         if ((! attr) || (! attr->parent) || (! attr->key))
548                 return -1;
550         hostname = attr->hostname;
551         if (attr->parent_type == SDB_HOST)
552                 hostname = attr->parent;
553         if (! hostname)
554                 return -1;
556         pthread_rwlock_wrlock(&st->host_lock);
557         host = HOST(sdb_avltree_lookup(st->hosts, hostname));
558         if (! host) {
559                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
560                                 "host '%s' not found", attr->key, hostname);
561                 status = -1;
562         }
564         switch (attr->parent_type) {
565         case SDB_HOST:
566                 parent = STORE_OBJ(host);
567                 attributes = get_host_children(host, SDB_ATTRIBUTE);
568                 break;
569         case SDB_SERVICE:
570                 children = get_host_children(host, SDB_SERVICE);
571                 break;
572         case SDB_METRIC:
573                 children = get_host_children(host, SDB_METRIC);
574                 break;
575         default:
576                 status = -1;
577                 break;
578         }
580         if (children) {
581                 parent = STORE_OBJ(sdb_avltree_lookup(children, attr->parent));
582                 if (! parent) {
583                         sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
584                                         "%s '%s/%s' not found", attr->key,
585                                         SDB_STORE_TYPE_TO_NAME(attr->parent_type),
586                                         attr->hostname, attr->parent);
587                         status = -1;
588                 }
589                 else
590                         attributes = attr->parent_type == SDB_SERVICE
591                                 ? SVC(parent)->attributes
592                                 : METRIC(parent)->attributes;
593         }
595         if (! status)
596                 status = store_obj(parent, attributes, SDB_ATTRIBUTE,
597                                 attr->key, attr->last_update, &new);
599         if (! status) {
600                 assert(new);
601                 /* update the value if it changed */
602                 if (sdb_data_cmp(&ATTR(new)->value, &attr->value))
603                         if (sdb_data_copy(&ATTR(new)->value, &attr->value))
604                                 status = -1;
605         }
607         if (parent != STORE_OBJ(host))
608                 sdb_object_deref(SDB_OBJ(parent));
609         sdb_object_deref(SDB_OBJ(host));
610         pthread_rwlock_unlock(&st->host_lock);
612         return status;
613 } /* store_attribute */
615 static int
616 store_host(sdb_store_host_t *host, sdb_object_t *user_data)
618         sdb_store_t *st = SDB_STORE(user_data);
619         int status = 0;
621         if ((! host) || (! host->name))
622                 return -1;
624         pthread_rwlock_wrlock(&st->host_lock);
625         status = store_obj(NULL, st->hosts,
626                         SDB_HOST, host->name, host->last_update, NULL);
627         pthread_rwlock_unlock(&st->host_lock);
629         return status;
630 } /* store_host */
632 static int
633 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
635         sdb_store_t *st = SDB_STORE(user_data);
637         host_t *host;
638         sdb_avltree_t *services;
640         int status = 0;
642         if ((! service) || (! service->hostname) || (! service->name))
643                 return -1;
645         pthread_rwlock_wrlock(&st->host_lock);
646         host = HOST(sdb_avltree_lookup(st->hosts, service->hostname));
647         services = get_host_children(host, SDB_SERVICE);
648         if (! services) {
649                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
650                                 "host '%s' not found", service->name, service->hostname);
651                 status = -1;
652         }
654         if (! status)
655                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
656                                 service->name, service->last_update, NULL);
658         sdb_object_deref(SDB_OBJ(host));
659         pthread_rwlock_unlock(&st->host_lock);
660         return status;
661 } /* store_service */
663 static int
664 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
666         sdb_store_t *st = SDB_STORE(user_data);
668         sdb_store_obj_t *obj = NULL;
669         sdb_avltree_t *metrics;
670         host_t *host;
672         int status = 0;
674         if ((! metric) || (! metric->hostname) || (! metric->name))
675                 return -1;
677         if ((metric->store.type != NULL) != (metric->store.id != NULL))
678                 return -1;
680         pthread_rwlock_wrlock(&st->host_lock);
681         host = HOST(sdb_avltree_lookup(st->hosts, metric->hostname));
682         metrics = get_host_children(host, SDB_METRIC);
683         if (! metrics) {
684                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
685                                 "host '%s' not found", metric->name, metric->hostname);
686                 status = -1;
687         }
689         if (! status)
690                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
691                                 metric->name, metric->last_update, &obj);
692         sdb_object_deref(SDB_OBJ(host));
694         if (status) {
695                 pthread_rwlock_unlock(&st->host_lock);
696                 return status;
697         }
699         assert(obj);
700         if (metric->store.type && metric->store.id)
701                 if (store_metric_store(METRIC(obj), metric))
702                         status = -1;
703         pthread_rwlock_unlock(&st->host_lock);
704         return status;
705 } /* store_metric */
707 sdb_store_writer_t sdb_store_writer = {
708         store_host, store_service, store_metric, store_attribute,
709 };
711 static sdb_object_t *
712 prepare_query(sdb_ast_node_t *ast,
713                 sdb_strbuf_t __attribute__((unused)) *errbuf,
714                 sdb_object_t __attribute__((unused)) *user_data)
716         return SDB_OBJ(sdb_store_query_prepare(ast));
717 } /* prepare_query */
719 static int
720 execute_query(sdb_object_t *q,
721                 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
722                 sdb_object_t *user_data)
724         return sdb_store_query_execute(SDB_STORE(user_data),
725                         QUERY(q), buf, errbuf);
726 } /* execute_query */
728 sdb_store_reader_t sdb_store_reader = {
729         prepare_query, execute_query,
730 };
732 /*
733  * public API
734  */
736 sdb_store_t *
737 sdb_store_create(void)
739         return SDB_STORE(sdb_object_create("store", store_type));
740 } /* sdb_store_create */
742 int
743 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
745         sdb_store_host_t host = {
746                 name, last_update, 0, NULL, 0,
747         };
748         return store_host(&host, SDB_OBJ(store));
749 } /* sdb_store_host */
751 int
752 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
753                 sdb_time_t last_update)
755         sdb_store_service_t service = {
756                 hostname, name, last_update, 0, NULL, 0,
757         };
758         return store_service(&service, SDB_OBJ(store));
759 } /* sdb_store_service */
761 int
762 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
763                 sdb_metric_store_t *metric_store, sdb_time_t last_update)
765         sdb_store_metric_t metric = {
766                 hostname, name, { NULL, NULL }, last_update, 0, NULL, 0,
767         };
768         if (metric_store) {
769                 metric.store.type = metric_store->type;
770                 metric.store.id = metric_store->id;
771         }
772         return store_metric(&metric, SDB_OBJ(store));
773 } /* sdb_store_metric */
775 int
776 sdb_store_attribute(sdb_store_t *store, const char *hostname,
777                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
779         sdb_store_attribute_t attr = {
780                 NULL, SDB_HOST, hostname, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
781         };
782         if (value) {
783                 attr.value = *value;
784         }
785         return store_attribute(&attr, SDB_OBJ(store));
786 } /* sdb_store_attribute */
788 int
789 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
790                 const char *service, const char *key, const sdb_data_t *value,
791                 sdb_time_t last_update)
793         sdb_store_attribute_t attr = {
794                 hostname, SDB_SERVICE, service, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
795         };
796         if (value) {
797                 attr.value = *value;
798         }
799         return store_attribute(&attr, SDB_OBJ(store));
800 } /* sdb_store_service_attr */
802 int
803 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
804                 const char *metric, const char *key, const sdb_data_t *value,
805                 sdb_time_t last_update)
807         sdb_store_attribute_t attr = {
808                 hostname, SDB_METRIC, metric, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
809         };
810         if (value) {
811                 attr.value = *value;
812         }
813         return store_attribute(&attr, SDB_OBJ(store));
814 } /* sdb_store_metric_attr */
816 sdb_store_obj_t *
817 sdb_store_get_host(sdb_store_t *store, const char *name)
819         host_t *host;
821         if ((! store) || (! name))
822                 return NULL;
824         host = HOST(sdb_avltree_lookup(store->hosts, name));
825         if (! host)
826                 return NULL;
828         return STORE_OBJ(host);
829 } /* sdb_store_get_host */
831 sdb_store_obj_t *
832 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
834         sdb_avltree_t *children;
836         if ((! host) || (host->type != SDB_HOST) || (! name))
837                 return NULL;
839         children = get_host_children(HOST(host), type);
840         if (! children)
841                 return NULL;
842         return STORE_OBJ(sdb_avltree_lookup(children, name));
843 } /* sdb_store_get_child */
845 int
846 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
848         sdb_data_t tmp;
850         if (! obj)
851                 return -1;
853         switch (field) {
854                 case SDB_FIELD_NAME:
855                         tmp.type = SDB_TYPE_STRING;
856                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
857                         if (! tmp.data.string)
858                                 return -1;
859                         break;
860                 case SDB_FIELD_LAST_UPDATE:
861                         tmp.type = SDB_TYPE_DATETIME;
862                         tmp.data.datetime = obj->last_update;
863                         break;
864                 case SDB_FIELD_AGE:
865                         tmp.type = SDB_TYPE_DATETIME;
866                         tmp.data.datetime = sdb_gettime() - obj->last_update;
867                         break;
868                 case SDB_FIELD_INTERVAL:
869                         tmp.type = SDB_TYPE_DATETIME;
870                         tmp.data.datetime = obj->interval;
871                         break;
872                 case SDB_FIELD_BACKEND:
873                         if (! res)
874                                 return 0;
875                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
876                         tmp.data.array.length = obj->backends_num;
877                         tmp.data.array.values = obj->backends;
878                         return sdb_data_copy(res, &tmp);
879                 case SDB_FIELD_VALUE:
880                         if (obj->type != SDB_ATTRIBUTE)
881                                 return -1;
882                         if (! res)
883                                 return 0;
884                         return sdb_data_copy(res, &ATTR(obj)->value);
885                 case SDB_FIELD_TIMESERIES:
886                         if (obj->type != SDB_METRIC)
887                                 return -1;
888                         tmp.type = SDB_TYPE_BOOLEAN;
889                         tmp.data.boolean = METRIC(obj)->store.type != NULL;
890                 default:
891                         return -1;
892         }
893         if (res)
894                 *res = tmp;
895         else
896                 sdb_data_free_datum(&tmp);
897         return 0;
898 } /* sdb_store_get_field */
900 int
901 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
902                 sdb_store_matcher_t *filter)
904         sdb_avltree_t *tree = NULL;
905         sdb_store_obj_t *attr;
907         if ((! obj) || (! name))
908                 return -1;
910         if (obj->type == SDB_HOST)
911                 tree = HOST(obj)->attributes;
912         else if (obj->type == SDB_SERVICE)
913                 tree = SVC(obj)->attributes;
914         else if (obj->type == SDB_METRIC)
915                 tree = METRIC(obj)->attributes;
917         if (! tree)
918                 return -1;
920         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
921         if (! attr)
922                 return -1;
923         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
924                 sdb_object_deref(SDB_OBJ(attr));
925                 return -1;
926         }
928         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
929         if (res)
930                 sdb_data_copy(res, &ATTR(attr)->value);
931         sdb_object_deref(SDB_OBJ(attr));
932         return 0;
933 } /* sdb_store_get_attr */
935 /* TODO: sdb_store_fetch_timeseries should move into the plugin module */
937 int
938 sdb_store_fetch_timeseries(sdb_store_t *store,
939                 const char *hostname, const char *metric,
940                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
942         sdb_avltree_t *metrics;
943         host_t *host;
944         sdb_metric_t *m;
946         sdb_timeseries_t *ts;
948         int status = 0;
950         if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
951                 return -1;
953         pthread_rwlock_rdlock(&store->host_lock);
954         host = HOST(sdb_avltree_lookup(store->hosts, hostname));
955         metrics = get_host_children(host, SDB_METRIC);
956         sdb_object_deref(SDB_OBJ(host));
957         if (! metrics) {
958                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
959                                 "- host '%s' not found", hostname, metric, hostname);
960                 pthread_rwlock_unlock(&store->host_lock);
961                 return -1;
962         }
964         m = METRIC(sdb_avltree_lookup(metrics, metric));
965         if (! m) {
966                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
967                                 "- metric '%s' not found", hostname, metric, metric);
968                 pthread_rwlock_unlock(&store->host_lock);
969                 return -1;
970         }
972         if ((! m->store.type) || (! m->store.id)) {
973                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
974                                 "- no data-store configured for the stored metric",
975                                 hostname, metric);
976                 sdb_object_deref(SDB_OBJ(m));
977                 pthread_rwlock_unlock(&store->host_lock);
978                 return -1;
979         }
981         {
982                 char type[strlen(m->store.type) + 1];
983                 char id[strlen(m->store.id) + 1];
985                 strncpy(type, m->store.type, sizeof(type));
986                 strncpy(id, m->store.id, sizeof(id));
987                 pthread_rwlock_unlock(&store->host_lock);
989                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
990                 if (! ts) {
991                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
992                                         "- %s fetcher callback returned no data for '%s'",
993                                         hostname, metric, type, id);
994                         status = -1;
995                 }
996         }
998         ts_tojson(ts, buf);
999         sdb_object_deref(SDB_OBJ(m));
1000         sdb_timeseries_destroy(ts);
1001         return status;
1002 } /* sdb_store_fetch_timeseries */
1004 int
1005 sdb_store_scan(sdb_store_t *store, int type,
1006                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1007                 sdb_store_lookup_cb cb, void *user_data)
1009         sdb_avltree_iter_t *host_iter = NULL;
1010         int status = 0;
1012         if ((! store) || (! cb))
1013                 return -1;
1015         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
1016                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
1017                 return -1;
1018         }
1020         pthread_rwlock_rdlock(&store->host_lock);
1021         host_iter = sdb_avltree_get_iter(store->hosts);
1022         if (! host_iter)
1023                 status = -1;
1025         /* has_next returns false if the iterator is NULL */
1026         while (sdb_avltree_iter_has_next(host_iter)) {
1027                 sdb_store_obj_t *host;
1028                 sdb_avltree_iter_t *iter = NULL;
1030                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1031                 assert(host);
1033                 if (! sdb_store_matcher_matches(filter, host, NULL))
1034                         continue;
1036                 if (type == SDB_SERVICE)
1037                         iter = sdb_avltree_get_iter(HOST(host)->services);
1038                 else if (type == SDB_METRIC)
1039                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
1041                 if (iter) {
1042                         while (sdb_avltree_iter_has_next(iter)) {
1043                                 sdb_store_obj_t *obj;
1044                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1045                                 assert(obj);
1047                                 if (sdb_store_matcher_matches(m, obj, filter)) {
1048                                         if (cb(obj, filter, user_data)) {
1049                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1050                                                                 "an error while scanning");
1051                                                 status = -1;
1052                                                 break;
1053                                         }
1054                                 }
1055                         }
1056                 }
1057                 else if (sdb_store_matcher_matches(m, host, filter)) {
1058                         if (cb(host, filter, user_data)) {
1059                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1060                                                 "an error while scanning");
1061                                 status = -1;
1062                         }
1063                 }
1065                 sdb_avltree_iter_destroy(iter);
1066                 if (status)
1067                         break;
1068         }
1070         sdb_avltree_iter_destroy(host_iter);
1071         pthread_rwlock_unlock(&store->host_lock);
1072         return status;
1073 } /* sdb_store_scan */
1075 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */