Code

Added missing daemon/config.h.
[sysdb.git] / src / backend / mk-livestatus.c
1 /*
2  * syscollector - 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 "syscollector.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 SC_PLUGIN_MAGIC;
46 /*
47  * private helper functions
48  */
50 static int
51 sc_livestatus_get_host(sc_unixsock_client_t __attribute__((unused)) *client,
52                 size_t n, sc_data_t *data, sc_object_t __attribute__((unused)) *user_data)
53 {
54         char *hostname = NULL;
55         sc_time_t timestamp = 0;
57         sc_host_t host = SC_HOST_INIT;
59         int status;
61         assert(n == 2);
62         assert((data[0].type == SC_TYPE_STRING)
63                         && (data[1].type == SC_TYPE_DATETIME));
65         hostname  = strdup(data[0].data.string);
66         timestamp = data[1].data.datetime;
68         host.host_name = hostname;
69         host.host_last_update = timestamp;
71         status = sc_store_host(&host);
73         if (status < 0) {
74                 fprintf(stderr, "MK Livestatus backend: Failed to store/update "
75                                 "host '%s'.\n", hostname);
76                 free(hostname);
77                 return -1;
78         }
79         else if (status > 0) /* value too old */
80                 return 0;
82         fprintf(stderr, "MK Livestatus backend: Added/updated host '%s' "
83                         "(last update timestamp = %"PRIscTIME").\n",
84                         hostname, timestamp);
85         free(hostname);
86         return 0;
87 } /* sc_livestatus_get_host */
89 static int
90 sc_livestatus_get_svc(sc_unixsock_client_t __attribute__((unused)) *client,
91                 size_t n, sc_data_t *data, sc_object_t __attribute__((unused)) *user_data)
92 {
93         char *hostname = NULL;
94         char *svcname = NULL;
95         sc_time_t timestamp = 0;
97         sc_service_t svc = SC_SVC_INIT;
99         int status;
101         assert(n == 3);
102         assert((data[0].type == SC_TYPE_STRING)
103                         && (data[1].type == SC_TYPE_STRING)
104                         && (data[2].type == SC_TYPE_DATETIME));
106         hostname  = strdup(data[0].data.string);
107         svcname   = strdup(data[1].data.string);
108         timestamp = data[2].data.datetime;
110         svc.hostname = hostname;
111         svc.svc_name = svcname;
112         svc.svc_last_update = timestamp;
114         status = sc_store_service(&svc);
116         if (status < 0) {
117                 fprintf(stderr, "MK Livestatus backend: Failed to store/update "
118                                 "service '%s / %s'.\n", hostname, svcname);
119                 free(hostname);
120                 free(svcname);
121                 return -1;
122         }
123         else if (status > 0) /* value too old */
124                 return 0;
126         fprintf(stderr, "MK Livestatus backend: Added/updated service '%s / %s' "
127                         "(last update timestamp = %"PRIscTIME").\n",
128                         hostname, svcname, timestamp);
129         free(hostname);
130         free(svcname);
131         return 0;
132 } /* sc_livestatus_get_svc */
134 /*
135  * plugin API
136  */
138 static int
139 sc_livestatus_init(sc_object_t *user_data)
141         sc_unixsock_client_t *client;
143         if (! user_data)
144                 return -1;
146         client = SC_OBJ_WRAPPER(user_data)->data;
147         if (sc_unixsock_client_connect(client)) {
148                 fprintf(stderr, "MK Livestatus backend: "
149                                 "Failed to connect to livestatus @ %s.\n",
150                                 sc_unixsock_client_path(client));
151                 return -1;
152         }
154         fprintf(stderr, "MK Livestatus backend: Successfully "
155                         "connected to livestatus @ %s.\n",
156                         sc_unixsock_client_path(client));
157         return 0;
158 } /* sc_livestatus_init */
160 static int
161 sc_livestatus_collect(sc_object_t *user_data)
163         sc_unixsock_client_t *client;
165         int status;
167         if (! user_data)
168                 return -1;
170         client = SC_OBJ_WRAPPER(user_data)->data;
172         status = sc_unixsock_client_send(client, "GET hosts\r\n"
173                         "Columns: name last_check");
174         if (status <= 0) {
175                 fprintf(stderr, "MK Livestatus backend: Failed to send "
176                                 "'GET hosts' command to livestatus @ %s.\n",
177                                 sc_unixsock_client_path(client));
178                 return -1;
179         }
181         sc_unixsock_client_shutdown(client, SHUT_WR);
183         if (sc_unixsock_client_process_lines(client, sc_livestatus_get_host,
184                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
185                                 /* column count */ 2, SC_TYPE_STRING, SC_TYPE_DATETIME)) {
186                 fprintf(stderr, "MK Livestatus backend: Failed to read response "
187                                 "from livestatus @ %s while reading hosts.\n",
188                                 sc_unixsock_client_path(client));
189                 return -1;
190         }
192         if ((! sc_unixsock_client_eof(client))
193                         || sc_unixsock_client_error(client)) {
194                 char errbuf[1024];
195                 fprintf(stderr, "MK Livestatus backend: Failed to read host "
196                                 "from livestatus @ %s: %s\n",
197                                 sc_unixsock_client_path(client),
198                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
199                 return -1;
200         }
202         status = sc_unixsock_client_send(client, "GET services\r\n"
203                         "Columns: host_name description last_check");
204         if (status <= 0) {
205                 fprintf(stderr, "MK Livestatus backend: Failed to send "
206                                 "'GET services' command to livestatus @ %s.\n",
207                                 sc_unixsock_client_path(client));
208                 return -1;
209         }
211         sc_unixsock_client_shutdown(client, SHUT_WR);
213         if (sc_unixsock_client_process_lines(client, sc_livestatus_get_svc,
214                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
215                                 /* column count */ 3, SC_TYPE_STRING, SC_TYPE_STRING,
216                                 SC_TYPE_DATETIME)) {
217                 fprintf(stderr, "MK Livestatus backend: Failed to read response "
218                                 "from livestatus @ %s while reading services.\n",
219                                 sc_unixsock_client_path(client));
220                 return -1;
221         }
223         if ((! sc_unixsock_client_eof(client))
224                         || sc_unixsock_client_error(client)) {
225                 char errbuf[1024];
226                 fprintf(stderr, "MK Livestatus backend: Failed to read services "
227                                 "from livestatus @ %s: %s\n",
228                                 sc_unixsock_client_path(client),
229                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
230                 return -1;
231         }
232         return 0;
233 } /* sc_livestatus_collect */
235 static int
236 sc_livestatus_config_instance(oconfig_item_t *ci)
238         char *name = NULL;
239         char *socket = NULL;
241         char cb_name[1024];
243         sc_object_t *user_data;
244         sc_unixsock_client_t *client;
246         int i;
248         if (oconfig_get_string(ci, &name)) {
249                 fprintf(stderr, "MK Livestatus backend: Instance requires a single "
250                                 "string argument\n\tUsage: <Instance NAME>\n");
251                 return -1;
252         }
254         for (i = 0; i < ci->children_num; ++i) {
255                 oconfig_item_t *child = ci->children + i;
257                 if (! strcasecmp(child->key, "Socket"))
258                         oconfig_get_string(child, &socket);
259                 else
260                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
261                                         "option '%s' inside <Instance %s>.\n",
262                                         child->key, name);
263         }
265         if (! socket) {
266                 fprintf(stderr, "MK Livestatus backend: Instance '%s' missing "
267                                 "the 'Socket' option.\n", name);
268                 return -1;
269         }
271         snprintf(cb_name, sizeof(cb_name), "mk-livestatus-%s", name);
272         cb_name[sizeof(cb_name) - 1] = '\0';
274         client = sc_unixsock_client_create(socket);
275         if (! client) {
276                 char errbuf[1024];
277                 fprintf(stderr, "MK Livestatus backend: Failed to create unixsock "
278                                 "client: %s\n", sc_strerror(errno, errbuf, sizeof(errbuf)));
279                 return -1;
280         }
282         user_data = sc_object_create_wrapper(client,
283                         (void (*)(void *))sc_unixsock_client_destroy);
284         if (! user_data) {
285                 sc_unixsock_client_destroy(client);
286                 fprintf(stderr, "MK Livestatus backend: Failed to "
287                                 "allocate sc_object_t\n");
288                 return -1;
289         }
291         sc_plugin_register_init(cb_name, sc_livestatus_init, user_data);
292         sc_plugin_register_collector(cb_name, sc_livestatus_collect,
293                         /* interval */ NULL, user_data);
295         /* pass control to the list */
296         sc_object_deref(user_data);
297         return 0;
298 } /* sc_livestatus_config_instance */
300 static int
301 sc_livestatus_config(oconfig_item_t *ci)
303         int i;
305         for (i = 0; i < ci->children_num; ++i) {
306                 oconfig_item_t *child = ci->children + i;
308                 if (! strcasecmp(child->key, "Instance"))
309                         sc_livestatus_config_instance(child);
310                 else
311                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
312                                         "option '%s'.\n", child->key);
313         }
314         return 0;
315 } /* sc_livestatus_config */
317 int
318 sc_module_init(sc_plugin_info_t *info)
320         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "MK-Livestatus");
321         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
322                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
323         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
324                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
325         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
326         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
327         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
329         sc_plugin_register_config("mk-livestatus", sc_livestatus_config);
330         return 0;
331 } /* sc_version_extra */
333 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */