cd8bbd0fc79d395e1d9b8fbb407020e4893dc105
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>
47 #include <strings.h>
49 SDB_PLUGIN_MAGIC;
51 /*
52 * private helper functions
53 */
55 static int
56 sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
57 size_t n, sdb_data_t *data,
58 sdb_object_t __attribute__((unused)) *user_data)
59 {
60 const char *hostname;
61 sdb_time_t timestamp;
63 int status;
65 assert(n == 2);
66 assert((data[0].type == SDB_TYPE_STRING)
67 && (data[1].type == SDB_TYPE_DATETIME));
69 hostname = data[0].data.string;
70 timestamp = data[1].data.datetime;
72 status = sdb_plugin_store_host(hostname, timestamp);
74 if (status < 0) {
75 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
76 "store/update host '%s'.", hostname);
77 return -1;
78 }
79 else if (status > 0) /* value too old */
80 return 0;
82 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
83 "host '%s' (last update timestamp = %"PRIsdbTIME").",
84 hostname, timestamp);
85 return 0;
86 } /* sdb_livestatus_get_host */
88 static int
89 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
90 size_t n, sdb_data_t *data,
91 sdb_object_t __attribute__((unused)) *user_data)
92 {
93 const char *hostname = NULL;
94 const char *svcname = NULL;
95 sdb_time_t timestamp = 0;
97 int status;
99 assert(n == 3);
100 assert((data[0].type == SDB_TYPE_STRING)
101 && (data[1].type == SDB_TYPE_STRING)
102 && (data[2].type == SDB_TYPE_DATETIME));
104 hostname = data[0].data.string;
105 svcname = data[1].data.string;
106 timestamp = data[2].data.datetime;
108 status = sdb_plugin_store_service(hostname, svcname, timestamp);
110 if (status < 0) {
111 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
112 "store/update service '%s / %s'.", hostname, svcname);
113 return -1;
114 }
115 else if (status > 0) /* value too old */
116 return 0;
118 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
119 "service '%s / %s' (last update timestamp = %"PRIsdbTIME").",
120 hostname, svcname, timestamp);
121 return 0;
122 } /* sdb_livestatus_get_svc */
124 /*
125 * plugin API
126 */
128 static int
129 sdb_livestatus_init(sdb_object_t *user_data)
130 {
131 sdb_unixsock_client_t *client;
133 if (! user_data)
134 return -1;
136 client = SDB_OBJ_WRAPPER(user_data)->data;
137 if (sdb_unixsock_client_connect(client)) {
138 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
139 "Failed to connect to livestatus @ %s.",
140 sdb_unixsock_client_path(client));
141 return -1;
142 }
144 sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
145 "connected to livestatus @ %s.",
146 sdb_unixsock_client_path(client));
147 return 0;
148 } /* sdb_livestatus_init */
150 static int
151 sdb_livestatus_shutdown(sdb_object_t *user_data)
152 {
153 if (! user_data)
154 return -1;
156 sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
157 SDB_OBJ_WRAPPER(user_data)->data = NULL;
158 return 0;
159 } /* sdb_livestatus_shutdown */
161 static int
162 sdb_livestatus_collect(sdb_object_t *user_data)
163 {
164 sdb_unixsock_client_t *client;
166 int status;
168 if (! user_data)
169 return -1;
171 client = SDB_OBJ_WRAPPER(user_data)->data;
173 status = sdb_unixsock_client_send(client, "GET hosts\r\n"
174 "Columns: name last_check");
175 if (status <= 0) {
176 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
177 "'GET hosts' command to livestatus @ %s.",
178 sdb_unixsock_client_path(client));
179 return -1;
180 }
182 sdb_unixsock_client_shutdown(client, SHUT_WR);
184 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
185 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
186 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
187 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
188 "response from livestatus @ %s while reading hosts.",
189 sdb_unixsock_client_path(client));
190 return -1;
191 }
193 if ((! sdb_unixsock_client_eof(client))
194 || sdb_unixsock_client_error(client)) {
195 char errbuf[1024];
196 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
197 "host from livestatus @ %s: %s",
198 sdb_unixsock_client_path(client),
199 sdb_strerror(errno, errbuf, sizeof(errbuf)));
200 return -1;
201 }
203 status = sdb_unixsock_client_send(client, "GET services\r\n"
204 "Columns: host_name description last_check");
205 if (status <= 0) {
206 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
207 "'GET services' command to livestatus @ %s.",
208 sdb_unixsock_client_path(client));
209 return -1;
210 }
212 sdb_unixsock_client_shutdown(client, SHUT_WR);
214 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
215 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
216 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
217 SDB_TYPE_DATETIME)) {
218 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
219 "response from livestatus @ %s while reading services.",
220 sdb_unixsock_client_path(client));
221 return -1;
222 }
224 if ((! sdb_unixsock_client_eof(client))
225 || sdb_unixsock_client_error(client)) {
226 char errbuf[1024];
227 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
228 "services from livestatus @ %s: %s",
229 sdb_unixsock_client_path(client),
230 sdb_strerror(errno, errbuf, sizeof(errbuf)));
231 return -1;
232 }
233 return 0;
234 } /* sdb_livestatus_collect */
236 static int
237 sdb_livestatus_config_instance(oconfig_item_t *ci)
238 {
239 char *name = NULL;
240 char *socket_path = NULL;
242 sdb_object_t *user_data;
243 sdb_unixsock_client_t *client;
245 int i;
247 if (oconfig_get_string(ci, &name)) {
248 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
249 "a single string argument\n\tUsage: <Instance NAME>");
250 return -1;
251 }
253 for (i = 0; i < ci->children_num; ++i) {
254 oconfig_item_t *child = ci->children + i;
256 if (! strcasecmp(child->key, "Socket"))
257 oconfig_get_string(child, &socket_path);
258 else
259 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
260 "unknown config option '%s' inside <Instance %s>.",
261 child->key, name);
262 }
264 if (! socket_path) {
265 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
266 "missing the 'Socket' option.", name);
267 return -1;
268 }
270 client = sdb_unixsock_client_create(socket_path);
271 if (! client) {
272 char errbuf[1024];
273 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
274 "unixsock client: %s",
275 sdb_strerror(errno, errbuf, sizeof(errbuf)));
276 return -1;
277 }
279 user_data = sdb_object_create_wrapper("unixsock-client", client,
280 (void (*)(void *))sdb_unixsock_client_destroy);
281 if (! user_data) {
282 sdb_unixsock_client_destroy(client);
283 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
284 "allocate sdb_object_t");
285 return -1;
286 }
288 sdb_plugin_register_init(name, sdb_livestatus_init, user_data);
289 sdb_plugin_register_shutdown(name, sdb_livestatus_shutdown, user_data);
290 sdb_plugin_register_collector(name, sdb_livestatus_collect,
291 /* interval */ NULL, user_data);
293 /* pass control to the list */
294 sdb_object_deref(user_data);
295 return 0;
296 } /* sdb_livestatus_config_instance */
298 static int
299 sdb_livestatus_config(oconfig_item_t *ci)
300 {
301 int i;
303 if (! ci) /* nothing to do to deconfigure this plugin */
304 return 0;
306 for (i = 0; i < ci->children_num; ++i) {
307 oconfig_item_t *child = ci->children + i;
309 if (! strcasecmp(child->key, "Instance"))
310 sdb_livestatus_config_instance(child);
311 else
312 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
313 "unknown config option '%s'.", child->key);
314 }
315 return 0;
316 } /* sdb_livestatus_config */
318 int
319 sdb_module_init(sdb_plugin_info_t *info)
320 {
321 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
322 "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
323 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
324 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
325 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
326 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
327 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
329 sdb_plugin_register_config(sdb_livestatus_config);
330 return 0;
331 } /* sdb_module_init */
333 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */