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 if (sdb_plugin_store_host(name, last_update))
540 status = -1;
542 free(cname);
543 return status;
544 } /* sdb_store_host */
546 bool
547 sdb_store_has_host(const char *name)
548 {
549 sdb_host_t *host;
551 if (! name)
552 return NULL;
554 host = lookup_host(name, /* canonicalize = */ 0);
555 sdb_object_deref(SDB_OBJ(host));
556 return host != NULL;
557 } /* sdb_store_has_host */
559 sdb_store_obj_t *
560 sdb_store_get_host(const char *name)
561 {
562 sdb_host_t *host;
564 if (! name)
565 return NULL;
567 host = lookup_host(name, /* canonicalize = */ 0);
568 if (! host)
569 return NULL;
571 return STORE_OBJ(host);
572 } /* sdb_store_get_host */
574 int
575 sdb_store_attribute(const char *hostname,
576 const char *key, const sdb_data_t *value,
577 sdb_time_t last_update)
578 {
579 sdb_host_t *host;
580 sdb_avltree_t *attrs;
581 int status = 0;
583 if ((! hostname) || (! key))
584 return -1;
586 pthread_rwlock_wrlock(&host_lock);
587 host = lookup_host(hostname, /* canonicalize = */ 1);
588 attrs = get_host_children(host, SDB_ATTRIBUTE);
589 if (! attrs) {
590 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
591 "host '%s' not found", key, hostname);
592 status = -1;
593 }
595 if (! status)
596 status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
598 sdb_object_deref(SDB_OBJ(host));
599 pthread_rwlock_unlock(&host_lock);
601 if (sdb_plugin_store_attribute(hostname, key, value, last_update))
602 status = -1;
603 return status;
604 } /* sdb_store_attribute */
606 int
607 sdb_store_service(const char *hostname, const char *name,
608 sdb_time_t last_update)
609 {
610 sdb_host_t *host;
611 sdb_avltree_t *services;
613 int status = 0;
615 if ((! hostname) || (! name))
616 return -1;
618 pthread_rwlock_wrlock(&host_lock);
619 host = lookup_host(hostname, /* canonicalize = */ 1);
620 services = get_host_children(host, SDB_SERVICE);
621 if (! services) {
622 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
623 "host '%s' not found", name, hostname);
624 status = -1;
625 }
627 if (! status)
628 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
629 name, last_update, NULL);
631 sdb_object_deref(SDB_OBJ(host));
632 pthread_rwlock_unlock(&host_lock);
634 if (sdb_plugin_store_service(hostname, name, last_update))
635 status = -1;
636 return status;
637 } /* sdb_store_service */
639 int
640 sdb_store_service_attr(const char *hostname, const char *service,
641 const char *key, const sdb_data_t *value, sdb_time_t last_update)
642 {
643 sdb_host_t *host;
644 sdb_service_t *svc;
645 sdb_avltree_t *services;
646 int status = 0;
648 if ((! hostname) || (! service) || (! key))
649 return -1;
651 pthread_rwlock_wrlock(&host_lock);
652 host = lookup_host(hostname, /* canonicalize = */ 1);
653 services = get_host_children(host, SDB_SERVICE);
654 sdb_object_deref(SDB_OBJ(host));
655 if (! services) {
656 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
657 "for service '%s' - host '%ss' not found",
658 key, service, hostname);
659 pthread_rwlock_unlock(&host_lock);
660 return -1;
661 }
663 svc = SVC(sdb_avltree_lookup(services, service));
664 if (! svc) {
665 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
666 "service '%s/%s' not found", key, hostname, service);
667 status = -1;
668 }
670 if (! status)
671 status = store_attr(STORE_OBJ(svc), svc->attributes,
672 key, value, last_update);
674 sdb_object_deref(SDB_OBJ(svc));
675 pthread_rwlock_unlock(&host_lock);
677 if (sdb_plugin_store_service_attribute(hostname, service,
678 key, value, last_update))
679 status = -1;
680 return status;
681 } /* sdb_store_service_attr */
683 int
684 sdb_store_metric(const char *hostname, const char *name,
685 sdb_metric_store_t *store, sdb_time_t last_update)
686 {
687 sdb_store_obj_t *obj = NULL;
688 sdb_host_t *host;
689 sdb_metric_t *metric;
691 sdb_avltree_t *metrics;
693 int status = 0;
695 if ((! hostname) || (! name))
696 return -1;
698 if (store) {
699 if ((store->type != NULL) != (store->id != NULL))
700 return -1;
701 else if (! store->type)
702 store = NULL;
703 }
705 pthread_rwlock_wrlock(&host_lock);
706 host = lookup_host(hostname, /* canonicalize = */ 1);
707 metrics = get_host_children(host, SDB_METRIC);
708 if (! metrics) {
709 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
710 "host '%s' not found", name, hostname);
711 status = -1;
712 }
714 if (! status)
715 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
716 name, last_update, &obj);
717 sdb_object_deref(SDB_OBJ(host));
719 if (status || (! store)) {
720 pthread_rwlock_unlock(&host_lock);
721 return status;
722 }
724 assert(obj);
725 metric = METRIC(obj);
727 if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
728 if (metric->store.type)
729 free(metric->store.type);
730 metric->store.type = strdup(store->type);
731 }
732 if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
733 if (metric->store.id)
734 free(metric->store.id);
735 metric->store.id = strdup(store->id);
736 }
738 if ((! metric->store.type) || (! metric->store.id)) {
739 if (metric->store.type)
740 free(metric->store.type);
741 if (metric->store.id)
742 free(metric->store.id);
743 metric->store.type = metric->store.id = NULL;
744 status = -1;
745 }
746 pthread_rwlock_unlock(&host_lock);
748 if (sdb_plugin_store_metric(hostname, name, store, last_update))
749 status = -1;
750 return status;
751 } /* sdb_store_metric */
753 int
754 sdb_store_metric_attr(const char *hostname, const char *metric,
755 const char *key, const sdb_data_t *value, sdb_time_t last_update)
756 {
757 sdb_avltree_t *metrics;
758 sdb_host_t *host;
759 sdb_metric_t *m;
760 int status = 0;
762 if ((! hostname) || (! metric) || (! key))
763 return -1;
765 pthread_rwlock_wrlock(&host_lock);
766 host = lookup_host(hostname, /* canonicalize = */ 1);
767 metrics = get_host_children(host, SDB_METRIC);
768 sdb_object_deref(SDB_OBJ(host));
769 if (! metrics) {
770 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
771 "for metric '%s' - host '%s' not found",
772 key, metric, hostname);
773 pthread_rwlock_unlock(&host_lock);
774 return -1;
775 }
777 m = METRIC(sdb_avltree_lookup(metrics, metric));
778 if (! m) {
779 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
780 "metric '%s/%s' not found", key, hostname, metric);
781 status = -1;
782 }
784 if (! status)
785 status = store_attr(STORE_OBJ(m), m->attributes,
786 key, value, last_update);
788 sdb_object_deref(SDB_OBJ(m));
789 pthread_rwlock_unlock(&host_lock);
791 if (sdb_plugin_store_metric_attribute(hostname, metric,
792 key, value, last_update))
793 status = -1;
794 return status;
795 } /* sdb_store_metric_attr */
797 sdb_store_obj_t *
798 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
799 {
800 sdb_avltree_t *children;
802 if ((! host) || (host->type != SDB_HOST) || (! name))
803 return NULL;
805 children = get_host_children(HOST(host), type);
806 if (! children)
807 return NULL;
808 return STORE_OBJ(sdb_avltree_lookup(children, name));
809 } /* sdb_store_get_child */
811 int
812 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
813 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
814 {
815 sdb_avltree_t *metrics;
816 sdb_host_t *host;
817 sdb_metric_t *m;
819 sdb_timeseries_t *ts;
821 if ((! hostname) || (! metric) || (! opts) || (! buf))
822 return -1;
824 pthread_rwlock_rdlock(&host_lock);
825 host = lookup_host(hostname, /* canonicalize = */ 1);
826 metrics = get_host_children(host, SDB_METRIC);
827 sdb_object_deref(SDB_OBJ(host));
828 if (! metrics) {
829 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
830 "- host '%s' not found", hostname, metric, hostname);
831 pthread_rwlock_unlock(&host_lock);
832 return -1;
833 }
835 m = METRIC(sdb_avltree_lookup(metrics, metric));
836 if (! m) {
837 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
838 "- metric '%s' not found", hostname, metric, metric);
839 pthread_rwlock_unlock(&host_lock);
840 return -1;
841 }
843 if ((! m->store.type) || (! m->store.id)) {
844 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
845 "- no data-store configured for the stored metric",
846 hostname, metric);
847 pthread_rwlock_unlock(&host_lock);
848 return -1;
849 }
851 {
852 char type[strlen(m->store.type) + 1];
853 char id[strlen(m->store.id) + 1];
855 strncpy(type, m->store.type, sizeof(type));
856 strncpy(id, m->store.id, sizeof(id));
857 pthread_rwlock_unlock(&host_lock);
859 ts = sdb_plugin_fetch_timeseries(type, id, opts);
860 if (! ts) {
861 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
862 "- %s fetcher callback returned no data for '%s'",
863 hostname, metric, type, id);
864 return -1;
865 }
866 }
868 ts_tojson(ts, buf);
869 sdb_timeseries_destroy(ts);
870 return 0;
871 } /* sdb_store_fetch_timeseries */
873 int
874 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
875 {
876 sdb_data_t tmp;
878 if (! obj)
879 return -1;
881 switch (field) {
882 case SDB_FIELD_NAME:
883 tmp.type = SDB_TYPE_STRING;
884 tmp.data.string = strdup(SDB_OBJ(obj)->name);
885 if (! tmp.data.string)
886 return -1;
887 break;
888 case SDB_FIELD_LAST_UPDATE:
889 tmp.type = SDB_TYPE_DATETIME;
890 tmp.data.datetime = obj->last_update;
891 break;
892 case SDB_FIELD_AGE:
893 tmp.type = SDB_TYPE_DATETIME;
894 tmp.data.datetime = sdb_gettime() - obj->last_update;
895 break;
896 case SDB_FIELD_INTERVAL:
897 tmp.type = SDB_TYPE_DATETIME;
898 tmp.data.datetime = obj->interval;
899 break;
900 case SDB_FIELD_BACKEND:
901 {
902 tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
903 tmp.data.array.length = obj->backends_num;
904 tmp.data.array.values = obj->backends;
905 return sdb_data_copy(res, &tmp);
906 }
907 default:
908 return -1;
909 }
910 if (res)
911 *res = tmp;
912 else
913 sdb_data_free_datum(&tmp);
914 return 0;
915 } /* sdb_store_get_field */
917 int
918 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
919 sdb_store_matcher_t *filter)
920 {
921 sdb_avltree_t *tree = NULL;
922 sdb_store_obj_t *attr;
924 if ((! obj) || (! name))
925 return -1;
927 if (obj->type == SDB_HOST)
928 tree = HOST(obj)->attributes;
929 else if (obj->type == SDB_SERVICE)
930 tree = SVC(obj)->attributes;
931 else if (obj->type == SDB_METRIC)
932 tree = METRIC(obj)->attributes;
934 if (! tree)
935 return -1;
937 attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
938 if (! attr)
939 return -1;
940 if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
941 sdb_object_deref(SDB_OBJ(attr));
942 return -1;
943 }
945 assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
946 if (res)
947 sdb_data_copy(res, &ATTR(attr)->value);
948 sdb_object_deref(SDB_OBJ(attr));
949 return 0;
950 } /* sdb_store_get_attr */
952 int
953 sdb_store_scan(int type, sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
954 sdb_store_lookup_cb cb, void *user_data)
955 {
956 sdb_avltree_iter_t *host_iter;
957 int status = 0;
959 if (! cb)
960 return -1;
962 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
963 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
964 return -1;
965 }
967 pthread_rwlock_rdlock(&host_lock);
969 host_iter = sdb_avltree_get_iter(hosts);
970 if (! host_iter)
971 status = -1;
973 /* has_next returns false if the iterator is NULL */
974 while (sdb_avltree_iter_has_next(host_iter)) {
975 sdb_store_obj_t *host;
976 sdb_avltree_iter_t *iter = NULL;
978 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
979 assert(host);
981 if (! sdb_store_matcher_matches(filter, host, NULL))
982 continue;
984 if (type == SDB_SERVICE)
985 iter = sdb_avltree_get_iter(HOST(host)->services);
986 else if (type == SDB_METRIC)
987 iter = sdb_avltree_get_iter(HOST(host)->metrics);
989 if (iter) {
990 while (sdb_avltree_iter_has_next(iter)) {
991 sdb_store_obj_t *obj;
992 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
993 assert(obj);
995 if (sdb_store_matcher_matches(m, obj, filter)) {
996 if (cb(obj, filter, user_data)) {
997 sdb_log(SDB_LOG_ERR, "store: Callback returned "
998 "an error while scanning");
999 status = -1;
1000 break;
1001 }
1002 }
1003 }
1004 }
1005 else if (sdb_store_matcher_matches(m, host, filter)) {
1006 if (cb(host, filter, user_data)) {
1007 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1008 "an error while scanning");
1009 status = -1;
1010 }
1011 }
1013 sdb_avltree_iter_destroy(iter);
1014 if (status)
1015 break;
1016 }
1018 sdb_avltree_iter_destroy(host_iter);
1019 pthread_rwlock_unlock(&host_lock);
1020 return status;
1021 } /* sdb_store_scan */
1023 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */