Code

collectd::unixsock backend: Added support for spaces in identifiers.
[sysdb.git] / src / backend / collectd / unixsock.c
index 0c5082af54c0b9ca99a95ef8fea295ffeeae1e3b..1faa9c80394aca5db5f5cc390ba6ed49b5521ddd 100644 (file)
@@ -135,29 +135,48 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
 {
        sdb_collectd_state_t *state;
 
-       const char *hostname;
+       char *hostname;
        const char *plugin;
        const char *type;
-       sdb_time_t last_update;
+       sdb_data_t last_update;
 
        assert(user_data);
 
-       assert(n == 4);
-       assert((data[0].type == SDB_TYPE_DATETIME)
+       /* 0: <last_update> <hostname>
+        * 1: <plugin>
+        * 2: <type> */
+       assert(n == 3);
+       assert((data[0].type == SDB_TYPE_STRING)
                        && (data[1].type == SDB_TYPE_STRING)
-                       && (data[2].type == SDB_TYPE_STRING)
-                       && (data[3].type == SDB_TYPE_STRING));
+                       && (data[2].type == SDB_TYPE_STRING));
 
-       last_update = data[0].data.datetime;
-       hostname = data[1].data.string;
-       plugin   = data[2].data.string;
-       type     = data[3].data.string;
+       hostname = data[0].data.string;
+       plugin   = data[1].data.string;
+       type     = data[2].data.string;
+
+       hostname = strchr(hostname, ' ');
+       if (! hostname) {
+               sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
+                               "a space character in the LISTVAL response");
+               return -1;
+       }
+       *hostname = '\0';
+       ++hostname;
+
+       if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
+               char errbuf[1024];
+               sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
+                               "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               return -1;
+       }
 
        state = SDB_OBJ_WRAPPER(user_data)->data;
-       if (sdb_collectd_store_host(state, hostname, last_update))
+       if (sdb_collectd_store_host(state, hostname, last_update.data.datetime))
                return -1;
 
-       if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
+       if (sdb_collectd_add_svc(hostname, plugin, type,
+                               last_update.data.datetime))
                ++state->svc_failed;
        else
                ++state->svc_updated;
@@ -192,6 +211,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 */
 
@@ -255,10 +279,9 @@ sdb_collectd_collect(sdb_object_t *user_data)
        }
 
        if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
-                               SDB_OBJ(&state_obj), count, /* delim */ " /",
-                               /* column count = */ 4,
-                               SDB_TYPE_DATETIME, SDB_TYPE_STRING,
-                               SDB_TYPE_STRING, SDB_TYPE_STRING)) {
+                               SDB_OBJ(&state_obj), count, /* delim */ "/",
+                               /* column count = */ 3,
+                               SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
                                "to read response from collectd @ %s.",
                                sdb_unixsock_client_path(client));
@@ -270,6 +293,7 @@ sdb_collectd_collect(sdb_object_t *user_data)
                                "%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 */
@@ -280,8 +304,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;
 
@@ -310,9 +332,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];
@@ -331,10 +350,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 */
@@ -365,7 +384,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");
@@ -375,7 +393,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 */