Code

store: Add separate store-object types for the store writer API.
[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_attr(sdb_store_obj_t *parent, sdb_avltree_t *attributes,
426                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
428         sdb_store_obj_t *attr = NULL;
429         int status;
431         status = store_obj(parent, attributes, SDB_ATTRIBUTE,
432                         key, last_update, &attr);
433         if (status)
434                 return status;
436         /* don't update unchanged values */
437         if (! sdb_data_cmp(&ATTR(attr)->value, value))
438                 return status;
440         assert(attr);
441         if (sdb_data_copy(&ATTR(attr)->value, value))
442                 return -1;
443         return status;
444 } /* store_attr */
446 static int
447 store_metric_store(sdb_metric_t *metric, sdb_store_metric_t *m)
449         char *type = metric->store.type;
450         char *id = metric->store.id;
452         if ((! metric->store.type) || strcasecmp(metric->store.type, m->store.type)) {
453                 if (! (type = strdup(m->store.type)))
454                         return -1;
455         }
456         if ((! metric->store.id) || strcasecmp(metric->store.id, m->store.id)) {
457                 if (! (id = strdup(m->store.id))) {
458                         if (type != metric->store.type)
459                                 free(type);
460                         return -1;
461                 }
462         }
464         if (type != metric->store.type) {
465                 if (metric->store.type)
466                         free(metric->store.type);
467                 metric->store.type = type;
468         }
469         if (id != metric->store.id) {
470                 if (metric->store.id)
471                         free(metric->store.id);
472                 metric->store.id = id;
473         }
474         return 0;
475 } /* store_metric_store */
477 /* The store's host_lock has to be acquired before calling this function. */
478 static sdb_avltree_t *
479 get_host_children(host_t *host, int type)
481         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
482                         && (type != SDB_ATTRIBUTE))
483                 return NULL;
485         if (! host)
486                 return NULL;
488         if (type == SDB_ATTRIBUTE)
489                 return host->attributes;
490         else if (type == SDB_METRIC)
491                 return host->metrics;
492         else
493                 return host->services;
494 } /* get_host_children */
496 /*
497  * ts_tojson serializes a time-series to JSON.
498  *
499  * The function never returns an error. Rather, an error message will be part
500  * of the serialized data.
501  */
502 static void
503 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
505         char start_str[64];
506         char end_str[64];
508         size_t i;
510         /* TODO: make time format configurable */
511         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
512                 snprintf(start_str, sizeof(start_str), "<error>");
513         start_str[sizeof(start_str) - 1] = '\0';
514         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
515                 snprintf(end_str, sizeof(end_str), "<error>");
516         end_str[sizeof(end_str) - 1] = '\0';
518         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
519                         start_str, end_str);
521         for (i = 0; i < ts->data_names_len; ++i) {
522                 size_t j;
523                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
525                 for (j = 0; j < ts->data_len; ++j) {
526                         char time_str[64];
528                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
529                                 snprintf(time_str, sizeof(time_str), "<error>");
530                         time_str[sizeof(time_str) - 1] = '\0';
532                         /* Some GNU libc versions may print '-nan' which we dont' want */
533                         if (isnan(ts->data[i][j].value))
534                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
535                                                 "\"value\": \"nan\"}", time_str);
536                         else
537                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
538                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
540                         if (j < ts->data_len - 1)
541                                 sdb_strbuf_append(buf, ",");
542                 }
544                 if (i < ts->data_names_len - 1)
545                         sdb_strbuf_append(buf, "],");
546                 else
547                         sdb_strbuf_append(buf, "]");
548         }
549         sdb_strbuf_append(buf, "}}");
550 } /* ts_tojson */
552 /*
553  * store writer API
554  */
556 static int
557 store_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
559         sdb_store_t *st = SDB_STORE(user_data);
560         const char *hostname;
561         host_t *host;
563         sdb_store_obj_t *obj = NULL;
564         sdb_avltree_t *children = NULL;
565         sdb_avltree_t *attrs;
566         int status = 0;
568         if ((! attr) || (! attr->parent) || (! attr->key))
569                 return -1;
571         hostname = attr->hostname;
572         if (attr->parent_type == SDB_HOST)
573                 hostname = attr->parent;
574         if (! hostname)
575                 return -1;
577         pthread_rwlock_wrlock(&st->host_lock);
578         host = HOST(sdb_avltree_lookup(st->hosts, hostname));
579         if (! host) {
580                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
581                                 "host '%s' not found", attr->key, hostname);
582                 status = -1;
583         }
585         switch (attr->parent_type) {
586         case SDB_HOST:
587                 obj = STORE_OBJ(host);
588                 attrs = get_host_children(host, SDB_ATTRIBUTE);
589                 break;
590         case SDB_SERVICE:
591                 children = get_host_children(host, SDB_SERVICE);
592                 break;
593         case SDB_METRIC:
594                 children = get_host_children(host, SDB_METRIC);
595                 break;
596         default:
597                 status = -1;
598                 break;
599         }
601         if (children) {
602                 obj = STORE_OBJ(sdb_avltree_lookup(children, attr->parent));
603                 if (! obj) {
604                         sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
605                                         "%s '%s/%s' not found", attr->key,
606                                         SDB_STORE_TYPE_TO_NAME(attr->parent_type),
607                                         attr->hostname, attr->parent);
608                         status = -1;
609                 }
610                 else
611                         attrs = attr->parent_type == SDB_SERVICE
612                                 ? SVC(obj)->attributes
613                                 : METRIC(obj)->attributes;
614         }
616         if (! status)
617                 status = store_attr(obj, attrs, attr->key, &attr->value, attr->last_update);
619         if (obj != STORE_OBJ(host))
620                 sdb_object_deref(SDB_OBJ(obj));
621         sdb_object_deref(SDB_OBJ(host));
622         pthread_rwlock_unlock(&st->host_lock);
624         return status;
625 } /* store_attribute */
627 static int
628 store_host(sdb_store_host_t *host, sdb_object_t *user_data)
630         sdb_store_t *st = SDB_STORE(user_data);
631         int status = 0;
633         if ((! host) || (! host->name))
634                 return -1;
636         pthread_rwlock_wrlock(&st->host_lock);
637         status = store_obj(NULL, st->hosts,
638                         SDB_HOST, host->name, host->last_update, NULL);
639         pthread_rwlock_unlock(&st->host_lock);
641         return status;
642 } /* store_host */
644 static int
645 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
647         sdb_store_t *st = SDB_STORE(user_data);
649         host_t *host;
650         sdb_avltree_t *services;
652         int status = 0;
654         if ((! service) || (! service->hostname) || (! service->name))
655                 return -1;
657         pthread_rwlock_wrlock(&st->host_lock);
658         host = HOST(sdb_avltree_lookup(st->hosts, service->hostname));
659         services = get_host_children(host, SDB_SERVICE);
660         if (! services) {
661                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
662                                 "host '%s' not found", service->name, service->hostname);
663                 status = -1;
664         }
666         if (! status)
667                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
668                                 service->name, service->last_update, NULL);
670         sdb_object_deref(SDB_OBJ(host));
671         pthread_rwlock_unlock(&st->host_lock);
672         return status;
673 } /* store_service */
675 static int
676 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
678         sdb_store_t *st = SDB_STORE(user_data);
680         sdb_store_obj_t *obj = NULL;
681         sdb_avltree_t *metrics;
682         host_t *host;
684         int status = 0;
686         if ((! metric) || (! metric->hostname) || (! metric->name))
687                 return -1;
689         if ((metric->store.type != NULL) != (metric->store.id != NULL))
690                 return -1;
692         pthread_rwlock_wrlock(&st->host_lock);
693         host = HOST(sdb_avltree_lookup(st->hosts, metric->hostname));
694         metrics = get_host_children(host, SDB_METRIC);
695         if (! metrics) {
696                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
697                                 "host '%s' not found", metric->name, metric->hostname);
698                 status = -1;
699         }
701         if (! status)
702                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
703                                 metric->name, metric->last_update, &obj);
704         sdb_object_deref(SDB_OBJ(host));
706         if (status) {
707                 pthread_rwlock_unlock(&st->host_lock);
708                 return status;
709         }
711         assert(obj);
712         if (metric->store.type && metric->store.id)
713                 if (store_metric_store(METRIC(obj), metric))
714                         status = -1;
715         pthread_rwlock_unlock(&st->host_lock);
716         return status;
717 } /* store_metric */
719 sdb_store_writer_t sdb_store_writer = {
720         store_host, store_service, store_metric, store_attribute,
721 };
723 static sdb_object_t *
724 prepare_query(sdb_ast_node_t *ast,
725                 sdb_strbuf_t __attribute__((unused)) *errbuf,
726                 sdb_object_t __attribute__((unused)) *user_data)
728         return SDB_OBJ(sdb_store_query_prepare(ast));
729 } /* prepare_query */
731 static int
732 execute_query(sdb_object_t *q,
733                 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
734                 sdb_object_t *user_data)
736         return sdb_store_query_execute(SDB_STORE(user_data),
737                         QUERY(q), buf, errbuf);
738 } /* execute_query */
740 sdb_store_reader_t sdb_store_reader = {
741         prepare_query, execute_query,
742 };
744 /*
745  * public API
746  */
748 sdb_store_t *
749 sdb_store_create(void)
751         return SDB_STORE(sdb_object_create("store", store_type));
752 } /* sdb_store_create */
754 int
755 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
757         sdb_store_host_t host = {
758                 name, last_update, 0, NULL, 0,
759         };
760         return store_host(&host, SDB_OBJ(store));
761 } /* sdb_store_host */
763 int
764 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
765                 sdb_time_t last_update)
767         sdb_store_service_t service = {
768                 hostname, name, last_update, 0, NULL, 0,
769         };
770         return store_service(&service, SDB_OBJ(store));
771 } /* sdb_store_service */
773 int
774 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
775                 sdb_metric_store_t *metric_store, sdb_time_t last_update)
777         sdb_store_metric_t metric = {
778                 hostname, name, { NULL, NULL }, last_update, 0, NULL, 0,
779         };
780         if (metric_store) {
781                 metric.store.type = metric_store->type;
782                 metric.store.id = metric_store->id;
783         }
784         return store_metric(&metric, SDB_OBJ(store));
785 } /* sdb_store_metric */
787 int
788 sdb_store_attribute(sdb_store_t *store, const char *hostname,
789                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
791         sdb_store_attribute_t attr = {
792                 NULL, SDB_HOST, hostname, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
793         };
794         if (value) {
795                 attr.value = *value;
796         }
797         return store_attribute(&attr, SDB_OBJ(store));
798 } /* sdb_store_attribute */
800 int
801 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
802                 const char *service, const char *key, const sdb_data_t *value,
803                 sdb_time_t last_update)
805         sdb_store_attribute_t attr = {
806                 hostname, SDB_SERVICE, service, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
807         };
808         if (value) {
809                 attr.value = *value;
810         }
811         return store_attribute(&attr, SDB_OBJ(store));
812 } /* sdb_store_service_attr */
814 int
815 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
816                 const char *metric, const char *key, const sdb_data_t *value,
817                 sdb_time_t last_update)
819         sdb_store_attribute_t attr = {
820                 hostname, SDB_METRIC, metric, key, SDB_DATA_INIT, last_update, 0, NULL, 0,
821         };
822         if (value) {
823                 attr.value = *value;
824         }
825         return store_attribute(&attr, SDB_OBJ(store));
826 } /* sdb_store_metric_attr */
828 sdb_store_obj_t *
829 sdb_store_get_host(sdb_store_t *store, const char *name)
831         host_t *host;
833         if ((! store) || (! name))
834                 return NULL;
836         host = HOST(sdb_avltree_lookup(store->hosts, name));
837         if (! host)
838                 return NULL;
840         return STORE_OBJ(host);
841 } /* sdb_store_get_host */
843 sdb_store_obj_t *
844 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
846         sdb_avltree_t *children;
848         if ((! host) || (host->type != SDB_HOST) || (! name))
849                 return NULL;
851         children = get_host_children(HOST(host), type);
852         if (! children)
853                 return NULL;
854         return STORE_OBJ(sdb_avltree_lookup(children, name));
855 } /* sdb_store_get_child */
857 int
858 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
860         sdb_data_t tmp;
862         if (! obj)
863                 return -1;
865         switch (field) {
866                 case SDB_FIELD_NAME:
867                         tmp.type = SDB_TYPE_STRING;
868                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
869                         if (! tmp.data.string)
870                                 return -1;
871                         break;
872                 case SDB_FIELD_LAST_UPDATE:
873                         tmp.type = SDB_TYPE_DATETIME;
874                         tmp.data.datetime = obj->last_update;
875                         break;
876                 case SDB_FIELD_AGE:
877                         tmp.type = SDB_TYPE_DATETIME;
878                         tmp.data.datetime = sdb_gettime() - obj->last_update;
879                         break;
880                 case SDB_FIELD_INTERVAL:
881                         tmp.type = SDB_TYPE_DATETIME;
882                         tmp.data.datetime = obj->interval;
883                         break;
884                 case SDB_FIELD_BACKEND:
885                         if (! res)
886                                 return 0;
887                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
888                         tmp.data.array.length = obj->backends_num;
889                         tmp.data.array.values = obj->backends;
890                         return sdb_data_copy(res, &tmp);
891                 case SDB_FIELD_VALUE:
892                         if (obj->type != SDB_ATTRIBUTE)
893                                 return -1;
894                         if (! res)
895                                 return 0;
896                         return sdb_data_copy(res, &ATTR(obj)->value);
897                 case SDB_FIELD_TIMESERIES:
898                         if (obj->type != SDB_METRIC)
899                                 return -1;
900                         tmp.type = SDB_TYPE_BOOLEAN;
901                         tmp.data.boolean = METRIC(obj)->store.type != NULL;
902                 default:
903                         return -1;
904         }
905         if (res)
906                 *res = tmp;
907         else
908                 sdb_data_free_datum(&tmp);
909         return 0;
910 } /* sdb_store_get_field */
912 int
913 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
914                 sdb_store_matcher_t *filter)
916         sdb_avltree_t *tree = NULL;
917         sdb_store_obj_t *attr;
919         if ((! obj) || (! name))
920                 return -1;
922         if (obj->type == SDB_HOST)
923                 tree = HOST(obj)->attributes;
924         else if (obj->type == SDB_SERVICE)
925                 tree = SVC(obj)->attributes;
926         else if (obj->type == SDB_METRIC)
927                 tree = METRIC(obj)->attributes;
929         if (! tree)
930                 return -1;
932         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
933         if (! attr)
934                 return -1;
935         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
936                 sdb_object_deref(SDB_OBJ(attr));
937                 return -1;
938         }
940         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
941         if (res)
942                 sdb_data_copy(res, &ATTR(attr)->value);
943         sdb_object_deref(SDB_OBJ(attr));
944         return 0;
945 } /* sdb_store_get_attr */
947 /* TODO: sdb_store_fetch_timeseries should move into the plugin module */
949 int
950 sdb_store_fetch_timeseries(sdb_store_t *store,
951                 const char *hostname, const char *metric,
952                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
954         sdb_avltree_t *metrics;
955         host_t *host;
956         sdb_metric_t *m;
958         sdb_timeseries_t *ts;
960         int status = 0;
962         if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
963                 return -1;
965         pthread_rwlock_rdlock(&store->host_lock);
966         host = HOST(sdb_avltree_lookup(store->hosts, hostname));
967         metrics = get_host_children(host, SDB_METRIC);
968         sdb_object_deref(SDB_OBJ(host));
969         if (! metrics) {
970                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
971                                 "- host '%s' not found", hostname, metric, hostname);
972                 pthread_rwlock_unlock(&store->host_lock);
973                 return -1;
974         }
976         m = METRIC(sdb_avltree_lookup(metrics, metric));
977         if (! m) {
978                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
979                                 "- metric '%s' not found", hostname, metric, metric);
980                 pthread_rwlock_unlock(&store->host_lock);
981                 return -1;
982         }
984         if ((! m->store.type) || (! m->store.id)) {
985                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
986                                 "- no data-store configured for the stored metric",
987                                 hostname, metric);
988                 sdb_object_deref(SDB_OBJ(m));
989                 pthread_rwlock_unlock(&store->host_lock);
990                 return -1;
991         }
993         {
994                 char type[strlen(m->store.type) + 1];
995                 char id[strlen(m->store.id) + 1];
997                 strncpy(type, m->store.type, sizeof(type));
998                 strncpy(id, m->store.id, sizeof(id));
999                 pthread_rwlock_unlock(&store->host_lock);
1001                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
1002                 if (! ts) {
1003                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1004                                         "- %s fetcher callback returned no data for '%s'",
1005                                         hostname, metric, type, id);
1006                         status = -1;
1007                 }
1008         }
1010         ts_tojson(ts, buf);
1011         sdb_object_deref(SDB_OBJ(m));
1012         sdb_timeseries_destroy(ts);
1013         return status;
1014 } /* sdb_store_fetch_timeseries */
1016 int
1017 sdb_store_scan(sdb_store_t *store, int type,
1018                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1019                 sdb_store_lookup_cb cb, void *user_data)
1021         sdb_avltree_iter_t *host_iter = NULL;
1022         int status = 0;
1024         if ((! store) || (! cb))
1025                 return -1;
1027         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
1028                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
1029                 return -1;
1030         }
1032         pthread_rwlock_rdlock(&store->host_lock);
1033         host_iter = sdb_avltree_get_iter(store->hosts);
1034         if (! host_iter)
1035                 status = -1;
1037         /* has_next returns false if the iterator is NULL */
1038         while (sdb_avltree_iter_has_next(host_iter)) {
1039                 sdb_store_obj_t *host;
1040                 sdb_avltree_iter_t *iter = NULL;
1042                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1043                 assert(host);
1045                 if (! sdb_store_matcher_matches(filter, host, NULL))
1046                         continue;
1048                 if (type == SDB_SERVICE)
1049                         iter = sdb_avltree_get_iter(HOST(host)->services);
1050                 else if (type == SDB_METRIC)
1051                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
1053                 if (iter) {
1054                         while (sdb_avltree_iter_has_next(iter)) {
1055                                 sdb_store_obj_t *obj;
1056                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1057                                 assert(obj);
1059                                 if (sdb_store_matcher_matches(m, obj, filter)) {
1060                                         if (cb(obj, filter, user_data)) {
1061                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1062                                                                 "an error while scanning");
1063                                                 status = -1;
1064                                                 break;
1065                                         }
1066                                 }
1067                         }
1068                 }
1069                 else if (sdb_store_matcher_matches(m, host, filter)) {
1070                         if (cb(host, filter, user_data)) {
1071                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1072                                                 "an error while scanning");
1073                                 status = -1;
1074                         }
1075                 }
1077                 sdb_avltree_iter_destroy(iter);
1078                 if (status)
1079                         break;
1080         }
1082         sdb_avltree_iter_destroy(host_iter);
1083         pthread_rwlock_unlock(&store->host_lock);
1084         return status;
1085 } /* sdb_store_scan */
1087 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */