Code

store: Renamed sdb_store_get_host() to sdb_store_has_host().
[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 "core/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         char *hostname = NULL;
89         char *svcname = NULL;
90         sdb_time_t timestamp = 0;
92         sdb_service_t svc = SDB_SVC_INIT;
94         int status;
96         assert(n == 3);
97         assert((data[0].type == SDB_TYPE_STRING)
98                         && (data[1].type == SDB_TYPE_STRING)
99                         && (data[2].type == SDB_TYPE_DATETIME));
101         hostname  = strdup(data[0].data.string);
102         svcname   = strdup(data[1].data.string);
103         timestamp = data[2].data.datetime;
105         svc.hostname = hostname;
106         SDB_OBJ(&svc)->name = svcname;
107         svc._last_update = timestamp;
109         status = sdb_store_service(&svc);
111         if (status < 0) {
112                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
113                                 "store/update service '%s / %s'.", hostname, svcname);
114                 free(hostname);
115                 free(svcname);
116                 return -1;
117         }
118         else if (status > 0) /* value too old */
119                 return 0;
121         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
122                         "service '%s / %s' (last update timestamp = %"PRIscTIME").",
123                         hostname, svcname, timestamp);
124         free(hostname);
125         free(svcname);
126         return 0;
127 } /* sdb_livestatus_get_svc */
129 /*
130  * plugin API
131  */
133 static int
134 sdb_livestatus_init(sdb_object_t *user_data)
136         sdb_unixsock_client_t *client;
138         if (! user_data)
139                 return -1;
141         client = SDB_OBJ_WRAPPER(user_data)->data;
142         if (sdb_unixsock_client_connect(client)) {
143                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
144                                 "Failed to connect to livestatus @ %s.",
145                                 sdb_unixsock_client_path(client));
146                 return -1;
147         }
149         sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
150                         "connected to livestatus @ %s.",
151                         sdb_unixsock_client_path(client));
152         return 0;
153 } /* sdb_livestatus_init */
155 static int
156 sdb_livestatus_collect(sdb_object_t *user_data)
158         sdb_unixsock_client_t *client;
160         int status;
162         if (! user_data)
163                 return -1;
165         client = SDB_OBJ_WRAPPER(user_data)->data;
167         status = sdb_unixsock_client_send(client, "GET hosts\r\n"
168                         "Columns: name last_check");
169         if (status <= 0) {
170                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
171                                 "'GET hosts' command to livestatus @ %s.",
172                                 sdb_unixsock_client_path(client));
173                 return -1;
174         }
176         sdb_unixsock_client_shutdown(client, SHUT_WR);
178         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
179                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
180                                 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
181                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
182                                 "response from livestatus @ %s while reading hosts.",
183                                 sdb_unixsock_client_path(client));
184                 return -1;
185         }
187         if ((! sdb_unixsock_client_eof(client))
188                         || sdb_unixsock_client_error(client)) {
189                 char errbuf[1024];
190                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
191                                 "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, "MK Livestatus backend: Failed to send "
201                                 "'GET services' command to livestatus @ %s.",
202                                 sdb_unixsock_client_path(client));
203                 return -1;
204         }
206         sdb_unixsock_client_shutdown(client, SHUT_WR);
208         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
209                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
210                                 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
211                                 SDB_TYPE_DATETIME)) {
212                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
213                                 "response from livestatus @ %s while reading services.",
214                                 sdb_unixsock_client_path(client));
215                 return -1;
216         }
218         if ((! sdb_unixsock_client_eof(client))
219                         || sdb_unixsock_client_error(client)) {
220                 char errbuf[1024];
221                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
222                                 "services from livestatus @ %s: %s",
223                                 sdb_unixsock_client_path(client),
224                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
225                 return -1;
226         }
227         return 0;
228 } /* sdb_livestatus_collect */
230 static int
231 sdb_livestatus_config_instance(oconfig_item_t *ci)
233         char *name = NULL;
234         char *socket_path = NULL;
236         char cb_name[1024];
238         sdb_object_t *user_data;
239         sdb_unixsock_client_t *client;
241         int i;
243         if (oconfig_get_string(ci, &name)) {
244                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
245                                 "a single string argument\n\tUsage: <Instance NAME>");
246                 return -1;
247         }
249         for (i = 0; i < ci->children_num; ++i) {
250                 oconfig_item_t *child = ci->children + i;
252                 if (! strcasecmp(child->key, "Socket"))
253                         oconfig_get_string(child, &socket_path);
254                 else
255                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
256                                         "unknown config option '%s' inside <Instance %s>.",
257                                         child->key, name);
258         }
260         if (! socket_path) {
261                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
262                                 "missing the 'Socket' option.", name);
263                 return -1;
264         }
266         snprintf(cb_name, sizeof(cb_name), "mk-livestatus::%s", name);
267         cb_name[sizeof(cb_name) - 1] = '\0';
269         client = sdb_unixsock_client_create(socket_path);
270         if (! client) {
271                 char errbuf[1024];
272                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
273                                 "unixsock client: %s",
274                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
275                 return -1;
276         }
278         user_data = sdb_object_create_wrapper("unixsock-client", client,
279                         (void (*)(void *))sdb_unixsock_client_destroy);
280         if (! user_data) {
281                 sdb_unixsock_client_destroy(client);
282                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
283                                 "allocate sdb_object_t");
284                 return -1;
285         }
287         sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
288         sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
289                         /* interval */ NULL, user_data);
291         /* pass control to the list */
292         sdb_object_deref(user_data);
293         return 0;
294 } /* sdb_livestatus_config_instance */
296 static int
297 sdb_livestatus_config(oconfig_item_t *ci)
299         int i;
301         for (i = 0; i < ci->children_num; ++i) {
302                 oconfig_item_t *child = ci->children + i;
304                 if (! strcasecmp(child->key, "Instance"))
305                         sdb_livestatus_config_instance(child);
306                 else
307                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
308                                         "unknown config option '%s'.", child->key);
309         }
310         return 0;
311 } /* sdb_livestatus_config */
313 int
314 sdb_module_init(sdb_plugin_info_t *info)
316         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
317         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
318                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
319         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
320                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
321         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
322         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
323         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
325         sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config);
326         return 0;
327 } /* sdb_version_extra */
329 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */