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 struct sdb_store {
54 sdb_object_t super;
56 /* hosts are the top-level entries and
57 * reference everything else */
58 sdb_avltree_t *hosts;
59 pthread_rwlock_t host_lock;
60 };
62 /*
63 * private types
64 */
66 static sdb_type_t host_type;
67 static sdb_type_t service_type;
68 static sdb_type_t metric_type;
69 static sdb_type_t attribute_type;
71 static int
72 store_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
73 {
74 int err;
75 if (! (SDB_STORE(obj)->hosts = sdb_avltree_create()))
76 return -1;
77 if ((err = pthread_rwlock_init(&SDB_STORE(obj)->host_lock,
78 /* attr = */ NULL))) {
79 char errbuf[128];
80 sdb_log(SDB_LOG_ERR, "store: Failed to initialize lock: %s",
81 sdb_strerror(err, errbuf, sizeof(errbuf)));
82 return -1;
83 }
84 return 0;
85 } /* store_init */
87 static void
88 store_destroy(sdb_object_t *obj)
89 {
90 int err;
91 if ((err = pthread_rwlock_destroy(&SDB_STORE(obj)->host_lock))) {
92 char errbuf[128];
93 sdb_log(SDB_LOG_ERR, "store: Failed to destroy lock: %s",
94 sdb_strerror(err, errbuf, sizeof(errbuf)));
95 return;
96 }
97 sdb_avltree_destroy(SDB_STORE(obj)->hosts);
98 SDB_STORE(obj)->hosts = NULL;
99 } /* store_destroy */
101 static int
102 store_obj_init(sdb_object_t *obj, va_list ap)
103 {
104 sdb_store_obj_t *sobj = STORE_OBJ(obj);
106 sobj->type = va_arg(ap, int);
108 sobj->last_update = va_arg(ap, sdb_time_t);
109 sobj->interval = 0;
110 sobj->backends = NULL;
111 sobj->backends_num = 0;
112 sobj->parent = NULL;
113 return 0;
114 } /* store_obj_init */
116 static void
117 store_obj_destroy(sdb_object_t *obj)
118 {
119 sdb_store_obj_t *sobj = STORE_OBJ(obj);
120 size_t i;
122 for (i = 0; i < sobj->backends_num; ++i)
123 free(sobj->backends[i]);
124 free(sobj->backends);
125 sobj->backends = NULL;
126 sobj->backends_num = 0;
128 // We don't currently keep an extra reference for parent objects to
129 // avoid circular self-references which are not handled correctly by
130 // the ref-count base management layer.
131 //sdb_object_deref(SDB_OBJ(sobj->parent));
132 } /* store_obj_destroy */
134 static int
135 host_init(sdb_object_t *obj, va_list ap)
136 {
137 host_t *sobj = HOST(obj);
138 int ret;
140 /* this will consume the first argument (type) of ap */
141 ret = store_obj_init(obj, ap);
142 if (ret)
143 return ret;
145 sobj->services = sdb_avltree_create();
146 if (! sobj->services)
147 return -1;
148 sobj->metrics = sdb_avltree_create();
149 if (! sobj->metrics)
150 return -1;
151 sobj->attributes = sdb_avltree_create();
152 if (! sobj->attributes)
153 return -1;
154 return 0;
155 } /* host_init */
157 static void
158 host_destroy(sdb_object_t *obj)
159 {
160 host_t *sobj = HOST(obj);
161 assert(obj);
163 store_obj_destroy(obj);
165 if (sobj->services)
166 sdb_avltree_destroy(sobj->services);
167 if (sobj->metrics)
168 sdb_avltree_destroy(sobj->metrics);
169 if (sobj->attributes)
170 sdb_avltree_destroy(sobj->attributes);
171 } /* host_destroy */
173 static int
174 service_init(sdb_object_t *obj, va_list ap)
175 {
176 service_t *sobj = SVC(obj);
177 int ret;
179 /* this will consume the first argument (type) of ap */
180 ret = store_obj_init(obj, ap);
181 if (ret)
182 return ret;
184 sobj->attributes = sdb_avltree_create();
185 if (! sobj->attributes)
186 return -1;
187 return 0;
188 } /* service_init */
190 static void
191 service_destroy(sdb_object_t *obj)
192 {
193 service_t *sobj = SVC(obj);
194 assert(obj);
196 store_obj_destroy(obj);
198 if (sobj->attributes)
199 sdb_avltree_destroy(sobj->attributes);
200 } /* service_destroy */
202 static int
203 metric_init(sdb_object_t *obj, va_list ap)
204 {
205 sdb_metric_t *sobj = METRIC(obj);
206 int ret;
208 /* this will consume the first argument (type) of ap */
209 ret = store_obj_init(obj, ap);
210 if (ret)
211 return ret;
213 sobj->attributes = sdb_avltree_create();
214 if (! sobj->attributes)
215 return -1;
217 sobj->store.type = sobj->store.id = NULL;
218 return 0;
219 } /* metric_init */
221 static void
222 metric_destroy(sdb_object_t *obj)
223 {
224 sdb_metric_t *sobj = METRIC(obj);
225 assert(obj);
227 store_obj_destroy(obj);
229 if (sobj->attributes)
230 sdb_avltree_destroy(sobj->attributes);
232 if (sobj->store.type)
233 free(sobj->store.type);
234 if (sobj->store.id)
235 free(sobj->store.id);
236 } /* metric_destroy */
238 static int
239 attr_init(sdb_object_t *obj, va_list ap)
240 {
241 const sdb_data_t *value;
242 int ret;
244 /* this will consume the first two arguments
245 * (type and last_update) of ap */
246 ret = store_obj_init(obj, ap);
247 if (ret)
248 return ret;
249 value = va_arg(ap, const sdb_data_t *);
251 if (value)
252 if (sdb_data_copy(&ATTR(obj)->value, value))
253 return -1;
254 return 0;
255 } /* attr_init */
257 static void
258 attr_destroy(sdb_object_t *obj)
259 {
260 assert(obj);
262 store_obj_destroy(obj);
263 sdb_data_free_datum(&ATTR(obj)->value);
264 } /* attr_destroy */
266 static sdb_type_t store_type = {
267 /* size = */ sizeof(sdb_store_t),
268 /* init = */ store_init,
269 /* destroy = */ store_destroy,
270 };
272 static sdb_type_t host_type = {
273 /* size = */ sizeof(host_t),
274 /* init = */ host_init,
275 /* destroy = */ host_destroy
276 };
278 static sdb_type_t service_type = {
279 /* size = */ sizeof(service_t),
280 /* init = */ service_init,
281 /* destroy = */ service_destroy
282 };
284 static sdb_type_t metric_type = {
285 /* size = */ sizeof(sdb_metric_t),
286 /* init = */ metric_init,
287 /* destroy = */ metric_destroy
288 };
290 static sdb_type_t attribute_type = {
291 /* size = */ sizeof(attr_t),
292 /* init = */ attr_init,
293 /* destroy = */ attr_destroy
294 };
296 /*
297 * private helper functions
298 */
300 static int
301 record_backend(sdb_store_obj_t *obj)
302 {
303 const sdb_plugin_info_t *info;
304 char **tmp;
305 size_t i;
307 info = sdb_plugin_current();
308 if (! info)
309 return 0;
311 for (i = 0; i < obj->backends_num; ++i)
312 if (!strcasecmp(obj->backends[i], info->plugin_name))
313 return 0;
315 tmp = realloc(obj->backends,
316 (obj->backends_num + 1) * sizeof(*obj->backends));
317 if (! tmp)
318 return -1;
320 obj->backends = tmp;
321 obj->backends[obj->backends_num] = strdup(info->plugin_name);
322 if (! obj->backends[obj->backends_num])
323 return -1;
325 ++obj->backends_num;
326 return 0;
327 } /* record_backend */
329 static int
330 store_obj(sdb_store_obj_t *parent, sdb_avltree_t *parent_tree,
331 int type, const char *name, sdb_time_t last_update,
332 sdb_store_obj_t **updated_obj)
333 {
334 sdb_store_obj_t *old, *new;
335 int status = 0;
337 assert(parent_tree);
339 if (last_update <= 0)
340 last_update = sdb_gettime();
342 old = STORE_OBJ(sdb_avltree_lookup(parent_tree, name));
343 if (old) {
344 if (old->last_update > last_update) {
345 sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
346 "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
347 SDB_STORE_TYPE_TO_NAME(type), name,
348 last_update, old->last_update);
349 /* don't report an error; the object may be updated by multiple
350 * backends */
351 status = 1;
352 }
353 else if (old->last_update == last_update) {
354 /* don't report an error and also don't even log this to avoid
355 * excessive noise on high sampling frequencies */
356 status = 1;
357 }
358 else {
359 sdb_time_t interval = last_update - old->last_update;
360 old->last_update = last_update;
361 if (interval) {
362 if (old->interval)
363 old->interval = (sdb_time_t)((0.9 * (double)old->interval)
364 + (0.1 * (double)interval));
365 else
366 old->interval = interval;
367 }
368 }
370 new = old;
371 sdb_object_deref(SDB_OBJ(old));
372 }
373 else {
374 if (type == SDB_ATTRIBUTE) {
375 /* the value will be updated by the caller */
376 new = STORE_OBJ(sdb_object_create(name, attribute_type,
377 type, last_update, NULL));
378 }
379 else {
380 sdb_type_t t;
381 t = type == SDB_HOST
382 ? host_type
383 : type == SDB_SERVICE
384 ? service_type
385 : metric_type;
386 new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
387 }
389 if (new) {
390 status = sdb_avltree_insert(parent_tree, SDB_OBJ(new));
392 /* pass control to the tree or destroy in case of an error */
393 sdb_object_deref(SDB_OBJ(new));
394 }
395 else {
396 char errbuf[1024];
397 sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
398 SDB_STORE_TYPE_TO_NAME(type), name,
399 sdb_strerror(errno, errbuf, sizeof(errbuf)));
400 status = -1;
401 }
402 }
404 if (status < 0)
405 return status;
406 assert(new);
408 if (new->parent != parent) {
409 // Avoid circular self-references which are not handled
410 // correctly by the ref-count based management layer.
411 //sdb_object_deref(SDB_OBJ(new->parent));
412 //sdb_object_ref(SDB_OBJ(parent));
413 new->parent = parent;
414 }
416 if (updated_obj)
417 *updated_obj = new;
419 if (record_backend(new))
420 return -1;
421 return status;
422 } /* store_obj */
424 static int
425 store_attr(sdb_store_obj_t *parent, sdb_avltree_t *attributes,
426 const char *key, const sdb_data_t *value, sdb_time_t last_update)
427 {
428 sdb_store_obj_t *attr = NULL;
429 int status;
431 status = store_obj(parent, attributes, SDB_ATTRIBUTE,
432 key, last_update, &attr);
433 if (status)
434 return status;
436 /* don't update unchanged values */
437 if (! sdb_data_cmp(&ATTR(attr)->value, value))
438 return status;
440 assert(attr);
441 if (sdb_data_copy(&ATTR(attr)->value, value))
442 return -1;
443 return status;
444 } /* store_attr */
446 static int
447 store_metric_store(sdb_metric_t *metric, sdb_metric_store_t *store)
448 {
449 char *type = metric->store.type;
450 char *id = metric->store.id;
452 if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
453 if (! (type = strdup(store->type)))
454 return -1;
455 }
456 if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
457 if (! (id = strdup(store->id))) {
458 if (type != metric->store.type)
459 free(type);
460 return -1;
461 }
462 }
464 if (type != metric->store.type) {
465 if (metric->store.type)
466 free(metric->store.type);
467 metric->store.type = type;
468 }
469 if (id != metric->store.id) {
470 if (metric->store.id)
471 free(metric->store.id);
472 metric->store.id = id;
473 }
474 return 0;
475 } /* store_metric_store */
477 /* The store's host_lock has to be acquired before calling this function. */
478 static sdb_avltree_t *
479 get_host_children(host_t *host, int type)
480 {
481 if ((type != SDB_SERVICE) && (type != SDB_METRIC)
482 && (type != SDB_ATTRIBUTE))
483 return NULL;
485 if (! host)
486 return NULL;
488 if (type == SDB_ATTRIBUTE)
489 return host->attributes;
490 else if (type == SDB_METRIC)
491 return host->metrics;
492 else
493 return host->services;
494 } /* get_host_children */
496 /*
497 * ts_tojson serializes a time-series to JSON.
498 *
499 * The function never returns an error. Rather, an error message will be part
500 * of the serialized data.
501 */
502 static void
503 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
504 {
505 char start_str[64];
506 char end_str[64];
508 size_t i;
510 /* TODO: make time format configurable */
511 if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
512 snprintf(start_str, sizeof(start_str), "<error>");
513 start_str[sizeof(start_str) - 1] = '\0';
514 if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
515 snprintf(end_str, sizeof(end_str), "<error>");
516 end_str[sizeof(end_str) - 1] = '\0';
518 sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
519 start_str, end_str);
521 for (i = 0; i < ts->data_names_len; ++i) {
522 size_t j;
523 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
525 for (j = 0; j < ts->data_len; ++j) {
526 char time_str[64];
528 if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
529 snprintf(time_str, sizeof(time_str), "<error>");
530 time_str[sizeof(time_str) - 1] = '\0';
532 /* Some GNU libc versions may print '-nan' which we dont' want */
533 if (isnan(ts->data[i][j].value))
534 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
535 "\"value\": \"nan\"}", time_str);
536 else
537 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
538 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
540 if (j < ts->data_len - 1)
541 sdb_strbuf_append(buf, ",");
542 }
544 if (i < ts->data_names_len - 1)
545 sdb_strbuf_append(buf, "],");
546 else
547 sdb_strbuf_append(buf, "]");
548 }
549 sdb_strbuf_append(buf, "}}");
550 } /* ts_tojson */
552 /*
553 * store writer API
554 */
556 static int
557 store_attribute(const char *hostname,
558 const char *key, const sdb_data_t *value,
559 sdb_time_t last_update, sdb_object_t *user_data)
560 {
561 sdb_store_t *st = SDB_STORE(user_data);
563 host_t *host;
564 sdb_avltree_t *attrs;
565 int status = 0;
567 if ((! hostname) || (! key))
568 return -1;
570 pthread_rwlock_wrlock(&st->host_lock);
571 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
572 attrs = get_host_children(host, SDB_ATTRIBUTE);
573 if (! attrs) {
574 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
575 "host '%s' not found", key, hostname);
576 status = -1;
577 }
579 if (! status)
580 status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
582 sdb_object_deref(SDB_OBJ(host));
583 pthread_rwlock_unlock(&st->host_lock);
585 return status;
586 } /* store_attribute */
588 static int
589 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
590 {
591 sdb_store_t *st = SDB_STORE(user_data);
592 int status = 0;
594 if (! name)
595 return -1;
597 pthread_rwlock_wrlock(&st->host_lock);
598 status = store_obj(NULL, st->hosts,
599 SDB_HOST, name, last_update, NULL);
600 pthread_rwlock_unlock(&st->host_lock);
602 return status;
603 } /* store_host */
605 static int
606 store_service_attr(const char *hostname, const char *service,
607 const char *key, const sdb_data_t *value, sdb_time_t last_update,
608 sdb_object_t *user_data)
609 {
610 sdb_store_t *st = SDB_STORE(user_data);
612 host_t *host;
613 service_t *svc;
614 sdb_avltree_t *services;
615 int status = 0;
617 if ((! hostname) || (! service) || (! key))
618 return -1;
620 pthread_rwlock_wrlock(&st->host_lock);
621 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
622 services = get_host_children(host, SDB_SERVICE);
623 sdb_object_deref(SDB_OBJ(host));
624 if (! services) {
625 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
626 "for service '%s' - host '%ss' not found",
627 key, service, hostname);
628 pthread_rwlock_unlock(&st->host_lock);
629 return -1;
630 }
632 svc = SVC(sdb_avltree_lookup(services, service));
633 if (! svc) {
634 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
635 "service '%s/%s' not found", key, hostname, service);
636 status = -1;
637 }
639 if (! status)
640 status = store_attr(STORE_OBJ(svc), svc->attributes,
641 key, value, last_update);
643 sdb_object_deref(SDB_OBJ(svc));
644 pthread_rwlock_unlock(&st->host_lock);
646 return status;
647 } /* store_service_attr */
649 static int
650 store_service(const char *hostname, const char *name,
651 sdb_time_t last_update, sdb_object_t *user_data)
652 {
653 sdb_store_t *st = SDB_STORE(user_data);
655 host_t *host;
656 sdb_avltree_t *services;
658 int status = 0;
660 if ((! hostname) || (! name))
661 return -1;
663 pthread_rwlock_wrlock(&st->host_lock);
664 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
665 services = get_host_children(host, SDB_SERVICE);
666 if (! services) {
667 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
668 "host '%s' not found", name, hostname);
669 status = -1;
670 }
672 if (! status)
673 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
674 name, last_update, NULL);
676 sdb_object_deref(SDB_OBJ(host));
677 pthread_rwlock_unlock(&st->host_lock);
678 return status;
679 } /* store_service */
681 static int
682 store_metric_attr(const char *hostname, const char *metric,
683 const char *key, const sdb_data_t *value, sdb_time_t last_update,
684 sdb_object_t *user_data)
685 {
686 sdb_store_t *st = SDB_STORE(user_data);
688 sdb_avltree_t *metrics;
689 host_t *host;
690 sdb_metric_t *m;
691 int status = 0;
693 if ((! hostname) || (! metric) || (! key))
694 return -1;
696 pthread_rwlock_wrlock(&st->host_lock);
697 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
698 metrics = get_host_children(host, SDB_METRIC);
699 sdb_object_deref(SDB_OBJ(host));
700 if (! metrics) {
701 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
702 "for metric '%s' - host '%s' not found",
703 key, metric, hostname);
704 pthread_rwlock_unlock(&st->host_lock);
705 return -1;
706 }
708 m = METRIC(sdb_avltree_lookup(metrics, metric));
709 if (! m) {
710 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
711 "metric '%s/%s' not found", key, hostname, metric);
712 status = -1;
713 }
715 if (! status)
716 status = store_attr(STORE_OBJ(m), m->attributes,
717 key, value, last_update);
719 sdb_object_deref(SDB_OBJ(m));
720 pthread_rwlock_unlock(&st->host_lock);
722 return status;
723 } /* store_metric_attr */
725 static int
726 store_metric(const char *hostname, const char *name,
727 sdb_metric_store_t *store, sdb_time_t last_update,
728 sdb_object_t *user_data)
729 {
730 sdb_store_t *st = SDB_STORE(user_data);
732 sdb_store_obj_t *obj = NULL;
733 host_t *host;
734 sdb_metric_t *metric;
736 sdb_avltree_t *metrics;
738 int status = 0;
740 if ((! hostname) || (! name))
741 return -1;
743 if (store) {
744 if ((store->type != NULL) != (store->id != NULL))
745 return -1;
746 else if (! store->type)
747 store = NULL;
748 }
750 pthread_rwlock_wrlock(&st->host_lock);
751 host = HOST(sdb_avltree_lookup(st->hosts, hostname));
752 metrics = get_host_children(host, SDB_METRIC);
753 if (! metrics) {
754 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
755 "host '%s' not found", name, hostname);
756 status = -1;
757 }
759 if (! status)
760 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
761 name, last_update, &obj);
762 sdb_object_deref(SDB_OBJ(host));
764 if (status) {
765 pthread_rwlock_unlock(&st->host_lock);
766 return status;
767 }
769 assert(obj);
770 metric = METRIC(obj);
772 if (store)
773 if (store_metric_store(metric, store))
774 status = -1;
775 pthread_rwlock_unlock(&st->host_lock);
776 return status;
777 } /* store_metric */
779 sdb_store_writer_t sdb_store_writer = {
780 store_host, store_service, store_metric,
781 store_attribute, store_service_attr, store_metric_attr,
782 };
784 static sdb_object_t *
785 prepare_query(sdb_ast_node_t *ast,
786 sdb_strbuf_t __attribute__((unused)) *errbuf,
787 sdb_object_t __attribute__((unused)) *user_data)
788 {
789 return SDB_OBJ(sdb_store_query_prepare(ast));
790 } /* prepare_query */
792 static int
793 execute_query(sdb_object_t *q,
794 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
795 sdb_object_t *user_data)
796 {
797 return sdb_store_query_execute(SDB_STORE(user_data),
798 QUERY(q), buf, errbuf);
799 } /* execute_query */
801 sdb_store_reader_t sdb_store_reader = {
802 prepare_query, execute_query,
803 };
805 /*
806 * public API
807 */
809 sdb_store_t *
810 sdb_store_create(void)
811 {
812 return SDB_STORE(sdb_object_create("store", store_type));
813 } /* sdb_store_create */
815 int
816 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
817 {
818 return store_host(name, last_update, SDB_OBJ(store));
819 } /* sdb_store_host */
821 int
822 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
823 sdb_time_t last_update)
824 {
825 return store_service(hostname, name, last_update, SDB_OBJ(store));
826 } /* sdb_store_service */
828 int
829 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
830 sdb_metric_store_t *metric_store, sdb_time_t last_update)
831 {
832 return store_metric(hostname, name, metric_store, last_update, SDB_OBJ(store));
833 } /* sdb_store_metric */
835 int
836 sdb_store_attribute(sdb_store_t *store, const char *hostname,
837 const char *key, const sdb_data_t *value, sdb_time_t last_update)
838 {
839 return store_attribute(hostname, key, value, last_update, SDB_OBJ(store));
840 } /* sdb_store_attribute */
842 int
843 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
844 const char *service, const char *key, const sdb_data_t *value,
845 sdb_time_t last_update)
846 {
847 return store_service_attr(hostname, service, key, value,
848 last_update, SDB_OBJ(store));
849 } /* sdb_store_service_attr */
851 int
852 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
853 const char *metric, const char *key, const sdb_data_t *value,
854 sdb_time_t last_update)
855 {
856 return store_metric_attr(hostname, metric, key, value,
857 last_update, SDB_OBJ(store));
858 } /* sdb_store_metric_attr */
860 sdb_store_obj_t *
861 sdb_store_get_host(sdb_store_t *store, const char *name)
862 {
863 host_t *host;
865 if ((! store) || (! name))
866 return NULL;
868 host = HOST(sdb_avltree_lookup(store->hosts, name));
869 if (! host)
870 return NULL;
872 return STORE_OBJ(host);
873 } /* sdb_store_get_host */
875 sdb_store_obj_t *
876 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
877 {
878 sdb_avltree_t *children;
880 if ((! host) || (host->type != SDB_HOST) || (! name))
881 return NULL;
883 children = get_host_children(HOST(host), type);
884 if (! children)
885 return NULL;
886 return STORE_OBJ(sdb_avltree_lookup(children, name));
887 } /* sdb_store_get_child */
889 int
890 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
891 {
892 sdb_data_t tmp;
894 if (! obj)
895 return -1;
897 switch (field) {
898 case SDB_FIELD_NAME:
899 tmp.type = SDB_TYPE_STRING;
900 tmp.data.string = strdup(SDB_OBJ(obj)->name);
901 if (! tmp.data.string)
902 return -1;
903 break;
904 case SDB_FIELD_LAST_UPDATE:
905 tmp.type = SDB_TYPE_DATETIME;
906 tmp.data.datetime = obj->last_update;
907 break;
908 case SDB_FIELD_AGE:
909 tmp.type = SDB_TYPE_DATETIME;
910 tmp.data.datetime = sdb_gettime() - obj->last_update;
911 break;
912 case SDB_FIELD_INTERVAL:
913 tmp.type = SDB_TYPE_DATETIME;
914 tmp.data.datetime = obj->interval;
915 break;
916 case SDB_FIELD_BACKEND:
917 if (! res)
918 return 0;
919 tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
920 tmp.data.array.length = obj->backends_num;
921 tmp.data.array.values = obj->backends;
922 return sdb_data_copy(res, &tmp);
923 case SDB_FIELD_VALUE:
924 if (obj->type != SDB_ATTRIBUTE)
925 return -1;
926 if (! res)
927 return 0;
928 return sdb_data_copy(res, &ATTR(obj)->value);
929 case SDB_FIELD_TIMESERIES:
930 if (obj->type != SDB_METRIC)
931 return -1;
932 tmp.type = SDB_TYPE_BOOLEAN;
933 tmp.data.boolean = METRIC(obj)->store.type != NULL;
934 default:
935 return -1;
936 }
937 if (res)
938 *res = tmp;
939 else
940 sdb_data_free_datum(&tmp);
941 return 0;
942 } /* sdb_store_get_field */
944 int
945 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
946 sdb_store_matcher_t *filter)
947 {
948 sdb_avltree_t *tree = NULL;
949 sdb_store_obj_t *attr;
951 if ((! obj) || (! name))
952 return -1;
954 if (obj->type == SDB_HOST)
955 tree = HOST(obj)->attributes;
956 else if (obj->type == SDB_SERVICE)
957 tree = SVC(obj)->attributes;
958 else if (obj->type == SDB_METRIC)
959 tree = METRIC(obj)->attributes;
961 if (! tree)
962 return -1;
964 attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
965 if (! attr)
966 return -1;
967 if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
968 sdb_object_deref(SDB_OBJ(attr));
969 return -1;
970 }
972 assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
973 if (res)
974 sdb_data_copy(res, &ATTR(attr)->value);
975 sdb_object_deref(SDB_OBJ(attr));
976 return 0;
977 } /* sdb_store_get_attr */
979 /* TODO: sdb_store_fetch_timeseries should move into the plugin module */
981 int
982 sdb_store_fetch_timeseries(sdb_store_t *store,
983 const char *hostname, const char *metric,
984 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
985 {
986 sdb_avltree_t *metrics;
987 host_t *host;
988 sdb_metric_t *m;
990 sdb_timeseries_t *ts;
992 int status = 0;
994 if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
995 return -1;
997 pthread_rwlock_rdlock(&store->host_lock);
998 host = HOST(sdb_avltree_lookup(store->hosts, hostname));
999 metrics = get_host_children(host, SDB_METRIC);
1000 sdb_object_deref(SDB_OBJ(host));
1001 if (! metrics) {
1002 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1003 "- host '%s' not found", hostname, metric, hostname);
1004 pthread_rwlock_unlock(&store->host_lock);
1005 return -1;
1006 }
1008 m = METRIC(sdb_avltree_lookup(metrics, metric));
1009 if (! m) {
1010 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1011 "- metric '%s' not found", hostname, metric, metric);
1012 pthread_rwlock_unlock(&store->host_lock);
1013 return -1;
1014 }
1016 if ((! m->store.type) || (! m->store.id)) {
1017 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1018 "- no data-store configured for the stored metric",
1019 hostname, metric);
1020 sdb_object_deref(SDB_OBJ(m));
1021 pthread_rwlock_unlock(&store->host_lock);
1022 return -1;
1023 }
1025 {
1026 char type[strlen(m->store.type) + 1];
1027 char id[strlen(m->store.id) + 1];
1029 strncpy(type, m->store.type, sizeof(type));
1030 strncpy(id, m->store.id, sizeof(id));
1031 pthread_rwlock_unlock(&store->host_lock);
1033 ts = sdb_plugin_fetch_timeseries(type, id, opts);
1034 if (! ts) {
1035 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1036 "- %s fetcher callback returned no data for '%s'",
1037 hostname, metric, type, id);
1038 status = -1;
1039 }
1040 }
1042 ts_tojson(ts, buf);
1043 sdb_object_deref(SDB_OBJ(m));
1044 sdb_timeseries_destroy(ts);
1045 return status;
1046 } /* sdb_store_fetch_timeseries */
1048 int
1049 sdb_store_scan(sdb_store_t *store, int type,
1050 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1051 sdb_store_lookup_cb cb, void *user_data)
1052 {
1053 sdb_avltree_iter_t *host_iter = NULL;
1054 int status = 0;
1056 if ((! store) || (! cb))
1057 return -1;
1059 if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
1060 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
1061 return -1;
1062 }
1064 pthread_rwlock_rdlock(&store->host_lock);
1065 host_iter = sdb_avltree_get_iter(store->hosts);
1066 if (! host_iter)
1067 status = -1;
1069 /* has_next returns false if the iterator is NULL */
1070 while (sdb_avltree_iter_has_next(host_iter)) {
1071 sdb_store_obj_t *host;
1072 sdb_avltree_iter_t *iter = NULL;
1074 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1075 assert(host);
1077 if (! sdb_store_matcher_matches(filter, host, NULL))
1078 continue;
1080 if (type == SDB_SERVICE)
1081 iter = sdb_avltree_get_iter(HOST(host)->services);
1082 else if (type == SDB_METRIC)
1083 iter = sdb_avltree_get_iter(HOST(host)->metrics);
1085 if (iter) {
1086 while (sdb_avltree_iter_has_next(iter)) {
1087 sdb_store_obj_t *obj;
1088 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1089 assert(obj);
1091 if (sdb_store_matcher_matches(m, obj, filter)) {
1092 if (cb(obj, filter, user_data)) {
1093 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1094 "an error while scanning");
1095 status = -1;
1096 break;
1097 }
1098 }
1099 }
1100 }
1101 else if (sdb_store_matcher_matches(m, host, filter)) {
1102 if (cb(host, filter, user_data)) {
1103 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1104 "an error while scanning");
1105 status = -1;
1106 }
1107 }
1109 sdb_avltree_iter_destroy(iter);
1110 if (status)
1111 break;
1112 }
1114 sdb_avltree_iter_destroy(host_iter);
1115 pthread_rwlock_unlock(&store->host_lock);
1116 return status;
1117 } /* sdb_store_scan */
1119 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */