X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fbackend%2Fmk-livestatus.c;h=0f94ce34fd2ebe2fc550d872ad595bc69028093b;hb=1b899f6e899047a521d473d00492bcf699f44ede;hp=5d8c37735e25cf30cf2e4772ca6757e84e1fe8d5;hpb=848efadda124a4778f08a84a082ff2436504d22c;p=sysdb.git diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index 5d8c377..0f94ce3 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -25,11 +25,14 @@ * 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 "utils/error.h" -#include "utils/string.h" #include "utils/unixsock.h" #include "liboconfig/utils.h" @@ -53,10 +56,8 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, size_t n, sdb_data_t *data, sdb_object_t __attribute__((unused)) *user_data) { - char *hostname = NULL; - sdb_time_t timestamp = 0; - - sdb_host_t host = SDB_HOST_INIT; + const char *hostname; + sdb_time_t timestamp; int status; @@ -64,27 +65,22 @@ sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client, assert((data[0].type == SDB_TYPE_STRING) && (data[1].type == SDB_TYPE_DATETIME)); - hostname = strdup(data[0].data.string); + hostname = data[0].data.string; timestamp = data[1].data.datetime; - host.host_name = hostname; - host.host_last_update = timestamp; - - status = sdb_store_host(&host); + status = sdb_store_host(hostname, timestamp); if (status < 0) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to " - "store/update host '%s'.\n", hostname); - free(hostname); + "store/update host '%s'.", hostname); return -1; } else if (status > 0) /* value too old */ return 0; sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " - "host '%s' (last update timestamp = %"PRIscTIME").\n", + "host '%s' (last update timestamp = %"PRIsdbTIME").", hostname, timestamp); - free(hostname); return 0; } /* sdb_livestatus_get_host */ @@ -93,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); @@ -106,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; - svc.svc_name = svcname; - svc.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'.\n", hostname, svcname); - free(hostname); - free(svcname); + "store/update service '%s / %s'.", hostname, 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").\n", + "service '%s / %s' (last update timestamp = %"PRIsdbTIME").", hostname, svcname, timestamp); - free(hostname); - free(svcname); return 0; } /* sdb_livestatus_get_svc */ @@ -149,17 +135,28 @@ sdb_livestatus_init(sdb_object_t *user_data) client = SDB_OBJ_WRAPPER(user_data)->data; if (sdb_unixsock_client_connect(client)) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: " - "Failed to connect to livestatus @ %s.\n", + "Failed to connect to livestatus @ %s.", sdb_unixsock_client_path(client)); return -1; } sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully " - "connected to livestatus @ %s.\n", + "connected to livestatus @ %s.", sdb_unixsock_client_path(client)); 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) { @@ -176,7 +173,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) "Columns: name last_check"); if (status <= 0) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send " - "'GET hosts' command to livestatus @ %s.\n", + "'GET hosts' command to livestatus @ %s.", sdb_unixsock_client_path(client)); return -1; } @@ -187,7 +184,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " - "response from livestatus @ %s while reading hosts.\n", + "response from livestatus @ %s while reading hosts.", sdb_unixsock_client_path(client)); return -1; } @@ -196,7 +193,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) || sdb_unixsock_client_error(client)) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " - "host from livestatus @ %s: %s\n", + "host from livestatus @ %s: %s", sdb_unixsock_client_path(client), sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; @@ -206,7 +203,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) "Columns: host_name description last_check"); if (status <= 0) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send " - "'GET services' command to livestatus @ %s.\n", + "'GET services' command to livestatus @ %s.", sdb_unixsock_client_path(client)); return -1; } @@ -218,7 +215,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " - "response from livestatus @ %s while reading services.\n", + "response from livestatus @ %s while reading services.", sdb_unixsock_client_path(client)); return -1; } @@ -227,7 +224,7 @@ sdb_livestatus_collect(sdb_object_t *user_data) || sdb_unixsock_client_error(client)) { char errbuf[1024]; sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " - "services from livestatus @ %s: %s\n", + "services from livestatus @ %s: %s", sdb_unixsock_client_path(client), sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; @@ -241,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; @@ -250,7 +245,7 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) if (oconfig_get_string(ci, &name)) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires " - "a single string argument\n\tUsage: \n"); + "a single string argument\n\tUsage: "); return -1; } @@ -261,39 +256,37 @@ sdb_livestatus_config_instance(oconfig_item_t *ci) oconfig_get_string(child, &socket_path); else sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " - "unknown config option '%s' inside .\n", + "unknown config option '%s' inside .", child->key, name); } if (! socket_path) { sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' " - "missing the 'Socket' option.\n", name); + "missing the 'Socket' option.", name); 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]; sdb_log(SDB_LOG_ERR, "MK Livestatus 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, "MK Livestatus backend: Failed to " - "allocate sdb_object_t\n"); + "allocate sdb_object_t"); 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 */ @@ -306,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 +309,7 @@ sdb_livestatus_config(oconfig_item_t *ci) sdb_livestatus_config_instance(child); else sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " - "unknown config option '%s'.\n", child->key); + "unknown config option '%s'.", child->key); } return 0; } /* sdb_livestatus_config */ @@ -321,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, @@ -330,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 */