Code

Use the sdb_error* function throughout the project.
[sysdb.git] / src / backend / collectd.c
index e0e924e2c959e946f7e645a6879aba0be5ede13c..2a834f51982ca1248763674cecb34c41370215ac 100644 (file)
@@ -28,6 +28,7 @@
 #include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
+#include "utils/error.h"
 #include "utils/string.h"
 #include "utils/unixsock.h"
 
@@ -75,14 +76,14 @@ sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
        status = sdb_store_host(&host);
 
        if (status < 0) {
-               fprintf(stderr, "collectd backend: Failed to store/update "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update "
                                "host '%s'.\n", name);
                return -1;
        }
        else if (status > 0) /* value too old */
                return 0;
 
-       fprintf(stderr, "collectd backend: Added/updated host '%s' "
+       sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated host '%s' "
                        "(last update timestamp = %"PRIscTIME").\n",
                        name, last_update);
        return 0;
@@ -107,7 +108,7 @@ sdb_collectd_add_svc(const char *hostname, const char *plugin,
 
        status = sdb_store_service(&svc);
        if (status < 0) {
-               fprintf(stderr, "collectd backend: Failed to store/update "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to store/update "
                                "service '%s/%s'.\n", host, name);
                return -1;
        }
@@ -147,7 +148,7 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
 
        if (! state->current_host) {
                char errbuf[1024];
-               fprintf(stderr, "collectd backend: Failed to allocate "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate "
                                "string buffer: %s\n",
                                sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
@@ -170,7 +171,7 @@ sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
        /* new host */
        sdb_collectd_add_host(hostname, last_update);
 
-       fprintf(stderr, "collectd backend: Added/updated "
+       sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated "
                        "%i service%s (%i failed) for host '%s'.\n",
                        state->svc_updated, state->svc_updated == 1 ? "" : "s",
                        state->svc_failed, state->current_host);
@@ -196,12 +197,12 @@ sdb_collectd_init(sdb_object_t *user_data)
 
        client = SDB_OBJ_WRAPPER(user_data)->data;
        if (sdb_unixsock_client_connect(client)) {
-               fprintf(stderr, "collectd backend: "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: "
                                "Failed to connect to collectd.\n");
                return -1;
        }
 
-       fprintf(stderr, "collectd backend: Successfully "
+       sdb_error_set(SDB_LOG_INFO, "collectd backend: Successfully "
                        "connected to collectd @ %s.\n",
                        sdb_unixsock_client_path(client));
        return 0;
@@ -235,14 +236,15 @@ sdb_collectd_collect(sdb_object_t *user_data)
        client = SDB_OBJ_WRAPPER(user_data)->data;
 
        if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
-               fprintf(stderr, "collectd backend: Failed to send LISTVAL command "
-                               "to collectd @ %s.\n", sdb_unixsock_client_path(client));
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to send LISTVAL "
+                               "command to collectd @ %s.\n",
+                               sdb_unixsock_client_path(client));
                return -1;
        }
 
        line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
        if (! line) {
-               fprintf(stderr, "collectd backend: Failed to read status "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read status "
                                "of LISTVAL command from collectd @ %s.\n",
                                sdb_unixsock_client_path(client));
                return -1;
@@ -257,15 +259,16 @@ sdb_collectd_collect(sdb_object_t *user_data)
        errno = 0;
        count = strtol(line, &endptr, /* base */ 0);
        if (errno || (line == endptr)) {
-               fprintf(stderr, "collectd backend: Failed to parse status "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to parse status "
                                "of LISTVAL command from collectd @ %s.\n",
                                sdb_unixsock_client_path(client));
                return -1;
        }
 
        if (count < 0) {
-               fprintf(stderr, "collectd backend: Failed to get value list "
-                               "from collectd @ %s: %s\n", sdb_unixsock_client_path(client),
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to get value "
+                               "list from collectd @ %s: %s\n",
+                               sdb_unixsock_client_path(client),
                                msg ? msg : line);
                return -1;
        }
@@ -275,14 +278,14 @@ sdb_collectd_collect(sdb_object_t *user_data)
                                /* column count = */ 4,
                                SDB_TYPE_DATETIME, SDB_TYPE_STRING,
                                SDB_TYPE_STRING, SDB_TYPE_STRING)) {
-               fprintf(stderr, "collectd backend: Failed to read response "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to read response "
                                "from collectd @ %s.\n", sdb_unixsock_client_path(client));
                return -1;
        }
 
        if (state.current_host) {
                sdb_collectd_add_host(state.current_host, state.current_timestamp);
-               fprintf(stderr, "collectd backend: Added/updated "
+               sdb_error_set(SDB_LOG_DEBUG, "collectd backend: Added/updated "
                                "%i service%s (%i failed) for host '%s'.\n",
                                state.svc_updated, state.svc_updated == 1 ? "" : "s",
                                state.svc_failed, state.current_host);
@@ -304,8 +307,8 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
        int i;
 
        if (oconfig_get_string(ci, &name)) {
-               fprintf(stderr, "collectd backend: Instance requires a single "
-                               "string argument\n\tUsage: <Instance NAME>\n");
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance requires a "
+                               "single string argument\n\tUsage: <Instance NAME>\n");
                return -1;
        }
 
@@ -315,13 +318,13 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                if (! strcasecmp(child->key, "Socket"))
                        oconfig_get_string(child, &socket_path);
                else
-                       fprintf(stderr, "collectd backend: Ignoring unknown config "
-                                       "option '%s' inside <Instance %s>.\n",
+                       sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring "
+                                       "unknown config option '%s' inside <Instance %s>.\n",
                                        child->key, name);
        }
 
        if (! socket_path) {
-               fprintf(stderr, "collectd backend: Instance '%s' missing "
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Instance '%s' missing "
                                "the 'Socket' option.\n", name);
                return -1;
        }
@@ -332,8 +335,9 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
        client = sdb_unixsock_client_create(socket_path);
        if (! client) {
                char errbuf[1024];
-               fprintf(stderr, "collectd backend: Failed to create unixsock client: "
-                               "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to create "
+                               "unixsock client: %s\n",
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
@@ -341,7 +345,8 @@ sdb_collectd_config_instance(oconfig_item_t *ci)
                        (void (*)(void *))sdb_unixsock_client_destroy);
        if (! user_data) {
                sdb_unixsock_client_destroy(client);
-               fprintf(stderr, "collectd backend: Failed to allocate sdb_object_t\n");
+               sdb_error_set(SDB_LOG_ERR, "collectd backend: Failed to allocate "
+                               "sdb_object_t\n");
                return -1;
        }
 
@@ -367,8 +372,8 @@ sdb_collectd_config(oconfig_item_t *ci)
                if (! strcasecmp(child->key, "Instance"))
                        sdb_collectd_config_instance(child);
                else
-                       fprintf(stderr, "collectd backend: Ignoring unknown config "
-                                       "option '%s'.\n", child->key);
+                       sdb_error_set(SDB_LOG_WARNING, "collectd backend: Ignoring "
+                                       "unknown config option '%s'.\n", child->key);
        }
        return 0;
 } /* sdb_collectd_config */