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/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 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 fprintf(stderr, "MK Livestatus backend: Failed to store/update "
76 "host '%s'.\n", hostname);
77 free(hostname);
78 return -1;
79 }
80 else if (status > 0) /* value too old */
81 return 0;
83 fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' "
84 "(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 fprintf(stderr, "MK Livestatus backend: Failed to store/update "
120 "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 fprintf(stderr, "MK Livestatus backend: Added/updated service '%s / %s' "
129 "(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 fprintf(stderr, "MK Livestatus backend: "
151 "Failed to connect to livestatus @ %s.\n",
152 sdb_unixsock_client_path(client));
153 return -1;
154 }
156 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "MK Livestatus backend: Failed to read response "
189 "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 fprintf(stderr, "MK Livestatus backend: Failed to read host "
198 "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 fprintf(stderr, "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 fprintf(stderr, "MK Livestatus backend: Failed to read response "
220 "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 fprintf(stderr, "MK Livestatus backend: Failed to read services "
229 "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 = 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 fprintf(stderr, "MK Livestatus backend: Instance requires a single "
252 "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);
261 else
262 fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
263 "option '%s' inside <Instance %s>.\n",
264 child->key, name);
265 }
267 if (! socket) {
268 fprintf(stderr, "MK Livestatus backend: Instance '%s' missing "
269 "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);
277 if (! client) {
278 char errbuf[1024];
279 fprintf(stderr, "MK Livestatus backend: Failed to create unixsock "
280 "client: %s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
281 return -1;
282 }
284 user_data = sdb_object_create_wrapper(client,
285 (void (*)(void *))sdb_unixsock_client_destroy);
286 if (! user_data) {
287 sdb_unixsock_client_destroy(client);
288 fprintf(stderr, "MK Livestatus backend: Failed to "
289 "allocate sdb_object_t\n");
290 return -1;
291 }
293 sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
294 sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
295 /* interval */ NULL, user_data);
297 /* pass control to the list */
298 sdb_object_deref(user_data);
299 return 0;
300 } /* sdb_livestatus_config_instance */
302 static int
303 sdb_livestatus_config(oconfig_item_t *ci)
304 {
305 int i;
307 for (i = 0; i < ci->children_num; ++i) {
308 oconfig_item_t *child = ci->children + i;
310 if (! strcasecmp(child->key, "Instance"))
311 sdb_livestatus_config_instance(child);
312 else
313 fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
314 "option '%s'.\n", child->key);
315 }
316 return 0;
317 } /* sdb_livestatus_config */
319 int
320 sdb_module_init(sdb_plugin_info_t *info)
321 {
322 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
323 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
324 "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
325 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
326 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
327 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
328 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
329 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
331 sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config);
332 return 0;
333 } /* sdb_version_extra */
335 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */