1 /*
2 * SysDB - src/backend/collectd/unixsock.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 data types
52 */
54 typedef struct {
55 char *current_host;
56 sdb_time_t current_timestamp;
57 int svc_updated;
58 int svc_failed;
59 } sdb_collectd_state_t;
60 #define SDB_COLLECTD_STATE_INIT { NULL, 0, 0, 0 }
62 /*
63 * private helper functions
64 */
66 static int
67 sdb_collectd_add_host(const char *hostname, sdb_time_t last_update)
68 {
69 int status;
71 status = sdb_store_host(hostname, last_update);
73 if (status < 0) {
74 sdb_log(SDB_LOG_ERR, "collectd::unixsock 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, "collectd::unixsock backend: Added/updated "
82 "host '%s' (last update timestamp = %"PRIscTIME").",
83 hostname, last_update);
84 return 0;
85 } /* sdb_collectd_add_host */
87 static int
88 sdb_collectd_add_svc(const char *hostname, const char *plugin,
89 const char *type, sdb_time_t last_update)
90 {
91 char name[strlen(plugin) + strlen(type) + 2];
92 int status;
94 snprintf(name, sizeof(name), "%s/%s", plugin, type);
96 status = sdb_store_service(hostname, name, last_update);
97 if (status < 0) {
98 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
99 "store/update service '%s/%s'.", hostname, name);
100 return -1;
101 }
102 return 0;
103 } /* sdb_collectd_add_svc */
105 static int
106 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
107 size_t n, sdb_data_t *data, sdb_object_t *user_data)
108 {
109 sdb_collectd_state_t *state;
111 const char *hostname;
112 const char *plugin;
113 const char *type;
114 sdb_time_t last_update;
116 assert(user_data);
118 assert(n == 4);
119 assert((data[0].type == SDB_TYPE_DATETIME)
120 && (data[1].type == SDB_TYPE_STRING)
121 && (data[2].type == SDB_TYPE_STRING)
122 && (data[3].type == SDB_TYPE_STRING));
124 last_update = data[0].data.datetime;
125 hostname = data[1].data.string;
126 plugin = data[2].data.string;
127 type = data[3].data.string;
129 state = SDB_OBJ_WRAPPER(user_data)->data;
131 if (! state->current_host) {
132 state->current_host = strdup(hostname);
133 state->current_timestamp = last_update;
134 }
136 if (! state->current_host) {
137 char errbuf[1024];
138 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
139 "string buffer: %s",
140 sdb_strerror(errno, errbuf, sizeof(errbuf)));
141 return -1;
142 }
144 if (! sdb_store_has_host(hostname))
145 sdb_collectd_add_host(hostname, last_update);
147 if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
148 ++state->svc_failed;
149 else
150 ++state->svc_updated;
152 if (! strcasecmp(state->current_host, hostname)) {
153 if (last_update > state->current_timestamp)
154 state->current_timestamp = last_update;
155 return 0;
156 }
158 /* new host */
159 sdb_collectd_add_host(hostname, last_update);
161 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
162 "%i service%s (%i failed) for host '%s'.",
163 state->svc_updated, state->svc_updated == 1 ? "" : "s",
164 state->svc_failed, state->current_host);
165 state->svc_updated = state->svc_failed = 0;
167 free(state->current_host);
168 state->current_host = strdup(hostname);
169 state->current_timestamp = last_update;
170 return 0;
171 } /* sdb_collectd_get_data */
173 /*
174 * plugin API
175 */
177 static int
178 sdb_collectd_init(sdb_object_t *user_data)
179 {
180 sdb_unixsock_client_t *client;
182 if (! user_data)
183 return -1;
185 client = SDB_OBJ_WRAPPER(user_data)->data;
186 if (sdb_unixsock_client_connect(client)) {
187 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
188 "Failed to connect to collectd.");
189 return -1;
190 }
192 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
193 "connected to collectd @ %s.",
194 sdb_unixsock_client_path(client));
195 return 0;
196 } /* sdb_collectd_init */
198 static int
199 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
200 {
201 return 0;
202 } /* sdb_collectd_shutdown */
204 static int
205 sdb_collectd_collect(sdb_object_t *user_data)
206 {
207 sdb_unixsock_client_t *client;
209 char buffer[1024];
210 char *line;
211 char *msg;
213 char *endptr = NULL;
214 long int count;
216 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
217 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
218 /* destructor = */ NULL);
220 if (! user_data)
221 return -1;
223 client = SDB_OBJ_WRAPPER(user_data)->data;
225 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
226 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
227 "LISTVAL command to collectd @ %s.",
228 sdb_unixsock_client_path(client));
229 return -1;
230 }
232 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
233 if (! line) {
234 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
235 "status of LISTVAL command from collectd @ %s.",
236 sdb_unixsock_client_path(client));
237 return -1;
238 }
240 msg = strchr(line, ' ');
241 if (msg) {
242 *msg = '\0';
243 ++msg;
244 }
246 errno = 0;
247 count = strtol(line, &endptr, /* base */ 0);
248 if (errno || (line == endptr)) {
249 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
250 "status of LISTVAL command from collectd @ %s.",
251 sdb_unixsock_client_path(client));
252 return -1;
253 }
255 if (count < 0) {
256 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
257 "value list from collectd @ %s: %s",
258 sdb_unixsock_client_path(client),
259 msg ? msg : line);
260 return -1;
261 }
263 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
264 SDB_OBJ(&state_obj), count, /* delim */ " /",
265 /* column count = */ 4,
266 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
267 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
268 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
269 "to read response from collectd @ %s.",
270 sdb_unixsock_client_path(client));
271 return -1;
272 }
274 if (state.current_host) {
275 sdb_collectd_add_host(state.current_host, state.current_timestamp);
276 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
277 "%i service%s (%i failed) for host '%s'.",
278 state.svc_updated, state.svc_updated == 1 ? "" : "s",
279 state.svc_failed, state.current_host);
280 }
281 return 0;
282 } /* sdb_collectd_collect */
284 static int
285 sdb_collectd_config_instance(oconfig_item_t *ci)
286 {
287 char *name = NULL;
288 char *socket_path = NULL;
290 char cb_name[1024];
292 sdb_object_t *user_data;
293 sdb_unixsock_client_t *client;
295 int i;
297 if (oconfig_get_string(ci, &name)) {
298 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
299 "a single string argument\n\tUsage: <Instance NAME>");
300 return -1;
301 }
303 for (i = 0; i < ci->children_num; ++i) {
304 oconfig_item_t *child = ci->children + i;
306 if (! strcasecmp(child->key, "Socket"))
307 oconfig_get_string(child, &socket_path);
308 else
309 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
310 "unknown config option '%s' inside <Instance %s>.",
311 child->key, name);
312 }
314 if (! socket_path) {
315 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
316 "missing the 'Socket' option.", name);
317 return -1;
318 }
320 snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
321 cb_name[sizeof(cb_name) - 1] = '\0';
323 client = sdb_unixsock_client_create(socket_path);
324 if (! client) {
325 char errbuf[1024];
326 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
327 "unixsock client: %s",
328 sdb_strerror(errno, errbuf, sizeof(errbuf)));
329 return -1;
330 }
332 user_data = sdb_object_create_wrapper("unixsock-client", client,
333 (void (*)(void *))sdb_unixsock_client_destroy);
334 if (! user_data) {
335 sdb_unixsock_client_destroy(client);
336 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
337 "sdb_object_t");
338 return -1;
339 }
341 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
342 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
344 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
345 /* interval */ NULL, user_data);
347 /* pass control to the list */
348 sdb_object_deref(user_data);
349 return 0;
350 } /* sdb_collectd_config_instance */
352 static int
353 sdb_collectd_config(oconfig_item_t *ci)
354 {
355 int i;
357 for (i = 0; i < ci->children_num; ++i) {
358 oconfig_item_t *child = ci->children + i;
360 if (! strcasecmp(child->key, "Instance"))
361 sdb_collectd_config_instance(child);
362 else
363 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
364 "unknown config option '%s'.", child->key);
365 }
366 return 0;
367 } /* sdb_collectd_config */
369 int
370 sdb_module_init(sdb_plugin_info_t *info)
371 {
372 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
373 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
374 "backend accessing the system statistics collection daemon "
375 "throught the UNIXSOCK interface");
376 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
377 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
378 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
379 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
380 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
382 sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
383 return 0;
384 } /* sdb_version_extra */
386 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */