Code

store: Use full initializers for store objects.
[sysdb.git] / src / core / plugin.c
1 /*
2  * SysDB - src/core/plugin.c
3  * Copyright (C) 2012 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/plugin.h"
34 #include "core/time.h"
35 #include "utils/error.h"
36 #include "utils/llist.h"
37 #include "utils/strbuf.h"
39 #include <assert.h>
41 #include <errno.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <strings.h>
47 #include <unistd.h>
49 #include <ltdl.h>
51 #include <pthread.h>
53 /* helper to access info attributes */
54 #define INFO_GET(i, attr) \
55         ((i)->attr ? (i)->attr : #attr" not set")
57 /*
58  * private data types
59  */
61 typedef struct {
62         sdb_object_t super;
63         sdb_plugin_ctx_t public;
65         sdb_plugin_info_t info;
66         lt_dlhandle handle;
68         /* The usage count differs from the object's ref count
69          * in that it provides higher level information about how
70          * the plugin is in use. */
71         size_t use_cnt;
72 } ctx_t;
73 #define CTX_INIT { SDB_OBJECT_INIT, \
74         SDB_PLUGIN_CTX_INIT, SDB_PLUGIN_INFO_INIT, NULL, 0 }
76 #define CTX(obj) ((ctx_t *)(obj))
78 typedef struct {
79         sdb_object_t super;
80         void *cb_callback;
81         sdb_object_t *cb_user_data;
82         ctx_t *cb_ctx;
83 } callback_t;
84 #define CB_INIT { SDB_OBJECT_INIT, \
85         /* callback = */ NULL, /* user_data = */ NULL, \
86         SDB_PLUGIN_CTX_INIT }
87 #define CB(obj) ((callback_t *)(obj))
88 #define CONST_CB(obj) ((const callback_t *)(obj))
90 typedef struct {
91         callback_t super;
92 #define ccb_callback super.cb_callback
93 #define ccb_user_data super.cb_user_data
94 #define ccb_ctx super.cb_ctx
95         sdb_time_t ccb_interval;
96         sdb_time_t ccb_next_update;
97 } collector_t;
98 #define CCB(obj) ((collector_t *)(obj))
99 #define CONST_CCB(obj) ((const collector_t *)(obj))
101 typedef struct {
102         callback_t super; /* cb_callback will always be NULL */
103 #define w_user_data super.cb_user_data
104 #define w_ctx super.cb_ctx
105         sdb_store_writer_t impl;
106 } writer_t;
107 #define WRITER(obj) ((writer_t *)(obj))
109 typedef struct {
110         callback_t super; /* cb_callback will always be NULL */
111 #define r_user_data super.cb_user_data
112 #define r_ctx super.cb_ctx
113         sdb_store_reader_t impl;
114 } reader_t;
115 #define READER(obj) ((reader_t *)(obj))
117 /*
118  * private variables
119  */
121 static sdb_plugin_ctx_t  plugin_default_ctx  = SDB_PLUGIN_CTX_INIT;
122 static sdb_plugin_info_t plugin_default_info = SDB_PLUGIN_INFO_INIT;
124 static pthread_key_t     plugin_ctx_key;
125 static bool              plugin_ctx_key_initialized = 0;
127 /* a list of the plugin contexts of all registered plugins */
128 static sdb_llist_t      *all_plugins = NULL;
130 static sdb_llist_t      *config_list = NULL;
131 static sdb_llist_t      *init_list = NULL;
132 static sdb_llist_t      *collector_list = NULL;
133 static sdb_llist_t      *cname_list = NULL;
134 static sdb_llist_t      *shutdown_list = NULL;
135 static sdb_llist_t      *log_list = NULL;
136 static sdb_llist_t      *ts_fetcher_list = NULL;
137 static sdb_llist_t      *writer_list = NULL;
138 static sdb_llist_t      *reader_list = NULL;
140 static struct {
141         const char   *type;
142         sdb_llist_t **list;
143 } all_lists[] = {
144         { "config",             &config_list },
145         { "init",               &init_list },
146         { "collector",          &collector_list },
147         { "cname",              &cname_list },
148         { "shutdown",           &shutdown_list },
149         { "log",                &log_list },
150         { "timeseries fetcher", &ts_fetcher_list },
151         { "store writer",       &writer_list },
152         { "store reader",       &reader_list },
153 };
155 /*
156  * private helper functions
157  */
159 static void
160 plugin_info_clear(sdb_plugin_info_t *info)
162         sdb_plugin_info_t empty_info = SDB_PLUGIN_INFO_INIT;
163         if (! info)
164                 return;
166         if (info->plugin_name)
167                 free(info->plugin_name);
168         if (info->filename)
169                 free(info->filename);
171         if (info->description)
172                 free(info->description);
173         if (info->copyright)
174                 free(info->copyright);
175         if (info->license)
176                 free(info->license);
178         *info = empty_info;
179 } /* plugin_info_clear */
181 static void
182 ctx_key_init(void)
184         if (plugin_ctx_key_initialized)
185                 return;
187         pthread_key_create(&plugin_ctx_key, /* destructor */ NULL);
188         plugin_ctx_key_initialized = 1;
189 } /* ctx_key_init */
191 static int
192 plugin_cmp_next_update(const sdb_object_t *a, const sdb_object_t *b)
194         const collector_t *ccb1 = (const collector_t *)a;
195         const collector_t *ccb2 = (const collector_t *)b;
197         assert(ccb1 && ccb2);
199         return (ccb1->ccb_next_update > ccb2->ccb_next_update)
200                 ? 1 : (ccb1->ccb_next_update < ccb2->ccb_next_update)
201                 ? -1 : 0;
202 } /* plugin_cmp_next_update */
204 static int
205 plugin_lookup_by_name(const sdb_object_t *obj, const void *id)
207         const callback_t *cb = CONST_CB(obj);
208         const char *name = id;
210         assert(cb && id);
212         /* when a plugin was registered from outside a plugin (e.g. the core),
213          * we don't have a plugin context */
214         if (! cb->cb_ctx)
215                 return 1;
217         if (!strcasecmp(cb->cb_ctx->info.plugin_name, name))
218                 return 0;
219         return 1;
220 } /* plugin_lookup_by_name */
222 /* since this function is called from sdb_plugin_reconfigure_finish()
223  * when iterating through all_plugins, we may not do any additional
224  * modifications to all_plugins except for the optional removal */
225 static void
226 plugin_unregister_by_name(const char *plugin_name)
228         sdb_object_t *obj;
229         size_t i;
231         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
232                 const char  *type =  all_lists[i].type;
233                 sdb_llist_t *list = *all_lists[i].list;
235                 while (1) {
236                         callback_t *cb;
238                         cb = CB(sdb_llist_remove(list,
239                                                 plugin_lookup_by_name, plugin_name));
240                         if (! cb)
241                                 break;
243                         assert(cb->cb_ctx);
245                         sdb_log(SDB_LOG_INFO, "core: Unregistering "
246                                         "%s callback '%s' (module %s)", type, cb->super.name,
247                                         cb->cb_ctx->info.plugin_name);
248                         sdb_object_deref(SDB_OBJ(cb));
249                 }
250         }
252         obj = sdb_llist_search_by_name(all_plugins, plugin_name);
253         /* when called from sdb_plugin_reconfigure_finish, the object has already
254          * been removed from the list */
255         if (obj && (obj->ref_cnt <= 1)) {
256                 sdb_llist_remove_by_name(all_plugins, plugin_name);
257                 sdb_object_deref(obj);
258         }
259         /* else: other callbacks still reference it */
260 } /* plugin_unregister_by_name */
262 /*
263  * private types
264  */
266 static int
267 ctx_init(sdb_object_t *obj, va_list __attribute__((unused)) ap)
269         ctx_t *ctx = CTX(obj);
271         assert(ctx);
273         ctx->public = plugin_default_ctx;
274         ctx->info = plugin_default_info;
275         ctx->handle = NULL;
276         ctx->use_cnt = 1;
277         return 0;
278 } /* ctx_init */
280 static void
281 ctx_destroy(sdb_object_t *obj)
283         ctx_t *ctx = CTX(obj);
285         if (ctx->handle) {
286                 const char *err;
288                 sdb_log(SDB_LOG_INFO, "core: Unloading module %s",
289                                 ctx->info.plugin_name);
291                 lt_dlerror();
292                 lt_dlclose(ctx->handle);
293                 if ((err = lt_dlerror()))
294                         sdb_log(SDB_LOG_WARNING, "core: Failed to unload module %s: %s",
295                                         ctx->info.plugin_name, err);
296         }
298         plugin_info_clear(&ctx->info);
299 } /* ctx_destroy */
301 static sdb_type_t ctx_type = {
302         sizeof(ctx_t),
304         ctx_init,
305         ctx_destroy
306 };
308 static ctx_t *
309 ctx_get(void)
311         if (! plugin_ctx_key_initialized)
312                 ctx_key_init();
313         return pthread_getspecific(plugin_ctx_key);
314 } /* ctx_get */
316 static ctx_t *
317 ctx_set(ctx_t *new)
319         ctx_t *old;
321         if (! plugin_ctx_key_initialized)
322                 ctx_key_init();
324         old = pthread_getspecific(plugin_ctx_key);
325         if (old)
326                 sdb_object_deref(SDB_OBJ(old));
327         if (new)
328                 sdb_object_ref(SDB_OBJ(new));
329         pthread_setspecific(plugin_ctx_key, new);
330         return old;
331 } /* ctx_set */
333 static ctx_t *
334 ctx_create(const char *name)
336         ctx_t *ctx;
338         ctx = CTX(sdb_object_create(name, ctx_type));
339         if (! ctx)
340                 return NULL;
342         if (! plugin_ctx_key_initialized)
343                 ctx_key_init();
344         ctx_set(ctx);
345         return ctx;
346 } /* ctx_create */
348 static int
349 plugin_cb_init(sdb_object_t *obj, va_list ap)
351         sdb_llist_t **list = va_arg(ap, sdb_llist_t **);
352         const char   *type = va_arg(ap, const char *);
353         void     *callback = va_arg(ap, void *);
354         sdb_object_t   *ud = va_arg(ap, sdb_object_t *);
356         assert(list);
357         assert(type);
358         assert(obj);
360         if (sdb_llist_search_by_name(*list, obj->name)) {
361                 sdb_log(SDB_LOG_WARNING, "core: %s callback '%s' "
362                                 "has already been registered. Ignoring newly "
363                                 "registered version.", type, obj->name);
364                 return -1;
365         }
367         /* cb_ctx may be NULL if the plugin was not registered by a plugin */
369         CB(obj)->cb_callback = callback;
370         CB(obj)->cb_ctx      = ctx_get();
371         sdb_object_ref(SDB_OBJ(CB(obj)->cb_ctx));
373         sdb_object_ref(ud);
374         CB(obj)->cb_user_data = ud;
375         return 0;
376 } /* plugin_cb_init */
378 static void
379 plugin_cb_destroy(sdb_object_t *obj)
381         assert(obj);
382         sdb_object_deref(CB(obj)->cb_user_data);
383         sdb_object_deref(SDB_OBJ(CB(obj)->cb_ctx));
384 } /* plugin_cb_destroy */
386 static sdb_type_t callback_type = {
387         sizeof(callback_t),
389         plugin_cb_init,
390         plugin_cb_destroy
391 };
393 static sdb_type_t collector_type = {
394         sizeof(collector_t),
396         plugin_cb_init,
397         plugin_cb_destroy
398 };
400 static int
401 plugin_writer_init(sdb_object_t *obj, va_list ap)
403         sdb_store_writer_t *impl = va_arg(ap, sdb_store_writer_t *);
404         sdb_object_t       *ud   = va_arg(ap, sdb_object_t *);
406         assert(impl);
408         if ((! impl->store_host) || (! impl->store_service)
409                         || (! impl->store_metric) || (! impl->store_attribute)) {
410                 sdb_log(SDB_LOG_ERR, "core: store writer callback '%s' "
411                                 "does not fully implement the writer interface.",
412                                 obj->name);
413                 return -1;
414         }
415         if (sdb_llist_search_by_name(writer_list, obj->name)) {
416                 sdb_log(SDB_LOG_WARNING, "core: store writer callback '%s' "
417                                 "has already been registered. Ignoring newly "
418                                 "registered version.", obj->name);
419                 return -1;
420         }
422         /* ctx may be NULL if the callback was not registered by a plugin */
424         WRITER(obj)->impl = *impl;
425         WRITER(obj)->w_ctx  = ctx_get();
426         sdb_object_ref(SDB_OBJ(WRITER(obj)->w_ctx));
428         sdb_object_ref(ud);
429         WRITER(obj)->w_user_data = ud;
430         return 0;
431 } /* plugin_writer_init */
433 static void
434 plugin_writer_destroy(sdb_object_t *obj)
436         assert(obj);
437         sdb_object_deref(WRITER(obj)->w_user_data);
438         sdb_object_deref(SDB_OBJ(WRITER(obj)->w_ctx));
439 } /* plugin_writer_destroy */
441 static sdb_type_t writer_type = {
442         sizeof(writer_t),
444         plugin_writer_init,
445         plugin_writer_destroy
446 };
448 static int
449 plugin_reader_init(sdb_object_t *obj, va_list ap)
451         sdb_store_reader_t *impl = va_arg(ap, sdb_store_reader_t *);
452         sdb_object_t       *ud   = va_arg(ap, sdb_object_t *);
454         assert(impl);
456         if ((! impl->prepare_query) || (! impl->execute_query)) {
457                 sdb_log(SDB_LOG_ERR, "core: store reader callback '%s' "
458                                 "does not fully implement the reader interface.",
459                                 obj->name);
460                 return -1;
461         }
462         if (sdb_llist_search_by_name(reader_list, obj->name)) {
463                 sdb_log(SDB_LOG_WARNING, "core: store reader callback '%s' "
464                                 "has already been registered. Ignoring newly "
465                                 "registered version.", obj->name);
466                 return -1;
467         }
469         /* ctx may be NULL if the callback was not registered by a plugin */
471         READER(obj)->impl = *impl;
472         READER(obj)->r_ctx  = ctx_get();
473         sdb_object_ref(SDB_OBJ(READER(obj)->r_ctx));
475         sdb_object_ref(ud);
476         READER(obj)->r_user_data = ud;
477         return 0;
478 } /* plugin_reader_init */
480 static void
481 plugin_reader_destroy(sdb_object_t *obj)
483         assert(obj);
484         sdb_object_deref(READER(obj)->r_user_data);
485         sdb_object_deref(SDB_OBJ(READER(obj)->r_ctx));
486 } /* plugin_reader_destroy */
488 static sdb_type_t reader_type = {
489         sizeof(reader_t),
491         plugin_reader_init,
492         plugin_reader_destroy
493 };
495 static int
496 module_init(const char *name, lt_dlhandle lh, sdb_plugin_info_t *info)
498         int (*mod_init)(sdb_plugin_info_t *);
499         int status;
501         mod_init = (int (*)(sdb_plugin_info_t *))lt_dlsym(lh, "sdb_module_init");
502         if (! mod_init) {
503                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
504                                 "could not find symbol 'sdb_module_init'", name);
505                 return -1;
506         }
508         status = mod_init(info);
509         if (status) {
510                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize "
511                                 "module '%s'", name);
512                 plugin_unregister_by_name(name);
513                 return -1;
514         }
515         return 0;
516 } /* module_init */
518 static int
519 module_load(const char *basedir, const char *name,
520                 const sdb_plugin_ctx_t *plugin_ctx)
522         char  base_name[name ? strlen(name) + 1 : 1];
523         const char *name_ptr;
524         char *tmp;
526         char filename[1024];
527         lt_dlhandle lh;
529         ctx_t *ctx;
531         int status;
533         assert(name);
535         base_name[0] = '\0';
536         name_ptr = name;
538         while ((tmp = strstr(name_ptr, "::"))) {
539                 strncat(base_name, name_ptr, (size_t)(tmp - name_ptr));
540                 strcat(base_name, "/");
541                 name_ptr = tmp + strlen("::");
542         }
543         strcat(base_name, name_ptr);
545         if (! basedir)
546                 basedir = PKGLIBDIR;
548         snprintf(filename, sizeof(filename), "%s/%s.so", basedir, base_name);
549         filename[sizeof(filename) - 1] = '\0';
551         if (access(filename, R_OK)) {
552                 char errbuf[1024];
553                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s' (%s): %s",
554                                 name, filename, sdb_strerror(errno, errbuf, sizeof(errbuf)));
555                 return -1;
556         }
558         lt_dlinit();
559         lt_dlerror();
561         lh = lt_dlopen(filename);
562         if (! lh) {
563                 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': %s"
564                                 "The most common cause for this problem are missing "
565                                 "dependencies.\n", name, lt_dlerror());
566                 return -1;
567         }
569         if (ctx_get())
570                 sdb_log(SDB_LOG_WARNING, "core: Discarding old plugin context");
572         ctx = ctx_create(name);
573         if (! ctx) {
574                 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin context");
575                 return -1;
576         }
578         ctx->info.plugin_name = strdup(name);
579         ctx->info.filename = strdup(filename);
580         ctx->handle = lh;
582         if (plugin_ctx)
583                 ctx->public = *plugin_ctx;
585         if ((status = module_init(name, lh, &ctx->info))) {
586                 sdb_object_deref(SDB_OBJ(ctx));
587                 return status;
588         }
590         /* compare minor version */
591         if ((ctx->info.version < 0)
592                         || ((int)(ctx->info.version / 100) != (int)(SDB_VERSION / 100)))
593                 sdb_log(SDB_LOG_WARNING, "core: WARNING: version of "
594                                 "plugin '%s' (%i.%i.%i) does not match our version "
595                                 "(%i.%i.%i); this might cause problems",
596                                 name, SDB_VERSION_DECODE(ctx->info.version),
597                                 SDB_VERSION_DECODE(SDB_VERSION));
599         sdb_llist_append(all_plugins, SDB_OBJ(ctx));
601         sdb_log(SDB_LOG_INFO, "core: Successfully loaded "
602                         "plugin %s v%i (%s)", ctx->info.plugin_name,
603                         ctx->info.plugin_version,
604                         INFO_GET(&ctx->info, description));
605         sdb_log(SDB_LOG_INFO, "core: Plugin %s: %s, License: %s",
606                         ctx->info.plugin_name,
607                         INFO_GET(&ctx->info, copyright),
608                         INFO_GET(&ctx->info, license));
610         /* any registered callbacks took ownership of the context */
611         sdb_object_deref(SDB_OBJ(ctx));
613         /* reset */
614         ctx_set(NULL);
615         return 0;
616 } /* module_load */
618 static char *
619 plugin_get_name(const char *name, char *buf, size_t bufsize)
621         ctx_t *ctx = ctx_get();
623         if (ctx)
624                 snprintf(buf, bufsize, "%s::%s", ctx->info.plugin_name, name);
625         else
626                 snprintf(buf, bufsize, "core::%s", name);
627         return buf;
628 } /* plugin_get_name */
630 static int
631 plugin_add_callback(sdb_llist_t **list, const char *type,
632                 const char *name, void *callback, sdb_object_t *user_data)
634         sdb_object_t *obj;
636         if ((! name) || (! callback))
637                 return -1;
639         assert(list);
641         if (! *list)
642                 *list = sdb_llist_create();
643         if (! *list)
644                 return -1;
646         obj = sdb_object_create(name, callback_type,
647                         list, type, callback, user_data);
648         if (! obj)
649                 return -1;
651         if (sdb_llist_append(*list, obj)) {
652                 sdb_object_deref(obj);
653                 return -1;
654         }
656         /* pass control to the list */
657         sdb_object_deref(obj);
659         sdb_log(SDB_LOG_INFO, "core: Registered %s callback '%s'.",
660                         type, name);
661         return 0;
662 } /* plugin_add_callback */
664 static void
665 get_backend(char **backends, size_t *backends_num)
667         const sdb_plugin_info_t *info;
669         info = sdb_plugin_current();
670         if ((! info) || (! info->plugin_name) || (! *info->plugin_name)) {
671                 *backends_num = 0;
672                 return;
673         }
675         backends[0] = info->plugin_name;
676         *backends_num = 1;
677 } /* get_backend */
679 /*
680  * public API
681  */
683 int
684 sdb_plugin_load(const char *basedir, const char *name,
685                 const sdb_plugin_ctx_t *plugin_ctx)
687         ctx_t *ctx;
689         int status;
691         if ((! name) || (! *name))
692                 return -1;
694         if (! all_plugins) {
695                 if (! (all_plugins = sdb_llist_create())) {
696                         sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
697                                         "internal error while creating linked list", name);
698                         return -1;
699                 }
700         }
702         ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
703         if (ctx) {
704                 /* plugin already loaded */
705                 if (! ctx->use_cnt) {
706                         /* reloading plugin */
707                         ctx_t *old_ctx = ctx_set(ctx);
709                         status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
710                         if (status)
711                                 return status;
713                         sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
714                                         "'%s' (%s)", ctx->info.plugin_name,
715                                         INFO_GET(&ctx->info, description));
716                         ctx_set(old_ctx);
717                 }
718                 ++ctx->use_cnt;
719                 return 0;
720         }
722         return module_load(basedir, name, plugin_ctx);
723 } /* sdb_plugin_load */
725 int
726 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
728         va_list ap;
730         if (! info)
731                 return -1;
733         va_start(ap, type);
735         switch (type) {
736                 case SDB_PLUGIN_INFO_DESC:
737                         {
738                                 char *desc = va_arg(ap, char *);
739                                 if (desc) {
740                                         if (info->description)
741                                                 free(info->description);
742                                         info->description = strdup(desc);
743                                 }
744                         }
745                         break;
746                 case SDB_PLUGIN_INFO_COPYRIGHT:
747                         {
748                                 char *copyright = va_arg(ap, char *);
749                                 if (copyright)
750                                         info->copyright = strdup(copyright);
751                         }
752                         break;
753                 case SDB_PLUGIN_INFO_LICENSE:
754                         {
755                                 char *license = va_arg(ap, char *);
756                                 if (license) {
757                                         if (info->license)
758                                                 free(info->license);
759                                         info->license = strdup(license);
760                                 }
761                         }
762                         break;
763                 case SDB_PLUGIN_INFO_VERSION:
764                         {
765                                 int version = va_arg(ap, int);
766                                 info->version = version;
767                         }
768                         break;
769                 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
770                         {
771                                 int version = va_arg(ap, int);
772                                 info->plugin_version = version;
773                         }
774                         break;
775                 default:
776                         va_end(ap);
777                         return -1;
778         }
780         va_end(ap);
781         return 0;
782 } /* sdb_plugin_set_info */
784 int
785 sdb_plugin_register_config(sdb_plugin_config_cb callback)
787         ctx_t *ctx = ctx_get();
789         if (! ctx) {
790                 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
791                                 "config callback from outside a plugin");
792                 return -1;
793         }
794         return plugin_add_callback(&config_list, "config", ctx->info.plugin_name,
795                         (void *)callback, NULL);
796 } /* sdb_plugin_register_config */
798 int
799 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
800                 sdb_object_t *user_data)
802         char cb_name[1024];
803         return plugin_add_callback(&init_list, "init",
804                         plugin_get_name(name, cb_name, sizeof(cb_name)),
805                         (void *)callback, user_data);
806 } /* sdb_plugin_register_init */
808 int
809 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
810                 sdb_object_t *user_data)
812         char cb_name[1024];
813         return plugin_add_callback(&shutdown_list, "shutdown",
814                         plugin_get_name(name, cb_name, sizeof(cb_name)),
815                         (void *)callback, user_data);
816 } /* sdb_plugin_register_shutdown */
818 int
819 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
820                 sdb_object_t *user_data)
822         char cb_name[1024];
823         return plugin_add_callback(&log_list, "log",
824                         plugin_get_name(name, cb_name, sizeof(cb_name)),
825                         callback, user_data);
826 } /* sdb_plugin_register_log */
828 int
829 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
830                 sdb_object_t *user_data)
832         char cb_name[1024];
833         return plugin_add_callback(&cname_list, "cname",
834                         plugin_get_name(name, cb_name, sizeof(cb_name)),
835                         callback, user_data);
836 } /* sdb_plugin_register_cname */
838 int
839 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
840                 const sdb_time_t *interval, sdb_object_t *user_data)
842         char cb_name[1024];
843         sdb_object_t *obj;
845         if ((! name) || (! callback))
846                 return -1;
848         if (! collector_list)
849                 collector_list = sdb_llist_create();
850         if (! collector_list)
851                 return -1;
853         plugin_get_name(name, cb_name, sizeof(cb_name));
855         obj = sdb_object_create(cb_name, collector_type,
856                         &collector_list, "collector", callback, user_data);
857         if (! obj)
858                 return -1;
860         if (interval)
861                 CCB(obj)->ccb_interval = *interval;
862         else {
863                 ctx_t *ctx = ctx_get();
865                 if (! ctx) {
866                         sdb_log(SDB_LOG_ERR, "core: Cannot determine interval "
867                                         "for collector %s; none specified and no plugin "
868                                         "context found", cb_name);
869                         return -1;
870                 }
872                 CCB(obj)->ccb_interval = ctx->public.interval;
873         }
875         if (! (CCB(obj)->ccb_next_update = sdb_gettime())) {
876                 char errbuf[1024];
877                 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
878                                 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
879                 sdb_object_deref(obj);
880                 return -1;
881         }
883         if (sdb_llist_insert_sorted(collector_list, obj,
884                                 plugin_cmp_next_update)) {
885                 sdb_object_deref(obj);
886                 return -1;
887         }
889         /* pass control to the list */
890         sdb_object_deref(obj);
892         sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
893                         "(interval = %.3fs).", cb_name,
894                         SDB_TIME_TO_DOUBLE(CCB(obj)->ccb_interval));
895         return 0;
896 } /* sdb_plugin_register_collector */
898 int
899 sdb_plugin_register_ts_fetcher(const char *name,
900                 sdb_plugin_fetch_ts_cb callback, sdb_object_t *user_data)
902         return plugin_add_callback(&ts_fetcher_list, "time-series fetcher",
903                         name, callback, user_data);
904 } /* sdb_plugin_register_ts_fetcher */
906 int
907 sdb_plugin_register_writer(const char *name,
908                 sdb_store_writer_t *writer, sdb_object_t *user_data)
910         char cb_name[1024];
911         sdb_object_t *obj;
913         if ((! name) || (! writer))
914                 return -1;
916         if (! writer_list)
917                 writer_list = sdb_llist_create();
918         if (! writer_list)
919                 return -1;
921         plugin_get_name(name, cb_name, sizeof(cb_name));
923         obj = sdb_object_create(cb_name, writer_type,
924                         writer, user_data);
925         if (! obj)
926                 return -1;
928         if (sdb_llist_append(writer_list, obj)) {
929                 sdb_object_deref(obj);
930                 return -1;
931         }
933         /* pass control to the list */
934         sdb_object_deref(obj);
936         sdb_log(SDB_LOG_INFO, "core: Registered store writer callback '%s'.",
937                         cb_name);
938         return 0;
939 } /* sdb_store_register_writer */
941 int
942 sdb_plugin_register_reader(const char *name,
943                 sdb_store_reader_t *reader, sdb_object_t *user_data)
945         char cb_name[1024];
946         sdb_object_t *obj;
948         if ((! name) || (! reader))
949                 return -1;
951         if (! reader_list)
952                 reader_list = sdb_llist_create();
953         if (! reader_list)
954                 return -1;
956         plugin_get_name(name, cb_name, sizeof(cb_name));
958         obj = sdb_object_create(cb_name, reader_type,
959                         reader, user_data);
960         if (! obj)
961                 return -1;
963         if (sdb_llist_append(reader_list, obj)) {
964                 sdb_object_deref(obj);
965                 return -1;
966         }
968         /* pass control to the list */
969         sdb_object_deref(obj);
971         sdb_log(SDB_LOG_INFO, "core: Registered store reader callback '%s'.",
972                         cb_name);
973         return 0;
974 } /* sdb_plugin_register_reader */
976 void
977 sdb_plugin_unregister_all(void)
979         size_t i;
981         for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
982                 const char  *type =  all_lists[i].type;
983                 sdb_llist_t *list = *all_lists[i].list;
985                 size_t len = sdb_llist_len(list);
987                 if (! len)
988                         continue;
990                 sdb_llist_clear(list);
991                 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
992                                 len, type, len == 1 ? "" : "s");
993         }
994 } /* sdb_plugin_unregister_all */
996 sdb_plugin_ctx_t
997 sdb_plugin_get_ctx(void)
999         ctx_t *c;
1001         c = ctx_get();
1002         if (! c) {
1003                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
1004                                 "context outside a plugin");
1005                 return plugin_default_ctx;
1006         }
1007         return c->public;
1008 } /* sdb_plugin_get_ctx */
1010 int
1011 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
1013         ctx_t *c;
1015         c = ctx_get();
1016         if (! c) {
1017                 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
1018                                 "context outside a plugin");
1019                 return -1;
1020         }
1022         if (old)
1023                 *old = c->public;
1024         c->public = ctx;
1025         return 0;
1026 } /* sdb_plugin_set_ctx */
1028 const sdb_plugin_info_t *
1029 sdb_plugin_current(void)
1031         ctx_t *ctx = ctx_get();
1033         if (! ctx)
1034                 return NULL;
1035         return &ctx->info;
1036 } /* sdb_plugin_current */
1038 int
1039 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
1041         callback_t *plugin;
1042         sdb_plugin_config_cb callback;
1044         ctx_t *old_ctx;
1046         int status;
1048         if ((! name) || (! ci))
1049                 return -1;
1051         plugin = CB(sdb_llist_search_by_name(config_list, name));
1052         if (! plugin) {
1053                 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
1054                 if (! ctx)
1055                         sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
1056                                         "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
1057                                         name, name);
1058                 else
1059                         sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
1060                                         "a config callback.", name);
1061                 errno = ENOENT;
1062                 return -1;
1063         }
1065         old_ctx = ctx_set(plugin->cb_ctx);
1066         callback = (sdb_plugin_config_cb)plugin->cb_callback;
1067         status = callback(ci);
1068         ctx_set(old_ctx);
1069         return status;
1070 } /* sdb_plugin_configure */
1072 int
1073 sdb_plugin_reconfigure_init(void)
1075         sdb_llist_iter_t *iter;
1077         iter = sdb_llist_get_iter(config_list);
1078         if (config_list && (! iter))
1079                 return -1;
1081         /* deconfigure all plugins */
1082         while (sdb_llist_iter_has_next(iter)) {
1083                 callback_t *plugin;
1084                 sdb_plugin_config_cb callback;
1085                 ctx_t *old_ctx;
1087                 plugin = CB(sdb_llist_iter_get_next(iter));
1088                 old_ctx = ctx_set(plugin->cb_ctx);
1089                 callback = (sdb_plugin_config_cb)plugin->cb_callback;
1090                 callback(NULL);
1091                 ctx_set(old_ctx);
1092         }
1093         sdb_llist_iter_destroy(iter);
1095         iter = sdb_llist_get_iter(all_plugins);
1096         if (all_plugins && (! iter))
1097                 return -1;
1099         /* record all plugins as being unused */
1100         while (sdb_llist_iter_has_next(iter))
1101                 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
1102         sdb_llist_iter_destroy(iter);
1104         sdb_plugin_unregister_all();
1105         return 0;
1106 } /* sdb_plugin_reconfigure_init */
1108 int
1109 sdb_plugin_reconfigure_finish(void)
1111         sdb_llist_iter_t *iter;
1113         iter = sdb_llist_get_iter(all_plugins);
1114         if (all_plugins && (! iter))
1115                 return -1;
1117         while (sdb_llist_iter_has_next(iter)) {
1118                 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
1119                 if (ctx->use_cnt)
1120                         continue;
1122                 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
1123                                 ctx->info.plugin_name);
1124                 sdb_llist_iter_remove_current(iter);
1125                 plugin_unregister_by_name(ctx->info.plugin_name);
1126                 sdb_object_deref(SDB_OBJ(ctx));
1127         }
1128         sdb_llist_iter_destroy(iter);
1129         return 0;
1130 } /* sdb_plugin_reconfigure_finish */
1132 int
1133 sdb_plugin_init_all(void)
1135         sdb_llist_iter_t *iter;
1136         int ret = 0;
1138         iter = sdb_llist_get_iter(init_list);
1139         while (sdb_llist_iter_has_next(iter)) {
1140                 callback_t *cb;
1141                 sdb_plugin_init_cb callback;
1142                 ctx_t *old_ctx;
1144                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1145                 assert(obj);
1146                 cb = CB(obj);
1148                 callback = (sdb_plugin_init_cb)cb->cb_callback;
1150                 old_ctx = ctx_set(cb->cb_ctx);
1151                 if (callback(cb->cb_user_data)) {
1152                         sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
1153                                         "'%s'. Unregistering all callbacks.", obj->name);
1154                         ctx_set(old_ctx);
1155                         plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
1156                         ++ret;
1157                 }
1158                 else
1159                         ctx_set(old_ctx);
1160         }
1161         sdb_llist_iter_destroy(iter);
1162         return ret;
1163 } /* sdb_plugin_init_all */
1165 int
1166 sdb_plugin_shutdown_all(void)
1168         sdb_llist_iter_t *iter;
1169         int ret = 0;
1171         iter = sdb_llist_get_iter(shutdown_list);
1172         while (sdb_llist_iter_has_next(iter)) {
1173                 callback_t *cb;
1174                 sdb_plugin_shutdown_cb callback;
1175                 ctx_t *old_ctx;
1177                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1178                 assert(obj);
1179                 cb = CB(obj);
1181                 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
1183                 old_ctx = ctx_set(cb->cb_ctx);
1184                 if (callback(cb->cb_user_data)) {
1185                         sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
1186                                         obj->name);
1187                         ++ret;
1188                 }
1189                 ctx_set(old_ctx);
1190         }
1191         sdb_llist_iter_destroy(iter);
1192         return ret;
1193 } /* sdb_plugin_shutdown_all */
1195 int
1196 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
1198         if (! collector_list) {
1199                 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
1200                                 "Quiting main loop.");
1201                 return -1;
1202         }
1204         if (! loop)
1205                 return -1;
1207         while (loop->do_loop) {
1208                 sdb_plugin_collector_cb callback;
1209                 ctx_t *old_ctx;
1211                 sdb_time_t interval, now;
1213                 sdb_object_t *obj = sdb_llist_shift(collector_list);
1214                 if (! obj)
1215                         return -1;
1217                 callback = (sdb_plugin_collector_cb)CCB(obj)->ccb_callback;
1219                 if (! (now = sdb_gettime())) {
1220                         char errbuf[1024];
1221                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1222                                         "time in collector main loop: %s",
1223                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1224                         now = CCB(obj)->ccb_next_update;
1225                 }
1227                 if (now < CCB(obj)->ccb_next_update) {
1228                         interval = CCB(obj)->ccb_next_update - now;
1230                         errno = 0;
1231                         while (loop->do_loop && sdb_sleep(interval, &interval)) {
1232                                 if (errno != EINTR) {
1233                                         char errbuf[1024];
1234                                         sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1235                                                         "in collector main loop: %s",
1236                                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1237                                         sdb_llist_insert_sorted(collector_list, obj,
1238                                                         plugin_cmp_next_update);
1239                                         sdb_object_deref(obj);
1240                                         return -1;
1241                                 }
1242                                 errno = 0;
1243                         }
1245                         if (! loop->do_loop) {
1246                                 /* put back; don't worry about errors */
1247                                 sdb_llist_insert_sorted(collector_list, obj,
1248                                                 plugin_cmp_next_update);
1249                                 sdb_object_deref(obj);
1250                                 return 0;
1251                         }
1252                 }
1254                 old_ctx = ctx_set(CCB(obj)->ccb_ctx);
1255                 if (callback(CCB(obj)->ccb_user_data)) {
1256                         /* XXX */
1257                 }
1258                 ctx_set(old_ctx);
1260                 interval = CCB(obj)->ccb_interval;
1261                 if (! interval)
1262                         interval = loop->default_interval;
1263                 if (! interval) {
1264                         sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1265                                         "for plugin '%s'; skipping any further "
1266                                         "iterations.", obj->name);
1267                         sdb_object_deref(obj);
1268                         continue;
1269                 }
1271                 CCB(obj)->ccb_next_update += interval;
1273                 if (! (now = sdb_gettime())) {
1274                         char errbuf[1024];
1275                         sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1276                                         "time in collector main loop: %s",
1277                                         sdb_strerror(errno, errbuf, sizeof(errbuf)));
1278                         now = CCB(obj)->ccb_next_update;
1279                 }
1281                 if (now > CCB(obj)->ccb_next_update) {
1282                         sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1283                                         "long; skipping iterations to keep up.",
1284                                         obj->name);
1285                         CCB(obj)->ccb_next_update = now;
1286                 }
1288                 if (sdb_llist_insert_sorted(collector_list, obj,
1289                                         plugin_cmp_next_update)) {
1290                         sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1291                                         "plugin '%s' into collector list. Unable to further "
1292                                         "use the plugin.",
1293                                         obj->name);
1294                         sdb_object_deref(obj);
1295                         return -1;
1296                 }
1298                 /* pass control back to the list */
1299                 sdb_object_deref(obj);
1300         }
1301         return 0;
1302 } /* sdb_plugin_read_loop */
1304 char *
1305 sdb_plugin_cname(char *hostname)
1307         sdb_llist_iter_t *iter;
1309         if (! hostname)
1310                 return NULL;
1312         if (! cname_list)
1313                 return hostname;
1315         iter = sdb_llist_get_iter(cname_list);
1316         while (sdb_llist_iter_has_next(iter)) {
1317                 sdb_plugin_cname_cb callback;
1318                 char *cname;
1320                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1321                 assert(obj);
1323                 callback = (sdb_plugin_cname_cb)CB(obj)->cb_callback;
1324                 cname = callback(hostname, CB(obj)->cb_user_data);
1325                 if (cname) {
1326                         free(hostname);
1327                         hostname = cname;
1328                 }
1329                 /* else: don't change hostname */
1330         }
1331         sdb_llist_iter_destroy(iter);
1332         return hostname;
1333 } /* sdb_plugin_cname */
1335 int
1336 sdb_plugin_log(int prio, const char *msg)
1338         sdb_llist_iter_t *iter;
1339         int ret = -1;
1341         bool logged = 0;
1343         if (! msg)
1344                 return 0;
1346         iter = sdb_llist_get_iter(log_list);
1347         while (sdb_llist_iter_has_next(iter)) {
1348                 sdb_plugin_log_cb callback;
1349                 int tmp;
1351                 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1352                 assert(obj);
1354                 callback = (sdb_plugin_log_cb)CB(obj)->cb_callback;
1355                 tmp = callback(prio, msg, CB(obj)->cb_user_data);
1356                 if (tmp > ret)
1357                         ret = tmp;
1359                 if (CB(obj)->cb_ctx)
1360                         logged = 1;
1361                 /* else: this is an internally registered callback */
1362         }
1363         sdb_llist_iter_destroy(iter);
1365         if (! logged)
1366                 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1367         return ret;
1368 } /* sdb_plugin_log */
1370 int
1371 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1373         sdb_strbuf_t *buf;
1374         int ret;
1376         if (! fmt)
1377                 return 0;
1379         buf = sdb_strbuf_create(64);
1380         if (! buf) {
1381                 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1382                 ret += vfprintf(stderr, fmt, ap);
1383                 return ret;
1384         }
1386         if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1387                 sdb_strbuf_destroy(buf);
1388                 return -1;
1389         }
1391         ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1392         sdb_strbuf_destroy(buf);
1393         return ret;
1394 } /* sdb_plugin_vlogf */
1396 int
1397 sdb_plugin_logf(int prio, const char *fmt, ...)
1399         va_list ap;
1400         int ret;
1402         if (! fmt)
1403                 return 0;
1405         va_start(ap, fmt);
1406         ret = sdb_plugin_vlogf(prio, fmt, ap);
1407         va_end(ap);
1408         return ret;
1409 } /* sdb_plugin_logf */
1411 sdb_timeseries_t *
1412 sdb_plugin_fetch_timeseries(const char *type, const char *id,
1413                 sdb_timeseries_opts_t *opts)
1415         callback_t *plugin;
1416         sdb_plugin_fetch_ts_cb callback;
1417         sdb_timeseries_t *ts;
1419         ctx_t *old_ctx;
1421         if ((! type) || (! id) || (! opts))
1422                 return NULL;
1424         plugin = CB(sdb_llist_search_by_name(ts_fetcher_list, type));
1425         if (! plugin) {
1426                 sdb_log(SDB_LOG_ERR, "core: Cannot fetch time-series of type %s: "
1427                                 "no such plugin loaded", type);
1428                 errno = ENOENT;
1429                 return NULL;
1430         }
1432         old_ctx = ctx_set(plugin->cb_ctx);
1433         callback = (sdb_plugin_fetch_ts_cb)plugin->cb_callback;
1434         ts = callback(id, opts, plugin->cb_user_data);
1435         ctx_set(old_ctx);
1436         return ts;
1437 } /* sdb_plugin_fetch_timeseries */
1439 int
1440 sdb_plugin_query(sdb_ast_node_t *ast,
1441                 sdb_store_writer_t *w, sdb_object_t *wd, sdb_strbuf_t *errbuf)
1443         size_t n = sdb_llist_len(reader_list);
1444         reader_t *reader;
1445         sdb_object_t *q;
1446         int status = 0;
1448         if (! ast)
1449                 return 0;
1451         if ((ast->type != SDB_AST_TYPE_FETCH)
1452                         && (ast->type != SDB_AST_TYPE_LIST)
1453                         && (ast->type != SDB_AST_TYPE_LOOKUP)) {
1454                 sdb_log(SDB_LOG_ERR, "core: Cannot execute query of type %s",
1455                                 SDB_AST_TYPE_TO_STRING(ast));
1456                 sdb_strbuf_sprintf(errbuf, "Cannot execute query of type %s",
1457                                 SDB_AST_TYPE_TO_STRING(ast));
1458                 return -1;
1459         }
1461         if (n != 1) {
1462                 char *msg = (n > 0)
1463                         ? "Cannot execute query: multiple readers not supported"
1464                         : "Cannot execute query: no readers registered";
1465                 sdb_strbuf_sprintf(errbuf, "%s", msg);
1466                 sdb_log(SDB_LOG_ERR, "core: %s", msg);
1467                 return -1;
1468         }
1470         reader = READER(sdb_llist_get(reader_list, 0));
1471         assert(reader);
1473         q = reader->impl.prepare_query(ast, errbuf, reader->r_user_data);
1474         if (q)
1475                 status = reader->impl.execute_query(q, w, SDB_OBJ(wd),
1476                                 errbuf, reader->r_user_data);
1477         else
1478                 status = -1;
1480         sdb_object_deref(SDB_OBJ(q));
1481         sdb_object_deref(SDB_OBJ(reader));
1482         return status;
1483 } /* sdb_plugin_query */
1485 int
1486 sdb_plugin_store_host(const char *name, sdb_time_t last_update)
1488         sdb_store_host_t host = SDB_STORE_HOST_INIT;
1489         char *backends[1];
1490         char *cname;
1492         sdb_llist_iter_t *iter;
1493         int status = 0;
1495         if (! name)
1496                 return -1;
1498         if (! sdb_llist_len(writer_list)) {
1499                 sdb_log(SDB_LOG_ERR, "core: Cannot store host: "
1500                                 "no writers registered");
1501                 return -1;
1502         }
1504         cname = sdb_plugin_cname(strdup(name));
1505         if (! cname) {
1506                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1507                 return -1;
1508         }
1510         host.name = cname;
1511         host.last_update = last_update;
1512         host.backends = (const char * const *)backends;
1513         get_backend(backends, &host.backends_num);
1515         iter = sdb_llist_get_iter(writer_list);
1516         while (sdb_llist_iter_has_next(iter)) {
1517                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1518                 int s;
1519                 assert(writer);
1520                 s = writer->impl.store_host(&host, writer->w_user_data);
1521                 if (((s > 0) && (status >= 0)) || (s < 0))
1522                         status = s;
1523         }
1524         sdb_llist_iter_destroy(iter);
1525         free(cname);
1526         return status;
1527 } /* sdb_plugin_store_host */
1529 int
1530 sdb_plugin_store_service(const char *hostname, const char *name,
1531                 sdb_time_t last_update)
1533         sdb_store_service_t service = SDB_STORE_SERVICE_INIT;
1534         char *backends[1];
1535         char *cname;
1537         sdb_llist_iter_t *iter;
1538         sdb_data_t d;
1540         int status = 0;
1542         if ((! hostname) || (! name))
1543                 return -1;
1545         if (! sdb_llist_len(writer_list)) {
1546                 sdb_log(SDB_LOG_ERR, "core: Cannot store service: "
1547                                 "no writers registered");
1548                 return -1;
1549         }
1551         cname = sdb_plugin_cname(strdup(hostname));
1552         if (! cname) {
1553                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1554                 return -1;
1555         }
1557         service.hostname = cname;
1558         service.name = name;
1559         service.last_update = last_update;
1560         service.backends = (const char * const *)backends;
1561         get_backend(backends, &service.backends_num);
1563         iter = sdb_llist_get_iter(writer_list);
1564         while (sdb_llist_iter_has_next(iter)) {
1565                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1566                 int s;
1567                 assert(writer);
1568                 s = writer->impl.store_service(&service, writer->w_user_data);
1569                 if (((s > 0) && (status >= 0)) || (s < 0))
1570                         status = s;
1571         }
1572         sdb_llist_iter_destroy(iter);
1574         if (! status) {
1575                 /* record the hostname as an attribute */
1576                 d.type = SDB_TYPE_STRING;
1577                 d.data.string = cname;
1578                 if (sdb_plugin_store_service_attribute(cname, name,
1579                                         "hostname", &d, last_update))
1580                         status = -1;
1581         }
1583         free(cname);
1584         return status;
1585 } /* sdb_plugin_store_service */
1587 int
1588 sdb_plugin_store_metric(const char *hostname, const char *name,
1589                 sdb_metric_store_t *store, sdb_time_t last_update)
1591         sdb_store_metric_t metric = SDB_STORE_METRIC_INIT;
1592         char *backends[1];
1593         char *cname;
1595         sdb_llist_iter_t *iter;
1596         sdb_data_t d;
1598         int status = 0;
1600         if ((! hostname) || (! name))
1601                 return -1;
1603         if (! sdb_llist_len(writer_list)) {
1604                 sdb_log(SDB_LOG_ERR, "core: Cannot store metric: "
1605                                 "no writers registered");
1606                 return -1;
1607         }
1609         cname = sdb_plugin_cname(strdup(hostname));
1610         if (! cname) {
1611                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1612                 return -1;
1613         }
1615         if (store && ((! store->type) || (! store->id)))
1616                 store = NULL;
1618         metric.hostname = cname;
1619         metric.name = name;
1620         if (store) {
1621                 metric.store.type = store->type;
1622                 metric.store.id = store->id;
1623         }
1624         metric.last_update = last_update;
1625         metric.backends = (const char * const *)backends;
1626         get_backend(backends, &metric.backends_num);
1628         iter = sdb_llist_get_iter(writer_list);
1629         while (sdb_llist_iter_has_next(iter)) {
1630                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1631                 int s;
1632                 assert(writer);
1633                 s = writer->impl.store_metric(&metric, writer->w_user_data);
1634                 if (((s > 0) && (status >= 0)) || (s < 0))
1635                         status = s;
1636         }
1637         sdb_llist_iter_destroy(iter);
1639         if (! status) {
1640                 /* record the hostname as an attribute */
1641                 d.type = SDB_TYPE_STRING;
1642                 d.data.string = cname;
1643                 if (sdb_plugin_store_metric_attribute(cname, name,
1644                                         "hostname", &d, last_update))
1645                         status = -1;
1646         }
1648         free(cname);
1649         return status;
1650 } /* sdb_plugin_store_metric */
1652 int
1653 sdb_plugin_store_attribute(const char *hostname, const char *key,
1654                 const sdb_data_t *value, sdb_time_t last_update)
1656         sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT;
1657         char *backends[1];
1658         char *cname;
1660         sdb_llist_iter_t *iter;
1661         int status = 0;
1663         if ((! hostname) || (! key) || (! value))
1664                 return -1;
1666         if (! sdb_llist_len(writer_list)) {
1667                 sdb_log(SDB_LOG_ERR, "core: Cannot store attribute: "
1668                                 "no writers registered");
1669                 return -1;
1670         }
1672         cname = sdb_plugin_cname(strdup(hostname));
1673         if (! cname) {
1674                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1675                 return -1;
1676         }
1678         attr.parent_type = SDB_HOST;
1679         attr.parent = cname;
1680         attr.key = key;
1681         attr.value = *value;
1682         attr.last_update = last_update;
1683         attr.backends = (const char * const *)backends;
1684         get_backend(backends, &attr.backends_num);
1686         iter = sdb_llist_get_iter(writer_list);
1687         while (sdb_llist_iter_has_next(iter)) {
1688                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1689                 int s;
1690                 assert(writer);
1691                 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1692                 if (((s > 0) && (status >= 0)) || (s < 0))
1693                         status = s;
1694         }
1695         sdb_llist_iter_destroy(iter);
1696         free(cname);
1697         return status;
1698 } /* sdb_plugin_store_attribute */
1700 int
1701 sdb_plugin_store_service_attribute(const char *hostname, const char *service,
1702                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1704         sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT;
1705         char *backends[1];
1706         char *cname;
1708         sdb_llist_iter_t *iter;
1709         int status = 0;
1711         if ((! hostname) || (! service) || (! key) || (! value))
1712                 return -1;
1714         if (! sdb_llist_len(writer_list)) {
1715                 sdb_log(SDB_LOG_ERR, "core: Cannot store service attribute: "
1716                                 "no writers registered");
1717                 return -1;
1718         }
1720         cname = sdb_plugin_cname(strdup(hostname));
1721         if (! cname) {
1722                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1723                 return -1;
1724         }
1726         attr.hostname = cname;
1727         attr.parent_type = SDB_SERVICE;
1728         attr.parent = service;
1729         attr.key = key;
1730         attr.value = *value;
1731         attr.last_update = last_update;
1732         attr.backends = (const char * const *)backends;
1733         get_backend(backends, &attr.backends_num);
1735         iter = sdb_llist_get_iter(writer_list);
1736         while (sdb_llist_iter_has_next(iter)) {
1737                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1738                 int s;
1739                 assert(writer);
1740                 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1741                 if (((s > 0) && (status >= 0)) || (s < 0))
1742                         status = s;
1743         }
1744         sdb_llist_iter_destroy(iter);
1745         free(cname);
1746         return status;
1747 } /* sdb_plugin_store_service_attribute */
1749 int
1750 sdb_plugin_store_metric_attribute(const char *hostname, const char *metric,
1751                 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1753         sdb_store_attribute_t attr = SDB_STORE_ATTRIBUTE_INIT;
1754         char *backends[1];
1755         char *cname;
1757         sdb_llist_iter_t *iter;
1758         int status = 0;
1760         if ((! hostname) || (! metric) || (! key) || (! value))
1761                 return -1;
1763         if (! sdb_llist_len(writer_list)) {
1764                 sdb_log(SDB_LOG_ERR, "core: Cannot store metric attribute: "
1765                                 "no writers registered");
1766                 return -1;
1767         }
1769         cname = sdb_plugin_cname(strdup(hostname));
1770         if (! cname) {
1771                 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1772                 return -1;
1773         }
1775         attr.hostname = cname;
1776         attr.parent_type = SDB_METRIC;
1777         attr.parent = metric;
1778         attr.key = key;
1779         attr.value = *value;
1780         attr.last_update = last_update;
1781         attr.backends = (const char * const *)backends;
1782         get_backend(backends, &attr.backends_num);
1784         iter = sdb_llist_get_iter(writer_list);
1785         while (sdb_llist_iter_has_next(iter)) {
1786                 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1787                 int s;
1788                 assert(writer);
1789                 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1790                 if (((s > 0) && (status >= 0)) || (s < 0))
1791                         status = s;
1792         }
1793         sdb_llist_iter_destroy(iter);
1794         free(cname);
1795         return status;
1796 } /* sdb_plugin_store_metric_attribute */
1798 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */