Code

plugin: Automatically prepend callback names with the plugin name.
authorSebastian Harl <sh@tokkee.org>
Sat, 28 Jun 2014 07:19:49 +0000 (09:19 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 28 Jun 2014 07:19:49 +0000 (09:19 +0200)
src/backend/collectd/unixsock.c
src/backend/mk-livestatus.c
src/backend/puppet/store-configs.c
src/core/plugin.c
src/include/core/plugin.h
src/plugins/cname/dns.c
src/plugins/syslog.c
t/integration/mock_plugin.c

index 3a41fdf..21e1f8b 100644 (file)
@@ -286,8 +286,6 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
        char *name = NULL;
        char *socket_path = NULL;
 
-       char cb_name[1024];
-
        sdb_object_t *user_data;
        sdb_unixsock_client_t *client;
 
@@ -316,9 +314,6 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
-       cb_name[sizeof(cb_name) - 1] = '\0';
-
        client = sdb_unixsock_client_create(socket_path);
        if (! client) {
                char errbuf[1024];
@@ -337,10 +332,10 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
-       sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
+       sdb_plugin_register_init(name, sdb_collectd_init, user_data);
+       sdb_plugin_register_shutdown(name, sdb_collectd_shutdown, user_data);
 
-       sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
+       sdb_plugin_register_collector(name, sdb_collectd_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
index b6dc1a7..eb544b8 100644 (file)
@@ -238,8 +238,6 @@ sdb_livestatus_config_instance(oconfig_item_t *ci)
        char *name = NULL;
        char *socket_path = NULL;
 
-       char cb_name[1024];
-
        sdb_object_t *user_data;
        sdb_unixsock_client_t *client;
 
@@ -268,9 +266,6 @@ sdb_livestatus_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       snprintf(cb_name, sizeof(cb_name), "mk-livestatus::%s", name);
-       cb_name[sizeof(cb_name) - 1] = '\0';
-
        client = sdb_unixsock_client_create(socket_path);
        if (! client) {
                char errbuf[1024];
@@ -289,9 +284,9 @@ sdb_livestatus_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
-       sdb_plugin_register_shutdown(cb_name, sdb_livestatus_shutdown, user_data);
-       sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
+       sdb_plugin_register_init(name, sdb_livestatus_init, user_data);
+       sdb_plugin_register_shutdown(name, sdb_livestatus_shutdown, user_data);
+       sdb_plugin_register_collector(name, sdb_livestatus_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
index 409343f..23d261b 100644 (file)
@@ -194,7 +194,6 @@ static int
 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
 {
        char *name = NULL;
-       char cb_name[1024];
 
        sdb_object_t *user_data;
        sdb_dbi_client_t *client;
@@ -315,9 +314,6 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                return -1;
        }
 
-       snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
-       cb_name[sizeof(cb_name) - 1] = '\0';
-
        client = sdb_dbi_client_create(driver, database);
        if (! client) {
                char errbuf[1024];
@@ -338,10 +334,10 @@ sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
                return -1;
        }
 
-       sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
-       sdb_plugin_register_shutdown(cb_name, sdb_puppet_stcfg_shutdown,
+       sdb_plugin_register_init(name, sdb_puppet_stcfg_init, user_data);
+       sdb_plugin_register_shutdown(name, sdb_puppet_stcfg_shutdown,
                        user_data);
-       sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
+       sdb_plugin_register_collector(name, sdb_puppet_stcfg_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
index 282a521..32966de 100644 (file)
@@ -539,6 +539,18 @@ module_load(const char *basedir, const char *name,
        return 0;
 } /* module_load */
 
+static char *
+plugin_get_name(const char *name, char *buf, size_t bufsize)
+{
+       ctx_t *ctx = ctx_get();
+
+       if (ctx)
+               snprintf(buf, bufsize, "%s::%s", ctx->info.plugin_name, name);
+       else
+               snprintf(buf, bufsize, "core::%s", name);
+       return buf;
+} /* plugin_get_name */
+
 static int
 plugin_add_callback(sdb_llist_t **list, const char *type,
                const char *name, void *callback, sdb_object_t *user_data)
@@ -706,7 +718,9 @@ int
 sdb_plugin_register_init(const char *name, sdb_plugin_init_cb callback,
                sdb_object_t *user_data)
 {
-       return plugin_add_callback(&init_list, "init", name,
+       char cb_name[1024];
+       return plugin_add_callback(&init_list, "init",
+                       plugin_get_name(name, cb_name, sizeof(cb_name)),
                        (void *)callback, user_data);
 } /* sdb_plugin_register_init */
 
@@ -714,7 +728,9 @@ int
 sdb_plugin_register_shutdown(const char *name, sdb_plugin_shutdown_cb callback,
                sdb_object_t *user_data)
 {
-       return plugin_add_callback(&shutdown_list, "shutdown", name,
+       char cb_name[1024];
+       return plugin_add_callback(&shutdown_list, "shutdown",
+                       plugin_get_name(name, cb_name, sizeof(cb_name)),
                        (void *)callback, user_data);
 } /* sdb_plugin_register_shutdown */
 
@@ -722,22 +738,27 @@ int
 sdb_plugin_register_log(const char *name, sdb_plugin_log_cb callback,
                sdb_object_t *user_data)
 {
-       return plugin_add_callback(&log_list, "log", name, (void *)callback,
-                       user_data);
+       char cb_name[1024];
+       return plugin_add_callback(&log_list, "log",
+                       plugin_get_name(name, cb_name, sizeof(cb_name)),
+                       callback, user_data);
 } /* sdb_plugin_register_log */
 
 int
 sdb_plugin_register_cname(const char *name, sdb_plugin_cname_cb callback,
                sdb_object_t *user_data)
 {
-       return plugin_add_callback(&cname_list, "cname", name, (void *)callback,
-                       user_data);
+       char cb_name[1024];
+       return plugin_add_callback(&cname_list, "cname",
+                       plugin_get_name(name, cb_name, sizeof(cb_name)),
+                       callback, user_data);
 } /* sdb_plugin_register_cname */
 
 int
 sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback,
                const sdb_time_t *interval, sdb_object_t *user_data)
 {
+       char cb_name[1024];
        sdb_object_t *obj;
 
        if ((! name) || (! callback))
@@ -748,7 +769,9 @@ sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback
        if (! collector_list)
                return -1;
 
-       obj = sdb_object_create(name, sdb_plugin_collector_cb_type,
+       plugin_get_name(name, cb_name, sizeof(cb_name));
+
+       obj = sdb_object_create(cb_name, sdb_plugin_collector_cb_type,
                        &collector_list, "collector", callback, user_data);
        if (! obj)
                return -1;
@@ -782,7 +805,7 @@ sdb_plugin_register_collector(const char *name, sdb_plugin_collector_cb callback
        sdb_object_deref(obj);
 
        sdb_log(SDB_LOG_INFO, "core: Registered collector callback '%s' "
-                       "(interval = %.3fs).", name,
+                       "(interval = %.3fs).", cb_name,
                        SDB_TIME_TO_DOUBLE(SDB_PLUGIN_CCB(obj)->ccb_interval));
        return 0;
 } /* sdb_plugin_register_collector */
index 51d5905..bcf01ec 100644 (file)
@@ -93,6 +93,9 @@ sdb_plugin_set_info(sdb_plugin_info_t *info, int type, ...);
  * plugin callback functions:
  * See the description of the respective register function for what arguments
  * the callbacks expect.
+ *
+ * The specified name of callback functions is prepended with the plugin name
+ * before being registered with the core.
  */
 
 typedef int (*sdb_plugin_config_cb)(oconfig_item_t *ci);
index 26b3500..b35ebf1 100644 (file)
@@ -93,7 +93,7 @@ sdb_module_init(sdb_plugin_info_t *info)
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
 
-       sdb_plugin_register_cname("cname::dns", sdb_dns_cname, NULL);
+       sdb_plugin_register_cname("main", sdb_dns_cname, NULL);
        return 0;
 } /* sdb_module_init */
 
index a487521..f9a285e 100644 (file)
@@ -89,9 +89,9 @@ sdb_module_init(sdb_plugin_info_t *info)
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
 
-       sdb_plugin_register_init("syslog", sdb_syslog_init, NULL);
-       sdb_plugin_register_log("syslog", sdb_syslog_log, NULL);
-       sdb_plugin_register_shutdown("syslog", sdb_syslog_shutdown, NULL);
+       sdb_plugin_register_init("main", sdb_syslog_init, NULL);
+       sdb_plugin_register_log("main", sdb_syslog_log, NULL);
+       sdb_plugin_register_shutdown("main", sdb_syslog_shutdown, NULL);
        return 0;
 } /* sdb_module_init */
 
index 7e2b4a6..2573e64 100644 (file)
@@ -183,9 +183,9 @@ mock_config(oconfig_item_t *ci)
                exit(1);
        }
 
-       sdb_plugin_register_init("mock::init", mock_init, user_data);
-       sdb_plugin_register_shutdown("mock::shutdown", mock_shutdown, user_data);
-       sdb_plugin_register_collector("mock::collect", mock_collect,
+       sdb_plugin_register_init("main", mock_init, user_data);
+       sdb_plugin_register_shutdown("main", mock_shutdown, user_data);
+       sdb_plugin_register_collector("main", mock_collect,
                        /* interval = */ NULL, user_data);
 
        sdb_object_deref(user_data);