Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[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;
643         sdb_data_t d;
645         int status = 0;
647         if ((! hostname) || (! name))
648                 return -1;
650         pthread_rwlock_wrlock(&host_lock);
651         host = lookup_host(hostname, /* canonicalize = */ 1);
652         services = get_host_children(host, SDB_SERVICE);
653         if (! services) {
654                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
655                                 "host '%s' not found", name, hostname);
656                 status = -1;
657         }
659         if (! status)
660                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
661                                 name, last_update, NULL);
663         sdb_object_deref(SDB_OBJ(host));
664         pthread_rwlock_unlock(&host_lock);
666         if (status)
667                 return status;
669         if (sdb_plugin_store_service(hostname, name, last_update))
670                 status = -1;
672         /* record the hostname as an attribute */
673         d.type = SDB_TYPE_STRING;
674         d.data.string = SDB_OBJ(host)->name;
675         if (sdb_store_service_attr(hostname, name, "hostname", &d, last_update))
676                 status = -1;
677         return status;
678 } /* sdb_store_service */
680 int
681 sdb_store_service_attr(const char *hostname, const char *service,
682                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
684         sdb_host_t *host;
685         sdb_service_t *svc;
686         sdb_avltree_t *services;
687         int status = 0;
689         if ((! hostname) || (! service) || (! key))
690                 return -1;
692         pthread_rwlock_wrlock(&host_lock);
693         host = lookup_host(hostname, /* canonicalize = */ 1);
694         services = get_host_children(host, SDB_SERVICE);
695         sdb_object_deref(SDB_OBJ(host));
696         if (! services) {
697                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
698                                 "for service '%s' - host '%ss' not found",
699                                 key, service, hostname);
700                 pthread_rwlock_unlock(&host_lock);
701                 return -1;
702         }
704         svc = SVC(sdb_avltree_lookup(services, service));
705         if (! svc) {
706                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
707                                 "service '%s/%s' not found", key, hostname, service);
708                 status = -1;
709         }
711         if (! status)
712                 status = store_attr(STORE_OBJ(svc), svc->attributes,
713                                 key, value, last_update);
715         sdb_object_deref(SDB_OBJ(svc));
716         pthread_rwlock_unlock(&host_lock);
718         if (sdb_plugin_store_service_attribute(hostname, service,
719                                 key, value, last_update))
720                 status = -1;
721         return status;
722 } /* sdb_store_service_attr */
724 int
725 sdb_store_metric(const char *hostname, const char *name,
726                 sdb_metric_store_t *store, sdb_time_t last_update)
728         sdb_store_obj_t *obj = NULL;
729         sdb_host_t *host;
730         sdb_metric_t *metric;
731         sdb_data_t d;
733         sdb_avltree_t *metrics;
735         int status = 0;
737         if ((! hostname) || (! name))
738                 return -1;
740         if (store) {
741                 if ((store->type != NULL) != (store->id != NULL))
742                         return -1;
743                 else if (! store->type)
744                         store = NULL;
745         }
747         pthread_rwlock_wrlock(&host_lock);
748         host = lookup_host(hostname, /* canonicalize = */ 1);
749         metrics = get_host_children(host, SDB_METRIC);
750         if (! metrics) {
751                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
752                                 "host '%s' not found", name, hostname);
753                 status = -1;
754         }
756         if (! status)
757                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
758                                 name, last_update, &obj);
759         sdb_object_deref(SDB_OBJ(host));
761         if (status) {
762                 pthread_rwlock_unlock(&host_lock);
763                 return status;
764         }
766         assert(obj);
767         metric = METRIC(obj);
769         if (store)
770                 if (store_metric_store(metric, store))
771                         status = -1;
772         pthread_rwlock_unlock(&host_lock);
774         if (sdb_plugin_store_metric(hostname, name, store, last_update))
775                 status = -1;
777         /* record the hostname as an attribute */
778         d.type = SDB_TYPE_STRING;
779         d.data.string = SDB_OBJ(host)->name;
780         if (sdb_store_metric_attr(hostname, name, "hostname", &d, last_update))
781                 status = -1;
782         return status;
783 } /* sdb_store_metric */
785 int
786 sdb_store_metric_attr(const char *hostname, const char *metric,
787                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
789         sdb_avltree_t *metrics;
790         sdb_host_t *host;
791         sdb_metric_t *m;
792         int status = 0;
794         if ((! hostname) || (! metric) || (! key))
795                 return -1;
797         pthread_rwlock_wrlock(&host_lock);
798         host = lookup_host(hostname, /* canonicalize = */ 1);
799         metrics = get_host_children(host, SDB_METRIC);
800         sdb_object_deref(SDB_OBJ(host));
801         if (! metrics) {
802                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
803                                 "for metric '%s' - host '%s' not found",
804                                 key, metric, hostname);
805                 pthread_rwlock_unlock(&host_lock);
806                 return -1;
807         }
809         m = METRIC(sdb_avltree_lookup(metrics, metric));
810         if (! m) {
811                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
812                                 "metric '%s/%s' not found", key, hostname, metric);
813                 status = -1;
814         }
816         if (! status)
817                 status = store_attr(STORE_OBJ(m), m->attributes,
818                                 key, value, last_update);
820         sdb_object_deref(SDB_OBJ(m));
821         pthread_rwlock_unlock(&host_lock);
823         if (sdb_plugin_store_metric_attribute(hostname, metric,
824                                 key, value, last_update))
825                 status = -1;
826         return status;
827 } /* sdb_store_metric_attr */
829 sdb_store_obj_t *
830 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
832         sdb_avltree_t *children;
834         if ((! host) || (host->type != SDB_HOST) || (! name))
835                 return NULL;
837         children = get_host_children(HOST(host), type);
838         if (! children)
839                 return NULL;
840         return STORE_OBJ(sdb_avltree_lookup(children, name));
841 } /* sdb_store_get_child */
843 int
844 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
845                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
847         sdb_avltree_t *metrics;
848         sdb_host_t *host;
849         sdb_metric_t *m;
851         sdb_timeseries_t *ts;
853         int status = 0;
855         if ((! hostname) || (! metric) || (! opts) || (! buf))
856                 return -1;
858         pthread_rwlock_rdlock(&host_lock);
859         host = lookup_host(hostname, /* canonicalize = */ 1);
860         metrics = get_host_children(host, SDB_METRIC);
861         sdb_object_deref(SDB_OBJ(host));
862         if (! metrics) {
863                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
864                                 "- host '%s' not found", hostname, metric, hostname);
865                 pthread_rwlock_unlock(&host_lock);
866                 return -1;
867         }
869         m = METRIC(sdb_avltree_lookup(metrics, metric));
870         if (! m) {
871                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
872                                 "- metric '%s' not found", hostname, metric, metric);
873                 pthread_rwlock_unlock(&host_lock);
874                 return -1;
875         }
877         if ((! m->store.type) || (! m->store.id)) {
878                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
879                                 "- no data-store configured for the stored metric",
880                                 hostname, metric);
881                 sdb_object_deref(SDB_OBJ(m));
882                 pthread_rwlock_unlock(&host_lock);
883                 return -1;
884         }
886         {
887                 char type[strlen(m->store.type) + 1];
888                 char id[strlen(m->store.id) + 1];
890                 strncpy(type, m->store.type, sizeof(type));
891                 strncpy(id, m->store.id, sizeof(id));
892                 pthread_rwlock_unlock(&host_lock);
894                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
895                 if (! ts) {
896                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
897                                         "- %s fetcher callback returned no data for '%s'",
898                                         hostname, metric, type, id);
899                         status = -1;
900                 }
901         }
903         ts_tojson(ts, buf);
904         sdb_object_deref(SDB_OBJ(m));
905         sdb_timeseries_destroy(ts);
906         return status;
907 } /* sdb_store_fetch_timeseries */
909 int
910 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
912         sdb_data_t tmp;
914         if (! obj)
915                 return -1;
917         switch (field) {
918                 case SDB_FIELD_NAME:
919                         tmp.type = SDB_TYPE_STRING;
920                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
921                         if (! tmp.data.string)
922                                 return -1;
923                         break;
924                 case SDB_FIELD_LAST_UPDATE:
925                         tmp.type = SDB_TYPE_DATETIME;
926                         tmp.data.datetime = obj->last_update;
927                         break;
928                 case SDB_FIELD_AGE:
929                         tmp.type = SDB_TYPE_DATETIME;
930                         tmp.data.datetime = sdb_gettime() - obj->last_update;
931                         break;
932                 case SDB_FIELD_INTERVAL:
933                         tmp.type = SDB_TYPE_DATETIME;
934                         tmp.data.datetime = obj->interval;
935                         break;
936                 case SDB_FIELD_BACKEND:
937                         if (! res)
938                                 return 0;
939                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
940                         tmp.data.array.length = obj->backends_num;
941                         tmp.data.array.values = obj->backends;
942                         return sdb_data_copy(res, &tmp);
943                 case SDB_FIELD_VALUE:
944                         if (obj->type != SDB_ATTRIBUTE)
945                                 return -1;
946                         if (! res)
947                                 return 0;
948                         return sdb_data_copy(res, &ATTR(obj)->value);
949                 default:
950                         return -1;
951         }
952         if (res)
953                 *res = tmp;
954         else
955                 sdb_data_free_datum(&tmp);
956         return 0;
957 } /* sdb_store_get_field */
959 int
960 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
961                 sdb_store_matcher_t *filter)
963         sdb_avltree_t *tree = NULL;
964         sdb_store_obj_t *attr;
966         if ((! obj) || (! name))
967                 return -1;
969         if (obj->type == SDB_HOST)
970                 tree = HOST(obj)->attributes;
971         else if (obj->type == SDB_SERVICE)
972                 tree = SVC(obj)->attributes;
973         else if (obj->type == SDB_METRIC)
974                 tree = METRIC(obj)->attributes;
976         if (! tree)
977                 return -1;
979         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
980         if (! attr)
981                 return -1;
982         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
983                 sdb_object_deref(SDB_OBJ(attr));
984                 return -1;
985         }
987         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
988         if (res)
989                 sdb_data_copy(res, &ATTR(attr)->value);
990         sdb_object_deref(SDB_OBJ(attr));
991         return 0;
992 } /* sdb_store_get_attr */
994 int
995 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
996                 sdb_store_lookup_cb cb, void *user_data)
998         sdb_avltree_iter_t *host_iter = NULL;
999         int status = 0;
1001         if (! cb)
1002                 return -1;
1004         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
1005                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
1006                 return -1;
1007         }
1009         pthread_rwlock_rdlock(&host_lock);
1011         if (hosts) {
1012                 host_iter = sdb_avltree_get_iter(hosts);
1013                 if (! host_iter)
1014                         status = -1;
1015         }
1017         /* has_next returns false if the iterator is NULL */
1018         while (sdb_avltree_iter_has_next(host_iter)) {
1019                 sdb_store_obj_t *host;
1020                 sdb_avltree_iter_t *iter = NULL;
1022                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1023                 assert(host);
1025                 if (! sdb_store_matcher_matches(filter, host, NULL))
1026                         continue;
1028                 if (type == SDB_SERVICE)
1029                         iter = sdb_avltree_get_iter(HOST(host)->services);
1030                 else if (type == SDB_METRIC)
1031                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
1033                 if (iter) {
1034                         while (sdb_avltree_iter_has_next(iter)) {
1035                                 sdb_store_obj_t *obj;
1036                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1037                                 assert(obj);
1039                                 if (sdb_store_matcher_matches(m, obj, filter)) {
1040                                         if (cb(obj, filter, user_data)) {
1041                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1042                                                                 "an error while scanning");
1043                                                 status = -1;
1044                                                 break;
1045                                         }
1046                                 }
1047                         }
1048                 }
1049                 else if (sdb_store_matcher_matches(m, host, filter)) {
1050                         if (cb(host, filter, user_data)) {
1051                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1052                                                 "an error while scanning");
1053                                 status = -1;
1054                         }
1055                 }
1057                 sdb_avltree_iter_destroy(iter);
1058                 if (status)
1059                         break;
1060         }
1062         sdb_avltree_iter_destroy(host_iter);
1063         pthread_rwlock_unlock(&host_lock);
1064         return status;
1065 } /* sdb_store_scan */
1067 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */