Code

store: Let store_metric() accept non-NULL but empty metric-store.
[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 /* The host_lock has to be acquired before calling this function. */
427 static sdb_avltree_t *
428 get_host_children(sdb_host_t *host, int type)
430         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
431                         && (type != SDB_ATTRIBUTE))
432                 return NULL;
434         if (! host)
435                 return NULL;
437         if (type == SDB_ATTRIBUTE)
438                 return host->attributes;
439         else if (type == SDB_METRIC)
440                 return host->metrics;
441         else
442                 return host->services;
443 } /* get_host_children */
445 /*
446  * ts_tojson serializes a time-series to JSON.
447  *
448  * The function never returns an error. Rather, an error message will be part
449  * of the serialized data.
450  */
451 static void
452 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
454         char start_str[64];
455         char end_str[64];
457         size_t i;
459         /* TODO: make time format configurable */
460         if (! sdb_strftime(start_str, sizeof(start_str),
461                                 "%F %T %z", ts->start))
462                 snprintf(start_str, sizeof(start_str), "<error>");
463         start_str[sizeof(start_str) - 1] = '\0';
464         if (! sdb_strftime(end_str, sizeof(end_str),
465                                 "%F %T %z", ts->end))
466                 snprintf(end_str, sizeof(end_str), "<error>");
467         end_str[sizeof(end_str) - 1] = '\0';
469         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
470                         start_str, end_str);
472         for (i = 0; i < ts->data_names_len; ++i) {
473                 size_t j;
474                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
476                 for (j = 0; j < ts->data_len; ++j) {
477                         char time_str[64];
479                         if (! sdb_strftime(time_str, sizeof(time_str),
480                                                 "%F %T %z", ts->data[i][j].timestamp))
481                                 snprintf(time_str, sizeof(time_str), "<error>");
482                         time_str[sizeof(time_str) - 1] = '\0';
484                         /* Some GNU libc versions may print '-nan' which we dont' want */
485                         if (isnan(ts->data[i][j].value))
486                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
487                                                 "\"value\": \"nan\"}", time_str);
488                         else
489                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
490                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
492                         if (j < ts->data_len - 1)
493                                 sdb_strbuf_append(buf, ",");
494                 }
496                 if (i < ts->data_names_len - 1)
497                         sdb_strbuf_append(buf, "],");
498                 else
499                         sdb_strbuf_append(buf, "]");
500         }
501         sdb_strbuf_append(buf, "}}");
502 } /* ts_tojson */
504 /*
505  * public API
506  */
508 void
509 sdb_store_clear(void)
511         sdb_avltree_destroy(hosts);
512         hosts = NULL;
513 } /* sdb_store_clear */
515 int
516 sdb_store_host(const char *name, sdb_time_t last_update)
518         char *cname = NULL;
519         int status = 0;
521         if (! name)
522                 return -1;
524         cname = sdb_plugin_cname(strdup(name));
525         if (! cname) {
526                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
527                 return -1;
528         }
530         pthread_rwlock_wrlock(&host_lock);
531         if (! hosts)
532                 if (! (hosts = sdb_avltree_create()))
533                         status = -1;
535         if (! status)
536                 status = store_obj(NULL, hosts, SDB_HOST, cname, last_update, NULL);
537         pthread_rwlock_unlock(&host_lock);
539         free(cname);
540         return status;
541 } /* sdb_store_host */
543 bool
544 sdb_store_has_host(const char *name)
546         sdb_host_t *host;
548         if (! name)
549                 return NULL;
551         host = lookup_host(name, /* canonicalize = */ 0);
552         sdb_object_deref(SDB_OBJ(host));
553         return host != NULL;
554 } /* sdb_store_has_host */
556 sdb_store_obj_t *
557 sdb_store_get_host(const char *name)
559         sdb_host_t *host;
561         if (! name)
562                 return NULL;
564         host = lookup_host(name, /* canonicalize = */ 0);
565         if (! host)
566                 return NULL;
568         return STORE_OBJ(host);
569 } /* sdb_store_get_host */
571 int
572 sdb_store_attribute(const char *hostname,
573                 const char *key, const sdb_data_t *value,
574                 sdb_time_t last_update)
576         sdb_host_t *host;
577         sdb_avltree_t *attrs;
578         int status = 0;
580         if ((! hostname) || (! key))
581                 return -1;
583         pthread_rwlock_wrlock(&host_lock);
584         host = lookup_host(hostname, /* canonicalize = */ 1);
585         attrs = get_host_children(host, SDB_ATTRIBUTE);
586         if (! attrs) {
587                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
588                                 "host '%s' not found", key, hostname);
589                 status = -1;
590         }
592         if (! status)
593                 status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
595         sdb_object_deref(SDB_OBJ(host));
596         pthread_rwlock_unlock(&host_lock);
597         return status;
598 } /* sdb_store_attribute */
600 int
601 sdb_store_service(const char *hostname, const char *name,
602                 sdb_time_t last_update)
604         sdb_host_t *host;
605         sdb_avltree_t *services;
607         int status = 0;
609         if ((! hostname) || (! name))
610                 return -1;
612         pthread_rwlock_wrlock(&host_lock);
613         host = lookup_host(hostname, /* canonicalize = */ 1);
614         services = get_host_children(host, SDB_SERVICE);
615         if (! services) {
616                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
617                                 "host '%s' not found", name, hostname);
618                 status = -1;
619         }
621         if (! status)
622                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
623                                 name, last_update, NULL);
625         sdb_object_deref(SDB_OBJ(host));
626         pthread_rwlock_unlock(&host_lock);
627         return status;
628 } /* sdb_store_service */
630 int
631 sdb_store_service_attr(const char *hostname, const char *service,
632                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
634         sdb_host_t *host;
635         sdb_service_t *svc;
636         sdb_avltree_t *services;
637         int status = 0;
639         if ((! hostname) || (! service) || (! key))
640                 return -1;
642         pthread_rwlock_wrlock(&host_lock);
643         host = lookup_host(hostname, /* canonicalize = */ 1);
644         services = get_host_children(host, SDB_SERVICE);
645         sdb_object_deref(SDB_OBJ(host));
646         if (! services) {
647                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
648                                 "for service '%s' - host '%ss' not found",
649                                 key, service, hostname);
650                 pthread_rwlock_unlock(&host_lock);
651                 return -1;
652         }
654         svc = SVC(sdb_avltree_lookup(services, service));
655         if (! svc) {
656                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
657                                 "service '%s/%s' not found", key, hostname, service);
658                 status = -1;
659         }
661         if (! status)
662                 status = store_attr(STORE_OBJ(svc), svc->attributes,
663                                 key, value, last_update);
665         sdb_object_deref(SDB_OBJ(svc));
666         pthread_rwlock_unlock(&host_lock);
667         return status;
668 } /* sdb_store_service_attr */
670 int
671 sdb_store_metric(const char *hostname, const char *name,
672                 sdb_metric_store_t *store, sdb_time_t last_update)
674         sdb_store_obj_t *obj = NULL;
675         sdb_host_t *host;
676         sdb_metric_t *metric;
678         sdb_avltree_t *metrics;
680         int status = 0;
682         if ((! hostname) || (! name))
683                 return -1;
685         if (store) {
686                 if ((store->type != NULL) != (store->id != NULL))
687                         return -1;
688                 else if (! store->type)
689                         store = NULL;
690         }
692         pthread_rwlock_wrlock(&host_lock);
693         host = lookup_host(hostname, /* canonicalize = */ 1);
694         metrics = get_host_children(host, SDB_METRIC);
695         if (! metrics) {
696                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
697                                 "host '%s' not found", name, hostname);
698                 status = -1;
699         }
701         if (! status)
702                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
703                                 name, last_update, &obj);
704         sdb_object_deref(SDB_OBJ(host));
706         if (status || (! store)) {
707                 pthread_rwlock_unlock(&host_lock);
708                 return status;
709         }
711         assert(obj);
712         metric = METRIC(obj);
714         if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
715                 if (metric->store.type)
716                         free(metric->store.type);
717                 metric->store.type = strdup(store->type);
718         }
719         if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
720                 if (metric->store.id)
721                         free(metric->store.id);
722                 metric->store.id = strdup(store->id);
723         }
725         if ((! metric->store.type) || (! metric->store.id)) {
726                 if (metric->store.type)
727                         free(metric->store.type);
728                 if (metric->store.id)
729                         free(metric->store.id);
730                 metric->store.type = metric->store.id = NULL;
731                 status = -1;
732         }
733         pthread_rwlock_unlock(&host_lock);
734         return status;
735 } /* sdb_store_metric */
737 int
738 sdb_store_metric_attr(const char *hostname, const char *metric,
739                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
741         sdb_avltree_t *metrics;
742         sdb_host_t *host;
743         sdb_metric_t *m;
744         int status = 0;
746         if ((! hostname) || (! metric) || (! key))
747                 return -1;
749         pthread_rwlock_wrlock(&host_lock);
750         host = lookup_host(hostname, /* canonicalize = */ 1);
751         metrics = get_host_children(host, SDB_METRIC);
752         sdb_object_deref(SDB_OBJ(host));
753         if (! metrics) {
754                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
755                                 "for metric '%s' - host '%s' not found",
756                                 key, metric, hostname);
757                 pthread_rwlock_unlock(&host_lock);
758                 return -1;
759         }
761         m = METRIC(sdb_avltree_lookup(metrics, metric));
762         if (! m) {
763                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
764                                 "metric '%s/%s' not found", key, hostname, metric);
765                 status = -1;
766         }
768         if (! status)
769                 status = store_attr(STORE_OBJ(m), m->attributes,
770                                 key, value, last_update);
772         sdb_object_deref(SDB_OBJ(m));
773         pthread_rwlock_unlock(&host_lock);
774         return status;
775 } /* sdb_store_metric_attr */
777 sdb_store_obj_t *
778 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
780         sdb_avltree_t *children;
782         if ((! host) || (host->type != SDB_HOST) || (! name))
783                 return NULL;
785         children = get_host_children(HOST(host), type);
786         if (! children)
787                 return NULL;
788         return STORE_OBJ(sdb_avltree_lookup(children, name));
789 } /* sdb_store_get_child */
791 int
792 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
793                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
795         sdb_avltree_t *metrics;
796         sdb_host_t *host;
797         sdb_metric_t *m;
799         sdb_timeseries_t *ts;
801         if ((! hostname) || (! metric) || (! opts) || (! buf))
802                 return -1;
804         pthread_rwlock_rdlock(&host_lock);
805         host = lookup_host(hostname, /* canonicalize = */ 1);
806         metrics = get_host_children(host, SDB_METRIC);
807         sdb_object_deref(SDB_OBJ(host));
808         if (! metrics) {
809                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
810                                 "- host '%s' not found", hostname, metric, hostname);
811                 pthread_rwlock_unlock(&host_lock);
812                 return -1;
813         }
815         m = METRIC(sdb_avltree_lookup(metrics, metric));
816         if (! m) {
817                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
818                                 "- metric '%s' not found", hostname, metric, metric);
819                 pthread_rwlock_unlock(&host_lock);
820                 return -1;
821         }
823         if ((! m->store.type) || (! m->store.id)) {
824                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
825                                 "- no data-store configured for the stored metric",
826                                 hostname, metric);
827                 pthread_rwlock_unlock(&host_lock);
828                 return -1;
829         }
831         {
832                 char type[strlen(m->store.type) + 1];
833                 char id[strlen(m->store.id) + 1];
835                 strncpy(type, m->store.type, sizeof(type));
836                 strncpy(id, m->store.id, sizeof(id));
837                 pthread_rwlock_unlock(&host_lock);
839                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
840                 if (! ts) {
841                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
842                                         "- %s fetcher callback returned no data for '%s'",
843                                         hostname, metric, type, id);
844                         return -1;
845                 }
846         }
848         ts_tojson(ts, buf);
849         sdb_timeseries_destroy(ts);
850         return 0;
851 } /* sdb_store_fetch_timeseries */
853 int
854 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
856         sdb_data_t tmp;
858         if (! obj)
859                 return -1;
861         switch (field) {
862                 case SDB_FIELD_NAME:
863                         tmp.type = SDB_TYPE_STRING;
864                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
865                         if (! tmp.data.string)
866                                 return -1;
867                         break;
868                 case SDB_FIELD_LAST_UPDATE:
869                         tmp.type = SDB_TYPE_DATETIME;
870                         tmp.data.datetime = obj->last_update;
871                         break;
872                 case SDB_FIELD_AGE:
873                         tmp.type = SDB_TYPE_DATETIME;
874                         tmp.data.datetime = sdb_gettime() - obj->last_update;
875                         break;
876                 case SDB_FIELD_INTERVAL:
877                         tmp.type = SDB_TYPE_DATETIME;
878                         tmp.data.datetime = obj->interval;
879                         break;
880                 case SDB_FIELD_BACKEND:
881                 {
882                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
883                         tmp.data.array.length = obj->backends_num;
884                         tmp.data.array.values = obj->backends;
885                         return sdb_data_copy(res, &tmp);
886                 }
887                 default:
888                         return -1;
889         }
890         if (res)
891                 *res = tmp;
892         else
893                 sdb_data_free_datum(&tmp);
894         return 0;
895 } /* sdb_store_get_field */
897 int
898 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
899                 sdb_store_matcher_t *filter)
901         sdb_avltree_t *tree = NULL;
902         sdb_store_obj_t *attr;
904         if ((! obj) || (! name))
905                 return -1;
907         if (obj->type == SDB_HOST)
908                 tree = HOST(obj)->attributes;
909         else if (obj->type == SDB_SERVICE)
910                 tree = SVC(obj)->attributes;
911         else if (obj->type == SDB_METRIC)
912                 tree = METRIC(obj)->attributes;
914         if (! tree)
915                 return -1;
917         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
918         if (! attr)
919                 return -1;
920         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
921                 sdb_object_deref(SDB_OBJ(attr));
922                 return -1;
923         }
925         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
926         if (res)
927                 sdb_data_copy(res, &ATTR(attr)->value);
928         sdb_object_deref(SDB_OBJ(attr));
929         return 0;
930 } /* sdb_store_get_attr */
932 int
933 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
934                 sdb_store_lookup_cb cb, void *user_data)
936         sdb_avltree_iter_t *host_iter;
937         int status = 0;
939         if (! cb)
940                 return -1;
942         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
943                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
944                 return -1;
945         }
947         pthread_rwlock_rdlock(&host_lock);
949         host_iter = sdb_avltree_get_iter(hosts);
950         if (! host_iter)
951                 status = -1;
953         /* has_next returns false if the iterator is NULL */
954         while (sdb_avltree_iter_has_next(host_iter)) {
955                 sdb_store_obj_t *host;
956                 sdb_avltree_iter_t *iter = NULL;
958                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
959                 assert(host);
961                 if (! sdb_store_matcher_matches(filter, host, NULL))
962                         continue;
964                 if (type == SDB_SERVICE)
965                         iter = sdb_avltree_get_iter(HOST(host)->services);
966                 else if (type == SDB_METRIC)
967                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
969                 if (iter) {
970                         while (sdb_avltree_iter_has_next(iter)) {
971                                 sdb_store_obj_t *obj;
972                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
973                                 assert(obj);
975                                 if (sdb_store_matcher_matches(m, obj, filter)) {
976                                         if (cb(obj, filter, user_data)) {
977                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
978                                                                 "an error while scanning");
979                                                 status = -1;
980                                                 break;
981                                         }
982                                 }
983                         }
984                 }
985                 else if (sdb_store_matcher_matches(m, host, filter)) {
986                         if (cb(host, filter, user_data)) {
987                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
988                                                 "an error while scanning");
989                                 status = -1;
990                         }
991                 }
993                 sdb_avltree_iter_destroy(iter);
994                 if (status)
995                         break;
996         }
998         sdb_avltree_iter_destroy(host_iter);
999         pthread_rwlock_unlock(&host_lock);
1000         return status;
1001 } /* sdb_store_scan */
1003 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */