Code

frontend: Let the parser not accept other grammars in default mode.
[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 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "core/store.h"
35 #include "utils/error.h"
36 #include "utils/unixsock.h"
38 #include "liboconfig/utils.h"
40 #include <assert.h>
42 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51  * private helper functions
52  */
54 static int
55 sdb_livestatus_get_host(sdb_unixsock_client_t __attribute__((unused)) *client,
56                 size_t n, sdb_data_t *data,
57                 sdb_object_t __attribute__((unused)) *user_data)
58 {
59         const char *hostname;
60         sdb_time_t timestamp;
62         int status;
64         assert(n == 2);
65         assert((data[0].type == SDB_TYPE_STRING)
66                         && (data[1].type == SDB_TYPE_DATETIME));
68         hostname  = data[0].data.string;
69         timestamp = data[1].data.datetime;
71         status = sdb_store_host(hostname, timestamp);
73         if (status < 0) {
74                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
75                                 "store/update host '%s'.", hostname);
76                 return -1;
77         }
78         else if (status > 0) /* value too old */
79                 return 0;
81         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
82                         "host '%s' (last update timestamp = %"PRIscTIME").",
83                         hostname, timestamp);
84         return 0;
85 } /* sdb_livestatus_get_host */
87 static int
88 sdb_livestatus_get_svc(sdb_unixsock_client_t __attribute__((unused)) *client,
89                 size_t n, sdb_data_t *data,
90                 sdb_object_t __attribute__((unused)) *user_data)
91 {
92         const char *hostname = NULL;
93         const char *svcname = NULL;
94         sdb_time_t timestamp = 0;
96         int status;
98         assert(n == 3);
99         assert((data[0].type == SDB_TYPE_STRING)
100                         && (data[1].type == SDB_TYPE_STRING)
101                         && (data[2].type == SDB_TYPE_DATETIME));
103         hostname  = data[0].data.string;
104         svcname   = data[1].data.string;
105         timestamp = data[2].data.datetime;
107         status = sdb_store_service(hostname, svcname, timestamp);
109         if (status < 0) {
110                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
111                                 "store/update service '%s / %s'.", hostname, svcname);
112                 return -1;
113         }
114         else if (status > 0) /* value too old */
115                 return 0;
117         sdb_log(SDB_LOG_DEBUG, "MK Livestatus backend: Added/updated "
118                         "service '%s / %s' (last update timestamp = %"PRIscTIME").",
119                         hostname, svcname, timestamp);
120         return 0;
121 } /* sdb_livestatus_get_svc */
123 /*
124  * plugin API
125  */
127 static int
128 sdb_livestatus_init(sdb_object_t *user_data)
130         sdb_unixsock_client_t *client;
132         if (! user_data)
133                 return -1;
135         client = SDB_OBJ_WRAPPER(user_data)->data;
136         if (sdb_unixsock_client_connect(client)) {
137                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: "
138                                 "Failed to connect to livestatus @ %s.",
139                                 sdb_unixsock_client_path(client));
140                 return -1;
141         }
143         sdb_log(SDB_LOG_INFO, "MK Livestatus backend: Successfully "
144                         "connected to livestatus @ %s.",
145                         sdb_unixsock_client_path(client));
146         return 0;
147 } /* sdb_livestatus_init */
149 static int
150 sdb_livestatus_collect(sdb_object_t *user_data)
152         sdb_unixsock_client_t *client;
154         int status;
156         if (! user_data)
157                 return -1;
159         client = SDB_OBJ_WRAPPER(user_data)->data;
161         status = sdb_unixsock_client_send(client, "GET hosts\r\n"
162                         "Columns: name last_check");
163         if (status <= 0) {
164                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
165                                 "'GET hosts' command to livestatus @ %s.",
166                                 sdb_unixsock_client_path(client));
167                 return -1;
168         }
170         sdb_unixsock_client_shutdown(client, SHUT_WR);
172         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_host,
173                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
174                                 /* column count */ 2, SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
175                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
176                                 "response from livestatus @ %s while reading hosts.",
177                                 sdb_unixsock_client_path(client));
178                 return -1;
179         }
181         if ((! sdb_unixsock_client_eof(client))
182                         || sdb_unixsock_client_error(client)) {
183                 char errbuf[1024];
184                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
185                                 "host from livestatus @ %s: %s",
186                                 sdb_unixsock_client_path(client),
187                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
188                 return -1;
189         }
191         status = sdb_unixsock_client_send(client, "GET services\r\n"
192                         "Columns: host_name description last_check");
193         if (status <= 0) {
194                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to send "
195                                 "'GET services' command to livestatus @ %s.",
196                                 sdb_unixsock_client_path(client));
197                 return -1;
198         }
200         sdb_unixsock_client_shutdown(client, SHUT_WR);
202         if (sdb_unixsock_client_process_lines(client, sdb_livestatus_get_svc,
203                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
204                                 /* column count */ 3, SDB_TYPE_STRING, SDB_TYPE_STRING,
205                                 SDB_TYPE_DATETIME)) {
206                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
207                                 "response from livestatus @ %s while reading services.",
208                                 sdb_unixsock_client_path(client));
209                 return -1;
210         }
212         if ((! sdb_unixsock_client_eof(client))
213                         || sdb_unixsock_client_error(client)) {
214                 char errbuf[1024];
215                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to read "
216                                 "services from livestatus @ %s: %s",
217                                 sdb_unixsock_client_path(client),
218                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
219                 return -1;
220         }
221         return 0;
222 } /* sdb_livestatus_collect */
224 static int
225 sdb_livestatus_config_instance(oconfig_item_t *ci)
227         char *name = NULL;
228         char *socket_path = NULL;
230         char cb_name[1024];
232         sdb_object_t *user_data;
233         sdb_unixsock_client_t *client;
235         int i;
237         if (oconfig_get_string(ci, &name)) {
238                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance requires "
239                                 "a single string argument\n\tUsage: <Instance NAME>");
240                 return -1;
241         }
243         for (i = 0; i < ci->children_num; ++i) {
244                 oconfig_item_t *child = ci->children + i;
246                 if (! strcasecmp(child->key, "Socket"))
247                         oconfig_get_string(child, &socket_path);
248                 else
249                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
250                                         "unknown config option '%s' inside <Instance %s>.",
251                                         child->key, name);
252         }
254         if (! socket_path) {
255                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Instance '%s' "
256                                 "missing the 'Socket' option.", name);
257                 return -1;
258         }
260         snprintf(cb_name, sizeof(cb_name), "mk-livestatus::%s", name);
261         cb_name[sizeof(cb_name) - 1] = '\0';
263         client = sdb_unixsock_client_create(socket_path);
264         if (! client) {
265                 char errbuf[1024];
266                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to create "
267                                 "unixsock client: %s",
268                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
269                 return -1;
270         }
272         user_data = sdb_object_create_wrapper("unixsock-client", client,
273                         (void (*)(void *))sdb_unixsock_client_destroy);
274         if (! user_data) {
275                 sdb_unixsock_client_destroy(client);
276                 sdb_log(SDB_LOG_ERR, "MK Livestatus backend: Failed to "
277                                 "allocate sdb_object_t");
278                 return -1;
279         }
281         sdb_plugin_register_init(cb_name, sdb_livestatus_init, user_data);
282         sdb_plugin_register_collector(cb_name, sdb_livestatus_collect,
283                         /* interval */ NULL, user_data);
285         /* pass control to the list */
286         sdb_object_deref(user_data);
287         return 0;
288 } /* sdb_livestatus_config_instance */
290 static int
291 sdb_livestatus_config(oconfig_item_t *ci)
293         int i;
295         for (i = 0; i < ci->children_num; ++i) {
296                 oconfig_item_t *child = ci->children + i;
298                 if (! strcasecmp(child->key, "Instance"))
299                         sdb_livestatus_config_instance(child);
300                 else
301                         sdb_log(SDB_LOG_WARNING, "MK Livestatus backend: Ignoring "
302                                         "unknown config option '%s'.", child->key);
303         }
304         return 0;
305 } /* sdb_livestatus_config */
307 int
308 sdb_module_init(sdb_plugin_info_t *info)
310         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "MK-Livestatus");
311         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
312                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
313         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
314                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
315         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
316         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
317         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
319         sdb_plugin_register_config("mk-livestatus", sdb_livestatus_config);
320         return 0;
321 } /* sdb_version_extra */
323 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */