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)
100 {
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)
123 {
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)
139 {
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)
156 {
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)
168 {
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)
187 {
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)
204 {
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)
223 {
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)
260 {
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)
282 {
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)
313 {
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)
407 {
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)
429 {
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)
453 {
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)
510 {
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)
517 {
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)
545 {
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)
558 {
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)
575 {
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)
603 {
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)
633 {
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)
673 {
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;
684 if (store && ((! store->type) || (! store->id)))
685 return -1;
687 pthread_rwlock_wrlock(&host_lock);
688 host = lookup_host(hostname, /* canonicalize = */ 1);
689 metrics = get_host_children(host, SDB_METRIC);
690 if (! metrics) {
691 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
692 "host '%s' not found", name, hostname);
693 status = -1;
694 }
696 if (! status)
697 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
698 name, last_update, &obj);
699 sdb_object_deref(SDB_OBJ(host));
701 if (status || (! store)) {
702 pthread_rwlock_unlock(&host_lock);
703 return status;
704 }
706 assert(obj);
707 metric = METRIC(obj);
709 if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
710 if (metric->store.type)
711 free(metric->store.type);
712 metric->store.type = strdup(store->type);
713 }
714 if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
715 if (metric->store.id)
716 free(metric->store.id);
717 metric->store.id = strdup(store->id);
718 }
720 if ((! metric->store.type) || (! metric->store.id)) {
721 if (metric->store.type)
722 free(metric->store.type);
723 if (metric->store.id)
724 free(metric->store.id);
725 metric->store.type = metric->store.id = NULL;
726 status = -1;
727 }
728 pthread_rwlock_unlock(&host_lock);
729 return status;
730 } /* sdb_store_metric */
732 int
733 sdb_store_metric_attr(const char *hostname, const char *metric,
734 const char *key, const sdb_data_t *value, sdb_time_t last_update)
735 {
736 sdb_avltree_t *metrics;
737 sdb_host_t *host;
738 sdb_metric_t *m;
739 int status = 0;
741 if ((! hostname) || (! metric) || (! key))
742 return -1;
744 pthread_rwlock_wrlock(&host_lock);
745 host = lookup_host(hostname, /* canonicalize = */ 1);
746 metrics = get_host_children(host, SDB_METRIC);
747 sdb_object_deref(SDB_OBJ(host));
748 if (! metrics) {
749 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
750 "for metric '%s' - host '%s' not found",
751 key, metric, hostname);
752 pthread_rwlock_unlock(&host_lock);
753 return -1;
754 }
756 m = METRIC(sdb_avltree_lookup(metrics, metric));
757 if (! m) {
758 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
759 "metric '%s/%s' not found", key, hostname, metric);
760 status = -1;
761 }
763 if (! status)
764 status = store_attr(STORE_OBJ(m), m->attributes,
765 key, value, last_update);
767 sdb_object_deref(SDB_OBJ(m));
768 pthread_rwlock_unlock(&host_lock);
769 return status;
770 } /* sdb_store_metric_attr */
772 sdb_store_obj_t *
773 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
774 {
775 sdb_avltree_t *children;
777 if ((! host) || (host->type != SDB_HOST) || (! name))
778 return NULL;
780 children = get_host_children(HOST(host), type);
781 if (! children)
782 return NULL;
783 return STORE_OBJ(sdb_avltree_lookup(children, name));
784 } /* sdb_store_get_child */
786 int
787 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
788 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
789 {
790 sdb_avltree_t *metrics;
791 sdb_host_t *host;
792 sdb_metric_t *m;
794 sdb_timeseries_t *ts;
796 if ((! hostname) || (! metric) || (! opts) || (! buf))
797 return -1;
799 pthread_rwlock_rdlock(&host_lock);
800 host = lookup_host(hostname, /* canonicalize = */ 1);
801 metrics = get_host_children(host, SDB_METRIC);
802 sdb_object_deref(SDB_OBJ(host));
803 if (! metrics) {
804 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
805 "- host '%s' not found", hostname, metric, hostname);
806 pthread_rwlock_unlock(&host_lock);
807 return -1;
808 }
810 m = METRIC(sdb_avltree_lookup(metrics, metric));
811 if (! m) {
812 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
813 "- metric '%s' not found", hostname, metric, metric);
814 pthread_rwlock_unlock(&host_lock);
815 return -1;
816 }
818 if ((! m->store.type) || (! m->store.id)) {
819 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
820 "- no data-store configured for the stored metric",
821 hostname, metric);
822 pthread_rwlock_unlock(&host_lock);
823 return -1;
824 }
826 {
827 char type[strlen(m->store.type) + 1];
828 char id[strlen(m->store.id) + 1];
830 strncpy(type, m->store.type, sizeof(type));
831 strncpy(id, m->store.id, sizeof(id));
832 pthread_rwlock_unlock(&host_lock);
834 ts = sdb_plugin_fetch_timeseries(type, id, opts);
835 if (! ts) {
836 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
837 "- %s fetcher callback returned no data for '%s'",
838 hostname, metric, type, id);
839 return -1;
840 }
841 }
843 ts_tojson(ts, buf);
844 sdb_timeseries_destroy(ts);
845 return 0;
846 } /* sdb_store_fetch_timeseries */
848 int
849 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
850 {
851 sdb_data_t tmp;
853 if (! obj)
854 return -1;
856 switch (field) {
857 case SDB_FIELD_NAME:
858 tmp.type = SDB_TYPE_STRING;
859 tmp.data.string = strdup(SDB_OBJ(obj)->name);
860 if (! tmp.data.string)
861 return -1;
862 break;
863 case SDB_FIELD_LAST_UPDATE:
864 tmp.type = SDB_TYPE_DATETIME;
865 tmp.data.datetime = obj->last_update;
866 break;
867 case SDB_FIELD_AGE:
868 tmp.type = SDB_TYPE_DATETIME;
869 tmp.data.datetime = sdb_gettime() - obj->last_update;
870 break;
871 case SDB_FIELD_INTERVAL:
872 tmp.type = SDB_TYPE_DATETIME;
873 tmp.data.datetime = obj->interval;
874 break;
875 case SDB_FIELD_BACKEND:
876 {
877 tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
878 tmp.data.array.length = obj->backends_num;
879 tmp.data.array.values = obj->backends;
880 return sdb_data_copy(res, &tmp);
881 }
882 default:
883 return -1;
884 }
885 if (res)
886 *res = tmp;
887 else
888 sdb_data_free_datum(&tmp);
889 return 0;
890 } /* sdb_store_get_field */
892 int
893 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
894 sdb_store_matcher_t *filter)
895 {
896 sdb_avltree_t *tree = NULL;
897 sdb_store_obj_t *attr;
899 if ((! obj) || (! name))
900 return -1;
902 if (obj->type == SDB_HOST)
903 tree = HOST(obj)->attributes;
904 else if (obj->type == SDB_SERVICE)
905 tree = SVC(obj)->attributes;
906 else if (obj->type == SDB_METRIC)
907 tree = METRIC(obj)->attributes;
909 if (! tree)
910 return -1;
912 attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
913 if (! attr)
914 return -1;
915 if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
916 sdb_object_deref(SDB_OBJ(attr));
917 return -1;
918 }
920 assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
921 if (res)
922 sdb_data_copy(res, &ATTR(attr)->value);
923 sdb_object_deref(SDB_OBJ(attr));
924 return 0;
925 } /* sdb_store_get_attr */
927 int
928 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
929 sdb_store_lookup_cb cb, void *user_data)
930 {
931 sdb_avltree_iter_t *host_iter;
932 int status = 0;
934 if (! cb)
935 return -1;
937 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
938 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
939 return -1;
940 }
942 pthread_rwlock_rdlock(&host_lock);
944 host_iter = sdb_avltree_get_iter(hosts);
945 if (! host_iter)
946 status = -1;
948 /* has_next returns false if the iterator is NULL */
949 while (sdb_avltree_iter_has_next(host_iter)) {
950 sdb_store_obj_t *host;
951 sdb_avltree_iter_t *iter = NULL;
953 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
954 assert(host);
956 if (! sdb_store_matcher_matches(filter, host, NULL))
957 continue;
959 if (type == SDB_SERVICE)
960 iter = sdb_avltree_get_iter(HOST(host)->services);
961 else if (type == SDB_METRIC)
962 iter = sdb_avltree_get_iter(HOST(host)->metrics);
964 if (iter) {
965 while (sdb_avltree_iter_has_next(iter)) {
966 sdb_store_obj_t *obj;
967 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
968 assert(obj);
970 if (sdb_store_matcher_matches(m, obj, filter)) {
971 if (cb(obj, filter, user_data)) {
972 sdb_log(SDB_LOG_ERR, "store: Callback returned "
973 "an error while scanning");
974 status = -1;
975 break;
976 }
977 }
978 }
979 }
980 else if (sdb_store_matcher_matches(m, host, filter)) {
981 if (cb(host, filter, user_data)) {
982 sdb_log(SDB_LOG_ERR, "store: Callback returned "
983 "an error while scanning");
984 status = -1;
985 }
986 }
988 sdb_avltree_iter_destroy(iter);
989 if (status)
990 break;
991 }
993 sdb_avltree_iter_destroy(host_iter);
994 pthread_rwlock_unlock(&host_lock);
995 return status;
996 } /* sdb_store_scan */
998 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */