Code

syscollectord: Include extra version information in startup/shutdown messages.
[sysdb.git] / src / backend / mk-livestatus.c
1 /*
2  * syscollector - src/backend/mk-livestatus.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #include "syscollector.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/string.h"
32 #include "utils/unixsock.h"
34 #include "liboconfig/utils.h"
36 #include <assert.h>
38 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
44 SC_PLUGIN_MAGIC;
46 /*
47  * private helper functions
48  */
50 static int
51 sc_livestatus_parse_line(char *line,
52                 char **host, sc_time_t *timestamp)
53 {
54         char *timestamp_str;
55         double timestamp_dbl;
57         char *hostname;
59         char *endptr = NULL;
61         hostname = line;
62         timestamp_str = strchr(hostname, ';');
64         if (! timestamp_str) {
65                 fprintf(stderr, "MK Livestatus backend: Failed to find timestamp "
66                                 "in 'GET hosts' output, line: %s\n", line);
67                 return -1;
68         }
70         *timestamp_str = '\0';
71         ++timestamp_str;
73         errno = 0;
74         timestamp_dbl = strtod(timestamp_str, &endptr);
75         if (errno || (timestamp_str == endptr)) {
76                 char errbuf[1024];
77                 fprintf(stderr, "MK Livestatus backend: Failed to "
78                                 "parse timestamp (%s): %s\n", timestamp_str,
79                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
80                 return -1;
81         }
82         if (endptr && (*endptr != '\0'))
83                 fprintf(stderr, "MK Livestatus backend: Ignoring garbage "
84                                 "after number when parsing timestamp: %s.\n",
85                                 endptr);
87         *timestamp = DOUBLE_TO_SC_TIME(timestamp_dbl);
88         *host = hostname;
89         return 0;
90 } /* sc_livestatus_parse_line */
92 /*
93  * plugin API
94  */
96 static int
97 sc_livestatus_init(sc_object_t *user_data)
98 {
99         sc_unixsock_client_t *client;
101         if (! user_data)
102                 return -1;
104         client = SC_OBJ_WRAPPER(user_data)->data;
105         if (sc_unixsock_client_connect(client)) {
106                 fprintf(stderr, "MK Livestatus backend: "
107                                 "Failed to connect to livestatus @ %s.\n",
108                                 sc_unixsock_client_path(client));
109                 return -1;
110         }
112         fprintf(stderr, "MK Livestatus backend: Successfully "
113                         "connected to livestatus @ %s.\n",
114                         sc_unixsock_client_path(client));
115         return 0;
116 } /* sc_livestatus_init */
118 static int
119 sc_livestatus_collect(sc_object_t *user_data)
121         sc_unixsock_client_t *client;
123         int status;
125         if (! user_data)
126                 return -1;
128         client = SC_OBJ_WRAPPER(user_data)->data;
130         status = sc_unixsock_client_send(client, "GET hosts\r\n"
131                         "Columns: name last_check");
132         if (status <= 0) {
133                 fprintf(stderr, "MK Livestatus backend: Failed to send "
134                                 "'GET hosts' command to livestatus @ %s.\n",
135                                 sc_unixsock_client_path(client));
136                 return -1;
137         }
139         sc_unixsock_client_shutdown(client, SHUT_WR);
141         while (42) {
142                 char  buffer[1024];
143                 char *line;
145                 char *hostname = NULL;
146                 sc_time_t timestamp = 0;
148                 sc_host_t host = SC_HOST_INIT;
150                 sc_unixsock_client_clearerr(client);
151                 line = sc_unixsock_client_recv(client, buffer, sizeof(buffer));
153                 if (! line)
154                         break;
156                 if (sc_livestatus_parse_line(line, &hostname, &timestamp)) {
157                         fprintf(stderr, "MK Livestatus backend: Failed to parse "
158                                         "hosts line: %s\n", line);
159                         continue;
160                 }
162                 host.host_name = hostname;
163                 host.host_last_update = timestamp;
165                 status = sc_store_host(&host);
167                 if (status < 0) {
168                         fprintf(stderr, "MK Livestatus backend: Failed to store/update "
169                                         "host '%s'.\n", hostname);
170                         continue;
171                 }
172                 else if (status > 0) /* value too old */
173                         continue;
175                 fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' "
176                                 "(last update timestamp = %"PRIscTIME").\n",
177                                 hostname, timestamp);
178         }
180         if ((! sc_unixsock_client_eof(client))
181                         || sc_unixsock_client_error(client)) {
182                 char errbuf[1024];
183                 fprintf(stderr, "MK Livestatus backend: Failed to read host "
184                                 "from livestatus @ %s: %s\n",
185                                 sc_unixsock_client_path(client),
186                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
187                 return -1;
188         }
189         return 0;
190 } /* sc_livestatus_collect */
192 static int
193 sc_livestatus_config_instance(oconfig_item_t *ci)
195         char *name = NULL;
196         char *socket = NULL;
198         char cb_name[1024];
200         sc_object_t *user_data;
201         sc_unixsock_client_t *client;
203         int i;
205         if (oconfig_get_string(ci, &name)) {
206                 fprintf(stderr, "MK Livestatus backend: Instance requires a single "
207                                 "string argument\n\tUsage: <Instance NAME>\n");
208                 return -1;
209         }
211         for (i = 0; i < ci->children_num; ++i) {
212                 oconfig_item_t *child = ci->children + i;
214                 if (! strcasecmp(child->key, "Socket"))
215                         oconfig_get_string(child, &socket);
216                 else
217                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
218                                         "option '%s' inside <Instance %s>.\n",
219                                         child->key, name);
220         }
222         if (! socket) {
223                 fprintf(stderr, "MK Livestatus backend: Instance '%s' missing "
224                                 "the 'Socket' option.\n", name);
225                 return -1;
226         }
228         snprintf(cb_name, sizeof(cb_name), "mk-livestatus-%s", name);
229         cb_name[sizeof(cb_name) - 1] = '\0';
231         client = sc_unixsock_client_create(socket);
232         if (! client) {
233                 char errbuf[1024];
234                 fprintf(stderr, "MK Livestatus backend: Failed to create unixsock "
235                                 "client: %s\n", sc_strerror(errno, errbuf, sizeof(errbuf)));
236                 return -1;
237         }
239         user_data = sc_object_create_wrapper(client,
240                         (void (*)(void *))sc_unixsock_client_destroy);
241         if (! user_data) {
242                 sc_unixsock_client_destroy(client);
243                 fprintf(stderr, "MK Livestatus backend: Failed to "
244                                 "allocate sc_object_t\n");
245                 return -1;
246         }
248         sc_plugin_register_init(cb_name, sc_livestatus_init, user_data);
249         sc_plugin_register_collector(cb_name, sc_livestatus_collect,
250                         /* interval */ NULL, user_data);
252         /* pass control to the list */
253         sc_object_deref(user_data);
254         return 0;
255 } /* sc_livestatus_config_instance */
257 static int
258 sc_livestatus_config(oconfig_item_t *ci)
260         int i;
262         for (i = 0; i < ci->children_num; ++i) {
263                 oconfig_item_t *child = ci->children + i;
265                 if (! strcasecmp(child->key, "Instance"))
266                         sc_livestatus_config_instance(child);
267                 else
268                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
269                                         "option '%s'.\n", child->key);
270         }
271         return 0;
272 } /* sc_livestatus_config */
274 int
275 sc_module_init(sc_plugin_info_t *info)
277         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "MK-Livestatus");
278         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
279                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
280         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
281                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
282         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
283         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
284         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
286         sc_plugin_register_config("mk-livestatus", sc_livestatus_config);
287         return 0;
288 } /* sc_version_extra */
290 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */