Code

Renamed error recording / logging functions.
[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/string.h"
33 #include "utils/unixsock.h"
35 #include "liboconfig/utils.h"
37 #include <assert.h>
39 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
45 SDB_PLUGIN_MAGIC;
47 /*
48  * private helper functions
49  */
51 static int
52 sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
53                 size_t n, sdb_data_t *data,
54                 sdb_object_t __attribute__((unused)) *user_data)
55 {
56         char *hostname = NULL;
57         sdb_time_t timestamp = 0;
59         sdb_host_t host = SDB_HOST_INIT;
61         int status;
63         assert(n == 2);
64         assert((data[0].type == SDB_TYPE_STRING)
65                         && (data[1].type == SDB_TYPE_DATETIME));
67         hostname  = strdup(data[0].data.string);
68         timestamp = data[1].data.datetime;
70         host.host_name = hostname;
71         host.host_last_update = timestamp;
73         status = sdb_store_host(&host);
75         if (status < 0) {
76                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
77                                 "store/update host '%s'.\n", hostname);
78                 free(hostname);
79                 return -1;
80         }
81         else if (status > 0) /* value too old */
82                 return 0;
84         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
85                         "host '%s' (last update timestamp = %"PRIscTIME").\n",
86                         hostname, timestamp);
87         free(hostname);
88         return 0;
89 } /* sdb_livestatus_get_host */
91 static int
92 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
93                 size_t n, sdb_data_t *data,
94                 sdb_object_t __attribute__((unused)) *user_data)
95 {
96         char *hostname = NULL;
97         char *svcname = NULL;
98         sdb_time_t timestamp = 0;
100         sdb_service_t svc = SDB_SVC_INIT;
102         int status;
104         assert(n == 3);
105         assert((data[0].type == SDB_TYPE_STRING)
106                         && (data[1].type == SDB_TYPE_STRING)
107                         && (data[2].type == SDB_TYPE_DATETIME));
109         hostname  = strdup(data[0].data.string);
110         svcname   = strdup(data[1].data.string);
111         timestamp = data[2].data.datetime;
113         svc.hostname = hostname;
114         svc.svc_name = svcname;
115         svc.svc_last_update = timestamp;
117         status = sdb_store_service(&svc);
119         if (status < 0) {
120                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
121                                 "store/update service '%s / %s'.\n", hostname, svcname);
122                 free(hostname);
123                 free(svcname);
124                 return -1;
125         }
126         else if (status > 0) /* value too old */
127                 return 0;
129         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
130                         "service '%s / %s' (last update timestamp = %"PRIscTIME").\n",
131                         hostname, svcname, timestamp);
132         free(hostname);
133         free(svcname);
134         return 0;
135 } /* sdb_livestatus_get_svc */
137 /*
138  * plugin API
139  */
141 static int
142 sdb_livestatus_init(sdb_object_t *user_data)
144         sdb_unixsock_client_t *client;
146         if (! user_data)
147                 return -1;
149         client = SDB_OBJ_WRAPPER(user_data)->data;
150         if (sdb_unixsock_client_connect(client)) {
151                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
152                                 "Failed to connect to livestatus @ %s.\n",
153                                 sdb_unixsock_client_path(client));
154                 return -1;
155         }
157         sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
158                         "connected to livestatus @ %s.\n",
159                         sdb_unixsock_client_path(client));
160         return 0;
161 } /* sdb_livestatus_init */
163 static int
164 sdb_livestatus_collect(sdb_object_t *user_data)
166         sdb_unixsock_client_t *client;
168         int status;
170         if (! user_data)
171                 return -1;
173         client = SDB_OBJ_WRAPPER(user_data)->data;
175         status = sdb_unixsock_client_send(client, "GET hosts\r\n"
176                         "Columns: name last_check");
177         if (status <= 0) {
178                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
179                                 "'GET hosts' command to livestatus @ %s.\n",
180                                 sdb_unixsock_client_path(client));
181                 return -1;
182         }
184         sdb_unixsock_client_shutdown(client, SHUT_WR);
186         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
187                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
188                                 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
189                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
190                                 "response from livestatus @ %s while reading hosts.\n",
191                                 sdb_unixsock_client_path(client));
192                 return -1;
193         }
195         if ((! sdb_unixsock_client_eof(client))
196                         || sdb_unixsock_client_error(client)) {
197                 char errbuf[1024];
198                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
199                                 "host from livestatus @ %s: %s\n",
200                                 sdb_unixsock_client_path(client),
201                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
202                 return -1;
203         }
205         status = sdb_unixsock_client_send(client, "GET services\r\n"
206                         "Columns: host_name description last_check");
207         if (status <= 0) {
208                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
209                                 "'GET services' command to livestatus @ %s.\n",
210                                 sdb_unixsock_client_path(client));
211                 return -1;
212         }
214         sdb_unixsock_client_shutdown(client, SHUT_WR);
216         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
217                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
218                                 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
219                                 SDB_TYPE_DATETIME)) {
220                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
221                                 "response from livestatus @ %s while reading services.\n",
222                                 sdb_unixsock_client_path(client));
223                 return -1;
224         }
226         if ((! sdb_unixsock_client_eof(client))
227                         || sdb_unixsock_client_error(client)) {
228                 char errbuf[1024];
229                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
230                                 "services from livestatus @ %s: %s\n",
231                                 sdb_unixsock_client_path(client),
232                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
233                 return -1;
234         }
235         return 0;
236 } /* sdb_livestatus_collect */
238 static int
239 sdb_livestatus_config_instance(oconfig_item_t *ci)
241         char *name = NULL;
242         char *socket_path = NULL;
244         char cb_name[1024];
246         sdb_object_t *user_data;
247         sdb_unixsock_client_t *client;
249         int i;
251         if (oconfig_get_string(ci, &name)) {
252                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
253                                 "a single string argument\n\tUsage: <Instance NAME>\n");
254                 return -1;
255         }
257         for (i = 0; i < ci->children_num; ++i) {
258                 oconfig_item_t *child = ci->children + i;
260                 if (! strcasecmp(child->key, "Socket"))
261                         oconfig_get_string(child, &socket_path);
262                 else
263                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
264                                         "unknown config option '%s' inside <Instance %s>.\n",
265                                         child->key, name);
266         }
268         if (! socket_path) {
269                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
270                                 "missing the 'Socket' option.\n", name);
271                 return -1;
272         }
274         snprintf(cb_name, sizeof(cb_name), "mk-livestatus-%s", name);
275         cb_name[sizeof(cb_name) - 1] = '\0';
277         client = sdb_unixsock_client_create(socket_path);
278         if (! client) {
279                 char errbuf[1024];
280                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
281                                 "unixsock client: %s\n",
282                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
283                 return -1;
284         }
286         user_data = sdb_object_create_wrapper(client,
287                         (void (*)(void *))sdb_unixsock_client_destroy);
288         if (! user_data) {
289                 sdb_unixsock_client_destroy(client);
290                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
291                                 "allocate sdb_object_t\n");
292                 return -1;
293         }
295         sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
296         sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
297                         /* interval */ NULL, user_data);
299         /* pass control to the list */
300         sdb_object_deref(user_data);
301         return 0;
302 } /* sdb_livestatus_config_instance */
304 static int
305 sdb_livestatus_config(oconfig_item_t *ci)
307         int i;
309         for (i = 0; i < ci->children_num; ++i) {
310                 oconfig_item_t *child = ci->children + i;
312                 if (! strcasecmp(child->key, "Instance"))
313                         sdb_livestatus_config_instance(child);
314                 else
315                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
316                                         "unknown config option '%s'.\n", child->key);
317         }
318         return 0;
319 } /* sdb_livestatus_config */
321 int
322 sdb_module_init(sdb_plugin_info_t *info)
324         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
325         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
326                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
327         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
328                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
329         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
330         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
331         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
333         sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config);
334         return 0;
335 } /* sdb_version_extra */
337 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */