Code

collectd::unixsock: Store metrics instead of services.
[sysdb.git] / src / backend / mk-livestatus.c
index da8c99c74cc04c7fae9c0ee9e586c8f01e7d228e..0f94ce34fd2ebe2fc550d872ad595bc69028093b 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"
@@ -75,7 +79,7 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
                return 0;
 
        sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
-                       "host '%s' (last update timestamp = %"PRIscTIME").",
+                       "host '%s' (last update timestamp = %"PRIsdbTIME").",
                        hostname, timestamp);
        return 0;
 } /* sdb_livestatus_get_host */
@@ -85,12 +89,10 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
                size_t n, sdb_data_t *data,
                sdb_object_t __attribute__((unused)) *user_data)
 {
-       char *hostname = NULL;
-       char *svcname = NULL;
+       const char *hostname = NULL;
+       const char *svcname = NULL;
        sdb_time_t timestamp = 0;
 
-       sdb_service_t svc = SDB_SVC_INIT;
-
        int status;
 
        assert(n == 3);
@@ -98,31 +100,23 @@ sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
                        && (data[1].type == SDB_TYPE_STRING)
                        && (data[2].type == SDB_TYPE_DATETIME));
 
-       hostname  = strdup(data[0].data.string);
-       svcname   = strdup(data[1].data.string);
+       hostname  = data[0].data.string;
+       svcname   = data[1].data.string;
        timestamp = data[2].data.datetime;
 
-       svc.hostname = hostname;
-       SDB_OBJ(&svc)->name = svcname;
-       svc._last_update = timestamp;
-
-       status = sdb_store_service(&svc);
+       status = sdb_store_service(hostname, svcname, timestamp);
 
        if (status < 0) {
                sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
                                "store/update service '%s / %s'.", hostname, svcname);
-               free(hostname);
-               free(svcname);
                return -1;
        }
        else if (status > 0) /* value too old */
                return 0;
 
        sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
-                       "service '%s / %s' (last update timestamp = %"PRIscTIME").",
+                       "service '%s / %s' (last update timestamp = %"PRIsdbTIME").",
                        hostname, svcname, timestamp);
-       free(hostname);
-       free(svcname);
        return 0;
 } /* sdb_livestatus_get_svc */
 
@@ -152,6 +146,17 @@ sdb_livestatus_init(sdb_object_t *user_data)
        return 0;
 } /* sdb_livestatus_init */
 
+static int
+sdb_livestatus_shutdown(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_livestatus_shutdown */
+
 static int
 sdb_livestatus_collect(sdb_object_t *user_data)
 {
@@ -233,8 +238,6 @@ sdb_livestatus_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;
 
@@ -263,9 +266,6 @@ sdb_livestatus_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       snprintf(cb_name, sizeof(cb_name), "mk-livestatus::%s", name);
-       cb_name[sizeof(cb_name) - 1] = '\0';
-
        client = sdb_unixsock_client_create(socket_path);
        if (! client) {
                char errbuf[1024];
@@ -284,8 +284,9 @@ sdb_livestatus_config_instance(oconfig_item_t *ci)
                return -1;
        }
 
-       sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
-       sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
+       sdb_plugin_register_init(name, sdb_livestatus_init, user_data);
+       sdb_plugin_register_shutdown(name, sdb_livestatus_shutdown, user_data);
+       sdb_plugin_register_collector(name, sdb_livestatus_collect,
                        /* interval */ NULL, user_data);
 
        /* pass control to the list */
@@ -298,6 +299,9 @@ sdb_livestatus_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;
 
@@ -313,7 +317,6 @@ sdb_livestatus_config(oconfig_item_t *ci)
 int
 sdb_module_init(sdb_plugin_info_t *info)
 {
-       sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
                        "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
        sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
@@ -322,7 +325,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("mk-livestatus", sdb_livestatus_config);
+       sdb_plugin_register_config(sdb_livestatus_config);
        return 0;
 } /* sdb_version_extra */