Code

Moved core/error to utils/error.
[sysdb.git] / src / backend / collectd / unixsock.c
index 01c2f25b43d17adceddd47c9c9a8195f033ca067..766bbce9f78bc4297890b5e7749cbbc4f494f407 100644 (file)
@@ -62,29 +62,21 @@ typedef struct {
 static int
 sdb_collectd_add_host(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));
-
-       host.host_name = name;
-       host.host_last_update = last_update;
-
-       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'.\n", name);
+                               "store/update host '%s'.", hostname);
                return -1;
        }
        else if (status > 0) /* value too old */
                return 0;
 
        sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
-                       "host '%s' (last update timestamp = %"PRIscTIME").\n",
-                       name, last_update);
+                       "host '%s' (last update timestamp = %"PRIscTIME").",
+                       hostname, last_update);
        return 0;
 } /* sdb_collectd_add_host */
 
@@ -92,23 +84,15 @@ 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.svc_name = name;
-       svc.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'.\n", host, name);
+                               "store/update service '%s/%s'.", hostname, name);
                return -1;
        }
        return 0;
@@ -148,12 +132,12 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
        if (! state->current_host) {
                char errbuf[1024];
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
-                               "string buffer: %s\n",
+                               "string buffer: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
-       if (! sdb_store_get_host(hostname))
+       if (! sdb_store_has_host(hostname))
                sdb_collectd_add_host(hostname, last_update);
 
        if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
@@ -171,7 +155,7 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
        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'.\n",
+                       "%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;
@@ -197,12 +181,12 @@ sdb_collectd_init(sdb_object_t *user_data)
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_unixsock_client_connect(client)) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
-                               "Failed to connect to collectd.\n");
+                               "Failed to connect to collectd.");
                return -1;
        }
 
        sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
-                       "connected to collectd @ %s.\n",
+                       "connected to collectd @ %s.",
                        sdb_unixsock_client_path(client));
        return 0;
 } /* sdb_collectd_init */
@@ -236,7 +220,7 @@ sdb_collectd_collect(sdb_object_t *user_data)
 
        if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
-                               "LISTVAL command to collectd @ %s.\n",
+                               "LISTVAL command to collectd @ %s.",
                                sdb_unixsock_client_path(client));
                return -1;
        }
@@ -244,7 +228,7 @@ sdb_collectd_collect(sdb_object_t *user_data)
        line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
        if (! line) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
-                               "status of LISTVAL command from collectd @ %s.\n",
+                               "status of LISTVAL command from collectd @ %s.",
                                sdb_unixsock_client_path(client));
                return -1;
        }
@@ -259,14 +243,14 @@ sdb_collectd_collect(sdb_object_t *user_data)
        count = strtol(line, &endptr, /* base */ 0);
        if (errno || (line == endptr)) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
-                               "status of LISTVAL command from collectd @ %s.\n",
+                               "status of LISTVAL command from collectd @ %s.",
                                sdb_unixsock_client_path(client));
                return -1;
        }
 
        if (count < 0) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
-                               "value list from collectd @ %s: %s\n",
+                               "value list from collectd @ %s: %s",
                                sdb_unixsock_client_path(client),
                                msg ? msg : line);
                return -1;
@@ -278,7 +262,7 @@ sdb_collectd_collect(sdb_object_t *user_data)
                                SDB_TYPE_DATETIME, SDB_TYPE_STRING,
                                SDB_TYPE_STRING, SDB_TYPE_STRING)) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
-                               "to read response from collectd @ %s.\n",
+                               "to read response from collectd @ %s.",
                                sdb_unixsock_client_path(client));
                return -1;
        }
@@ -286,7 +270,7 @@ 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'.\n",
+                               "%i service%s (%i failed) for host '%s'.",
                                state.svc_updated, state.svc_updated == 1 ? "" : "s",
                                state.svc_failed, state.current_host);
        }
@@ -308,7 +292,7 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
 
        if (oconfig_get_string(ci, &name)) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
-                               "a single string argument\n\tUsage: <Instance NAME>\n");
+                               "a single string argument\n\tUsage: <Instance NAME>");
                return -1;
        }
 
@@ -319,13 +303,13 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                        oconfig_get_string(child, &socket_path);
                else
                        sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
-                                       "unknown config option '%s' inside <Instance %s>.\n",
+                                       "unknown config option '%s' inside <Instance %s>.",
                                        child->key, name);
        }
 
        if (! socket_path) {
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
-                               "missing the 'Socket' option.\n", name);
+                               "missing the 'Socket' option.", name);
                return -1;
        }
 
@@ -336,17 +320,17 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
        if (! client) {
                char errbuf[1024];
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
-                               "unixsock client: %s\n",
+                               "unixsock client: %s",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                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);
                sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
-                               "sdb_object_t\n");
+                               "sdb_object_t");
                return -1;
        }
 
@@ -373,7 +357,7 @@ sdb_collectd_config(oconfig_item_t *ci)
                        sdb_collectd_config_instance(child);
                else
                        sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
-                                       "unknown config option '%s'.\n", child->key);
+                                       "unknown config option '%s'.", child->key);
        }
        return 0;
 } /* sdb_collectd_config */