Code

MK Livestatus backend: Converted to use sc_unixsock_client_process_lines().
[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_data(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_data */
89 /*
90  * plugin API
91  */
93 static int
94 sc_livestatus_init(sc_object_t *user_data)
95 {
96         sc_unixsock_client_t *client;
98         if (! user_data)
99                 return -1;
101         client = SC_OBJ_WRAPPER(user_data)->data;
102         if (sc_unixsock_client_connect(client)) {
103                 fprintf(stderr, "MK Livestatus backend: "
104                                 "Failed to connect to livestatus @ %s.\n",
105                                 sc_unixsock_client_path(client));
106                 return -1;
107         }
109         fprintf(stderr, "MK Livestatus backend: Successfully "
110                         "connected to livestatus @ %s.\n",
111                         sc_unixsock_client_path(client));
112         return 0;
113 } /* sc_livestatus_init */
115 static int
116 sc_livestatus_collect(sc_object_t *user_data)
118         sc_unixsock_client_t *client;
120         int status;
122         if (! user_data)
123                 return -1;
125         client = SC_OBJ_WRAPPER(user_data)->data;
127         status = sc_unixsock_client_send(client, "GET hosts\r\n"
128                         "Columns: name last_check");
129         if (status <= 0) {
130                 fprintf(stderr, "MK Livestatus backend: Failed to send "
131                                 "'GET hosts' command to livestatus @ %s.\n",
132                                 sc_unixsock_client_path(client));
133                 return -1;
134         }
136         sc_unixsock_client_shutdown(client, SHUT_WR);
138         if (sc_unixsock_client_process_lines(client, sc_livestatus_get_data,
139                                 /* user data */ NULL, /* -> EOF */ -1, /* delim */ ";",
140                                 /* column count */ 2, SC_TYPE_STRING, SC_TYPE_DATETIME)) {
141                 fprintf(stderr, "MK Livestatus backend: Failed to read response "
142                                 "from livestatus @ %s.\n", sc_unixsock_client_path(client));
143                 return -1;
144         }
146         if ((! sc_unixsock_client_eof(client))
147                         || sc_unixsock_client_error(client)) {
148                 char errbuf[1024];
149                 fprintf(stderr, "MK Livestatus backend: Failed to read host "
150                                 "from livestatus @ %s: %s\n",
151                                 sc_unixsock_client_path(client),
152                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
153                 return -1;
154         }
155         return 0;
156 } /* sc_livestatus_collect */
158 static int
159 sc_livestatus_config_instance(oconfig_item_t *ci)
161         char *name = NULL;
162         char *socket = NULL;
164         char cb_name[1024];
166         sc_object_t *user_data;
167         sc_unixsock_client_t *client;
169         int i;
171         if (oconfig_get_string(ci, &name)) {
172                 fprintf(stderr, "MK Livestatus backend: Instance requires a single "
173                                 "string argument\n\tUsage: <Instance NAME>\n");
174                 return -1;
175         }
177         for (i = 0; i < ci->children_num; ++i) {
178                 oconfig_item_t *child = ci->children + i;
180                 if (! strcasecmp(child->key, "Socket"))
181                         oconfig_get_string(child, &socket);
182                 else
183                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
184                                         "option '%s' inside <Instance %s>.\n",
185                                         child->key, name);
186         }
188         if (! socket) {
189                 fprintf(stderr, "MK Livestatus backend: Instance '%s' missing "
190                                 "the 'Socket' option.\n", name);
191                 return -1;
192         }
194         snprintf(cb_name, sizeof(cb_name), "mk-livestatus-%s", name);
195         cb_name[sizeof(cb_name) - 1] = '\0';
197         client = sc_unixsock_client_create(socket);
198         if (! client) {
199                 char errbuf[1024];
200                 fprintf(stderr, "MK Livestatus backend: Failed to create unixsock "
201                                 "client: %s\n", sc_strerror(errno, errbuf, sizeof(errbuf)));
202                 return -1;
203         }
205         user_data = sc_object_create_wrapper(client,
206                         (void (*)(void *))sc_unixsock_client_destroy);
207         if (! user_data) {
208                 sc_unixsock_client_destroy(client);
209                 fprintf(stderr, "MK Livestatus backend: Failed to "
210                                 "allocate sc_object_t\n");
211                 return -1;
212         }
214         sc_plugin_register_init(cb_name, sc_livestatus_init, user_data);
215         sc_plugin_register_collector(cb_name, sc_livestatus_collect,
216                         /* interval */ NULL, user_data);
218         /* pass control to the list */
219         sc_object_deref(user_data);
220         return 0;
221 } /* sc_livestatus_config_instance */
223 static int
224 sc_livestatus_config(oconfig_item_t *ci)
226         int i;
228         for (i = 0; i < ci->children_num; ++i) {
229                 oconfig_item_t *child = ci->children + i;
231                 if (! strcasecmp(child->key, "Instance"))
232                         sc_livestatus_config_instance(child);
233                 else
234                         fprintf(stderr, "MK Livestatus backend: Ignoring unknown config "
235                                         "option '%s'.\n", child->key);
236         }
237         return 0;
238 } /* sc_livestatus_config */
240 int
241 sc_module_init(sc_plugin_info_t *info)
243         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "MK-Livestatus");
244         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
245                         "backend accessing Nagios/Icinga/Shinken using MK Livestatus");
246         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
247                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
248         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
249         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
250         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
252         sc_plugin_register_config("mk-livestatus", sc_livestatus_config);
253         return 0;
254 } /* sc_version_extra */
256 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */