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, "Failed to 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, "Added/updated host '%s' "
82 "(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_plugin_store_service(hostname, svcname, timestamp);
109 if (status < 0) {
110 sdb_log(SDB_LOG_ERR, "Failed to store/update service '%s / %s'.",
111 hostname, svcname);
112 return -1;
113 }
114 else if (status > 0) /* value too old */
115 return 0;
117 sdb_log(SDB_LOG_DEBUG, "Added/updated service '%s / %s' "
118 "(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, "Failed to connect to livestatus @ %s.",
138 sdb_unixsock_client_path(client));
139 return -1;
140 }
142 sdb_log(SDB_LOG_INFO, "Successfully connected to livestatus @ %s.",
143 sdb_unixsock_client_path(client));
144 return 0;
145 } /* sdb_livestatus_init */
147 static int
148 sdb_livestatus_shutdown(sdb_object_t *user_data)
149 {
150 if (! user_data)
151 return -1;
153 sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
154 SDB_OBJ_WRAPPER(user_data)->data = NULL;
155 return 0;
156 } /* sdb_livestatus_shutdown */
158 static int
159 sdb_livestatus_collect(sdb_object_t *user_data)
160 {
161 sdb_unixsock_client_t *client;
163 int status;
165 if (! user_data)
166 return -1;
168 client = SDB_OBJ_WRAPPER(user_data)->data;
170 status = sdb_unixsock_client_send(client, "GET hosts\r\n"
171 "Columns: name last_check");
172 if (status <= 0) {
173 sdb_log(SDB_LOG_ERR, "Failed to send 'GET hosts' command to livestatus @ %s.",
174 sdb_unixsock_client_path(client));
175 return -1;
176 }
178 sdb_unixsock_client_shutdown(client, SHUT_WR);
180 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
181 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
182 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
183 sdb_log(SDB_LOG_ERR, "Failed to read response from livestatus @ %s "
184 "while reading hosts.", sdb_unixsock_client_path(client));
185 return -1;
186 }
188 if ((! sdb_unixsock_client_eof(client))
189 || sdb_unixsock_client_error(client)) {
190 char errbuf[1024];
191 sdb_log(SDB_LOG_ERR, "Failed to read host from livestatus @ %s: %s",
192 sdb_unixsock_client_path(client),
193 sdb_strerror(errno, errbuf, sizeof(errbuf)));
194 return -1;
195 }
197 status = sdb_unixsock_client_send(client, "GET services\r\n"
198 "Columns: host_name description last_check");
199 if (status <= 0) {
200 sdb_log(SDB_LOG_ERR, "Failed to send 'GET services' command to livestatus @ %s.",
201 sdb_unixsock_client_path(client));
202 return -1;
203 }
205 sdb_unixsock_client_shutdown(client, SHUT_WR);
207 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
208 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
209 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
210 SDB_TYPE_DATETIME)) {
211 sdb_log(SDB_LOG_ERR, "Failed to read response from livestatus @ %s while reading services.",
212 sdb_unixsock_client_path(client));
213 return -1;
214 }
216 if ((! sdb_unixsock_client_eof(client))
217 || sdb_unixsock_client_error(client)) {
218 char errbuf[1024];
219 sdb_log(SDB_LOG_ERR, "Failed to read services from livestatus @ %s: %s",
220 sdb_unixsock_client_path(client),
221 sdb_strerror(errno, errbuf, sizeof(errbuf)));
222 return -1;
223 }
224 return 0;
225 } /* sdb_livestatus_collect */
227 static int
228 sdb_livestatus_config_instance(oconfig_item_t *ci)
229 {
230 char *name = NULL;
231 char *socket_path = NULL;
233 sdb_object_t *user_data;
234 sdb_unixsock_client_t *client;
236 int i;
238 if (oconfig_get_string(ci, &name)) {
239 sdb_log(SDB_LOG_ERR, "Instance requires a single string argument\n"
240 "\tUsage: <Instance NAME>");
241 return -1;
242 }
244 for (i = 0; i < ci->children_num; ++i) {
245 oconfig_item_t *child = ci->children + i;
247 if (! strcasecmp(child->key, "Socket"))
248 oconfig_get_string(child, &socket_path);
249 else
250 sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s' "
251 "inside <Instance %s>.", child->key, name);
252 }
254 if (! socket_path) {
255 sdb_log(SDB_LOG_ERR, "Instance '%s' missing the 'Socket' option.", name);
256 return -1;
257 }
259 client = sdb_unixsock_client_create(socket_path);
260 if (! client) {
261 char errbuf[1024];
262 sdb_log(SDB_LOG_ERR, "Failed to create unixsock client: %s",
263 sdb_strerror(errno, errbuf, sizeof(errbuf)));
264 return -1;
265 }
267 user_data = sdb_object_create_wrapper("unixsock-client", client,
268 (void (*)(void *))sdb_unixsock_client_destroy);
269 if (! user_data) {
270 sdb_unixsock_client_destroy(client);
271 sdb_log(SDB_LOG_ERR, "Failed to allocate sdb_object_t");
272 return -1;
273 }
275 sdb_plugin_register_init(name, sdb_livestatus_init, user_data);
276 sdb_plugin_register_shutdown(name, sdb_livestatus_shutdown, user_data);
277 sdb_plugin_register_collector(name, sdb_livestatus_collect,
278 /* interval */ NULL, user_data);
280 /* pass control to the list */
281 sdb_object_deref(user_data);
282 return 0;
283 } /* sdb_livestatus_config_instance */
285 static int
286 sdb_livestatus_config(oconfig_item_t *ci)
287 {
288 int i;
290 if (! ci) /* nothing to do to deconfigure this plugin */
291 return 0;
293 for (i = 0; i < ci->children_num; ++i) {
294 oconfig_item_t *child = ci->children + i;
296 if (! strcasecmp(child->key, "Instance"))
297 sdb_livestatus_config_instance(child);
298 else
299 sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s'.", child->key);
300 }
301 return 0;
302 } /* sdb_livestatus_config */
304 int
305 sdb_module_init(sdb_plugin_info_t *info)
306 {
307 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
308 "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
309 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
310 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
311 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
312 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
313 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
315 sdb_plugin_register_config(sdb_livestatus_config);
316 return 0;
317 } /* sdb_module_init */
319 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */