X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fbackend%2Fmk-livestatus.c;h=57fda523ddf9c81419bb6b565e0d7b305be834fe;hp=f6b9b5f6641d300a505a404ec11e21d2a76e0f46;hb=bef7167f0dc1fd405e35d5cbffb3c0820945a9ea;hpb=d938338c37665a6ac78c292d906726dbc92c68ee diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index f6b9b5f..57fda52 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,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,164 +41,209 @@ #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; + char *hostname = NULL; + sdb_time_t timestamp = 0; - char *hostname; + sdb_host_t host = SDB_HOST_INIT; - char *endptr = NULL; + int status; + + assert(n == 2); + assert((data[0].type == SDB_TYPE_STRING) + && (data[1].type == SDB_TYPE_DATETIME)); + + hostname = strdup(data[0].data.string); + timestamp = data[1].data.datetime; - hostname = line; - timestamp_str = strchr(hostname, ';'); + host.host_name = hostname; + host.host_last_update = timestamp; - if (! timestamp_str) { - fprintf(stderr, "MK Livestatus backend: Failed to find timestamp " - "in 'GET hosts' output, line: %s\n", line); + status = sdb_store_host(&host); + + if (status < 0) { + fprintf(stderr, "MK Livestatus backend: Failed to store/update " + "host '%s'.\n", hostname); + free(hostname); return -1; } + else if (status > 0) /* value too old */ + return 0; - *timestamp_str = '\0'; - ++timestamp_str; + fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' " + "(last update timestamp = %"PRIscTIME").\n", + hostname, timestamp); + free(hostname); + 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) +{ + char *hostname = NULL; + char *svcname = NULL; + sdb_time_t timestamp = 0; + + sdb_service_t svc = SDB_SVC_INIT; + + 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 = strdup(data[0].data.string); + svcname = strdup(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); + + if (status < 0) { + fprintf(stderr, "MK Livestatus backend: Failed to store/update " + "service '%s / %s'.\n", hostname, svcname); + free(hostname); + free(svcname); return -1; } - if (endptr && (*endptr != '\0')) - fprintf(stderr, "MK Livestatus backend: Ignoring garbage " - "after number when parsing timestamp: %s.\n", - endptr); - - *timestamp = DOUBLE_TO_SC_TIME(timestamp_dbl); - *host = hostname; + else if (status > 0) /* value too old */ + return 0; + + fprintf(stderr, "MK Livestatus backend: Added/updated service '%s / %s' " + "(last update timestamp = %"PRIscTIME").\n", + hostname, svcname, timestamp); + free(hostname); + free(svcname); 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)) { + client = SDB_OBJ_WRAPPER(user_data)->data; + if (sdb_unixsock_client_connect(client)) { fprintf(stderr, "MK Livestatus backend: " "Failed to connect to livestatus @ %s.\n", - sc_unixsock_client_path(client)); + sdb_unixsock_client_path(client)); return -1; } fprintf(stderr, "MK Livestatus backend: Successfully " "connected to livestatus @ %s.\n", - sc_unixsock_client_path(client)); + 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_unixsock_client_path(client)); return -1; } - sc_unixsock_client_shutdown(client, SHUT_WR); + sdb_unixsock_client_shutdown(client, SHUT_WR); - while (42) { - char buffer[1024]; - char *line; - - 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)) { + fprintf(stderr, "MK Livestatus backend: Failed to read response " + "from livestatus @ %s while reading hosts.\n", + 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]; + fprintf(stderr, "MK Livestatus backend: Failed to read host " + "from livestatus @ %s: %s\n", + 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) { + fprintf(stderr, "MK Livestatus backend: Failed to send " + "'GET services' command to livestatus @ %s.\n", + 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)) { + fprintf(stderr, "MK Livestatus backend: Failed to read response " + "from livestatus @ %s while reading services.\n", + 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 " + fprintf(stderr, "MK Livestatus backend: Failed to read services " "from livestatus @ %s: %s\n", - sc_unixsock_client_path(client), - sc_strerror(errno, errbuf, sizeof(errbuf))); + 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]; - sc_object_t *user_data; - sc_unixsock_client_t *client; + sdb_object_t *user_data; + sdb_unixsock_client_t *client; int i; @@ -228,34 +273,34 @@ sc_livestatus_config_instance(oconfig_item_t *ci) 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); if (! client) { char errbuf[1024]; fprintf(stderr, "MK Livestatus backend: Failed to create unixsock " - "client: %s\n", sc_strerror(errno, errbuf, sizeof(errbuf))); + "client: %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); + sdb_unixsock_client_destroy(client); fprintf(stderr, "MK Livestatus backend: Failed to " - "allocate sc_object_t\n"); + "allocate sdb_object_t\n"); 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 +308,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); } 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 : */