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)
161 {
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)
183 {
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)
193 {
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)
206 {
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)
227 {
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)
268 {
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)
282 {
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)
310 {
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)
318 {
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)
335 {
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)
350 {
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)
380 {
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)
402 {
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)
435 {
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)
450 {
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)
482 {
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)
497 {
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)
521 {
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)
620 {
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)
633 {
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 /*
665 * public API
666 */
668 int
669 sdb_plugin_load(const char *basedir, const char *name,
670 const sdb_plugin_ctx_t *plugin_ctx)
671 {
672 ctx_t *ctx;
674 int status;
676 if ((! name) || (! *name))
677 return -1;
679 if (! all_plugins) {
680 if (! (all_plugins = sdb_llist_create())) {
681 sdb_log(SDB_LOG_ERR, "core: Failed to load plugin '%s': "
682 "internal error while creating linked list", name);
683 return -1;
684 }
685 }
687 ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
688 if (ctx) {
689 /* plugin already loaded */
690 if (! ctx->use_cnt) {
691 /* reloading plugin */
692 ctx_t *old_ctx = ctx_set(ctx);
694 status = module_init(ctx->info.plugin_name, ctx->handle, NULL);
695 if (status)
696 return status;
698 sdb_log(SDB_LOG_INFO, "core: Successfully reloaded plugin "
699 "'%s' (%s)", ctx->info.plugin_name,
700 INFO_GET(&ctx->info, description));
701 ctx_set(old_ctx);
702 }
703 ++ctx->use_cnt;
704 return 0;
705 }
707 return module_load(basedir, name, plugin_ctx);
708 } /* sdb_plugin_load */
710 int
711 sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...)
712 {
713 va_list ap;
715 if (! info)
716 return -1;
718 va_start(ap, type);
720 switch (type) {
721 case SDB_PLUGIN_INFO_DESC:
722 {
723 char *desc = va_arg(ap, char *);
724 if (desc) {
725 if (info->description)
726 free(info->description);
727 info->description = strdup(desc);
728 }
729 }
730 break;
731 case SDB_PLUGIN_INFO_COPYRIGHT:
732 {
733 char *copyright = va_arg(ap, char *);
734 if (copyright)
735 info->copyright = strdup(copyright);
736 }
737 break;
738 case SDB_PLUGIN_INFO_LICENSE:
739 {
740 char *license = va_arg(ap, char *);
741 if (license) {
742 if (info->license)
743 free(info->license);
744 info->license = strdup(license);
745 }
746 }
747 break;
748 case SDB_PLUGIN_INFO_VERSION:
749 {
750 int version = va_arg(ap, int);
751 info->version = version;
752 }
753 break;
754 case SDB_PLUGIN_INFO_PLUGIN_VERSION:
755 {
756 int version = va_arg(ap, int);
757 info->plugin_version = version;
758 }
759 break;
760 default:
761 va_end(ap);
762 return -1;
763 }
765 va_end(ap);
766 return 0;
767 } /* sdb_plugin_set_info */
769 int
770 sdb_plugin_register_config(sdb_plugin_config_cb callback)
771 {
772 ctx_t *ctx = ctx_get();
774 if (! ctx) {
775 sdb_log(SDB_LOG_ERR, "core: Invalid attempt to register a "
776 "config callback from outside a plugin");
777 return -1;
778 }
779 return plugin_add_callback(&config_list, "config", ctx->info.plugin_name,
780 (void *)callback, NULL);
781 } /* sdb_plugin_register_config */
783 int
784 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
785 sdb_object_t *user_data)
786 {
787 char cb_name[1024];
788 return plugin_add_callback(&init_list, "init",
789 plugin_get_name(name, cb_name, sizeof(cb_name)),
790 (void *)callback, user_data);
791 } /* sdb_plugin_register_init */
793 int
794 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
795 sdb_object_t *user_data)
796 {
797 char cb_name[1024];
798 return plugin_add_callback(&shutdown_list, "shutdown",
799 plugin_get_name(name, cb_name, sizeof(cb_name)),
800 (void *)callback, user_data);
801 } /* sdb_plugin_register_shutdown */
803 int
804 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
805 sdb_object_t *user_data)
806 {
807 char cb_name[1024];
808 return plugin_add_callback(&log_list, "log",
809 plugin_get_name(name, cb_name, sizeof(cb_name)),
810 callback, user_data);
811 } /* sdb_plugin_register_log */
813 int
814 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
815 sdb_object_t *user_data)
816 {
817 char cb_name[1024];
818 return plugin_add_callback(&cname_list, "cname",
819 plugin_get_name(name, cb_name, sizeof(cb_name)),
820 callback, user_data);
821 } /* sdb_plugin_register_cname */
823 int
824 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
825 const sdb_time_t *interval, sdb_object_t *user_data)
826 {
827 char cb_name[1024];
828 sdb_object_t *obj;
830 if ((! name) || (! callback))
831 return -1;
833 if (! collector_list)
834 collector_list = sdb_llist_create();
835 if (! collector_list)
836 return -1;
838 plugin_get_name(name, cb_name, sizeof(cb_name));
840 obj = sdb_object_create(cb_name, collector_type,
841 &collector_list, "collector", callback, user_data);
842 if (! obj)
843 return -1;
845 if (interval)
846 CCB(obj)->ccb_interval = *interval;
847 else {
848 ctx_t *ctx = ctx_get();
850 if (! ctx) {
851 sdb_log(SDB_LOG_ERR, "core: Cannot determine interval "
852 "for collector %s; none specified and no plugin "
853 "context found", cb_name);
854 return -1;
855 }
857 CCB(obj)->ccb_interval = ctx->public.interval;
858 }
860 if (! (CCB(obj)->ccb_next_update = sdb_gettime())) {
861 char errbuf[1024];
862 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
863 "time: %s", sdb_strerror(errno, errbuf, sizeof(errbuf)));
864 sdb_object_deref(obj);
865 return -1;
866 }
868 if (sdb_llist_insert_sorted(collector_list, obj,
869 plugin_cmp_next_update)) {
870 sdb_object_deref(obj);
871 return -1;
872 }
874 /* pass control to the list */
875 sdb_object_deref(obj);
877 sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
878 "(interval = %.3fs).", cb_name,
879 SDB_TIME_TO_DOUBLE(CCB(obj)->ccb_interval));
880 return 0;
881 } /* sdb_plugin_register_collector */
883 int
884 sdb_plugin_register_ts_fetcher(const char *name,
885 sdb_plugin_fetch_ts_cb callback, sdb_object_t *user_data)
886 {
887 return plugin_add_callback(&ts_fetcher_list, "time-series fetcher",
888 name, callback, user_data);
889 } /* sdb_plugin_register_ts_fetcher */
891 int
892 sdb_plugin_register_writer(const char *name,
893 sdb_store_writer_t *writer, sdb_object_t *user_data)
894 {
895 char cb_name[1024];
896 sdb_object_t *obj;
898 if ((! name) || (! writer))
899 return -1;
901 if (! writer_list)
902 writer_list = sdb_llist_create();
903 if (! writer_list)
904 return -1;
906 plugin_get_name(name, cb_name, sizeof(cb_name));
908 obj = sdb_object_create(cb_name, writer_type,
909 writer, user_data);
910 if (! obj)
911 return -1;
913 if (sdb_llist_append(writer_list, obj)) {
914 sdb_object_deref(obj);
915 return -1;
916 }
918 /* pass control to the list */
919 sdb_object_deref(obj);
921 sdb_log(SDB_LOG_INFO, "core: Registered store writer callback '%s'.",
922 cb_name);
923 return 0;
924 } /* sdb_store_register_writer */
926 int
927 sdb_plugin_register_reader(const char *name,
928 sdb_store_reader_t *reader, sdb_object_t *user_data)
929 {
930 char cb_name[1024];
931 sdb_object_t *obj;
933 if ((! name) || (! reader))
934 return -1;
936 if (! reader_list)
937 reader_list = sdb_llist_create();
938 if (! reader_list)
939 return -1;
941 plugin_get_name(name, cb_name, sizeof(cb_name));
943 obj = sdb_object_create(cb_name, reader_type,
944 reader, user_data);
945 if (! obj)
946 return -1;
948 if (sdb_llist_append(reader_list, obj)) {
949 sdb_object_deref(obj);
950 return -1;
951 }
953 /* pass control to the list */
954 sdb_object_deref(obj);
956 sdb_log(SDB_LOG_INFO, "core: Registered store reader callback '%s'.",
957 cb_name);
958 return 0;
959 } /* sdb_plugin_register_reader */
961 void
962 sdb_plugin_unregister_all(void)
963 {
964 size_t i;
966 for (i = 0; i < SDB_STATIC_ARRAY_LEN(all_lists); ++i) {
967 const char *type = all_lists[i].type;
968 sdb_llist_t *list = *all_lists[i].list;
970 size_t len = sdb_llist_len(list);
972 if (! len)
973 continue;
975 sdb_llist_clear(list);
976 sdb_log(SDB_LOG_INFO, "core: Unregistered %zu %s callback%s",
977 len, type, len == 1 ? "" : "s");
978 }
979 } /* sdb_plugin_unregister_all */
981 sdb_plugin_ctx_t
982 sdb_plugin_get_ctx(void)
983 {
984 ctx_t *c;
986 c = ctx_get();
987 if (! c) {
988 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid read access to plugin "
989 "context outside a plugin");
990 return plugin_default_ctx;
991 }
992 return c->public;
993 } /* sdb_plugin_get_ctx */
995 int
996 sdb_plugin_set_ctx(sdb_plugin_ctx_t ctx, sdb_plugin_ctx_t *old)
997 {
998 ctx_t *c;
1000 c = ctx_get();
1001 if (! c) {
1002 sdb_plugin_log(SDB_LOG_ERR, "core: Invalid write access to plugin "
1003 "context outside a plugin");
1004 return -1;
1005 }
1007 if (old)
1008 *old = c->public;
1009 c->public = ctx;
1010 return 0;
1011 } /* sdb_plugin_set_ctx */
1013 const sdb_plugin_info_t *
1014 sdb_plugin_current(void)
1015 {
1016 ctx_t *ctx = ctx_get();
1018 if (! ctx)
1019 return NULL;
1020 return &ctx->info;
1021 } /* sdb_plugin_current */
1023 int
1024 sdb_plugin_configure(const char *name, oconfig_item_t *ci)
1025 {
1026 callback_t *plugin;
1027 sdb_plugin_config_cb callback;
1029 ctx_t *old_ctx;
1031 int status;
1033 if ((! name) || (! ci))
1034 return -1;
1036 plugin = CB(sdb_llist_search_by_name(config_list, name));
1037 if (! plugin) {
1038 ctx_t *ctx = CTX(sdb_llist_search_by_name(all_plugins, name));
1039 if (! ctx)
1040 sdb_log(SDB_LOG_ERR, "core: Cannot configure unknown "
1041 "plugin '%s'. Missing 'LoadPlugin \"%s\"'?",
1042 name, name);
1043 else
1044 sdb_log(SDB_LOG_ERR, "core: Plugin '%s' did not register "
1045 "a config callback.", name);
1046 errno = ENOENT;
1047 return -1;
1048 }
1050 old_ctx = ctx_set(plugin->cb_ctx);
1051 callback = (sdb_plugin_config_cb)plugin->cb_callback;
1052 status = callback(ci);
1053 ctx_set(old_ctx);
1054 return status;
1055 } /* sdb_plugin_configure */
1057 int
1058 sdb_plugin_reconfigure_init(void)
1059 {
1060 sdb_llist_iter_t *iter;
1062 iter = sdb_llist_get_iter(config_list);
1063 if (config_list && (! iter))
1064 return -1;
1066 /* deconfigure all plugins */
1067 while (sdb_llist_iter_has_next(iter)) {
1068 callback_t *plugin;
1069 sdb_plugin_config_cb callback;
1070 ctx_t *old_ctx;
1072 plugin = CB(sdb_llist_iter_get_next(iter));
1073 old_ctx = ctx_set(plugin->cb_ctx);
1074 callback = (sdb_plugin_config_cb)plugin->cb_callback;
1075 callback(NULL);
1076 ctx_set(old_ctx);
1077 }
1078 sdb_llist_iter_destroy(iter);
1080 iter = sdb_llist_get_iter(all_plugins);
1081 if (all_plugins && (! iter))
1082 return -1;
1084 /* record all plugins as being unused */
1085 while (sdb_llist_iter_has_next(iter))
1086 CTX(sdb_llist_iter_get_next(iter))->use_cnt = 0;
1087 sdb_llist_iter_destroy(iter);
1089 sdb_plugin_unregister_all();
1090 return 0;
1091 } /* sdb_plugin_reconfigure_init */
1093 int
1094 sdb_plugin_reconfigure_finish(void)
1095 {
1096 sdb_llist_iter_t *iter;
1098 iter = sdb_llist_get_iter(all_plugins);
1099 if (all_plugins && (! iter))
1100 return -1;
1102 while (sdb_llist_iter_has_next(iter)) {
1103 ctx_t *ctx = CTX(sdb_llist_iter_get_next(iter));
1104 if (ctx->use_cnt)
1105 continue;
1107 sdb_log(SDB_LOG_INFO, "core: Module %s no longer in use",
1108 ctx->info.plugin_name);
1109 sdb_llist_iter_remove_current(iter);
1110 plugin_unregister_by_name(ctx->info.plugin_name);
1111 sdb_object_deref(SDB_OBJ(ctx));
1112 }
1113 sdb_llist_iter_destroy(iter);
1114 return 0;
1115 } /* sdb_plugin_reconfigure_finish */
1117 int
1118 sdb_plugin_init_all(void)
1119 {
1120 sdb_llist_iter_t *iter;
1121 int ret = 0;
1123 iter = sdb_llist_get_iter(init_list);
1124 while (sdb_llist_iter_has_next(iter)) {
1125 callback_t *cb;
1126 sdb_plugin_init_cb callback;
1127 ctx_t *old_ctx;
1129 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1130 assert(obj);
1131 cb = CB(obj);
1133 callback = (sdb_plugin_init_cb)cb->cb_callback;
1135 old_ctx = ctx_set(cb->cb_ctx);
1136 if (callback(cb->cb_user_data)) {
1137 sdb_log(SDB_LOG_ERR, "core: Failed to initialize plugin "
1138 "'%s'. Unregistering all callbacks.", obj->name);
1139 ctx_set(old_ctx);
1140 plugin_unregister_by_name(cb->cb_ctx->info.plugin_name);
1141 ++ret;
1142 }
1143 else
1144 ctx_set(old_ctx);
1145 }
1146 sdb_llist_iter_destroy(iter);
1147 return ret;
1148 } /* sdb_plugin_init_all */
1150 int
1151 sdb_plugin_shutdown_all(void)
1152 {
1153 sdb_llist_iter_t *iter;
1154 int ret = 0;
1156 iter = sdb_llist_get_iter(shutdown_list);
1157 while (sdb_llist_iter_has_next(iter)) {
1158 callback_t *cb;
1159 sdb_plugin_shutdown_cb callback;
1160 ctx_t *old_ctx;
1162 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1163 assert(obj);
1164 cb = CB(obj);
1166 callback = (sdb_plugin_shutdown_cb)cb->cb_callback;
1168 old_ctx = ctx_set(cb->cb_ctx);
1169 if (callback(cb->cb_user_data)) {
1170 sdb_log(SDB_LOG_ERR, "core: Failed to shutdown plugin '%s'.",
1171 obj->name);
1172 ++ret;
1173 }
1174 ctx_set(old_ctx);
1175 }
1176 sdb_llist_iter_destroy(iter);
1177 return ret;
1178 } /* sdb_plugin_shutdown_all */
1180 int
1181 sdb_plugin_collector_loop(sdb_plugin_loop_t *loop)
1182 {
1183 if (! collector_list) {
1184 sdb_log(SDB_LOG_WARNING, "core: No collectors registered. "
1185 "Quiting main loop.");
1186 return -1;
1187 }
1189 if (! loop)
1190 return -1;
1192 while (loop->do_loop) {
1193 sdb_plugin_collector_cb callback;
1194 ctx_t *old_ctx;
1196 sdb_time_t interval, now;
1198 sdb_object_t *obj = sdb_llist_shift(collector_list);
1199 if (! obj)
1200 return -1;
1202 callback = (sdb_plugin_collector_cb)CCB(obj)->ccb_callback;
1204 if (! (now = sdb_gettime())) {
1205 char errbuf[1024];
1206 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1207 "time in collector main loop: %s",
1208 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1209 now = CCB(obj)->ccb_next_update;
1210 }
1212 if (now < CCB(obj)->ccb_next_update) {
1213 interval = CCB(obj)->ccb_next_update - now;
1215 errno = 0;
1216 while (loop->do_loop && sdb_sleep(interval, &interval)) {
1217 if (errno != EINTR) {
1218 char errbuf[1024];
1219 sdb_log(SDB_LOG_ERR, "core: Failed to sleep "
1220 "in collector main loop: %s",
1221 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1222 sdb_llist_insert_sorted(collector_list, obj,
1223 plugin_cmp_next_update);
1224 sdb_object_deref(obj);
1225 return -1;
1226 }
1227 errno = 0;
1228 }
1230 if (! loop->do_loop) {
1231 /* put back; don't worry about errors */
1232 sdb_llist_insert_sorted(collector_list, obj,
1233 plugin_cmp_next_update);
1234 sdb_object_deref(obj);
1235 return 0;
1236 }
1237 }
1239 old_ctx = ctx_set(CCB(obj)->ccb_ctx);
1240 if (callback(CCB(obj)->ccb_user_data)) {
1241 /* XXX */
1242 }
1243 ctx_set(old_ctx);
1245 interval = CCB(obj)->ccb_interval;
1246 if (! interval)
1247 interval = loop->default_interval;
1248 if (! interval) {
1249 sdb_log(SDB_LOG_WARNING, "core: No interval configured "
1250 "for plugin '%s'; skipping any further "
1251 "iterations.", obj->name);
1252 sdb_object_deref(obj);
1253 continue;
1254 }
1256 CCB(obj)->ccb_next_update += interval;
1258 if (! (now = sdb_gettime())) {
1259 char errbuf[1024];
1260 sdb_log(SDB_LOG_ERR, "core: Failed to determine current "
1261 "time in collector main loop: %s",
1262 sdb_strerror(errno, errbuf, sizeof(errbuf)));
1263 now = CCB(obj)->ccb_next_update;
1264 }
1266 if (now > CCB(obj)->ccb_next_update) {
1267 sdb_log(SDB_LOG_WARNING, "core: Plugin '%s' took too "
1268 "long; skipping iterations to keep up.",
1269 obj->name);
1270 CCB(obj)->ccb_next_update = now;
1271 }
1273 if (sdb_llist_insert_sorted(collector_list, obj,
1274 plugin_cmp_next_update)) {
1275 sdb_log(SDB_LOG_ERR, "core: Failed to re-insert "
1276 "plugin '%s' into collector list. Unable to further "
1277 "use the plugin.",
1278 obj->name);
1279 sdb_object_deref(obj);
1280 return -1;
1281 }
1283 /* pass control back to the list */
1284 sdb_object_deref(obj);
1285 }
1286 return 0;
1287 } /* sdb_plugin_read_loop */
1289 char *
1290 sdb_plugin_cname(char *hostname)
1291 {
1292 sdb_llist_iter_t *iter;
1294 if (! hostname)
1295 return NULL;
1297 if (! cname_list)
1298 return hostname;
1300 iter = sdb_llist_get_iter(cname_list);
1301 while (sdb_llist_iter_has_next(iter)) {
1302 sdb_plugin_cname_cb callback;
1303 char *cname;
1305 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1306 assert(obj);
1308 callback = (sdb_plugin_cname_cb)CB(obj)->cb_callback;
1309 cname = callback(hostname, CB(obj)->cb_user_data);
1310 if (cname) {
1311 free(hostname);
1312 hostname = cname;
1313 }
1314 /* else: don't change hostname */
1315 }
1316 sdb_llist_iter_destroy(iter);
1317 return hostname;
1318 } /* sdb_plugin_cname */
1320 int
1321 sdb_plugin_log(int prio, const char *msg)
1322 {
1323 sdb_llist_iter_t *iter;
1324 int ret = -1;
1326 bool logged = 0;
1328 if (! msg)
1329 return 0;
1331 iter = sdb_llist_get_iter(log_list);
1332 while (sdb_llist_iter_has_next(iter)) {
1333 sdb_plugin_log_cb callback;
1334 int tmp;
1336 sdb_object_t *obj = sdb_llist_iter_get_next(iter);
1337 assert(obj);
1339 callback = (sdb_plugin_log_cb)CB(obj)->cb_callback;
1340 tmp = callback(prio, msg, CB(obj)->cb_user_data);
1341 if (tmp > ret)
1342 ret = tmp;
1344 if (CB(obj)->cb_ctx)
1345 logged = 1;
1346 /* else: this is an internally registered callback */
1347 }
1348 sdb_llist_iter_destroy(iter);
1350 if (! logged)
1351 return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg);
1352 return ret;
1353 } /* sdb_plugin_log */
1355 int
1356 sdb_plugin_vlogf(int prio, const char *fmt, va_list ap)
1357 {
1358 sdb_strbuf_t *buf;
1359 int ret;
1361 if (! fmt)
1362 return 0;
1364 buf = sdb_strbuf_create(64);
1365 if (! buf) {
1366 ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio));
1367 ret += vfprintf(stderr, fmt, ap);
1368 return ret;
1369 }
1371 if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) {
1372 sdb_strbuf_destroy(buf);
1373 return -1;
1374 }
1376 ret = sdb_plugin_log(prio, sdb_strbuf_string(buf));
1377 sdb_strbuf_destroy(buf);
1378 return ret;
1379 } /* sdb_plugin_vlogf */
1381 int
1382 sdb_plugin_logf(int prio, const char *fmt, ...)
1383 {
1384 va_list ap;
1385 int ret;
1387 if (! fmt)
1388 return 0;
1390 va_start(ap, fmt);
1391 ret = sdb_plugin_vlogf(prio, fmt, ap);
1392 va_end(ap);
1393 return ret;
1394 } /* sdb_plugin_logf */
1396 sdb_timeseries_t *
1397 sdb_plugin_fetch_timeseries(const char *type, const char *id,
1398 sdb_timeseries_opts_t *opts)
1399 {
1400 callback_t *plugin;
1401 sdb_plugin_fetch_ts_cb callback;
1402 sdb_timeseries_t *ts;
1404 ctx_t *old_ctx;
1406 if ((! type) || (! id) || (! opts))
1407 return NULL;
1409 plugin = CB(sdb_llist_search_by_name(ts_fetcher_list, type));
1410 if (! plugin) {
1411 sdb_log(SDB_LOG_ERR, "core: Cannot fetch time-series of type %s: "
1412 "no such plugin loaded", type);
1413 errno = ENOENT;
1414 return NULL;
1415 }
1417 old_ctx = ctx_set(plugin->cb_ctx);
1418 callback = (sdb_plugin_fetch_ts_cb)plugin->cb_callback;
1419 ts = callback(id, opts, plugin->cb_user_data);
1420 ctx_set(old_ctx);
1421 return ts;
1422 } /* sdb_plugin_fetch_timeseries */
1424 int
1425 sdb_plugin_query(sdb_ast_node_t *ast, sdb_strbuf_t *buf, sdb_strbuf_t *errbuf)
1426 {
1427 size_t n = sdb_llist_len(reader_list);
1428 reader_t *reader;
1429 sdb_object_t *q;
1430 int status = 0;
1432 if (! ast)
1433 return 0;
1435 if ((ast->type != SDB_AST_TYPE_FETCH)
1436 && (ast->type != SDB_AST_TYPE_LIST)
1437 && (ast->type != SDB_AST_TYPE_LOOKUP)
1438 && (ast->type != SDB_AST_TYPE_TIMESERIES)) {
1439 sdb_log(SDB_LOG_ERR, "core: Cannot execute query of type %s",
1440 SDB_AST_TYPE_TO_STRING(ast));
1441 sdb_strbuf_sprintf(errbuf, "Cannot execute query of type %s",
1442 SDB_AST_TYPE_TO_STRING(ast));
1443 return -1;
1444 }
1446 if (n != 1) {
1447 char *msg = (n > 0)
1448 ? "Cannot execute query: multiple readers not supported"
1449 : "Cannot execute query: no readers registered";
1450 sdb_strbuf_sprintf(errbuf, "%s", msg);
1451 sdb_log(SDB_LOG_ERR, "core: %s", msg);
1452 return -1;
1453 }
1455 reader = READER(sdb_llist_get(reader_list, 0));
1456 assert(reader);
1458 q = reader->impl.prepare_query(ast, errbuf, reader->r_user_data);
1459 if (q)
1460 status = reader->impl.execute_query(q, buf, errbuf, reader->r_user_data);
1461 else
1462 status = -1;
1464 sdb_object_deref(SDB_OBJ(q));
1465 sdb_object_deref(SDB_OBJ(reader));
1466 return status;
1467 } /* sdb_plugin_query */
1469 int
1470 sdb_plugin_store_host(const char *name, sdb_time_t last_update)
1471 {
1472 sdb_store_host_t host = { 0 };
1473 char *cname;
1475 sdb_llist_iter_t *iter;
1476 int status = 0;
1478 if (! name)
1479 return -1;
1481 if (! sdb_llist_len(writer_list)) {
1482 sdb_log(SDB_LOG_ERR, "core: Cannot store host: "
1483 "no writers registered");
1484 return -1;
1485 }
1487 cname = sdb_plugin_cname(strdup(name));
1488 if (! cname) {
1489 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1490 return -1;
1491 }
1493 host.name = cname;
1494 host.last_update = last_update;
1496 iter = sdb_llist_get_iter(writer_list);
1497 while (sdb_llist_iter_has_next(iter)) {
1498 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1499 int s;
1500 assert(writer);
1501 s = writer->impl.store_host(&host, writer->w_user_data);
1502 if (((s > 0) && (status >= 0)) || (s < 0))
1503 status = s;
1504 }
1505 sdb_llist_iter_destroy(iter);
1506 free(cname);
1507 return status;
1508 } /* sdb_plugin_store_host */
1510 int
1511 sdb_plugin_store_service(const char *hostname, const char *name,
1512 sdb_time_t last_update)
1513 {
1514 sdb_store_service_t service = { 0 };
1515 char *cname;
1517 sdb_llist_iter_t *iter;
1518 sdb_data_t d;
1520 int status = 0;
1522 if ((! hostname) || (! name))
1523 return -1;
1525 if (! sdb_llist_len(writer_list)) {
1526 sdb_log(SDB_LOG_ERR, "core: Cannot store service: "
1527 "no writers registered");
1528 return -1;
1529 }
1531 cname = sdb_plugin_cname(strdup(hostname));
1532 if (! cname) {
1533 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1534 return -1;
1535 }
1537 service.hostname = cname;
1538 service.name = name;
1539 service.last_update = last_update;
1541 iter = sdb_llist_get_iter(writer_list);
1542 while (sdb_llist_iter_has_next(iter)) {
1543 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1544 int s;
1545 assert(writer);
1546 s = writer->impl.store_service(&service, writer->w_user_data);
1547 if (((s > 0) && (status >= 0)) || (s < 0))
1548 status = s;
1549 }
1550 sdb_llist_iter_destroy(iter);
1552 if (! status) {
1553 /* record the hostname as an attribute */
1554 d.type = SDB_TYPE_STRING;
1555 d.data.string = cname;
1556 if (sdb_plugin_store_service_attribute(cname, name,
1557 "hostname", &d, last_update))
1558 status = -1;
1559 }
1561 free(cname);
1562 return status;
1563 } /* sdb_plugin_store_service */
1565 int
1566 sdb_plugin_store_metric(const char *hostname, const char *name,
1567 sdb_metric_store_t *store, sdb_time_t last_update)
1568 {
1569 sdb_store_metric_t metric = { 0 };
1570 char *cname;
1572 sdb_llist_iter_t *iter;
1573 sdb_data_t d;
1575 int status = 0;
1577 if ((! hostname) || (! name))
1578 return -1;
1580 if (! sdb_llist_len(writer_list)) {
1581 sdb_log(SDB_LOG_ERR, "core: Cannot store metric: "
1582 "no writers registered");
1583 return -1;
1584 }
1586 cname = sdb_plugin_cname(strdup(hostname));
1587 if (! cname) {
1588 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1589 return -1;
1590 }
1592 if (store && ((! store->type) || (! store->id)))
1593 store = NULL;
1595 metric.hostname = cname;
1596 metric.name = name;
1597 if (store) {
1598 metric.store.type = store->type;
1599 metric.store.id = store->id;
1600 }
1601 metric.last_update = last_update;
1603 iter = sdb_llist_get_iter(writer_list);
1604 while (sdb_llist_iter_has_next(iter)) {
1605 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1606 int s;
1607 assert(writer);
1608 s = writer->impl.store_metric(&metric, writer->w_user_data);
1609 if (((s > 0) && (status >= 0)) || (s < 0))
1610 status = s;
1611 }
1612 sdb_llist_iter_destroy(iter);
1614 if (! status) {
1615 /* record the hostname as an attribute */
1616 d.type = SDB_TYPE_STRING;
1617 d.data.string = cname;
1618 if (sdb_plugin_store_metric_attribute(cname, name,
1619 "hostname", &d, last_update))
1620 status = -1;
1621 }
1623 free(cname);
1624 return status;
1625 } /* sdb_plugin_store_metric */
1627 int
1628 sdb_plugin_store_attribute(const char *hostname, const char *key,
1629 const sdb_data_t *value, sdb_time_t last_update)
1630 {
1631 sdb_store_attribute_t attr = { 0 };
1632 char *cname;
1634 sdb_llist_iter_t *iter;
1635 int status = 0;
1637 if ((! hostname) || (! key) || (! value))
1638 return -1;
1640 if (! sdb_llist_len(writer_list)) {
1641 sdb_log(SDB_LOG_ERR, "core: Cannot store attribute: "
1642 "no writers registered");
1643 return -1;
1644 }
1646 cname = sdb_plugin_cname(strdup(hostname));
1647 if (! cname) {
1648 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1649 return -1;
1650 }
1652 attr.parent_type = SDB_HOST;
1653 attr.parent = cname;
1654 attr.key = key;
1655 attr.value = *value;
1656 attr.last_update = last_update;
1658 iter = sdb_llist_get_iter(writer_list);
1659 while (sdb_llist_iter_has_next(iter)) {
1660 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1661 int s;
1662 assert(writer);
1663 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1664 if (((s > 0) && (status >= 0)) || (s < 0))
1665 status = s;
1666 }
1667 sdb_llist_iter_destroy(iter);
1668 free(cname);
1669 return status;
1670 } /* sdb_plugin_store_attribute */
1672 int
1673 sdb_plugin_store_service_attribute(const char *hostname, const char *service,
1674 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1675 {
1676 sdb_store_attribute_t attr = { 0 };
1677 char *cname;
1679 sdb_llist_iter_t *iter;
1680 int status = 0;
1682 if ((! hostname) || (! service) || (! key) || (! value))
1683 return -1;
1685 if (! sdb_llist_len(writer_list)) {
1686 sdb_log(SDB_LOG_ERR, "core: Cannot store service attribute: "
1687 "no writers registered");
1688 return -1;
1689 }
1691 cname = sdb_plugin_cname(strdup(hostname));
1692 if (! cname) {
1693 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1694 return -1;
1695 }
1697 attr.hostname = cname;
1698 attr.parent_type = SDB_SERVICE;
1699 attr.parent = service;
1700 attr.key = key;
1701 attr.value = *value;
1702 attr.last_update = last_update;
1704 iter = sdb_llist_get_iter(writer_list);
1705 while (sdb_llist_iter_has_next(iter)) {
1706 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1707 int s;
1708 assert(writer);
1709 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1710 if (((s > 0) && (status >= 0)) || (s < 0))
1711 status = s;
1712 }
1713 sdb_llist_iter_destroy(iter);
1714 free(cname);
1715 return status;
1716 } /* sdb_plugin_store_service_attribute */
1718 int
1719 sdb_plugin_store_metric_attribute(const char *hostname, const char *metric,
1720 const char *key, const sdb_data_t *value, sdb_time_t last_update)
1721 {
1722 sdb_store_attribute_t attr = { 0 };
1723 char *cname;
1725 sdb_llist_iter_t *iter;
1726 int status = 0;
1728 if ((! hostname) || (! metric) || (! key) || (! value))
1729 return -1;
1731 if (! sdb_llist_len(writer_list)) {
1732 sdb_log(SDB_LOG_ERR, "core: Cannot store metric attribute: "
1733 "no writers registered");
1734 return -1;
1735 }
1737 cname = sdb_plugin_cname(strdup(hostname));
1738 if (! cname) {
1739 sdb_log(SDB_LOG_ERR, "core: strdup failed");
1740 return -1;
1741 }
1743 attr.hostname = cname;
1744 attr.parent_type = SDB_METRIC;
1745 attr.parent = metric;
1746 attr.key = key;
1747 attr.value = *value;
1748 attr.last_update = last_update;
1750 iter = sdb_llist_get_iter(writer_list);
1751 while (sdb_llist_iter_has_next(iter)) {
1752 writer_t *writer = WRITER(sdb_llist_iter_get_next(iter));
1753 int s;
1754 assert(writer);
1755 s = writer->impl.store_attribute(&attr, writer->w_user_data);
1756 if (((s > 0) && (status >= 0)) || (s < 0))
1757 status = s;
1758 }
1759 sdb_llist_iter_destroy(iter);
1760 free(cname);
1761 return status;
1762 } /* sdb_plugin_store_metric_attribute */
1764 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */