Code

plugin: The replaced SDB_PLUGIN_INFO_NAME with the actual plugin name.
[sysdb.git] / src / backend / collectd / unixsock.c
index 945773983ee173c726049d32e22db59a06000d24..00e1258eb222761d052a556824deac68fb690997 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
-#include "core/error.h"
+#include "utils/error.h"
 #include "utils/unixsock.h"
 
 #include "liboconfig/utils.h"
@@ -59,24 +63,43 @@ typedef struct {
  * private helper functions
  */
 
+/* store the specified host-name (once per iteration) */
 static int
-sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
+sdb_collectd_store_host(sdb_collectd_state_t *state,
+               const char *hostname, sdb_time_t last_update)
 {
-       sdb_host_t host = SDB_HOST_INIT;
-       char name[strlen(hostname) + 1];
-
        int status;
 
-       strncpy(name, hostname, sizeof(name));
+       if (last_update > state->current_timestamp)
+               state->current_timestamp = last_update;
+
+       if (state->current_host && (! strcasecmp(state->current_host, hostname)))
+               return 0;
+       /* else: first/new host */
+
+       if (state->current_host) {
+               sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
+                               "%i service%s (%i failed) for host '%s'.",
+                               state->svc_updated, state->svc_updated == 1 ? "" : "s",
+                               state->svc_failed, state->current_host);
+               state->svc_updated = state->svc_failed = 0;
+               free(state->current_host);
+       }
 
-       host._name = name;
-       host._last_update = last_update;
+       state->current_host = strdup(hostname);
+       if (! state->current_host) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
+                               "string buffer: %s",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               return -1;
+       }
 
-       status = sdb_store_host(&host);
+       status = sdb_store_host(hostname, last_update);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
-                               "store/update host '%s'.", name);
+                               "store/update host '%s'.", hostname);
                return -1;
        }
        else if (status > 0) /* value too old */
@@ -84,31 +107,23 @@ sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
 
        sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
                        "host '%s' (last update timestamp = %"PRIscTIME").",
-                       name, last_update);
+                       hostname, last_update);
        return 0;
-} /* sdb_collectd_add_host */
+} /* sdb_collectd_store_host */
 
 static int
 sdb_collectd_add_svc(const char *hostname, const char *plugin,
                const char *type, sdb_time_t last_update)
 {
-       sdb_service_t svc = SDB_SVC_INIT;
-       char host[strlen(hostname) + 1];
        char name[strlen(plugin) + strlen(type) + 2];
+       int  status;
 
-       int status;
-
-       strncpy(host, hostname, sizeof(host));
        snprintf(name, sizeof(name), "%s/%s", plugin, type);
 
-       svc.hostname = host;
-       svc._name = name;
-       svc._last_update = last_update;
-
-       status = sdb_store_service(&svc);
+       status = sdb_store_service(hostname, name, last_update);
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
-                               "store/update service '%s/%s'.", host, name);
+                               "store/update service '%s/%s'.", hostname, name);
                return -1;
        }
        return 0;
@@ -139,46 +154,13 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
        type     = data[3].data.string;
 
        state = SDB_OBJ_WRAPPER(user_data)->data;
-
-       if (! state->current_host) {
-               state->current_host = strdup(hostname);
-               state->current_timestamp = last_update;
-       }
-
-       if (! state->current_host) {
-               char errbuf[1024];
-               sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
-                               "string buffer: %s",
-                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+       if (sdb_collectd_store_host(state, hostname, last_update))
                return -1;
-       }
-
-       if (! sdb_store_get_host(hostname))
-               sdb_collectd_add_host(hostname, last_update);
 
        if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
                ++state->svc_failed;
        else
                ++state->svc_updated;
-
-       if (! strcasecmp(state->current_host, hostname)) {
-               if (last_update > state->current_timestamp)
-                       state->current_timestamp = last_update;
-               return 0;
-       }
-
-       /* new host */
-       sdb_collectd_add_host(hostname, last_update);
-
-       sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
-                       "%i service%s (%i failed) for host '%s'.",
-                       state->svc_updated, state->svc_updated == 1 ? "" : "s",
-                       state->svc_failed, state->current_host);
-       state->svc_updated = state->svc_failed = 0;
-
-       free(state->current_host);
-       state->current_host = strdup(hostname);
-       state->current_timestamp = last_update;
        return 0;
 } /* sdb_collectd_get_data */
 
@@ -210,6 +192,11 @@ sdb_collectd_init(sdb_object_t *user_data)
 static int
 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
 {
+       if (! user_data)
+               return -1;
+
+       sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
+       SDB_OBJ_WRAPPER(user_data)->data = NULL;
        return 0;
 } /* sdb_collectd_shutdown */
 
@@ -284,11 +271,11 @@ sdb_collectd_collect(sdb_object_t *user_data)
        }
 
        if (state.current_host) {
-               sdb_collectd_add_host(state.current_host, state.current_timestamp);
                sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
                                "%i service%s (%i failed) for host '%s'.",
                                state.svc_updated, state.svc_updated == 1 ? "" : "s",
                                state.svc_failed, state.current_host);
+               free(state.current_host);
        }
        return 0;
 } /* sdb_collectd_collect */
@@ -299,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;
 
@@ -329,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];
@@ -341,7 +323,7 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       user_data = sdb_object_create_wrapper(client,
+       user_data = sdb_object_create_wrapper("unixsock-client", client,
                        (void (*)(void *))sdb_unixsock_client_destroy);
        if (! user_data) {
                sdb_unixsock_client_destroy(client);
@@ -350,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 */
@@ -366,6 +348,9 @@ sdb_collectd_config(oconfig_item_t *ci)
 {
        int i;
 
+       if (! ci) /* nothing to do to deconfigure this plugin */
+               return 0;
+
        for (i = 0; i < ci->children_num; ++i) {
                oconfig_item_t *child = ci->children + i;
 
@@ -381,7 +366,6 @@ sdb_collectd_config(oconfig_item_t *ci)
 int
 sdb_module_init(sdb_plugin_info_t *info)
 {
-       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
                        "backend accessing the system statistics collection daemon "
                        "throught the UNIXSOCK interface");
@@ -391,7 +375,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_config("collectd::unixsock", sdb_collectd_config);
+       sdb_plugin_register_config(sdb_collectd_config);
        return 0;
 } /* sdb_version_extra */