Code

store: Drop the global (default) 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 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)
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)
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)
137         sdb_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)
160         sdb_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)
176         sdb_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)
193         sdb_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)
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)
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)
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)
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(sdb_host_t),
274         /* init = */ host_init,
275         /* destroy = */ host_destroy
276 };
278 static sdb_type_t service_type = {
279         /* size = */ sizeof(sdb_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(sdb_attribute_t),
292         /* init = */ attr_init,
293         /* destroy = */ attr_destroy
294 };
296 /*
297  * private helper functions
298  */
300 static sdb_host_t *
301 lookup_host(sdb_store_t *st, const char *name, bool canonicalize)
303         sdb_host_t *host;
304         char *cname;
306         assert(name);
307         if (! canonicalize)
308                 return HOST(sdb_avltree_lookup(st->hosts, name));
310         cname = strdup(name);
311         cname = sdb_plugin_cname(cname);
312         if (! cname) {
313                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
314                 return NULL;
315         }
317         host = HOST(sdb_avltree_lookup(st->hosts, cname));
318         free(cname);
319         return host;
320 } /* lookup_host */
322 static int
323 record_backend(sdb_store_obj_t *obj)
325         const sdb_plugin_info_t *info;
326         char **tmp;
327         size_t i;
329         info = sdb_plugin_current();
330         if (! info)
331                 return 0;
333         for (i = 0; i < obj->backends_num; ++i)
334                 if (!strcasecmp(obj->backends[i], info->plugin_name))
335                         return 0;
337         tmp = realloc(obj->backends,
338                         (obj->backends_num + 1) * sizeof(*obj->backends));
339         if (! tmp)
340                 return -1;
342         obj->backends = tmp;
343         obj->backends[obj->backends_num] = strdup(info->plugin_name);
344         if (! obj->backends[obj->backends_num])
345                 return -1;
347         ++obj->backends_num;
348         return 0;
349 } /* record_backend */
351 static int
352 store_obj(sdb_store_obj_t *parent, sdb_avltree_t *parent_tree,
353                 int type, const char *name, sdb_time_t last_update,
354                 sdb_store_obj_t **updated_obj)
356         sdb_store_obj_t *old, *new;
357         int status = 0;
359         assert(parent_tree);
361         if (last_update <= 0)
362                 last_update = sdb_gettime();
364         old = STORE_OBJ(sdb_avltree_lookup(parent_tree, name));
365         if (old) {
366                 if (old->last_update > last_update) {
367                         sdb_log(SDB_LOG_DEBUG, "store: Cannot update %s '%s' - "
368                                         "value too old (%"PRIsdbTIME" < %"PRIsdbTIME")",
369                                         SDB_STORE_TYPE_TO_NAME(type), name,
370                                         last_update, old->last_update);
371                         /* don't report an error; the object may be updated by multiple
372                          * backends */
373                         status = 1;
374                 }
375                 else if (old->last_update == last_update) {
376                         /* don't report an error and also don't even log this to avoid
377                          * excessive noise on high sampling frequencies */
378                         status = 1;
379                 }
380                 else {
381                         sdb_time_t interval = last_update - old->last_update;
382                         old->last_update = last_update;
383                         if (interval) {
384                                 if (old->interval)
385                                         old->interval = (sdb_time_t)((0.9 * (double)old->interval)
386                                                         + (0.1 * (double)interval));
387                                 else
388                                         old->interval = interval;
389                         }
390                 }
392                 new = old;
393                 sdb_object_deref(SDB_OBJ(old));
394         }
395         else {
396                 if (type == SDB_ATTRIBUTE) {
397                         /* the value will be updated by the caller */
398                         new = STORE_OBJ(sdb_object_create(name, attribute_type,
399                                                 type, last_update, NULL));
400                 }
401                 else {
402                         sdb_type_t t;
403                         t = type == SDB_HOST
404                                 ? host_type
405                                 : type == SDB_SERVICE
406                                         ? service_type
407                                         : metric_type;
408                         new = STORE_OBJ(sdb_object_create(name, t, type, last_update));
409                 }
411                 if (new) {
412                         status = sdb_avltree_insert(parent_tree, SDB_OBJ(new));
414                         /* pass control to the tree or destroy in case of an error */
415                         sdb_object_deref(SDB_OBJ(new));
416                 }
417                 else {
418                         char errbuf[1024];
419                         sdb_log(SDB_LOG_ERR, "store: Failed to create %s '%s': %s",
420                                         SDB_STORE_TYPE_TO_NAME(type), name,
421                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
422                         status = -1;
423                 }
424         }
426         if (status < 0)
427                 return status;
428         assert(new);
430         if (new->parent != parent) {
431                 // Avoid circular self-references which are not handled
432                 // correctly by the ref-count based management layer.
433                 //sdb_object_deref(SDB_OBJ(new->parent));
434                 //sdb_object_ref(SDB_OBJ(parent));
435                 new->parent = parent;
436         }
438         if (updated_obj)
439                 *updated_obj = new;
441         if (record_backend(new))
442                 return -1;
443         return status;
444 } /* store_obj */
446 static int
447 store_attr(sdb_store_obj_t *parent, sdb_avltree_t *attributes,
448                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
450         sdb_store_obj_t *attr = NULL;
451         int status;
453         status = store_obj(parent, attributes, SDB_ATTRIBUTE,
454                         key, last_update, &attr);
455         if (status)
456                 return status;
458         /* don't update unchanged values */
459         if (! sdb_data_cmp(&ATTR(attr)->value, value))
460                 return status;
462         assert(attr);
463         if (sdb_data_copy(&ATTR(attr)->value, value))
464                 return -1;
465         return status;
466 } /* store_attr */
468 static int
469 store_metric_store(sdb_metric_t *metric, sdb_metric_store_t *store)
471         char *type = metric->store.type;
472         char *id = metric->store.id;
474         if ((! metric->store.type) || strcasecmp(metric->store.type, store->type)) {
475                 if (! (type = strdup(store->type)))
476                         return -1;
477         }
478         if ((! metric->store.id) || strcasecmp(metric->store.id, store->id)) {
479                 if (! (id = strdup(store->id))) {
480                         if (type != metric->store.type)
481                                 free(type);
482                         return -1;
483                 }
484         }
486         if (type != metric->store.type) {
487                 if (metric->store.type)
488                         free(metric->store.type);
489                 metric->store.type = type;
490         }
491         if (id != metric->store.id) {
492                 if (metric->store.id)
493                         free(metric->store.id);
494                 metric->store.id = id;
495         }
496         return 0;
497 } /* store_metric_store */
499 /* The store's host_lock has to be acquired before calling this function. */
500 static sdb_avltree_t *
501 get_host_children(sdb_host_t *host, int type)
503         if ((type != SDB_SERVICE) && (type != SDB_METRIC)
504                         && (type != SDB_ATTRIBUTE))
505                 return NULL;
507         if (! host)
508                 return NULL;
510         if (type == SDB_ATTRIBUTE)
511                 return host->attributes;
512         else if (type == SDB_METRIC)
513                 return host->metrics;
514         else
515                 return host->services;
516 } /* get_host_children */
518 /*
519  * ts_tojson serializes a time-series to JSON.
520  *
521  * The function never returns an error. Rather, an error message will be part
522  * of the serialized data.
523  */
524 static void
525 ts_tojson(sdb_timeseries_t *ts, sdb_strbuf_t *buf)
527         char start_str[64];
528         char end_str[64];
530         size_t i;
532         /* TODO: make time format configurable */
533         if (! sdb_strftime(start_str, sizeof(start_str), ts->start))
534                 snprintf(start_str, sizeof(start_str), "<error>");
535         start_str[sizeof(start_str) - 1] = '\0';
536         if (! sdb_strftime(end_str, sizeof(end_str), ts->end))
537                 snprintf(end_str, sizeof(end_str), "<error>");
538         end_str[sizeof(end_str) - 1] = '\0';
540         sdb_strbuf_append(buf, "{\"start\": \"%s\", \"end\": \"%s\", \"data\": {",
541                         start_str, end_str);
543         for (i = 0; i < ts->data_names_len; ++i) {
544                 size_t j;
545                 sdb_strbuf_append(buf, "\"%s\": [", ts->data_names[i]);
547                 for (j = 0; j < ts->data_len; ++j) {
548                         char time_str[64];
550                         if (! sdb_strftime(time_str, sizeof(time_str), ts->data[i][j].timestamp))
551                                 snprintf(time_str, sizeof(time_str), "<error>");
552                         time_str[sizeof(time_str) - 1] = '\0';
554                         /* Some GNU libc versions may print '-nan' which we dont' want */
555                         if (isnan(ts->data[i][j].value))
556                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
557                                                 "\"value\": \"nan\"}", time_str);
558                         else
559                                 sdb_strbuf_append(buf, "{\"timestamp\": \"%s\", "
560                                                 "\"value\": \"%f\"}", time_str, ts->data[i][j].value);
562                         if (j < ts->data_len - 1)
563                                 sdb_strbuf_append(buf, ",");
564                 }
566                 if (i < ts->data_names_len - 1)
567                         sdb_strbuf_append(buf, "],");
568                 else
569                         sdb_strbuf_append(buf, "]");
570         }
571         sdb_strbuf_append(buf, "}}");
572 } /* ts_tojson */
574 /*
575  * store writer API
576  */
578 static int
579 store_attribute(const char *hostname,
580                 const char *key, const sdb_data_t *value,
581                 sdb_time_t last_update, sdb_object_t *user_data)
583         sdb_store_t *st = SDB_STORE(user_data);
585         sdb_host_t *host;
586         sdb_avltree_t *attrs;
587         int status = 0;
589         if ((! hostname) || (! key))
590                 return -1;
592         pthread_rwlock_wrlock(&st->host_lock);
593         host = lookup_host(st, hostname, /* canonicalize = */ 1);
594         attrs = get_host_children(host, SDB_ATTRIBUTE);
595         if (! attrs) {
596                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
597                                 "host '%s' not found", key, hostname);
598                 status = -1;
599         }
601         if (! status)
602                 status = store_attr(STORE_OBJ(host), attrs, key, value, last_update);
604         sdb_object_deref(SDB_OBJ(host));
605         pthread_rwlock_unlock(&st->host_lock);
607         return status;
608 } /* store_attribute */
610 static int
611 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
613         sdb_store_t *st = SDB_STORE(user_data);
615         char *cname = NULL;
616         int status = 0;
618         if (! name)
619                 return -1;
621         cname = sdb_plugin_cname(strdup(name));
622         if (! cname) {
623                 sdb_log(SDB_LOG_ERR, "store: strdup failed");
624                 return -1;
625         }
627         pthread_rwlock_wrlock(&st->host_lock);
628         status = store_obj(NULL, st->hosts,
629                         SDB_HOST, cname, last_update, NULL);
630         pthread_rwlock_unlock(&st->host_lock);
632         free(cname);
633         return status;
634 } /* store_host */
636 static int
637 store_service_attr(const char *hostname, const char *service,
638                 const char *key, const sdb_data_t *value, sdb_time_t last_update,
639                 sdb_object_t *user_data)
641         sdb_store_t *st = SDB_STORE(user_data);
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(&st->host_lock);
652         host = lookup_host(st, 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(&st->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(&st->host_lock);
677         return status;
678 } /* store_service_attr */
680 static int
681 store_service(const char *hostname, const char *name,
682                 sdb_time_t last_update, sdb_object_t *user_data)
684         sdb_store_t *st = SDB_STORE(user_data);
686         sdb_host_t *host;
687         sdb_avltree_t *services;
688         sdb_data_t d;
690         int status = 0;
692         if ((! hostname) || (! name))
693                 return -1;
695         pthread_rwlock_wrlock(&st->host_lock);
696         host = lookup_host(st, hostname, /* canonicalize = */ 1);
697         services = get_host_children(host, SDB_SERVICE);
698         if (! services) {
699                 sdb_log(SDB_LOG_ERR, "store: Failed to store service '%s' - "
700                                 "host '%s' not found", name, hostname);
701                 status = -1;
702         }
704         if (! status)
705                 status = store_obj(STORE_OBJ(host), services, SDB_SERVICE,
706                                 name, last_update, NULL);
708         sdb_object_deref(SDB_OBJ(host));
709         pthread_rwlock_unlock(&st->host_lock);
711         if (status)
712                 return status;
714         /* record the hostname as an attribute */
715         d.type = SDB_TYPE_STRING;
716         d.data.string = SDB_OBJ(host)->name;
717         if (store_service_attr(hostname, name, "hostname", &d, last_update, user_data))
718                 status = -1;
719         return status;
720 } /* store_service */
722 static int
723 store_metric_attr(const char *hostname, const char *metric,
724                 const char *key, const sdb_data_t *value, sdb_time_t last_update,
725                 sdb_object_t *user_data)
727         sdb_store_t *st = SDB_STORE(user_data);
729         sdb_avltree_t *metrics;
730         sdb_host_t *host;
731         sdb_metric_t *m;
732         int status = 0;
734         if ((! hostname) || (! metric) || (! key))
735                 return -1;
737         pthread_rwlock_wrlock(&st->host_lock);
738         host = lookup_host(st, hostname, /* canonicalize = */ 1);
739         metrics = get_host_children(host, SDB_METRIC);
740         sdb_object_deref(SDB_OBJ(host));
741         if (! metrics) {
742                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' "
743                                 "for metric '%s' - host '%s' not found",
744                                 key, metric, hostname);
745                 pthread_rwlock_unlock(&st->host_lock);
746                 return -1;
747         }
749         m = METRIC(sdb_avltree_lookup(metrics, metric));
750         if (! m) {
751                 sdb_log(SDB_LOG_ERR, "store: Failed to store attribute '%s' - "
752                                 "metric '%s/%s' not found", key, hostname, metric);
753                 status = -1;
754         }
756         if (! status)
757                 status = store_attr(STORE_OBJ(m), m->attributes,
758                                 key, value, last_update);
760         sdb_object_deref(SDB_OBJ(m));
761         pthread_rwlock_unlock(&st->host_lock);
763         return status;
764 } /* store_metric_attr */
766 static int
767 store_metric(const char *hostname, const char *name,
768                 sdb_metric_store_t *store, sdb_time_t last_update,
769                 sdb_object_t *user_data)
771         sdb_store_t *st = SDB_STORE(user_data);
773         sdb_store_obj_t *obj = NULL;
774         sdb_host_t *host;
775         sdb_metric_t *metric;
776         sdb_data_t d;
778         sdb_avltree_t *metrics;
780         int status = 0;
782         if ((! hostname) || (! name))
783                 return -1;
785         if (store) {
786                 if ((store->type != NULL) != (store->id != NULL))
787                         return -1;
788                 else if (! store->type)
789                         store = NULL;
790         }
792         pthread_rwlock_wrlock(&st->host_lock);
793         host = lookup_host(st, hostname, /* canonicalize = */ 1);
794         metrics = get_host_children(host, SDB_METRIC);
795         if (! metrics) {
796                 sdb_log(SDB_LOG_ERR, "store: Failed to store metric '%s' - "
797                                 "host '%s' not found", name, hostname);
798                 status = -1;
799         }
801         if (! status)
802                 status = store_obj(STORE_OBJ(host), metrics, SDB_METRIC,
803                                 name, last_update, &obj);
804         sdb_object_deref(SDB_OBJ(host));
806         if (status) {
807                 pthread_rwlock_unlock(&st->host_lock);
808                 return status;
809         }
811         assert(obj);
812         metric = METRIC(obj);
814         if (store)
815                 if (store_metric_store(metric, store))
816                         status = -1;
817         pthread_rwlock_unlock(&st->host_lock);
819         /* record the hostname as an attribute */
820         d.type = SDB_TYPE_STRING;
821         d.data.string = SDB_OBJ(host)->name;
822         if (store_metric_attr(hostname, name, "hostname", &d, last_update, user_data))
823                 status = -1;
824         return status;
825 } /* store_metric */
827 sdb_store_writer_t sdb_store_writer = {
828         store_host, store_service, store_metric,
829         store_attribute, store_service_attr, store_metric_attr,
830 };
832 /*
833  * TODO: let prepare and execute accept a store object as their user_data
834  * object
835  */
837 static sdb_object_t *
838 prepare_query(sdb_ast_node_t *ast,
839                 sdb_strbuf_t __attribute__((unused)) *errbuf,
840                 sdb_object_t __attribute__((unused)) *user_data)
842         return SDB_OBJ(sdb_store_query_prepare(ast));
843 } /* prepare_query */
845 static int
846 execute_query(sdb_object_t *q,
847                 sdb_strbuf_t *buf, sdb_strbuf_t *errbuf,
848                 sdb_object_t *user_data)
850         return sdb_store_query_execute(SDB_STORE(user_data),
851                         QUERY(q), buf, errbuf);
852 } /* execute_query */
854 sdb_store_reader_t sdb_store_reader = {
855         prepare_query, execute_query,
856 };
858 /*
859  * public API
860  */
862 sdb_store_t *
863 sdb_store_create(void)
865         return SDB_STORE(sdb_object_create("store", store_type));
866 } /* sdb_store_create */
868 int
869 sdb_store_host(sdb_store_t *store, const char *name, sdb_time_t last_update)
871         return store_host(name, last_update, SDB_OBJ(store));
872 } /* sdb_store_host */
874 int
875 sdb_store_service(sdb_store_t *store, const char *hostname, const char *name,
876                 sdb_time_t last_update)
878         return store_service(hostname, name, last_update, SDB_OBJ(store));
879 } /* sdb_store_service */
881 int
882 sdb_store_metric(sdb_store_t *store, const char *hostname, const char *name,
883                 sdb_metric_store_t *metric_store, sdb_time_t last_update)
885         return store_metric(hostname, name, metric_store, last_update, SDB_OBJ(store));
886 } /* sdb_store_metric */
888 int
889 sdb_store_attribute(sdb_store_t *store, const char *hostname,
890                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
892         return store_attribute(hostname, key, value, last_update, SDB_OBJ(store));
893 } /* sdb_store_attribute */
895 int
896 sdb_store_service_attr(sdb_store_t *store, const char *hostname,
897                 const char *service, const char *key, const sdb_data_t *value,
898                 sdb_time_t last_update)
900         return store_service_attr(hostname, service, key, value,
901                         last_update, SDB_OBJ(store));
902 } /* sdb_store_service_attr */
904 int
905 sdb_store_metric_attr(sdb_store_t *store, const char *hostname,
906                 const char *metric, const char *key, const sdb_data_t *value,
907                 sdb_time_t last_update)
909         return store_metric_attr(hostname, metric, key, value,
910                         last_update, SDB_OBJ(store));
911 } /* sdb_store_metric_attr */
913 sdb_store_obj_t *
914 sdb_store_get_host(sdb_store_t *store, const char *name)
916         sdb_host_t *host;
918         if ((! store) || (! name))
919                 return NULL;
921         host = lookup_host(store, name, /* canonicalize = */ 0);
922         if (! host)
923                 return NULL;
925         return STORE_OBJ(host);
926 } /* sdb_store_get_host */
928 sdb_store_obj_t *
929 sdb_store_get_child(sdb_store_obj_t *host, int type, const char *name)
931         sdb_avltree_t *children;
933         if ((! host) || (host->type != SDB_HOST) || (! name))
934                 return NULL;
936         children = get_host_children(HOST(host), type);
937         if (! children)
938                 return NULL;
939         return STORE_OBJ(sdb_avltree_lookup(children, name));
940 } /* sdb_store_get_child */
942 int
943 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res)
945         sdb_data_t tmp;
947         if (! obj)
948                 return -1;
950         switch (field) {
951                 case SDB_FIELD_NAME:
952                         tmp.type = SDB_TYPE_STRING;
953                         tmp.data.string = strdup(SDB_OBJ(obj)->name);
954                         if (! tmp.data.string)
955                                 return -1;
956                         break;
957                 case SDB_FIELD_LAST_UPDATE:
958                         tmp.type = SDB_TYPE_DATETIME;
959                         tmp.data.datetime = obj->last_update;
960                         break;
961                 case SDB_FIELD_AGE:
962                         tmp.type = SDB_TYPE_DATETIME;
963                         tmp.data.datetime = sdb_gettime() - obj->last_update;
964                         break;
965                 case SDB_FIELD_INTERVAL:
966                         tmp.type = SDB_TYPE_DATETIME;
967                         tmp.data.datetime = obj->interval;
968                         break;
969                 case SDB_FIELD_BACKEND:
970                         if (! res)
971                                 return 0;
972                         tmp.type = SDB_TYPE_ARRAY | SDB_TYPE_STRING;
973                         tmp.data.array.length = obj->backends_num;
974                         tmp.data.array.values = obj->backends;
975                         return sdb_data_copy(res, &tmp);
976                 case SDB_FIELD_VALUE:
977                         if (obj->type != SDB_ATTRIBUTE)
978                                 return -1;
979                         if (! res)
980                                 return 0;
981                         return sdb_data_copy(res, &ATTR(obj)->value);
982                 case SDB_FIELD_TIMESERIES:
983                         if (obj->type != SDB_METRIC)
984                                 return -1;
985                         tmp.type = SDB_TYPE_BOOLEAN;
986                         tmp.data.boolean = METRIC(obj)->store.type != NULL;
987                 default:
988                         return -1;
989         }
990         if (res)
991                 *res = tmp;
992         else
993                 sdb_data_free_datum(&tmp);
994         return 0;
995 } /* sdb_store_get_field */
997 int
998 sdb_store_get_attr(sdb_store_obj_t *obj, const char *name, sdb_data_t *res,
999                 sdb_store_matcher_t *filter)
1001         sdb_avltree_t *tree = NULL;
1002         sdb_store_obj_t *attr;
1004         if ((! obj) || (! name))
1005                 return -1;
1007         if (obj->type == SDB_HOST)
1008                 tree = HOST(obj)->attributes;
1009         else if (obj->type == SDB_SERVICE)
1010                 tree = SVC(obj)->attributes;
1011         else if (obj->type == SDB_METRIC)
1012                 tree = METRIC(obj)->attributes;
1014         if (! tree)
1015                 return -1;
1017         attr = STORE_OBJ(sdb_avltree_lookup(tree, name));
1018         if (! attr)
1019                 return -1;
1020         if (filter && (! sdb_store_matcher_matches(filter, attr, NULL))) {
1021                 sdb_object_deref(SDB_OBJ(attr));
1022                 return -1;
1023         }
1025         assert(STORE_OBJ(attr)->type == SDB_ATTRIBUTE);
1026         if (res)
1027                 sdb_data_copy(res, &ATTR(attr)->value);
1028         sdb_object_deref(SDB_OBJ(attr));
1029         return 0;
1030 } /* sdb_store_get_attr */
1032 /* TODO: sdb_store_fetch_timeseries should move into the plugin module */
1034 int
1035 sdb_store_fetch_timeseries(sdb_store_t *store,
1036                 const char *hostname, const char *metric,
1037                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf)
1039         sdb_avltree_t *metrics;
1040         sdb_host_t *host;
1041         sdb_metric_t *m;
1043         sdb_timeseries_t *ts;
1045         int status = 0;
1047         if ((! store) || (! hostname) || (! metric) || (! opts) || (! buf))
1048                 return -1;
1050         pthread_rwlock_rdlock(&store->host_lock);
1051         host = lookup_host(store, hostname, /* canonicalize = */ 1);
1052         metrics = get_host_children(host, SDB_METRIC);
1053         sdb_object_deref(SDB_OBJ(host));
1054         if (! metrics) {
1055                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1056                                 "- host '%s' not found", hostname, metric, hostname);
1057                 pthread_rwlock_unlock(&store->host_lock);
1058                 return -1;
1059         }
1061         m = METRIC(sdb_avltree_lookup(metrics, metric));
1062         if (! m) {
1063                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1064                                 "- metric '%s' not found", hostname, metric, metric);
1065                 pthread_rwlock_unlock(&store->host_lock);
1066                 return -1;
1067         }
1069         if ((! m->store.type) || (! m->store.id)) {
1070                 sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1071                                 "- no data-store configured for the stored metric",
1072                                 hostname, metric);
1073                 sdb_object_deref(SDB_OBJ(m));
1074                 pthread_rwlock_unlock(&store->host_lock);
1075                 return -1;
1076         }
1078         {
1079                 char type[strlen(m->store.type) + 1];
1080                 char id[strlen(m->store.id) + 1];
1082                 strncpy(type, m->store.type, sizeof(type));
1083                 strncpy(id, m->store.id, sizeof(id));
1084                 pthread_rwlock_unlock(&store->host_lock);
1086                 ts = sdb_plugin_fetch_timeseries(type, id, opts);
1087                 if (! ts) {
1088                         sdb_log(SDB_LOG_ERR, "store: Failed to fetch time-series '%s/%s' "
1089                                         "- %s fetcher callback returned no data for '%s'",
1090                                         hostname, metric, type, id);
1091                         status = -1;
1092                 }
1093         }
1095         ts_tojson(ts, buf);
1096         sdb_object_deref(SDB_OBJ(m));
1097         sdb_timeseries_destroy(ts);
1098         return status;
1099 } /* sdb_store_fetch_timeseries */
1101 int
1102 sdb_store_scan(sdb_store_t *store, int type,
1103                 sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
1104                 sdb_store_lookup_cb cb, void *user_data)
1106         sdb_avltree_iter_t *host_iter = NULL;
1107         int status = 0;
1109         if ((! store) || (! cb))
1110                 return -1;
1112         if ((type != SDB_HOST) && (type != SDB_SERVICE) && (type != SDB_METRIC)) {
1113                 sdb_log(SDB_LOG_ERR, "store: Cannot scan objects of type %d", type);
1114                 return -1;
1115         }
1117         pthread_rwlock_rdlock(&store->host_lock);
1118         host_iter = sdb_avltree_get_iter(store->hosts);
1119         if (! host_iter)
1120                 status = -1;
1122         /* has_next returns false if the iterator is NULL */
1123         while (sdb_avltree_iter_has_next(host_iter)) {
1124                 sdb_store_obj_t *host;
1125                 sdb_avltree_iter_t *iter = NULL;
1127                 host = STORE_OBJ(sdb_avltree_iter_get_next(host_iter));
1128                 assert(host);
1130                 if (! sdb_store_matcher_matches(filter, host, NULL))
1131                         continue;
1133                 if (type == SDB_SERVICE)
1134                         iter = sdb_avltree_get_iter(HOST(host)->services);
1135                 else if (type == SDB_METRIC)
1136                         iter = sdb_avltree_get_iter(HOST(host)->metrics);
1138                 if (iter) {
1139                         while (sdb_avltree_iter_has_next(iter)) {
1140                                 sdb_store_obj_t *obj;
1141                                 obj = STORE_OBJ(sdb_avltree_iter_get_next(iter));
1142                                 assert(obj);
1144                                 if (sdb_store_matcher_matches(m, obj, filter)) {
1145                                         if (cb(obj, filter, user_data)) {
1146                                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1147                                                                 "an error while scanning");
1148                                                 status = -1;
1149                                                 break;
1150                                         }
1151                                 }
1152                         }
1153                 }
1154                 else if (sdb_store_matcher_matches(m, host, filter)) {
1155                         if (cb(host, filter, user_data)) {
1156                                 sdb_log(SDB_LOG_ERR, "store: Callback returned "
1157                                                 "an error while scanning");
1158                                 status = -1;
1159                         }
1160                 }
1162                 sdb_avltree_iter_destroy(iter);
1163                 if (status)
1164                         break;
1165         }
1167         sdb_avltree_iter_destroy(host_iter);
1168         pthread_rwlock_unlock(&store->host_lock);
1169         return status;
1170 } /* sdb_store_scan */
1172 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */