Code

9c83218f1efccbde39512f6831383d5f99c5801e
[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 types
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 /* internal representation of a to-be-stored object */
63 typedef struct {
64         sdb_store_obj_t *parent;
65         sdb_avltree_t *parent_tree;
66         int type;
67         const char *name;
68         sdb_time_t last_update;
69 } store_obj_t;
70 #define STORE_OBJ_INIT { NULL, NULL, 0, NULL, 0 }
72 static sdb_type_t host_type;
73 static sdb_type_t service_type;
74 static sdb_type_t metric_type;
75 static sdb_type_t attribute_type;
77 static int
78 store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
79 {
80         int err;
81         if (! (SDB_STORE(obj)->hosts = sdb_avltree_create()))
82                 return -1;
83         if ((err = pthread_rwlock_init(&SDB_STORE(obj)->host_lock,
84                                         /* attr = */ NULL))) {
85                 char errbuf[128];
86                 sdb_log(SDB_LOG_ERR, "store: Failed to initialize lock: %s",
87                                 sdb_strerror(err, errbuf, sizeof(errbuf)));
88                 return -1;
89         }
90         return 0;
91 } /* store_init */
93 static void
94 store_destroy(sdb_object_t *obj)
95 {
96         int err;
97         if ((err = pthread_rwlock_destroy(&SDB_STORE(obj)->host_lock))) {
98                 char errbuf[128];
99                 sdb_log(SDB_LOG_ERR, "store: Failed to destroy lock: %s",
100                                 sdb_strerror(err, errbuf, sizeof(errbuf)));
101                 return;
102         }
103         sdb_avltree_destroy(SDB_STORE(obj)->hosts);
104         SDB_STORE(obj)->hosts = NULL;
105 } /* store_destroy */
107 static int
108 store_obj_init(sdb_object_t *obj, va_list ap)
110         sdb_store_obj_t *sobj = STORE_OBJ(obj);
112         sobj->type = va_arg(ap, int);
114         sobj->last_update = va_arg(ap, sdb_time_t);
115         sobj->interval = 0;
116         sobj->backends = NULL;
117         sobj->backends_num = 0;
118         sobj->parent = NULL;
119         return 0;
120 } /* store_obj_init */
122 static void
123 store_obj_destroy(sdb_object_t *obj)
125         sdb_store_obj_t *sobj = STORE_OBJ(obj);
126         size_t i;
128         for (i = 0; i < sobj->backends_num; ++i)
129                 free(sobj->backends[i]);
130         free(sobj->backends);
131         sobj->backends = NULL;
132         sobj->backends_num = 0;
134         // We don't currently keep an extra reference for parent objects to
135         // avoid circular self-references which are not handled correctly by
136         // the ref-count base management layer.
137         //sdb_object_deref(SDB_OBJ(sobj->parent));
138 } /* store_obj_destroy */
140 static int
141 host_init(sdb_object_t *obj, va_list ap)
143         host_t *sobj = HOST(obj);
144         int ret;
146         /* this will consume the first argument (type) of ap */
147         ret = store_obj_init(obj, ap);
148         if (ret)
149                 return ret;
151         sobj->services = sdb_avltree_create();
152         if (! sobj->services)
153                 return -1;
154         sobj->metrics = sdb_avltree_create();
155         if (! sobj->metrics)
156                 return -1;
157         sobj->attributes = sdb_avltree_create();
158         if (! sobj->attributes)
159                 return -1;
160         return 0;
161 } /* host_init */
163 static void
164 host_destroy(sdb_object_t *obj)
166         host_t *sobj = HOST(obj);
167         assert(obj);
169         store_obj_destroy(obj);
171         if (sobj->services)
172                 sdb_avltree_destroy(sobj->services);
173         if (sobj->metrics)
174                 sdb_avltree_destroy(sobj->metrics);
175         if (sobj->attributes)
176                 sdb_avltree_destroy(sobj->attributes);
177 } /* host_destroy */
179 static int
180 service_init(sdb_object_t *obj, va_list ap)
182         service_t *sobj = SVC(obj);
183         int ret;
185         /* this will consume the first argument (type) of ap */
186         ret = store_obj_init(obj, ap);
187         if (ret)
188                 return ret;
190         sobj->attributes = sdb_avltree_create();
191         if (! sobj->attributes)
192                 return -1;
193         return 0;
194 } /* service_init */
196 static void
197 service_destroy(sdb_object_t *obj)
199         service_t *sobj = SVC(obj);
200         assert(obj);
202         store_obj_destroy(obj);
204         if (sobj->attributes)
205                 sdb_avltree_destroy(sobj->attributes);
206 } /* service_destroy */
208 static int
209 metric_init(sdb_object_t *obj, va_list ap)
211         sdb_metric_t *sobj = METRIC(obj);
212         int ret;
214         /* this will consume the first argument (type) of ap */
215         ret = store_obj_init(obj, ap);
216         if (ret)
217                 return ret;
219         sobj->attributes = sdb_avltree_create();
220         if (! sobj->attributes)
221                 return -1;
223         sobj->store.type = sobj->store.id = NULL;
224         return 0;
225 } /* metric_init */
227 static void
228 metric_destroy(sdb_object_t *obj)
230         sdb_metric_t *sobj = METRIC(obj);
231         assert(obj);
233         store_obj_destroy(obj);
235         if (sobj->attributes)
236                 sdb_avltree_destroy(sobj->attributes);
238         if (sobj->store.type)
239                 free(sobj->store.type);
240         if (sobj->store.id)
241                 free(sobj->store.id);
242 } /* metric_destroy */
244 static int
245 attr_init(sdb_object_t *obj, va_list ap)
247         const sdb_data_t *value;
248         int ret;
250         /* this will consume the first two arguments
251          * (type and last_update) of ap */
252         ret = store_obj_init(obj, ap);
253         if (ret)
254                 return ret;
255         value = va_arg(ap, const sdb_data_t *);
257         if (value)
258                 if (sdb_data_copy(&ATTR(obj)->value, value))
259                         return -1;
260         return 0;
261 } /* attr_init */
263 static void
264 attr_destroy(sdb_object_t *obj)
266         assert(obj);
268         store_obj_destroy(obj);
269         sdb_data_free_datum(&ATTR(obj)->value);
270 } /* attr_destroy */
272 static sdb_type_t store_type = {
273         /* size = */ sizeof(sdb_store_t),
274         /* init = */ store_init,
275         /* destroy = */ store_destroy,
276 };
278 static sdb_type_t host_type = {
279         /* size = */ sizeof(host_t),
280         /* init = */ host_init,
281         /* destroy = */ host_destroy
282 };
284 static sdb_type_t service_type = {
285         /* size = */ sizeof(service_t),
286         /* init = */ service_init,
287         /* destroy = */ service_destroy
288 };
290 static sdb_type_t metric_type = {
291         /* size = */ sizeof(sdb_metric_t),
292         /* init = */ metric_init,
293         /* destroy = */ metric_destroy
294 };
296 static sdb_type_t attribute_type = {
297         /* size = */ sizeof(attr_t),
298         /* init = */ attr_init,
299         /* destroy = */ attr_destroy
300 };
302 /*
303  * private helper functions
304  */
306 static int
307 record_backend(sdb_store_obj_t *obj)
309         const sdb_plugin_info_t *info;
310         char **tmp;
311         size_t i;
313         info = sdb_plugin_current();
314         if (! info)
315                 return 0;
317         for (i = 0; i < obj->backends_num; ++i)
318                 if (!strcasecmp(obj->backends[i], info->plugin_name))
319                         return 0;
321         tmp = realloc(obj->backends,
322                         (obj->backends_num + 1) * sizeof(*obj->backends));
323         if (! tmp)
324                 return -1;
326         obj->backends = tmp;
327         obj->backends[obj->backends_num] = strdup(info->plugin_name);
328         if (! obj->backends[obj->backends_num])
329                 return -1;
331         ++obj->backends_num;
332         return 0;
333 } /* record_backend */
335 static int
336 store_obj(store_obj_t *obj, sdb_store_obj_t **updated_obj)
338         sdb_store_obj_t *old, *new;
339         int status = 0;
341         assert(obj->parent_tree);
343         if (obj->last_update <= 0)
344                 obj->last_update = sdb_gettime();
346         old = STORE_OBJ(sdb_avltree_lookup(obj->parent_tree, obj->name));
347         if (old) {
348                 if (old->last_update > obj->last_update) {
349                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
350                                         "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
351                                         SDB_STORE_TYPE_TO_NAME(obj->type), obj->name,
352                                         obj->last_update, old->last_update);
353                         /* don't report an error; the object may be updated by multiple
354                          * backends */
355                         status = 1;
356                 }
357                 else if (old->last_update == obj->last_update) {
358                         /* don't report an error and also don't even log this to avoid
359                          * excessive noise on high sampling frequencies */
360                         status = 1;
361                 }
362                 else {
363                         sdb_time_t interval = obj->last_update - old->last_update;
364                         old->last_update = obj->last_update;
365                         if (interval) {
366                                 if (old->interval)
367                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
368                                                         + (0.1 * (double)interval));
369                                 else
370                                         old->interval = interval;
371                         }
372                 }
374                 new = old;
375                 sdb_object_deref(SDB_OBJ(old));
376         }
377         else {
378                 if (obj->type == SDB_ATTRIBUTE) {
379                         /* the value will be updated by the caller */
380                         new = STORE_OBJ(sdb_object_create(obj->name, attribute_type,
381                                                 obj->type, obj->last_update, NULL));
382                 }
383                 else {
384                         sdb_type_t t;
385                         t = obj->type == SDB_HOST
386                                 ? host_type
387                                 : obj->type == SDB_SERVICE
388                                         ? service_type
389                                         : metric_type;
390                         new = STORE_OBJ(sdb_object_create(obj->name, t,
391                                                 obj->type, obj->last_update));
392                 }
394                 if (new) {
395                         status = sdb_avltree_insert(obj->parent_tree, SDB_OBJ(new));
397                         /* pass control to the tree or destroy in case of an error */
398                         sdb_object_deref(SDB_OBJ(new));
399                 }
400                 else {
401                         char errbuf[1024];
402                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
403                                         SDB_STORE_TYPE_TO_NAME(obj->type), obj->name,
404                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
405                         status = -1;
406                 }
407         }
409         if (status < 0)
410                 return status;
411         assert(new);
413         if (new->parent != obj->parent) {
414                 // Avoid circular self-references which are not handled
415                 // correctly by the ref-count based management layer.
416                 //sdb_object_deref(SDB_OBJ(new->parent));
417                 //sdb_object_ref(SDB_OBJ(obj->parent));
418                 new->parent = obj->parent;
419         }
421         if (updated_obj)
422                 *updated_obj = new;
424         if (record_backend(new))
425                 return -1;
426         return status;
427 } /* store_obj */
429 static int
430 store_metric_store(sdb_metric_t *metric, sdb_store_metric_t *m)
432         char *type = metric->store.type;
433         char *id = metric->store.id;
435         if ((! metric->store.type) || strcasecmp(metric->store.type, m->store.type)) {
436                 if (! (type = strdup(m->store.type)))
437                         return -1;
438         }
439         if ((! metric->store.id) || strcasecmp(metric->store.id, m->store.id)) {
440                 if (! (id = strdup(m->store.id))) {
441                         if (type != metric->store.type)
442                                 free(type);
443                         return -1;
444                 }
445         }
447         if (type != metric->store.type) {
448                 if (metric->store.type)
449                         free(metric->store.type);
450                 metric->store.type = type;
451         }
452         if (id != metric->store.id) {
453                 if (metric->store.id)
454                         free(metric->store.id);
455                 metric->store.id = id;
456         }
457         return 0;
458 } /* store_metric_store */
460 /* The store's host_lock has to be acquired before calling this function. */
461 static sdb_avltree_t *
462 get_host_children(host_t *host, int type)
464         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
465                         && (type != SDB_ATTRIBUTE))
466                 return NULL;
468         if (! host)
469                 return NULL;
471         if (type == SDB_ATTRIBUTE)
472                 return host->attributes;
473         else if (type == SDB_METRIC)
474                 return host->metrics;
475         else
476                 return host->services;
477 } /* get_host_children */
479 /*
480  * ts_tojson serializes a time-series to JSON.
481  *
482  * The function never returns an error. Rather, an error message will be part
483  * of the serialized data.
484  */
485 static void
486 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
488         char start_str[64];
489         char end_str[64];
491         size_t i;
493         /* TODO: make time format configurable */
494         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
495                 snprintf(start_str, sizeof(start_str), "<error>");
496         start_str[sizeof(start_str) - 1] = '\0';
497         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
498                 snprintf(end_str, sizeof(end_str), "<error>");
499         end_str[sizeof(end_str) - 1] = '\0';
501         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
502                         start_str, end_str);
504         for (i = 0; i < ts->data_names_len; ++i) {
505                 size_t j;
506                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
508                 for (j = 0; j < ts->data_len; ++j) {
509                         char time_str[64];
511                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
512                                 snprintf(time_str, sizeof(time_str), "<error>");
513                         time_str[sizeof(time_str) - 1] = '\0';
515                         /* Some GNU libc versions may print '-nan' which we dont' want */
516                         if (isnan(ts->data[i][j].value))
517                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
518                                                 "\"value\": \"nan\"}", time_str);
519                         else
520                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
521                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
523                         if (j < ts->data_len - 1)
524                                 sdb_strbuf_append(buf, ",");
525                 }
527                 if (i < ts->data_names_len - 1)
528                         sdb_strbuf_append(buf, "],");
529                 else
530                         sdb_strbuf_append(buf, "]");
531         }
532         sdb_strbuf_append(buf, "}}");
533 } /* ts_tojson */
535 /*
536  * store writer API
537  */
539 static int
540 store_attribute(sdb_store_attribute_t *attr, sdb_object_t *user_data)
542         sdb_store_t *st = SDB_STORE(user_data);
543         store_obj_t obj = STORE_OBJ_INIT;
544         sdb_store_obj_t *new = NULL;
545         const char *hostname;
546         host_t *host;
548         sdb_avltree_t *children = NULL;
549         int status = 0;
551         if ((! attr) || (! attr->parent) || (! attr->key))
552                 return -1;
554         hostname = attr->hostname;
555         if (attr->parent_type == SDB_HOST)
556                 hostname = attr->parent;
557         if (! hostname)
558                 return -1;
560         pthread_rwlock_wrlock(&st->host_lock);
561         host = HOST(sdb_avltree_lookup(st->hosts, hostname));
562         if (! host) {
563                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
564                                 "host '%s' not found", attr->key, hostname);
565                 status = -1;
566         }
568         switch (attr->parent_type) {
569         case SDB_HOST:
570                 obj.parent = STORE_OBJ(host);
571                 obj.parent_tree = get_host_children(host, SDB_ATTRIBUTE);
572                 break;
573         case SDB_SERVICE:
574                 children = get_host_children(host, SDB_SERVICE);
575                 break;
576         case SDB_METRIC:
577                 children = get_host_children(host, SDB_METRIC);
578                 break;
579         default:
580                 status = -1;
581                 break;
582         }
584         if (children) {
585                 obj.parent = STORE_OBJ(sdb_avltree_lookup(children, attr->parent));
586                 if (! obj.parent) {
587                         sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
588                                         "%s '%s/%s' not found", attr->key,
589                                         SDB_STORE_TYPE_TO_NAME(attr->parent_type),
590                                         attr->hostname, attr->parent);
591                         status = -1;
592                 }
593                 else
594                         obj.parent_tree = attr->parent_type == SDB_SERVICE
595                                 ? SVC(obj.parent)->attributes
596                                 : METRIC(obj.parent)->attributes;
597         }
599         obj.type = SDB_ATTRIBUTE;
600         obj.name = attr->key;
601         obj.last_update = attr->last_update;
602         if (! status)
603                 status = store_obj(&obj, &new);
605         if (! status) {
606                 assert(new);
607                 /* update the value if it changed */
608                 if (sdb_data_cmp(&ATTR(new)->value, &attr->value))
609                         if (sdb_data_copy(&ATTR(new)->value, &attr->value))
610                                 status = -1;
611         }
613         if (obj.parent != STORE_OBJ(host))
614                 sdb_object_deref(SDB_OBJ(obj.parent));
615         sdb_object_deref(SDB_OBJ(host));
616         pthread_rwlock_unlock(&st->host_lock);
618         return status;
619 } /* store_attribute */
621 static int
622 store_host(sdb_store_host_t *host, sdb_object_t *user_data)
624         sdb_store_t *st = SDB_STORE(user_data);
625         store_obj_t obj = { NULL, st->hosts, SDB_HOST, NULL, 0 };
626         int status = 0;
628         if ((! host) || (! host->name))
629                 return -1;
631         obj.name = host->name;
632         obj.last_update = host->last_update;
633         pthread_rwlock_wrlock(&st->host_lock);
634         status = store_obj(&obj, NULL);
635         pthread_rwlock_unlock(&st->host_lock);
637         return status;
638 } /* store_host */
640 static int
641 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
643         sdb_store_t *st = SDB_STORE(user_data);
644         store_obj_t obj = STORE_OBJ_INIT;
645         host_t *host;
647         int status = 0;
649         if ((! service) || (! service->hostname) || (! service->name))
650                 return -1;
652         pthread_rwlock_wrlock(&st->host_lock);
653         host = HOST(sdb_avltree_lookup(st->hosts, service->hostname));
654         obj.parent = STORE_OBJ(host);
655         obj.parent_tree = get_host_children(host, SDB_SERVICE);
656         obj.type = SDB_SERVICE;
657         if (! obj.parent_tree) {
658                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
659                                 "host '%s' not found", service->name, service->hostname);
660                 status = -1;
661         }
663         obj.name = service->name;
664         obj.last_update = service->last_update;
665         if (! status)
666                 status = store_obj(&obj, NULL);
668         sdb_object_deref(SDB_OBJ(host));
669         pthread_rwlock_unlock(&st->host_lock);
670         return status;
671 } /* store_service */
673 static int
674 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
676         sdb_store_t *st = SDB_STORE(user_data);
677         store_obj_t obj = STORE_OBJ_INIT;
678         sdb_store_obj_t *new = NULL;
679         host_t *host;
681         int status = 0;
683         if ((! metric) || (! metric->hostname) || (! metric->name))
684                 return -1;
686         if ((metric->store.type != NULL) != (metric->store.id != NULL))
687                 return -1;
689         pthread_rwlock_wrlock(&st->host_lock);
690         host = HOST(sdb_avltree_lookup(st->hosts, metric->hostname));
691         obj.parent = STORE_OBJ(host);
692         obj.parent_tree = get_host_children(host, SDB_METRIC);
693         obj.type = SDB_METRIC;
694         if (! obj.parent_tree) {
695                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
696                                 "host '%s' not found", metric->name, metric->hostname);
697                 status = -1;
698         }
700         obj.name = metric->name;
701         obj.last_update = metric->last_update;
702         if (! status)
703                 status = store_obj(&obj, &new);
704         sdb_object_deref(SDB_OBJ(host));
706         if (status) {
707                 pthread_rwlock_unlock(&st->host_lock);
708                 return status;
709         }
711         assert(new);
712         if (metric->store.type && metric->store.id)
713                 if (store_metric_store(METRIC(new), 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 : */