Code

Renamed the project to SysDB (System DataBase).
[sysdb.git] / src / backend / collectd.c
index 1cd650e7a4fc637df7adba007c8de15c809f23ce..f745fc9316d66e9849c219c4ebe2e1d3769a78a9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * syscollector - src/backend/collectd.c
+ * SysDB - src/backend/collectd.c
  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
  * All rights reserved.
  *
@@ -25,7 +25,7 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "syscollector.h"
+#include "sysdb.h"
 #include "core/plugin.h"
 #include "core/store.h"
 #include "utils/string.h"
@@ -41,7 +41,7 @@
 #include <stdlib.h>
 #include <string.h>
 
-SC_PLUGIN_MAGIC;
+SDB_PLUGIN_MAGIC;
 
 /*
  * private data types
@@ -49,20 +49,20 @@ SC_PLUGIN_MAGIC;
 
 typedef struct {
        char *current_host;
-       sc_time_t current_timestamp;
+       sdb_time_t current_timestamp;
        int svc_updated;
        int svc_failed;
-} sc_collectd_state_t;
-#define SC_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
+} sdb_collectd_state_t;
+#define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
 
 /*
  * private helper functions
  */
 
 static int
-sc_collectd_add_host(const char *hostname, sc_time_t last_update)
+sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
 {
-       sc_host_t host = SC_HOST_INIT;
+       sdb_host_t host = SDB_HOST_INIT;
        char name[strlen(hostname) + 1];
 
        int status;
@@ -72,7 +72,7 @@ sc_collectd_add_host(const char *hostname, sc_time_t last_update)
        host.host_name = name;
        host.host_last_update = last_update;
 
-       status = sc_store_host(&host);
+       status = sdb_store_host(&host);
 
        if (status < 0) {
                fprintf(stderr, "collectd backend: Failed to store/update "
@@ -86,13 +86,13 @@ sc_collectd_add_host(const char *hostname, sc_time_t last_update)
                        "(last update timestamp = %"PRIscTIME").\n",
                        name, last_update);
        return 0;
-} /* sc_collectd_add_host */
+} /* sdb_collectd_add_host */
 
 static int
-sc_collectd_add_svc(const char *hostname, const char *plugin,
-               const char *type, sc_time_t last_update)
+sdb_collectd_add_svc(const char *hostname, const char *plugin,
+               const char *type, sdb_time_t last_update)
 {
-       sc_service_t svc = SC_SVC_INIT;
+       sdb_service_t svc = SDB_SVC_INIT;
        char host[strlen(hostname) + 1];
        char name[strlen(plugin) + strlen(type) + 2];
 
@@ -105,40 +105,40 @@ sc_collectd_add_svc(const char *hostname, const char *plugin,
        svc.svc_name = name;
        svc.svc_last_update = last_update;
 
-       status = sc_store_service(&svc);
+       status = sdb_store_service(&svc);
        if (status < 0) {
                fprintf(stderr, "collectd backend: Failed to store/update "
                                "service '%s/%s'.\n", host, name);
                return -1;
        }
        return 0;
-} /* sc_collectd_add_svc */
+} /* sdb_collectd_add_svc */
 
 static int
-sc_collectd_get_data(sc_unixsock_client_t __attribute__((unused)) *client,
-               size_t n, sc_data_t *data, sc_object_t *user_data)
+sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
+               size_t n, sdb_data_t *data, sdb_object_t *user_data)
 {
-       sc_collectd_state_t *state;
+       sdb_collectd_state_t *state;
 
        const char *hostname;
        const char *plugin;
        const char *type;
-       sc_time_t last_update;
+       sdb_time_t last_update;
 
        assert(user_data);
 
        assert(n == 4);
-       assert((data[0].type == SC_TYPE_DATETIME)
-                       && (data[1].type == SC_TYPE_STRING)
-                       && (data[2].type == SC_TYPE_STRING)
-                       && (data[3].type == SC_TYPE_STRING));
+       assert((data[0].type == SDB_TYPE_DATETIME)
+                       && (data[1].type == SDB_TYPE_STRING)
+                       && (data[2].type == SDB_TYPE_STRING)
+                       && (data[3].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;
 
-       state = SC_OBJ_WRAPPER(user_data)->data;
+       state = SDB_OBJ_WRAPPER(user_data)->data;
 
        if (! state->current_host) {
                state->current_host = strdup(hostname);
@@ -149,14 +149,14 @@ sc_collectd_get_data(sc_unixsock_client_t __attribute__((unused)) *client,
                char errbuf[1024];
                fprintf(stderr, "collectd backend: Failed to allocate "
                                "string buffer: %s\n",
-                               sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
-       if (! sc_store_get_host(hostname))
-               sc_collectd_add_host(hostname, last_update);
+       if (! sdb_store_get_host(hostname))
+               sdb_collectd_add_host(hostname, last_update);
 
-       if (sc_collectd_add_svc(hostname, plugin, type, last_update))
+       if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
                ++state->svc_failed;
        else
                ++state->svc_updated;
@@ -168,7 +168,7 @@ sc_collectd_get_data(sc_unixsock_client_t __attribute__((unused)) *client,
        }
 
        /* new host */
-       sc_collectd_add_host(hostname, last_update);
+       sdb_collectd_add_host(hostname, last_update);
 
        fprintf(stderr, "collectd backend: Added/updated "
                        "%i service%s (%i failed) for host '%s'.\n",
@@ -180,22 +180,22 @@ sc_collectd_get_data(sc_unixsock_client_t __attribute__((unused)) *client,
        state->current_host = strdup(hostname);
        state->current_timestamp = last_update;
        return 0;
-} /* sc_collectd_get_data */
+} /* sdb_collectd_get_data */
 
 /*
  * plugin API
  */
 
 static int
-sc_collectd_init(sc_object_t *user_data)
+sdb_collectd_init(sdb_object_t *user_data)
 {
-       sc_unixsock_client_t *client;
+       sdb_unixsock_client_t *client;
 
        if (! user_data)
                return -1;
 
-       client = SC_OBJ_WRAPPER(user_data)->data;
-       if (sc_unixsock_client_connect(client)) {
+       client = SDB_OBJ_WRAPPER(user_data)->data;
+       if (sdb_unixsock_client_connect(client)) {
                fprintf(stderr, "collectd backend: "
                                "Failed to connect to collectd.\n");
                return -1;
@@ -203,20 +203,20 @@ sc_collectd_init(sc_object_t *user_data)
 
        fprintf(stderr, "collectd backend: Successfully "
                        "connected to collectd @ %s.\n",
-                       sc_unixsock_client_path(client));
+                       sdb_unixsock_client_path(client));
        return 0;
-} /* sc_collectd_init */
+} /* sdb_collectd_init */
 
 static int
-sc_collectd_shutdown(__attribute__((unused)) sc_object_t *user_data)
+sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
 {
        return 0;
-} /* sc_collectd_shutdown */
+} /* sdb_collectd_shutdown */
 
 static int
-sc_collectd_collect(sc_object_t *user_data)
+sdb_collectd_collect(sdb_object_t *user_data)
 {
-       sc_unixsock_client_t *client;
+       sdb_unixsock_client_t *client;
 
        char  buffer[1024];
        char *line;
@@ -225,26 +225,26 @@ sc_collectd_collect(sc_object_t *user_data)
        char *endptr = NULL;
        long int count;
 
-       sc_collectd_state_t state = SC_COLLECTD_STATE_INIT;
-       sc_object_wrapper_t state_obj = SC_OBJECT_WRAPPER_STATIC(&state,
+       sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
+       sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
                        /* destructor = */ NULL);
 
        if (! user_data)
                return -1;
 
-       client = SC_OBJ_WRAPPER(user_data)->data;
+       client = SDB_OBJ_WRAPPER(user_data)->data;
 
-       if (sc_unixsock_client_send(client, "LISTVAL") <= 0) {
+       if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
                fprintf(stderr, "collectd backend: Failed to send LISTVAL command "
-                               "to collectd @ %s.\n", sc_unixsock_client_path(client));
+                               "to collectd @ %s.\n", sdb_unixsock_client_path(client));
                return -1;
        }
 
-       line = sc_unixsock_client_recv(client, buffer, sizeof(buffer));
+       line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
        if (! line) {
                fprintf(stderr, "collectd backend: Failed to read status "
                                "of LISTVAL command from collectd @ %s.\n",
-                               sc_unixsock_client_path(client));
+                               sdb_unixsock_client_path(client));
                return -1;
        }
 
@@ -259,47 +259,47 @@ sc_collectd_collect(sc_object_t *user_data)
        if (errno || (line == endptr)) {
                fprintf(stderr, "collectd backend: Failed to parse status "
                                "of LISTVAL command from collectd @ %s.\n",
-                               sc_unixsock_client_path(client));
+                               sdb_unixsock_client_path(client));
                return -1;
        }
 
        if (count < 0) {
                fprintf(stderr, "collectd backend: Failed to get value list "
-                               "from collectd @ %s: %s\n", sc_unixsock_client_path(client),
+                               "from collectd @ %s: %s\n", sdb_unixsock_client_path(client),
                                msg ? msg : line);
                return -1;
        }
 
-       if (sc_unixsock_client_process_lines(client, sc_collectd_get_data,
-                               SC_OBJ(&state_obj), count, /* delim */ " /",
+       if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
+                               SDB_OBJ(&state_obj), count, /* delim */ " /",
                                /* column count = */ 4,
-                               SC_TYPE_DATETIME, SC_TYPE_STRING,
-                               SC_TYPE_STRING, SC_TYPE_STRING)) {
+                               SDB_TYPE_DATETIME, SDB_TYPE_STRING,
+                               SDB_TYPE_STRING, SDB_TYPE_STRING)) {
                fprintf(stderr, "collectd backend: Failed to read response "
-                               "from collectd @ %s.\n", sc_unixsock_client_path(client));
+                               "from collectd @ %s.\n", sdb_unixsock_client_path(client));
                return -1;
        }
 
        if (state.current_host) {
-               sc_collectd_add_host(state.current_host, state.current_timestamp);
+               sdb_collectd_add_host(state.current_host, state.current_timestamp);
                fprintf(stderr, "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);
        }
        return 0;
-} /* sc_collectd_collect */
+} /* sdb_collectd_collect */
 
 static int
-sc_collectd_config_instance(oconfig_item_t *ci)
+sdb_collectd_config_instance(oconfig_item_t *ci)
 {
        char *name = NULL;
        char *socket = NULL;
 
        char cb_name[1024];
 
-       sc_object_t *user_data;
-       sc_unixsock_client_t *client;
+       sdb_object_t *user_data;
+       sdb_unixsock_client_t *client;
 
        int i;
 
@@ -329,35 +329,35 @@ sc_collectd_config_instance(oconfig_item_t *ci)
        snprintf(cb_name, sizeof(cb_name), "collectd-%s", name);
        cb_name[sizeof(cb_name) - 1] = '\0';
 
-       client = sc_unixsock_client_create(socket);
+       client = sdb_unixsock_client_create(socket);
        if (! client) {
                char errbuf[1024];
                fprintf(stderr, "collectd backend: Failed to create unixsock client: "
-                               "%s\n", sc_strerror(errno, errbuf, sizeof(errbuf)));
+                               "%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
                return -1;
        }
 
-       user_data = sc_object_create_wrapper(client,
-                       (void (*)(void *))sc_unixsock_client_destroy);
+       user_data = sdb_object_create_wrapper(client,
+                       (void (*)(void *))sdb_unixsock_client_destroy);
        if (! user_data) {
-               sc_unixsock_client_destroy(client);
-               fprintf(stderr, "collectd backend: Failed to allocate sc_object_t\n");
+               sdb_unixsock_client_destroy(client);
+               fprintf(stderr, "collectd backend: Failed to allocate sdb_object_t\n");
                return -1;
        }
 
-       sc_plugin_register_init(cb_name, sc_collectd_init, user_data);
-       sc_plugin_register_shutdown(cb_name, sc_collectd_shutdown, user_data);
+       sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
+       sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
 
-       sc_plugin_register_collector(cb_name, sc_collectd_collect,
+       sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
-       sc_object_deref(user_data);
+       sdb_object_deref(user_data);
        return 0;
-} /* sc_collectd_config_instance */
+} /* sdb_collectd_config_instance */
 
 static int
-sc_collectd_config(oconfig_item_t *ci)
+sdb_collectd_config(oconfig_item_t *ci)
 {
        int i;
 
@@ -365,29 +365,29 @@ sc_collectd_config(oconfig_item_t *ci)
                oconfig_item_t *child = ci->children + i;
 
                if (! strcasecmp(child->key, "Instance"))
-                       sc_collectd_config_instance(child);
+                       sdb_collectd_config_instance(child);
                else
                        fprintf(stderr, "collectd backend: Ignoring unknown config "
                                        "option '%s'.\n", child->key);
        }
        return 0;
-} /* sc_collectd_config */
+} /* sdb_collectd_config */
 
 int
-sc_module_init(sc_plugin_info_t *info)
+sdb_module_init(sdb_plugin_info_t *info)
 {
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "collectd");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd");
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
                        "backend accessing the system statistics collection daemon");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
                        "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
-       sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
+       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
 
-       sc_plugin_register_config("collectd", sc_collectd_config);
+       sdb_plugin_register_config("collectd", sdb_collectd_config);
        return 0;
-} /* sc_version_extra */
+} /* sdb_version_extra */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */