1 /*
2 * SysDB - 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 "sysdb.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/error.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 SDB_PLUGIN_MAGIC;
46 /*
47 * private helper functions
48 */
50 static int
51 sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
52 size_t n, sdb_data_t *data,
53 sdb_object_t __attribute__((unused)) *user_data)
54 {
55 char *hostname = NULL;
56 sdb_time_t timestamp = 0;
58 sdb_host_t host = SDB_HOST_INIT;
60 int status;
62 assert(n == 2);
63 assert((data[0].type == SDB_TYPE_STRING)
64 && (data[1].type == SDB_TYPE_DATETIME));
66 hostname = strdup(data[0].data.string);
67 timestamp = data[1].data.datetime;
69 host.host_name = hostname;
70 host.host_last_update = timestamp;
72 status = sdb_store_host(&host);
74 if (status < 0) {
75 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
76 "store/update host '%s'.\n", hostname);
77 free(hostname);
78 return -1;
79 }
80 else if (status > 0) /* value too old */
81 return 0;
83 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
84 "host '%s' (last update timestamp = %"PRIscTIME").\n",
85 hostname, timestamp);
86 free(hostname);
87 return 0;
88 } /* sdb_livestatus_get_host */
90 static int
91 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
92 size_t n, sdb_data_t *data,
93 sdb_object_t __attribute__((unused)) *user_data)
94 {
95 char *hostname = NULL;
96 char *svcname = NULL;
97 sdb_time_t timestamp = 0;
99 sdb_service_t svc = SDB_SVC_INIT;
101 int status;
103 assert(n == 3);
104 assert((data[0].type == SDB_TYPE_STRING)
105 && (data[1].type == SDB_TYPE_STRING)
106 && (data[2].type == SDB_TYPE_DATETIME));
108 hostname = strdup(data[0].data.string);
109 svcname = strdup(data[1].data.string);
110 timestamp = data[2].data.datetime;
112 svc.hostname = hostname;
113 svc.svc_name = svcname;
114 svc.svc_last_update = timestamp;
116 status = sdb_store_service(&svc);
118 if (status < 0) {
119 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
120 "store/update service '%s / %s'.\n", hostname, svcname);
121 free(hostname);
122 free(svcname);
123 return -1;
124 }
125 else if (status > 0) /* value too old */
126 return 0;
128 sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
129 "service '%s / %s' (last update timestamp = %"PRIscTIME").\n",
130 hostname, svcname, timestamp);
131 free(hostname);
132 free(svcname);
133 return 0;
134 } /* sdb_livestatus_get_svc */
136 /*
137 * plugin API
138 */
140 static int
141 sdb_livestatus_init(sdb_object_t *user_data)
142 {
143 sdb_unixsock_client_t *client;
145 if (! user_data)
146 return -1;
148 client = SDB_OBJ_WRAPPER(user_data)->data;
149 if (sdb_unixsock_client_connect(client)) {
150 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
151 "Failed to connect to livestatus @ %s.\n",
152 sdb_unixsock_client_path(client));
153 return -1;
154 }
156 sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
157 "connected to livestatus @ %s.\n",
158 sdb_unixsock_client_path(client));
159 return 0;
160 } /* sdb_livestatus_init */
162 static int
163 sdb_livestatus_collect(sdb_object_t *user_data)
164 {
165 sdb_unixsock_client_t *client;
167 int status;
169 if (! user_data)
170 return -1;
172 client = SDB_OBJ_WRAPPER(user_data)->data;
174 status = sdb_unixsock_client_send(client, "GET hosts\r\n"
175 "Columns: name last_check");
176 if (status <= 0) {
177 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
178 "'GET hosts' command to livestatus @ %s.\n",
179 sdb_unixsock_client_path(client));
180 return -1;
181 }
183 sdb_unixsock_client_shutdown(client, SHUT_WR);
185 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
186 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
187 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
188 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
189 "response from livestatus @ %s while reading hosts.\n",
190 sdb_unixsock_client_path(client));
191 return -1;
192 }
194 if ((! sdb_unixsock_client_eof(client))
195 || sdb_unixsock_client_error(client)) {
196 char errbuf[1024];
197 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
198 "host from livestatus @ %s: %s\n",
199 sdb_unixsock_client_path(client),
200 sdb_strerror(errno, errbuf, sizeof(errbuf)));
201 return -1;
202 }
204 status = sdb_unixsock_client_send(client, "GET services\r\n"
205 "Columns: host_name description last_check");
206 if (status <= 0) {
207 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
208 "'GET services' command to livestatus @ %s.\n",
209 sdb_unixsock_client_path(client));
210 return -1;
211 }
213 sdb_unixsock_client_shutdown(client, SHUT_WR);
215 if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
216 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
217 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
218 SDB_TYPE_DATETIME)) {
219 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
220 "response from livestatus @ %s while reading services.\n",
221 sdb_unixsock_client_path(client));
222 return -1;
223 }
225 if ((! sdb_unixsock_client_eof(client))
226 || sdb_unixsock_client_error(client)) {
227 char errbuf[1024];
228 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
229 "services from livestatus @ %s: %s\n",
230 sdb_unixsock_client_path(client),
231 sdb_strerror(errno, errbuf, sizeof(errbuf)));
232 return -1;
233 }
234 return 0;
235 } /* sdb_livestatus_collect */
237 static int
238 sdb_livestatus_config_instance(oconfig_item_t *ci)
239 {
240 char *name = NULL;
241 char *socket_path = NULL;
243 char cb_name[1024];
245 sdb_object_t *user_data;
246 sdb_unixsock_client_t *client;
248 int i;
250 if (oconfig_get_string(ci, &name)) {
251 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
252 "a single string argument\n\tUsage: <Instance NAME>\n");
253 return -1;
254 }
256 for (i = 0; i < ci->children_num; ++i) {
257 oconfig_item_t *child = ci->children + i;
259 if (! strcasecmp(child->key, "Socket"))
260 oconfig_get_string(child, &socket_path);
261 else
262 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
263 "unknown config option '%s' inside <Instance %s>.\n",
264 child->key, name);
265 }
267 if (! socket_path) {
268 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
269 "missing the 'Socket' option.\n", name);
270 return -1;
271 }
273 snprintf(cb_name, sizeof(cb_name), "mk-livestatus-%s", name);
274 cb_name[sizeof(cb_name) - 1] = '\0';
276 client = sdb_unixsock_client_create(socket_path);
277 if (! client) {
278 char errbuf[1024];
279 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
280 "unixsock client: %s\n",
281 sdb_strerror(errno, errbuf, sizeof(errbuf)));
282 return -1;
283 }
285 user_data = sdb_object_create_wrapper(client,
286 (void (*)(void *))sdb_unixsock_client_destroy);
287 if (! user_data) {
288 sdb_unixsock_client_destroy(client);
289 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
290 "allocate sdb_object_t\n");
291 return -1;
292 }
294 sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
295 sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
296 /* interval */ NULL, user_data);
298 /* pass control to the list */
299 sdb_object_deref(user_data);
300 return 0;
301 } /* sdb_livestatus_config_instance */
303 static int
304 sdb_livestatus_config(oconfig_item_t *ci)
305 {
306 int i;
308 for (i = 0; i < ci->children_num; ++i) {
309 oconfig_item_t *child = ci->children + i;
311 if (! strcasecmp(child->key, "Instance"))
312 sdb_livestatus_config_instance(child);
313 else
314 sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
315 "unknown config option '%s'.\n", child->key);
316 }
317 return 0;
318 } /* sdb_livestatus_config */
320 int
321 sdb_module_init(sdb_plugin_info_t *info)
322 {
323 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
324 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
325 "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
326 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
327 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
328 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
329 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
330 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
332 sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config);
333 return 0;
334 } /* sdb_version_extra */
336 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */