X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fbackend%2Fmk-livestatus.c;h=0f94ce34fd2ebe2fc550d872ad595bc69028093b;hp=9f99c46191517a532275df8ce9fbebfaabce9ddb;hb=8b3452e508a38e9a28aa82a4636a3dbe03d32e65;hpb=dd7a268854d23425d2cea6b3e896e21f2fbc9aba diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index 9f99c46..0f94ce3 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -1,5 +1,5 @@ /* - * syscollector - src/backend/mk-livestatus.c + * SysDB - src/backend/mk-livestatus.c * Copyright (C) 2012 Sebastian 'tokkee' Harl * All rights reserved. * @@ -25,10 +25,14 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "syscollector.h" +#if HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "utils/string.h" +#include "utils/error.h" #include "utils/unixsock.h" #include "liboconfig/utils.h" @@ -41,136 +45,207 @@ #include #include -SC_PLUGIN_MAGIC; +SDB_PLUGIN_MAGIC; /* * private helper functions */ static int -sc_livestatus_get_data(sc_unixsock_client_t __attribute__((unused)) *client, - size_t n, sc_data_t *data, sc_object_t __attribute__((unused)) *user_data) +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; - sc_time_t timestamp = 0; - - sc_host_t host = SC_HOST_INIT; + const char *hostname; + sdb_time_t timestamp; int status; assert(n == 2); - assert((data[0].type == SC_TYPE_STRING) - && (data[1].type == SC_TYPE_DATETIME)); + 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 = sc_store_host(&host); + status = sdb_store_host(hostname, timestamp); if (status < 0) { - fprintf(stderr, "MK Livestatus backend: Failed to store/update " - "host '%s'.\n", hostname); - free(hostname); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to " + "store/update host '%s'.", hostname); return -1; } else if (status > 0) /* value too old */ return 0; - fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' " - "(last update timestamp = %"PRIscTIME").\n", + sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " + "host '%s' (last update timestamp = %"PRIsdbTIME").", hostname, timestamp); - free(hostname); return 0; -} /* sc_livestatus_get_data */ +} /* sdb_livestatus_get_host */ + +static int +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) +{ + const char *hostname = NULL; + const char *svcname = NULL; + sdb_time_t timestamp = 0; + + int status; + + assert(n == 3); + assert((data[0].type == SDB_TYPE_STRING) + && (data[1].type == SDB_TYPE_STRING) + && (data[2].type == SDB_TYPE_DATETIME)); + + hostname = data[0].data.string; + svcname = data[1].data.string; + timestamp = data[2].data.datetime; + + 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); + 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 = %"PRIsdbTIME").", + hostname, svcname, timestamp); + return 0; +} /* sdb_livestatus_get_svc */ /* * plugin API */ static int -sc_livestatus_init(sc_object_t *user_data) +sdb_livestatus_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)) { - fprintf(stderr, "MK Livestatus backend: " - "Failed to connect to livestatus @ %s.\n", - sc_unixsock_client_path(client)); + 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.", + sdb_unixsock_client_path(client)); return -1; } - fprintf(stderr, "MK Livestatus backend: Successfully " - "connected to livestatus @ %s.\n", - sc_unixsock_client_path(client)); + sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully " + "connected to livestatus @ %s.", + sdb_unixsock_client_path(client)); return 0; -} /* sc_livestatus_init */ +} /* sdb_livestatus_init */ static int -sc_livestatus_collect(sc_object_t *user_data) +sdb_livestatus_shutdown(sdb_object_t *user_data) { - sc_unixsock_client_t *client; + 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) +{ + sdb_unixsock_client_t *client; int status; if (! user_data) return -1; - client = SC_OBJ_WRAPPER(user_data)->data; + client = SDB_OBJ_WRAPPER(user_data)->data; - status = sc_unixsock_client_send(client, "GET hosts\r\n" + status = sdb_unixsock_client_send(client, "GET hosts\r\n" "Columns: name last_check"); if (status <= 0) { - fprintf(stderr, "MK Livestatus backend: Failed to send " - "'GET hosts' command to livestatus @ %s.\n", - sc_unixsock_client_path(client)); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send " + "'GET hosts' command to livestatus @ %s.", + sdb_unixsock_client_path(client)); return -1; } - sc_unixsock_client_shutdown(client, SHUT_WR); + sdb_unixsock_client_shutdown(client, SHUT_WR); - if (sc_unixsock_client_process_lines(client, sc_livestatus_get_data, + if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host, /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", - /* column count */ 2, SC_TYPE_STRING, SC_TYPE_DATETIME)) { - fprintf(stderr, "MK Livestatus backend: Failed to read response " - "from livestatus @ %s.\n", sc_unixsock_client_path(client)); + /* 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.", + sdb_unixsock_client_path(client)); return -1; } - if ((! sc_unixsock_client_eof(client)) - || sc_unixsock_client_error(client)) { + if ((! sdb_unixsock_client_eof(client)) + || sdb_unixsock_client_error(client)) { char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to read host " - "from livestatus @ %s: %s\n", - sc_unixsock_client_path(client), - sc_strerror(errno, errbuf, sizeof(errbuf))); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "host from livestatus @ %s: %s", + sdb_unixsock_client_path(client), + sdb_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } + + status = sdb_unixsock_client_send(client, "GET services\r\n" + "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.", + sdb_unixsock_client_path(client)); + return -1; + } + + sdb_unixsock_client_shutdown(client, SHUT_WR); + + if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc, + /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", + /* 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.", + sdb_unixsock_client_path(client)); + return -1; + } + + if ((! sdb_unixsock_client_eof(client)) + || sdb_unixsock_client_error(client)) { + char errbuf[1024]; + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read " + "services from livestatus @ %s: %s", + sdb_unixsock_client_path(client), + sdb_strerror(errno, errbuf, sizeof(errbuf))); return -1; } return 0; -} /* sc_livestatus_collect */ +} /* sdb_livestatus_collect */ static int -sc_livestatus_config_instance(oconfig_item_t *ci) +sdb_livestatus_config_instance(oconfig_item_t *ci) { char *name = NULL; - char *socket = NULL; - - char cb_name[1024]; + char *socket_path = NULL; - sc_object_t *user_data; - sc_unixsock_client_t *client; + sdb_object_t *user_data; + sdb_unixsock_client_t *client; int i; if (oconfig_get_string(ci, &name)) { - fprintf(stderr, "MK Livestatus backend: Instance requires a single " - "string argument\n\tUsage: \n"); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires " + "a single string argument\n\tUsage: "); return -1; } @@ -178,80 +253,81 @@ sc_livestatus_config_instance(oconfig_item_t *ci) oconfig_item_t *child = ci->children + i; if (! strcasecmp(child->key, "Socket")) - oconfig_get_string(child, &socket); + oconfig_get_string(child, &socket_path); else - fprintf(stderr, "MK Livestatus backend: Ignoring unknown config " - "option '%s' inside .\n", + sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " + "unknown config option '%s' inside .", child->key, name); } - if (! socket) { - fprintf(stderr, "MK Livestatus backend: Instance '%s' missing " - "the 'Socket' option.\n", name); + if (! socket_path) { + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' " + "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 = sc_unixsock_client_create(socket); + client = sdb_unixsock_client_create(socket_path); if (! client) { char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to create unixsock " - "client: %s\n", sc_strerror(errno, errbuf, sizeof(errbuf))); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create " + "unixsock client: %s", + 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("unixsock-client", client, + (void (*)(void *))sdb_unixsock_client_destroy); if (! user_data) { - sc_unixsock_client_destroy(client); - fprintf(stderr, "MK Livestatus backend: Failed to " - "allocate sc_object_t\n"); + sdb_unixsock_client_destroy(client); + sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to " + "allocate sdb_object_t"); return -1; } - sc_plugin_register_init(cb_name, sc_livestatus_init, user_data); - sc_plugin_register_collector(cb_name, sc_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 */ - sc_object_deref(user_data); + sdb_object_deref(user_data); return 0; -} /* sc_livestatus_config_instance */ +} /* sdb_livestatus_config_instance */ static int -sc_livestatus_config(oconfig_item_t *ci) +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; if (! strcasecmp(child->key, "Instance")) - sc_livestatus_config_instance(child); + sdb_livestatus_config_instance(child); else - fprintf(stderr, "MK Livestatus backend: Ignoring unknown config " - "option '%s'.\n", child->key); + sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring " + "unknown config option '%s'.", child->key); } return 0; -} /* sc_livestatus_config */ +} /* sdb_livestatus_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, "MK-Livestatus"); - sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC, + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC, "backend accessing Nagios/Icinga/Shinken using MK Livestatus"); - sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT, + sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT, "Copyright (C) 2012 Sebastian 'tokkee' Harl "); - 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("mk-livestatus", sc_livestatus_config); + sdb_plugin_register_config(sdb_livestatus_config); return 0; -} /* sc_version_extra */ +} /* sdb_version_extra */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */