Code

Merged branch 'master' of git://git.tokkee.org/sysdb.
[sysdb.git] / src / backend / mk-livestatus.c
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         const char *hostname;
56         sdb_time_t timestamp;
58         int status;
60         assert(n == 2);
61         assert((data[0].type == SDB_TYPE_STRING)
62                         && (data[1].type == SDB_TYPE_DATETIME));
64         hostname  = data[0].data.string;
65         timestamp = data[1].data.datetime;
67         status = sdb_store_host(hostname, timestamp);
69         if (status < 0) {
70                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
71                                 "store/update host '%s'.", hostname);
72                 return -1;
73         }
74         else if (status > 0) /* value too old */
75                 return 0;
77         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
78                         "host '%s' (last update timestamp = %"PRIscTIME").",
79                         hostname, timestamp);
80         return 0;
81 } /* sdb_livestatus_get_host */
83 static int
84 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
85                 size_t n, sdb_data_t *data,
86                 sdb_object_t __attribute__((unused)) *user_data)
87 {
88         const char *hostname = NULL;
89         const char *svcname = NULL;
90         sdb_time_t timestamp = 0;
92         int status;
94         assert(n == 3);
95         assert((data[0].type == SDB_TYPE_STRING)
96                         && (data[1].type == SDB_TYPE_STRING)
97                         && (data[2].type == SDB_TYPE_DATETIME));
99         hostname  = data[0].data.string;
100         svcname   = data[1].data.string;
101         timestamp = data[2].data.datetime;
103         status = sdb_store_service(hostname, svcname, timestamp);
105         if (status < 0) {
106                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
107                                 "store/update service '%s / %s'.", hostname, svcname);
108                 return -1;
109         }
110         else if (status > 0) /* value too old */
111                 return 0;
113         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
114                         "service '%s / %s' (last update timestamp = %"PRIscTIME").",
115                         hostname, svcname, timestamp);
116         return 0;
117 } /* sdb_livestatus_get_svc */
119 /*
120  * plugin API
121  */
123 static int
124 sdb_livestatus_init(sdb_object_t *user_data)
126         sdb_unixsock_client_t *client;
128         if (! user_data)
129                 return -1;
131         client = SDB_OBJ_WRAPPER(user_data)->data;
132         if (sdb_unixsock_client_connect(client)) {
133                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
134                                 "Failed to connect to livestatus @ %s.",
135                                 sdb_unixsock_client_path(client));
136                 return -1;
137         }
139         sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
140                         "connected to livestatus @ %s.",
141                         sdb_unixsock_client_path(client));
142         return 0;
143 } /* sdb_livestatus_init */
145 static int
146 sdb_livestatus_collect(sdb_object_t *user_data)
148         sdb_unixsock_client_t *client;
150         int status;
152         if (! user_data)
153                 return -1;
155         client = SDB_OBJ_WRAPPER(user_data)->data;
157         status = sdb_unixsock_client_send(client, "GET hosts\r\n"
158                         "Columns: name last_check");
159         if (status <= 0) {
160                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
161                                 "'GET hosts' command to livestatus @ %s.",
162                                 sdb_unixsock_client_path(client));
163                 return -1;
164         }
166         sdb_unixsock_client_shutdown(client, SHUT_WR);
168         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
169                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
170                                 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
171                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
172                                 "response from livestatus @ %s while reading hosts.",
173                                 sdb_unixsock_client_path(client));
174                 return -1;
175         }
177         if ((! sdb_unixsock_client_eof(client))
178                         || sdb_unixsock_client_error(client)) {
179                 char errbuf[1024];
180                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
181                                 "host from livestatus @ %s: %s",
182                                 sdb_unixsock_client_path(client),
183                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
184                 return -1;
185         }
187         status = sdb_unixsock_client_send(client, "GET services\r\n"
188                         "Columns: host_name description last_check");
189         if (status <= 0) {
190                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
191                                 "'GET services' command to livestatus @ %s.",
192                                 sdb_unixsock_client_path(client));
193                 return -1;
194         }
196         sdb_unixsock_client_shutdown(client, SHUT_WR);
198         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
199                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
200                                 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
201                                 SDB_TYPE_DATETIME)) {
202                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
203                                 "response from livestatus @ %s while reading services.",
204                                 sdb_unixsock_client_path(client));
205                 return -1;
206         }
208         if ((! sdb_unixsock_client_eof(client))
209                         || sdb_unixsock_client_error(client)) {
210                 char errbuf[1024];
211                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
212                                 "services from livestatus @ %s: %s",
213                                 sdb_unixsock_client_path(client),
214                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
215                 return -1;
216         }
217         return 0;
218 } /* sdb_livestatus_collect */
220 static int
221 sdb_livestatus_config_instance(oconfig_item_t *ci)
223         char *name = NULL;
224         char *socket_path = NULL;
226         char cb_name[1024];
228         sdb_object_t *user_data;
229         sdb_unixsock_client_t *client;
231         int i;
233         if (oconfig_get_string(ci, &name)) {
234                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
235                                 "a single string argument\n\tUsage: <Instance NAME>");
236                 return -1;
237         }
239         for (i = 0; i < ci->children_num; ++i) {
240                 oconfig_item_t *child = ci->children + i;
242                 if (! strcasecmp(child->key, "Socket"))
243                         oconfig_get_string(child, &socket_path);
244                 else
245                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
246                                         "unknown config option '%s' inside <Instance %s>.",
247                                         child->key, name);
248         }
250         if (! socket_path) {
251                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
252                                 "missing the 'Socket' option.", name);
253                 return -1;
254         }
256         snprintf(cb_name, sizeof(cb_name), "mk-livestatus::%s", name);
257         cb_name[sizeof(cb_name) - 1] = '\0';
259         client = sdb_unixsock_client_create(socket_path);
260         if (! client) {
261                 char errbuf[1024];
262                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
263                                 "unixsock client: %s",
264                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
265                 return -1;
266         }
268         user_data = sdb_object_create_wrapper("unixsock-client", client,
269                         (void (*)(void *))sdb_unixsock_client_destroy);
270         if (! user_data) {
271                 sdb_unixsock_client_destroy(client);
272                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
273                                 "allocate sdb_object_t");
274                 return -1;
275         }
277         sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
278         sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
279                         /* interval */ NULL, user_data);
281         /* pass control to the list */
282         sdb_object_deref(user_data);
283         return 0;
284 } /* sdb_livestatus_config_instance */
286 static int
287 sdb_livestatus_config(oconfig_item_t *ci)
289         int i;
291         for (i = 0; i < ci->children_num; ++i) {
292                 oconfig_item_t *child = ci->children + i;
294                 if (! strcasecmp(child->key, "Instance"))
295                         sdb_livestatus_config_instance(child);
296                 else
297                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
298                                         "unknown config option '%s'.", child->key);
299         }
300         return 0;
301 } /* sdb_livestatus_config */
303 int
304 sdb_module_init(sdb_plugin_info_t *info)
306         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
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("mk-livestatus", sdb_livestatus_config);
316         return 0;
317 } /* sdb_version_extra */
319 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */