X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=blobdiff_plain;f=src%2Fbackend%2Fmk-livestatus.c;h=0edbc3d25f842012c1140e61112036e47d3e4615;hp=f6b9b5f6641d300a505a404ec11e21d2a76e0f46;hb=b5890679b40f95e30a25f71ad0f674e1defa42d5;hpb=d938338c37665a6ac78c292d906726dbc92c68ee diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index f6b9b5f..0edbc3d 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -48,46 +48,88 @@ SC_PLUGIN_MAGIC; */ static int -sc_livestatus_parse_line(char *line, - char **host, sc_time_t *timestamp) +sc_livestatus_get_host(sc_unixsock_client_t __attribute__((unused)) *client, + size_t n, sc_data_t *data, sc_object_t __attribute__((unused)) *user_data) { - char *timestamp_str; - double timestamp_dbl; + char *hostname = NULL; + sc_time_t timestamp = 0; - char *hostname; + sc_host_t host = SC_HOST_INIT; - char *endptr = NULL; + int status; + + assert(n == 2); + assert((data[0].type == SC_TYPE_STRING) + && (data[1].type == SC_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 = sc_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; +} /* sc_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 +sc_livestatus_get_svc(sc_unixsock_client_t __attribute__((unused)) *client, + size_t n, sc_data_t *data, sc_object_t __attribute__((unused)) *user_data) +{ + char *hostname = NULL; + char *svcname = NULL; + sc_time_t timestamp = 0; + + sc_service_t svc = SC_SVC_INIT; + + int status; + + assert(n == 3); + assert((data[0].type == SC_TYPE_STRING) + && (data[1].type == SC_TYPE_STRING) + && (data[2].type == SC_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 = sc_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 */ +} /* sc_livestatus_get_svc */ /* * plugin API @@ -138,49 +180,50 @@ sc_livestatus_collect(sc_object_t *user_data) sc_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 (sc_unixsock_client_process_lines(client, sc_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 while reading hosts.\n", + sc_unixsock_client_path(client)); + return -1; + } - host.host_name = hostname; - host.host_last_update = timestamp; + if ((! sc_unixsock_client_eof(client)) + || sc_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))); + return -1; + } - status = sc_store_host(&host); + status = sc_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", + sc_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; + sc_unixsock_client_shutdown(client, SHUT_WR); - fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' " - "(last update timestamp = %"PRIscTIME").\n", - hostname, timestamp); + if (sc_unixsock_client_process_lines(client, sc_livestatus_get_svc, + /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";", + /* column count */ 3, SC_TYPE_STRING, SC_TYPE_STRING, + SC_TYPE_DATETIME)) { + fprintf(stderr, "MK Livestatus backend: Failed to read response " + "from livestatus @ %s while reading services.\n", + sc_unixsock_client_path(client)); + return -1; } if ((! sc_unixsock_client_eof(client)) || sc_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)));