1 /*
2 * SysDB - src/plugins/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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
36 #include "utils/unixsock.h"
38 #include "liboconfig/utils.h"
40 #include <assert.h>
42 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51 * private helper functions
52 */
54 static int
55 sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
56 size_t n, sdb_data_t *data,
57 sdb_object_t __attribute__((unused)) *user_data)
58 {
59 const char *hostname;
60 sdb_time_t timestamp;
62 int status;
64 assert(n == 2);
65 assert((data[0].type == SDB_TYPE_STRING)
66 && (data[1].type == SDB_TYPE_DATETIME));
68 hostname = data[0].data.string;
69 timestamp = data[1].data.datetime;
71 status = sdb_store_host(hostname, timestamp);
73 if (status < 0) {
74 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
75 "store/update host '%s'.", hostname);
76 return -1;
77 }
78 else if (status > 0) /* value too old */
79 return 0;
81 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
82 "host '%s' (last update timestamp = %"PRIsdbTIME").",
83 hostname, timestamp);
84 return 0;
85 } /* sdb_livestatus_get_host */
87 static int
88 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
89 size_t n, sdb_data_t *data,
90 sdb_object_t __attribute__((unused)) *user_data)
91 {
92 const char *hostname = NULL;
93 const char *svcname = NULL;
94 sdb_time_t timestamp = 0;
96 int status;
98 assert(n == 3);
99 assert((data[0].type == SDB_TYPE_STRING)
100 && (data[1].type == SDB_TYPE_STRING)
101 && (data[2].type == SDB_TYPE_DATETIME));
103 hostname = data[0].data.string;
104 svcname = data[1].data.string;
105 timestamp = data[2].data.datetime;
107 status = sdb_store_service(hostname, svcname, timestamp);
109 if (status < 0) {
110 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
111 "store/update service '%s / %s'.", hostname, svcname);
112 return -1;
113 }
114 else if (status > 0) /* value too old */
115 return 0;
117 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
118 "service '%s / %s' (last update timestamp = %"PRIsdbTIME").",
119 hostname, svcname, timestamp);
120 return 0;
121 } /* sdb_livestatus_get_svc */
123 /*
124 * plugin API
125 */
127 static int
128 sdb_livestatus_init(sdb_object_t *user_data)
129 {
130 sdb_unixsock_client_t *client;
132 if (! user_data)
133 return -1;
135 client = SDB_OBJ_WRAPPER(user_data)->data;
136 if (sdb_unixsock_client_connect(client)) {
137 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
138 "Failed to connect to livestatus @ %s.",
139 sdb_unixsock_client_path(client));
140 return -1;
141 }
143 sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
144 "connected to livestatus @ %s.",
145 sdb_unixsock_client_path(client));
146 return 0;
147 } /* sdb_livestatus_init */
149 static int
150 sdb_livestatus_shutdown(sdb_object_t *user_data)
151 {
152 if (! user_data)
153 return -1;
155 sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
156 SDB_OBJ_WRAPPER(user_data)->data = NULL;
157 return 0;
158 } /* sdb_livestatus_shutdown */
160 static int
161 sdb_livestatus_collect(sdb_object_t *user_data)
162 {
163 sdb_unixsock_client_t *client;
165 int status;
167 if (! user_data)
168 return -1;
170 client = SDB_OBJ_WRAPPER(user_data)->data;
172 status = sdb_unixsock_client_send(client, "GET hosts\r\n"
173 "Columns: name last_check");
174 if (status <= 0) {
175 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
176 "'GET hosts' command to livestatus @ %s.",
177 sdb_unixsock_client_path(client));
178 return -1;
179 }
181 sdb_unixsock_client_shutdown(client, SHUT_WR);
183 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
184 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
185 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
186 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
187 "response from livestatus @ %s while reading hosts.",
188 sdb_unixsock_client_path(client));
189 return -1;
190 }
192 if ((! sdb_unixsock_client_eof(client))
193 || sdb_unixsock_client_error(client)) {
194 char errbuf[1024];
195 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
196 "host from livestatus @ %s: %s",
197 sdb_unixsock_client_path(client),
198 sdb_strerror(errno, errbuf, sizeof(errbuf)));
199 return -1;
200 }
202 status = sdb_unixsock_client_send(client, "GET services\r\n"
203 "Columns: host_name description last_check");
204 if (status <= 0) {
205 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
206 "'GET services' command to livestatus @ %s.",
207 sdb_unixsock_client_path(client));
208 return -1;
209 }
211 sdb_unixsock_client_shutdown(client, SHUT_WR);
213 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
214 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
215 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
216 SDB_TYPE_DATETIME)) {
217 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
218 "response from livestatus @ %s while reading services.",
219 sdb_unixsock_client_path(client));
220 return -1;
221 }
223 if ((! sdb_unixsock_client_eof(client))
224 || sdb_unixsock_client_error(client)) {
225 char errbuf[1024];
226 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
227 "services from livestatus @ %s: %s",
228 sdb_unixsock_client_path(client),
229 sdb_strerror(errno, errbuf, sizeof(errbuf)));
230 return -1;
231 }
232 return 0;
233 } /* sdb_livestatus_collect */
235 static int
236 sdb_livestatus_config_instance(oconfig_item_t *ci)
237 {
238 char *name = NULL;
239 char *socket_path = NULL;
241 sdb_object_t *user_data;
242 sdb_unixsock_client_t *client;
244 int i;
246 if (oconfig_get_string(ci, &name)) {
247 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
248 "a single string argument\n\tUsage: <Instance NAME>");
249 return -1;
250 }
252 for (i = 0; i < ci->children_num; ++i) {
253 oconfig_item_t *child = ci->children + i;
255 if (! strcasecmp(child->key, "Socket"))
256 oconfig_get_string(child, &socket_path);
257 else
258 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
259 "unknown config option '%s' inside <Instance %s>.",
260 child->key, name);
261 }
263 if (! socket_path) {
264 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
265 "missing the 'Socket' option.", name);
266 return -1;
267 }
269 client = sdb_unixsock_client_create(socket_path);
270 if (! client) {
271 char errbuf[1024];
272 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
273 "unixsock client: %s",
274 sdb_strerror(errno, errbuf, sizeof(errbuf)));
275 return -1;
276 }
278 user_data = sdb_object_create_wrapper("unixsock-client", client,
279 (void (*)(void *))sdb_unixsock_client_destroy);
280 if (! user_data) {
281 sdb_unixsock_client_destroy(client);
282 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
283 "allocate sdb_object_t");
284 return -1;
285 }
287 sdb_plugin_register_init(name, sdb_livestatus_init, user_data);
288 sdb_plugin_register_shutdown(name, sdb_livestatus_shutdown, user_data);
289 sdb_plugin_register_collector(name, sdb_livestatus_collect,
290 /* interval */ NULL, user_data);
292 /* pass control to the list */
293 sdb_object_deref(user_data);
294 return 0;
295 } /* sdb_livestatus_config_instance */
297 static int
298 sdb_livestatus_config(oconfig_item_t *ci)
299 {
300 int i;
302 if (! ci) /* nothing to do to deconfigure this plugin */
303 return 0;
305 for (i = 0; i < ci->children_num; ++i) {
306 oconfig_item_t *child = ci->children + i;
308 if (! strcasecmp(child->key, "Instance"))
309 sdb_livestatus_config_instance(child);
310 else
311 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
312 "unknown config option '%s'.", child->key);
313 }
314 return 0;
315 } /* sdb_livestatus_config */
317 int
318 sdb_module_init(sdb_plugin_info_t *info)
319 {
320 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
321 "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
322 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
323 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
324 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
325 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
326 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
328 sdb_plugin_register_config(sdb_livestatus_config);
329 return 0;
330 } /* sdb_version_extra */
332 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */