Code

store: Fixed handling of metric store values.
[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 static sdb_avltree_t *hosts = NULL;
54 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
56 /*
57  * private types
58  */
60 static sdb_type_t sdb_host_type;
61 static sdb_type_t sdb_service_type;
62 static sdb_type_t sdb_metric_type;
63 static sdb_type_t sdb_attribute_type;
65 static int
66 store_obj_init(sdb_object_t *obj, va_list ap)
67 {
68         sdb_store_obj_t *sobj = STORE_OBJ(obj);
70         sobj->type = va_arg(ap, int);
72         sobj->last_update = va_arg(ap, sdb_time_t);
73         sobj->interval = 0;
74         sobj->backends = NULL;
75         sobj->backends_num = 0;
76         sobj->parent = NULL;
77         return 0;
78 } /* store_obj_init */
80 static void
81 store_obj_destroy(sdb_object_t *obj)
82 {
83         sdb_store_obj_t *sobj = STORE_OBJ(obj);
84         size_t i;
86         for (i = 0; i < sobj->backends_num; ++i)
87                 free(sobj->backends[i]);
88         free(sobj->backends);
89         sobj->backends = NULL;
90         sobj->backends_num = 0;
92         // We don't currently keep an extra reference for parent objects to
93         // avoid circular self-references which are not handled correctly by
94         // the ref-count base management layer.
95         //sdb_object_deref(SDB_OBJ(sobj->parent));
96 } /* store_obj_destroy */
98 static int
99 sdb_host_init(sdb_object_t *obj, va_list ap)
101         sdb_host_t *sobj = HOST(obj);
102         int ret;
104         /* this will consume the first argument (type) of ap */
105         ret = store_obj_init(obj, ap);
106         if (ret)
107                 return ret;
109         sobj->services = sdb_avltree_create();
110         if (! sobj->services)
111                 return -1;
112         sobj->metrics = sdb_avltree_create();
113         if (! sobj->metrics)
114                 return -1;
115         sobj->attributes = sdb_avltree_create();
116         if (! sobj->attributes)
117                 return -1;
118         return 0;
119 } /* sdb_host_init */
121 static void
122 sdb_host_destroy(sdb_object_t *obj)
124         sdb_host_t *sobj = HOST(obj);
125         assert(obj);
127         store_obj_destroy(obj);
129         if (sobj->services)
130                 sdb_avltree_destroy(sobj->services);
131         if (sobj->metrics)
132                 sdb_avltree_destroy(sobj->metrics);
133         if (sobj->attributes)
134                 sdb_avltree_destroy(sobj->attributes);
135 } /* sdb_host_destroy */
137 static int
138 sdb_service_init(sdb_object_t *obj, va_list ap)
140         sdb_service_t *sobj = SVC(obj);
141         int ret;
143         /* this will consume the first argument (type) of ap */
144         ret = store_obj_init(obj, ap);
145         if (ret)
146                 return ret;
148         sobj->attributes = sdb_avltree_create();
149         if (! sobj->attributes)
150                 return -1;
151         return 0;
152 } /* sdb_service_init */
154 static void
155 sdb_service_destroy(sdb_object_t *obj)
157         sdb_service_t *sobj = SVC(obj);
158         assert(obj);
160         store_obj_destroy(obj);
162         if (sobj->attributes)
163                 sdb_avltree_destroy(sobj->attributes);
164 } /* sdb_service_destroy */
166 static int
167 sdb_metric_init(sdb_object_t *obj, va_list ap)
169         sdb_metric_t *sobj = METRIC(obj);
170         int ret;
172         /* this will consume the first argument (type) of ap */
173         ret = store_obj_init(obj, ap);
174         if (ret)
175                 return ret;
177         sobj->attributes = sdb_avltree_create();
178         if (! sobj->attributes)
179                 return -1;
181         sobj->store.type = sobj->store.id = NULL;
182         return 0;
183 } /* sdb_metric_init */
185 static void
186 sdb_metric_destroy(sdb_object_t *obj)
188         sdb_metric_t *sobj = METRIC(obj);
189         assert(obj);
191         store_obj_destroy(obj);
193         if (sobj->attributes)
194                 sdb_avltree_destroy(sobj->attributes);
196         if (sobj->store.type)
197                 free(sobj->store.type);
198         if (sobj->store.id)
199                 free(sobj->store.id);
200 } /* sdb_metric_destroy */
202 static int
203 sdb_attr_init(sdb_object_t *obj, va_list ap)
205         const sdb_data_t *value;
206         int ret;
208         /* this will consume the first two arguments
209          * (type and last_update) of ap */
210         ret = store_obj_init(obj, ap);
211         if (ret)
212                 return ret;
213         value = va_arg(ap, const sdb_data_t *);
215         if (value)
216                 if (sdb_data_copy(&ATTR(obj)->value, value))
217                         return -1;
218         return 0;
219 } /* sdb_attr_init */
221 static void
222 sdb_attr_destroy(sdb_object_t *obj)
224         assert(obj);
226         store_obj_destroy(obj);
227         sdb_data_free_datum(&ATTR(obj)->value);
228 } /* sdb_attr_destroy */
230 static sdb_type_t sdb_host_type = {
231         sizeof(sdb_host_t),
232         sdb_host_init,
233         sdb_host_destroy
234 };
236 static sdb_type_t sdb_service_type = {
237         sizeof(sdb_service_t),
238         sdb_service_init,
239         sdb_service_destroy
240 };
242 static sdb_type_t sdb_metric_type = {
243         sizeof(sdb_metric_t),
244         sdb_metric_init,
245         sdb_metric_destroy
246 };
248 static sdb_type_t sdb_attribute_type = {
249         sizeof(sdb_attribute_t),
250         sdb_attr_init,
251         sdb_attr_destroy
252 };
254 /*
255  * private helper functions
256  */
258 static sdb_host_t *
259 lookup_host(const char *name, bool canonicalize)
261         sdb_host_t *host;
262         char *cname;
264         assert(name);
265         if (! canonicalize)
266                 return HOST(sdb_avltree_lookup(hosts, name));
268         cname = strdup(name);
269         cname = sdb_plugin_cname(cname);
270         if (! cname) {
271                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
272                 return NULL;
273         }
275         host = HOST(sdb_avltree_lookup(hosts, cname));
276         free(cname);
277         return host;
278 } /* lookup_host */
280 static int
281 record_backend(sdb_store_obj_t *obj)
283         const sdb_plugin_info_t *info;
284         char **tmp;
285         size_t i;
287         info = sdb_plugin_current();
288         if (! info)
289                 return 0;
291         for (i = 0; i < obj->backends_num; ++i)
292                 if (!strcasecmp(obj->backends[i], info->plugin_name))
293                         return 0;
295         tmp = realloc(obj->backends,
296                         (obj->backends_num + 1) * sizeof(*obj->backends));
297         if (! tmp)
298                 return -1;
300         obj->backends = tmp;
301         obj->backends[obj->backends_num] = strdup(info->plugin_name);
302         if (! obj->backends[obj->backends_num])
303                 return -1;
305         ++obj->backends_num;
306         return 0;
307 } /* record_backend */
309 static int
310 store_obj(sdb_store_obj_t *parent, sdb_avltree_t *parent_tree,
311                 int type, const char *name, sdb_time_t last_update,
312                 sdb_store_obj_t **updated_obj)
314         sdb_store_obj_t *old, *new;
315         int status = 0;
317         assert(parent_tree);
319         if (last_update <= 0)
320                 last_update = sdb_gettime();
322         old = STORE_OBJ(sdb_avltree_lookup(parent_tree, name));
323         if (old) {
324                 if (old->last_update > last_update) {
325                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
326                                         "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
327                                         SDB_STORE_TYPE_TO_NAME(type), name,
328                                         last_update, old->last_update);
329                         /* don't report an error; the object may be updated by multiple
330                          * backends */
331                         status = 1;
332                 }
333                 else if (old->last_update == last_update) {
334                         /* don't report an error and also don't even log this to avoid
335                          * excessive noise on high sampling frequencies */
336                         status = 1;
337                 }
338                 else {
339                         sdb_time_t interval = last_update - old->last_update;
340                         old->last_update = last_update;
341                         if (interval) {
342                                 if (old->interval)
343                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
344                                                         + (0.1 * (double)interval));
345                                 else
346                                         old->interval = interval;
347                         }
348                 }
350                 new = old;
351                 sdb_object_deref(SDB_OBJ(old));
352         }
353         else {
354                 if (type == SDB_ATTRIBUTE) {
355                         /* the value will be updated by the caller */
356                         new = STORE_OBJ(sdb_object_create(name, sdb_attribute_type,
357                                                 type, last_update, NULL));
358                 }
359                 else {
360                         sdb_type_t t;
361                         t = type == SDB_HOST
362                                 ? sdb_host_type
363                                 : type == SDB_SERVICE
364                                         ? sdb_service_type
365                                         : sdb_metric_type;
366                         new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
367                 }
369                 if (new) {
370                         status = sdb_avltree_insert(parent_tree, SDB_OBJ(new));
372                         /* pass control to the tree or destroy in case of an error */
373                         sdb_object_deref(SDB_OBJ(new));
374                 }
375                 else {
376                         char errbuf[1024];
377                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
378                                         SDB_STORE_TYPE_TO_NAME(type), name,
379                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
380                         status = -1;
381                 }
382         }
384         if (status < 0)
385                 return status;
386         assert(new);
388         if (new->parent != parent) {
389                 // Avoid circular self-references which are not handled
390                 // correctly by the ref-count based management layer.
391                 //sdb_object_deref(SDB_OBJ(new->parent));
392                 //sdb_object_ref(SDB_OBJ(parent));
393                 new->parent = parent;
394         }
396         if (updated_obj)
397                 *updated_obj = new;
399         if (record_backend(new))
400                 return -1;
401         return status;
402 } /* store_obj */
404 static int
405 store_attr(sdb_store_obj_t *parent, sdb_avltree_t *attributes,
406                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
408         sdb_store_obj_t *attr = NULL;
409         int status;
411         status = store_obj(parent, attributes, SDB_ATTRIBUTE,
412                         key, last_update, &attr);
413         if (status)
414                 return status;
416         /* don't update unchanged values */
417         if (! sdb_data_cmp(&ATTR(attr)->value, value))
418                 return status;
420         assert(attr);
421         if (sdb_data_copy(&ATTR(attr)->value, value))
422                 return -1;
423         return status;
424 } /* store_attr */
426 static int
427 store_metric_store(sdb_metric_t *metric, sdb_metric_store_t *store)
429         char *type = metric->store.type;
430         char *id = metric->store.id;
432         if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
433                 if (! (type = strdup(store->type)))
434                         return -1;
435         }
436         if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
437                 if (! (id = strdup(store->id))) {
438                         if (type != metric->store.type)
439                                 free(type);
440                         return -1;
441                 }
442         }
444         if (type != metric->store.type) {
445                 if (metric->store.type)
446                         free(metric->store.type);
447                 metric->store.type = type;
448         }
449         if (id != metric->store.id) {
450                 if (metric->store.id)
451                         free(metric->store.id);
452                 metric->store.id = id;
453         }
454         return 0;
455 } /* store_metric_store */
457 /* The host_lock has to be acquired before calling this function. */
458 static sdb_avltree_t *
459 get_host_children(sdb_host_t *host, int type)
461         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
462                         && (type != SDB_ATTRIBUTE))
463                 return NULL;
465         if (! host)
466                 return NULL;
468         if (type == SDB_ATTRIBUTE)
469                 return host->attributes;
470         else if (type == SDB_METRIC)
471                 return host->metrics;
472         else
473                 return host->services;
474 } /* get_host_children */
476 /*
477  * ts_tojson serializes a time-series to JSON.
478  *
479  * The function never returns an error. Rather, an error message will be part
480  * of the serialized data.
481  */
482 static void
483 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
485         char start_str[64];
486         char end_str[64];
488         size_t i;
490         /* TODO: make time format configurable */
491         if (! sdb_strftime(start_str, sizeof(start_str),
492                                 "%F %T %z", ts->start))
493                 snprintf(start_str, sizeof(start_str), "<error>");
494         start_str[sizeof(start_str) - 1] = '\0';
495         if (! sdb_strftime(end_str, sizeof(end_str),
496                                 "%F %T %z", ts->end))
497                 snprintf(end_str, sizeof(end_str), "<error>");
498         end_str[sizeof(end_str) - 1] = '\0';
500         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
501                         start_str, end_str);
503         for (i = 0; i < ts->data_names_len; ++i) {
504                 size_t j;
505                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
507                 for (j = 0; j < ts->data_len; ++j) {
508                         char time_str[64];
510                         if (! sdb_strftime(time_str, sizeof(time_str),
511                                                 "%F %T %z", 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  * public API
537  */
539 void
540 sdb_store_clear(void)
542         sdb_avltree_destroy(hosts);
543         hosts = NULL;
544 } /* sdb_store_clear */
546 int
547 sdb_store_host(const char *name, sdb_time_t last_update)
549         char *cname = NULL;
550         int status = 0;
552         if (! name)
553                 return -1;
555         cname = sdb_plugin_cname(strdup(name));
556         if (! cname) {
557                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
558                 return -1;
559         }
561         pthread_rwlock_wrlock(&host_lock);
562         if (! hosts)
563                 if (! (hosts = sdb_avltree_create()))
564                         status = -1;
566         if (! status)
567                 status = store_obj(NULL, hosts, SDB_HOST, cname, last_update, NULL);
568         pthread_rwlock_unlock(&host_lock);
570         if (sdb_plugin_store_host(name, last_update))
571                 status = -1;
573         free(cname);
574         return status;
575 } /* sdb_store_host */
577 bool
578 sdb_store_has_host(const char *name)
580         sdb_host_t *host;
582         if (! name)
583                 return NULL;
585         host = lookup_host(name, /* canonicalize = */ 0);
586         sdb_object_deref(SDB_OBJ(host));
587         return host != NULL;
588 } /* sdb_store_has_host */
590 sdb_store_obj_t *
591 sdb_store_get_host(const char *name)
593         sdb_host_t *host;
595         if (! name)
596                 return NULL;
598         host = lookup_host(name, /* canonicalize = */ 0);
599         if (! host)
600                 return NULL;
602         return STORE_OBJ(host);
603 } /* sdb_store_get_host */
605 int
606 sdb_store_attribute(const char *hostname,
607                 const char *key, const sdb_data_t *value,
608                 sdb_time_t last_update)
610         sdb_host_t *host;
611         sdb_avltree_t *attrs;
612         int status = 0;
614         if ((! hostname) || (! key))
615                 return -1;
617         pthread_rwlock_wrlock(&host_lock);
618         host = lookup_host(hostname, /* canonicalize = */ 1);
619         attrs = get_host_children(host, SDB_ATTRIBUTE);
620         if (! attrs) {
621                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
622                                 "host '%s' not found", key, hostname);
623                 status = -1;
624         }
626         if (! status)
627                 status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
629         sdb_object_deref(SDB_OBJ(host));
630         pthread_rwlock_unlock(&host_lock);
632         if (sdb_plugin_store_attribute(hostname, key, value, last_update))
633                 status = -1;
634         return status;
635 } /* sdb_store_attribute */
637 int
638 sdb_store_service(const char *hostname, const char *name,
639                 sdb_time_t last_update)
641         sdb_host_t *host;
642         sdb_avltree_t *services;
644         int status = 0;
646         if ((! hostname) || (! name))
647                 return -1;
649         pthread_rwlock_wrlock(&host_lock);
650         host = lookup_host(hostname, /* canonicalize = */ 1);
651         services = get_host_children(host, SDB_SERVICE);
652         if (! services) {
653                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
654                                 "host '%s' not found", name, hostname);
655                 status = -1;
656         }
658         if (! status)
659                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
660                                 name, last_update, NULL);
662         sdb_object_deref(SDB_OBJ(host));
663         pthread_rwlock_unlock(&host_lock);
665         if (status)
666                 return status;
668         if (sdb_plugin_store_service(hostname, name, last_update))
669                 status = -1;
670         return status;
671 } /* sdb_store_service */
673 int
674 sdb_store_service_attr(const char *hostname, const char *service,
675                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
677         sdb_host_t *host;
678         sdb_service_t *svc;
679         sdb_avltree_t *services;
680         int status = 0;
682         if ((! hostname) || (! service) || (! key))
683                 return -1;
685         pthread_rwlock_wrlock(&host_lock);
686         host = lookup_host(hostname, /* canonicalize = */ 1);
687         services = get_host_children(host, SDB_SERVICE);
688         sdb_object_deref(SDB_OBJ(host));
689         if (! services) {
690                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
691                                 "for service '%s' - host '%ss' not found",
692                                 key, service, hostname);
693                 pthread_rwlock_unlock(&host_lock);
694                 return -1;
695         }
697         svc = SVC(sdb_avltree_lookup(services, service));
698         if (! svc) {
699                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
700                                 "service '%s/%s' not found", key, hostname, service);
701                 status = -1;
702         }
704         if (! status)
705                 status = store_attr(STORE_OBJ(svc), svc->attributes,
706                                 key, value, last_update);
708         sdb_object_deref(SDB_OBJ(svc));
709         pthread_rwlock_unlock(&host_lock);
711         if (sdb_plugin_store_service_attribute(hostname, service,
712                                 key, value, last_update))
713                 status = -1;
714         return status;
715 } /* sdb_store_service_attr */
717 int
718 sdb_store_metric(const char *hostname, const char *name,
719                 sdb_metric_store_t *store, sdb_time_t last_update)
721         sdb_store_obj_t *obj = NULL;
722         sdb_host_t *host;
723         sdb_metric_t *metric;
725         sdb_avltree_t *metrics;
727         int status = 0;
729         if ((! hostname) || (! name))
730                 return -1;
732         if (store) {
733                 if ((store->type != NULL) != (store->id != NULL))
734                         return -1;
735                 else if (! store->type)
736                         store = NULL;
737         }
739         pthread_rwlock_wrlock(&host_lock);
740         host = lookup_host(hostname, /* canonicalize = */ 1);
741         metrics = get_host_children(host, SDB_METRIC);
742         if (! metrics) {
743                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
744                                 "host '%s' not found", name, hostname);
745                 status = -1;
746         }
748         if (! status)
749                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
750                                 name, last_update, &obj);
751         sdb_object_deref(SDB_OBJ(host));
753         if (status) {
754                 pthread_rwlock_unlock(&host_lock);
755                 return status;
756         }
758         assert(obj);
759         metric = METRIC(obj);
761         if (store)
762                 if (store_metric_store(metric, store))
763                         status = -1;
764         pthread_rwlock_unlock(&host_lock);
766         if (sdb_plugin_store_metric(hostname, name, store, last_update))
767                 status = -1;
768         return status;
769 } /* sdb_store_metric */
771 int
772 sdb_store_metric_attr(const char *hostname, const char *metric,
773                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
775         sdb_avltree_t *metrics;
776         sdb_host_t *host;
777         sdb_metric_t *m;
778         int status = 0;
780         if ((! hostname) || (! metric) || (! key))
781                 return -1;
783         pthread_rwlock_wrlock(&host_lock);
784         host = lookup_host(hostname, /* canonicalize = */ 1);
785         metrics = get_host_children(host, SDB_METRIC);
786         sdb_object_deref(SDB_OBJ(host));
787         if (! metrics) {
788                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
789                                 "for metric '%s' - host '%s' not found",
790                                 key, metric, hostname);
791                 pthread_rwlock_unlock(&host_lock);
792                 return -1;
793         }
795         m = METRIC(sdb_avltree_lookup(metrics, metric));
796         if (! m) {
797                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
798                                 "metric '%s/%s' not found", key, hostname, metric);
799                 status = -1;
800         }
802         if (! status)
803                 status = store_attr(STORE_OBJ(m), m->attributes,
804                                 key, value, last_update);
806         sdb_object_deref(SDB_OBJ(m));
807         pthread_rwlock_unlock(&host_lock);
809         if (sdb_plugin_store_metric_attribute(hostname, metric,
810                                 key, value, last_update))
811                 status = -1;
812         return status;
813 } /* sdb_store_metric_attr */
815 sdb_store_obj_t *
816 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
818         sdb_avltree_t *children;
820         if ((! host) || (host->type != SDB_HOST) || (! name))
821                 return NULL;
823         children = get_host_children(HOST(host), type);
824         if (! children)
825                 return NULL;
826         return STORE_OBJ(sdb_avltree_lookup(children, name));
827 } /* sdb_store_get_child */
829 int
830 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
831                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
833         sdb_avltree_t *metrics;
834         sdb_host_t *host;
835         sdb_metric_t *m;
837         sdb_timeseries_t *ts;
839         if ((! hostname) || (! metric) || (! opts) || (! buf))
840                 return -1;
842         pthread_rwlock_rdlock(&host_lock);
843         host = lookup_host(hostname, /* canonicalize = */ 1);
844         metrics = get_host_children(host, SDB_METRIC);
845         sdb_object_deref(SDB_OBJ(host));
846         if (! metrics) {
847                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
848                                 "- host '%s' not found", hostname, metric, hostname);
849                 pthread_rwlock_unlock(&host_lock);
850                 return -1;
851         }
853         m = METRIC(sdb_avltree_lookup(metrics, metric));
854         if (! m) {
855                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
856                                 "- metric '%s' not found", hostname, metric, metric);
857                 pthread_rwlock_unlock(&host_lock);
858                 return -1;
859         }
861         if ((! m->store.type) || (! m->store.id)) {
862                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
863                                 "- no data-store configured for the stored metric",
864                                 hostname, metric);
865                 pthread_rwlock_unlock(&host_lock);
866                 return -1;
867         }
869         {
870                 char type[strlen(m->store.type) + 1];
871                 char id[strlen(m->store.id) + 1];
873                 strncpy(type, m->store.type, sizeof(type));
874                 strncpy(id, m->store.id, sizeof(id));
875                 pthread_rwlock_unlock(&host_lock);
877                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
878                 if (! ts) {
879                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
880                                         "- %s fetcher callback returned no data for '%s'",
881                                         hostname, metric, type, id);
882                         return -1;
883                 }
884         }
886         ts_tojson(ts, buf);
887         sdb_timeseries_destroy(ts);
888         return 0;
889 } /* sdb_store_fetch_timeseries */
891 int
892 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
894         sdb_data_t tmp;
896         if (! obj)
897                 return -1;
899         switch (field) {
900                 case SDB_FIELD_NAME:
901                         tmp.type = SDB_TYPE_STRING;
902                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
903                         if (! tmp.data.string)
904                                 return -1;
905                         break;
906                 case SDB_FIELD_LAST_UPDATE:
907                         tmp.type = SDB_TYPE_DATETIME;
908                         tmp.data.datetime = obj->last_update;
909                         break;
910                 case SDB_FIELD_AGE:
911                         tmp.type = SDB_TYPE_DATETIME;
912                         tmp.data.datetime = sdb_gettime() - obj->last_update;
913                         break;
914                 case SDB_FIELD_INTERVAL:
915                         tmp.type = SDB_TYPE_DATETIME;
916                         tmp.data.datetime = obj->interval;
917                         break;
918                 case SDB_FIELD_BACKEND:
919                         if (! res)
920                                 return 0;
921                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
922                         tmp.data.array.length = obj->backends_num;
923                         tmp.data.array.values = obj->backends;
924                         return sdb_data_copy(res, &tmp);
925                 case SDB_FIELD_VALUE:
926                         if (obj->type != SDB_ATTRIBUTE)
927                                 return -1;
928                         if (! res)
929                                 return 0;
930                         return sdb_data_copy(res, &ATTR(obj)->value);
931                 default:
932                         return -1;
933         }
934         if (res)
935                 *res = tmp;
936         else
937                 sdb_data_free_datum(&tmp);
938         return 0;
939 } /* sdb_store_get_field */
941 int
942 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
943                 sdb_store_matcher_t *filter)
945         sdb_avltree_t *tree = NULL;
946         sdb_store_obj_t *attr;
948         if ((! obj) || (! name))
949                 return -1;
951         if (obj->type == SDB_HOST)
952                 tree = HOST(obj)->attributes;
953         else if (obj->type == SDB_SERVICE)
954                 tree = SVC(obj)->attributes;
955         else if (obj->type == SDB_METRIC)
956                 tree = METRIC(obj)->attributes;
958         if (! tree)
959                 return -1;
961         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
962         if (! attr)
963                 return -1;
964         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
965                 sdb_object_deref(SDB_OBJ(attr));
966                 return -1;
967         }
969         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
970         if (res)
971                 sdb_data_copy(res, &ATTR(attr)->value);
972         sdb_object_deref(SDB_OBJ(attr));
973         return 0;
974 } /* sdb_store_get_attr */
976 int
977 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
978                 sdb_store_lookup_cb cb, void *user_data)
980         sdb_avltree_iter_t *host_iter = NULL;
981         int status = 0;
983         if (! cb)
984                 return -1;
986         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
987                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
988                 return -1;
989         }
991         pthread_rwlock_rdlock(&host_lock);
993         if (hosts) {
994                 host_iter = sdb_avltree_get_iter(hosts);
995                 if (! host_iter)
996                         status = -1;
997         }
999         /* has_next returns false if the iterator is NULL */
1000         while (sdb_avltree_iter_has_next(host_iter)) {
1001                 sdb_store_obj_t *host;
1002                 sdb_avltree_iter_t *iter = NULL;
1004                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1005                 assert(host);
1007                 if (! sdb_store_matcher_matches(filter, host, NULL))
1008                         continue;
1010                 if (type == SDB_SERVICE)
1011                         iter = sdb_avltree_get_iter(HOST(host)->services);
1012                 else if (type == SDB_METRIC)
1013                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
1015                 if (iter) {
1016                         while (sdb_avltree_iter_has_next(iter)) {
1017                                 sdb_store_obj_t *obj;
1018                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1019                                 assert(obj);
1021                                 if (sdb_store_matcher_matches(m, obj, filter)) {
1022                                         if (cb(obj, filter, user_data)) {
1023                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1024                                                                 "an error while scanning");
1025                                                 status = -1;
1026                                                 break;
1027                                         }
1028                                 }
1029                         }
1030                 }
1031                 else if (sdb_store_matcher_matches(m, host, filter)) {
1032                         if (cb(host, filter, user_data)) {
1033                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1034                                                 "an error while scanning");
1035                                 status = -1;
1036                         }
1037                 }
1039                 sdb_avltree_iter_destroy(iter);
1040                 if (status)
1041                         break;
1042         }
1044         sdb_avltree_iter_destroy(host_iter);
1045         pthread_rwlock_unlock(&host_lock);
1046         return status;
1047 } /* sdb_store_scan */
1049 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */