X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fbackend%2Fmk-livestatus.c;h=bf45da6fdd35df6d3170ed4a4ebafbc1b513b49a;hp=f6b9b5f6641d300a505a404ec11e21d2a76e0f46;hb=ddb933096618a6bceded29e4dc2b37cb72134366;hpb=d938338c37665a6ac78c292d906726dbc92c68ee diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index f6b9b5f..bf45da6 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,10 @@ * 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" +#include "utils/error.h" #include "utils/unixsock.h" #include "liboconfig/utils.h" @@ -41,170 +41,198 @@ #include #include -SC_PLUGIN_MAGIC; +SDB_PLUGIN_MAGIC; /* * private helper functions */ static int -sc_livestatus_parse_line(char *line, - char **host, sc_time_t *timestamp) +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 *timestamp_str; - double timestamp_dbl; + const char *hostname; + sdb_time_t timestamp; - char *hostname; + int status; + + assert(n == 2); + assert((data[0].type == SDB_TYPE_STRING) + && (data[1].type == SDB_TYPE_DATETIME)); - char *endptr = NULL; + hostname = data[0].data.string; + timestamp = data[1].data.datetime; - hostname = line; - timestamp_str = strchr(hostname, ';'); + status = sdb_store_host(hostname, timestamp); - if (! timestamp_str) { - fprintf(stderr, "MK Livestatus backend: Failed to find timestamp " - "in 'GET hosts' output, line: %s\n", line); + if (status < 0) { + 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; - *timestamp_str = '\0'; - ++timestamp_str; + sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " + "host '%s' (last update timestamp = %"PRIscTIME").", + hostname, timestamp); + return 0; +} /* sdb_livestatus_get_host */ - errno = 0; - timestamp_dbl = strtod(timestamp_str, &endptr); - if (errno || (timestamp_str == endptr)) { - char errbuf[1024]; - fprintf(stderr, "MK Livestatus backend: Failed to " - "parse timestamp (%s): %s\n", timestamp_str, - sc_strerror(errno, errbuf, sizeof(errbuf))); +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; } - if (endptr && (*endptr != '\0')) - fprintf(stderr, "MK Livestatus backend: Ignoring garbage " - "after number when parsing timestamp: %s.\n", - endptr); + else if (status > 0) /* value too old */ + return 0; - *timestamp = DOUBLE_TO_SC_TIME(timestamp_dbl); - *host = hostname; + sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated " + "service '%s / %s' (last update timestamp = %"PRIscTIME").", + hostname, svcname, timestamp); return 0; -} /* sc_livestatus_parse_line */ +} /* 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_collect(sdb_object_t *user_data) { - sc_unixsock_client_t *client; + 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); - - while (42) { - char buffer[1024]; - char *line; + sdb_unixsock_client_shutdown(client, SHUT_WR); - char *hostname = NULL; - sc_time_t timestamp = 0; - - sc_host_t host = SC_HOST_INIT; - - sc_unixsock_client_clearerr(client); - line = sc_unixsock_client_recv(client, buffer, sizeof(buffer)); - - if (! line) - break; - - if (sc_livestatus_parse_line(line, &hostname, ×tamp)) { - fprintf(stderr, "MK Livestatus backend: Failed to parse " - "hosts line: %s\n", line); - continue; - } + if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host, + /* 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.", + sdb_unixsock_client_path(client)); + return -1; + } - host.host_name = hostname; - host.host_last_update = timestamp; + 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 " + "host from livestatus @ %s: %s", + sdb_unixsock_client_path(client), + sdb_strerror(errno, errbuf, sizeof(errbuf))); + return -1; + } - status = sc_store_host(&host); + 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; + } - if (status < 0) { - fprintf(stderr, "MK Livestatus backend: Failed to store/update " - "host '%s'.\n", hostname); - continue; - } - else if (status > 0) /* value too old */ - continue; + sdb_unixsock_client_shutdown(client, SHUT_WR); - fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' " - "(last update timestamp = %"PRIscTIME").\n", - hostname, timestamp); + 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 ((! 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 " + "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 *socket_path = 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; 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; } @@ -212,50 +240,51 @@ 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); + 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(cb_name, sdb_livestatus_init, user_data); + sdb_plugin_register_collector(cb_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; @@ -263,29 +292,29 @@ sc_livestatus_config(oconfig_item_t *ci) 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_NAME, "MK-Livestatus"); + 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("mk-livestatus", sdb_livestatus_config); return 0; -} /* sc_version_extra */ +} /* sdb_version_extra */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */