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 /* store the specified host-name (once per iteration) */
67 static int
68 sdb_collectd_store_host(sdb_collectd_state_t *state,
69 const char *hostname, sdb_time_t last_update)
70 {
71 int status;
73 if (last_update > state->current_timestamp)
74 state->current_timestamp = last_update;
76 if (state->current_host && (! strcasecmp(state->current_host, hostname)))
77 return 0;
78 /* else: first/new host */
80 if (state->current_host) {
81 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
82 "%i service%s (%i failed) for host '%s'.",
83 state->svc_updated, state->svc_updated == 1 ? "" : "s",
84 state->svc_failed, state->current_host);
85 state->svc_updated = state->svc_failed = 0;
86 free(state->current_host);
87 }
89 state->current_host = strdup(hostname);
90 if (! state->current_host) {
91 char errbuf[1024];
92 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
93 "string buffer: %s",
94 sdb_strerror(errno, errbuf, sizeof(errbuf)));
95 return -1;
96 }
98 status = sdb_store_host(hostname, last_update);
100 if (status < 0) {
101 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
102 "store/update host '%s'.", hostname);
103 return -1;
104 }
105 else if (status > 0) /* value too old */
106 return 0;
108 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
109 "host '%s' (last update timestamp = %"PRIscTIME").",
110 hostname, last_update);
111 return 0;
112 } /* sdb_collectd_store_host */
114 static int
115 sdb_collectd_add_svc(const char *hostname, const char *plugin,
116 const char *type, sdb_time_t last_update)
117 {
118 char name[strlen(plugin) + strlen(type) + 2];
119 int status;
121 snprintf(name, sizeof(name), "%s/%s", plugin, type);
123 status = sdb_store_service(hostname, name, last_update);
124 if (status < 0) {
125 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to "
126 "store/update service '%s/%s'.", hostname, name);
127 return -1;
128 }
129 return 0;
130 } /* sdb_collectd_add_svc */
132 static int
133 sdb_collectd_get_data(sdb_unixsock_client_t __attribute__((unused)) *client,
134 size_t n, sdb_data_t *data, sdb_object_t *user_data)
135 {
136 sdb_collectd_state_t *state;
138 char *hostname;
139 const char *plugin;
140 const char *type;
141 sdb_data_t last_update;
143 assert(user_data);
145 /* 0: <last_update> <hostname>
146 * 1: <plugin>
147 * 2: <type> */
148 assert(n == 3);
149 assert((data[0].type == SDB_TYPE_STRING)
150 && (data[1].type == SDB_TYPE_STRING)
151 && (data[2].type == SDB_TYPE_STRING));
153 hostname = data[0].data.string;
154 plugin = data[1].data.string;
155 type = data[2].data.string;
157 hostname = strchr(hostname, ' ');
158 if (! hostname) {
159 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Expected to find "
160 "a space character in the LISTVAL response");
161 return -1;
162 }
163 *hostname = '\0';
164 ++hostname;
166 if (sdb_data_parse(data[0].data.string, SDB_TYPE_DATETIME, &last_update)) {
167 char errbuf[1024];
168 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
169 "timestamp '%s' returned by LISTVAL: %s", data[0].data.string,
170 sdb_strerror(errno, errbuf, sizeof(errbuf)));
171 return -1;
172 }
174 state = SDB_OBJ_WRAPPER(user_data)->data;
175 if (sdb_collectd_store_host(state, hostname, last_update.data.datetime))
176 return -1;
178 if (sdb_collectd_add_svc(hostname, plugin, type,
179 last_update.data.datetime))
180 ++state->svc_failed;
181 else
182 ++state->svc_updated;
183 return 0;
184 } /* sdb_collectd_get_data */
186 /*
187 * plugin API
188 */
190 static int
191 sdb_collectd_init(sdb_object_t *user_data)
192 {
193 sdb_unixsock_client_t *client;
195 if (! user_data)
196 return -1;
198 client = SDB_OBJ_WRAPPER(user_data)->data;
199 if (sdb_unixsock_client_connect(client)) {
200 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
201 "Failed to connect to collectd.");
202 return -1;
203 }
205 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
206 "connected to collectd @ %s.",
207 sdb_unixsock_client_path(client));
208 return 0;
209 } /* sdb_collectd_init */
211 static int
212 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
213 {
214 if (! user_data)
215 return -1;
217 sdb_unixsock_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
218 SDB_OBJ_WRAPPER(user_data)->data = NULL;
219 return 0;
220 } /* sdb_collectd_shutdown */
222 static int
223 sdb_collectd_collect(sdb_object_t *user_data)
224 {
225 sdb_unixsock_client_t *client;
227 char buffer[1024];
228 char *line;
229 char *msg;
231 char *endptr = NULL;
232 long int count;
234 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
235 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
236 /* destructor = */ NULL);
238 if (! user_data)
239 return -1;
241 client = SDB_OBJ_WRAPPER(user_data)->data;
243 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
244 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
245 "LISTVAL command to collectd @ %s.",
246 sdb_unixsock_client_path(client));
247 return -1;
248 }
250 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
251 if (! line) {
252 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
253 "status of LISTVAL command from collectd @ %s.",
254 sdb_unixsock_client_path(client));
255 return -1;
256 }
258 msg = strchr(line, ' ');
259 if (msg) {
260 *msg = '\0';
261 ++msg;
262 }
264 errno = 0;
265 count = strtol(line, &endptr, /* base */ 0);
266 if (errno || (line == endptr)) {
267 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
268 "status of LISTVAL command from collectd @ %s.",
269 sdb_unixsock_client_path(client));
270 return -1;
271 }
273 if (count < 0) {
274 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
275 "value list from collectd @ %s: %s",
276 sdb_unixsock_client_path(client),
277 msg ? msg : line);
278 return -1;
279 }
281 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
282 SDB_OBJ(&state_obj), count, /* delim */ "/",
283 /* column count = */ 3,
284 SDB_TYPE_STRING, SDB_TYPE_STRING, SDB_TYPE_STRING)) {
285 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
286 "to read response from collectd @ %s.",
287 sdb_unixsock_client_path(client));
288 return -1;
289 }
291 if (state.current_host) {
292 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
293 "%i service%s (%i failed) for host '%s'.",
294 state.svc_updated, state.svc_updated == 1 ? "" : "s",
295 state.svc_failed, state.current_host);
296 free(state.current_host);
297 }
298 return 0;
299 } /* sdb_collectd_collect */
301 static int
302 sdb_collectd_config_instance(oconfig_item_t *ci)
303 {
304 char *name = NULL;
305 char *socket_path = NULL;
307 sdb_object_t *user_data;
308 sdb_unixsock_client_t *client;
310 int i;
312 if (oconfig_get_string(ci, &name)) {
313 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
314 "a single string argument\n\tUsage: <Instance NAME>");
315 return -1;
316 }
318 for (i = 0; i < ci->children_num; ++i) {
319 oconfig_item_t *child = ci->children + i;
321 if (! strcasecmp(child->key, "Socket"))
322 oconfig_get_string(child, &socket_path);
323 else
324 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
325 "unknown config option '%s' inside <Instance %s>.",
326 child->key, name);
327 }
329 if (! socket_path) {
330 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
331 "missing the 'Socket' option.", name);
332 return -1;
333 }
335 client = sdb_unixsock_client_create(socket_path);
336 if (! client) {
337 char errbuf[1024];
338 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
339 "unixsock client: %s",
340 sdb_strerror(errno, errbuf, sizeof(errbuf)));
341 return -1;
342 }
344 user_data = sdb_object_create_wrapper("unixsock-client", client,
345 (void (*)(void *))sdb_unixsock_client_destroy);
346 if (! user_data) {
347 sdb_unixsock_client_destroy(client);
348 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
349 "sdb_object_t");
350 return -1;
351 }
353 sdb_plugin_register_init(name, sdb_collectd_init, user_data);
354 sdb_plugin_register_shutdown(name, sdb_collectd_shutdown, user_data);
356 sdb_plugin_register_collector(name, sdb_collectd_collect,
357 /* interval */ NULL, user_data);
359 /* pass control to the list */
360 sdb_object_deref(user_data);
361 return 0;
362 } /* sdb_collectd_config_instance */
364 static int
365 sdb_collectd_config(oconfig_item_t *ci)
366 {
367 int i;
369 if (! ci) /* nothing to do to deconfigure this plugin */
370 return 0;
372 for (i = 0; i < ci->children_num; ++i) {
373 oconfig_item_t *child = ci->children + i;
375 if (! strcasecmp(child->key, "Instance"))
376 sdb_collectd_config_instance(child);
377 else
378 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
379 "unknown config option '%s'.", child->key);
380 }
381 return 0;
382 } /* sdb_collectd_config */
384 int
385 sdb_module_init(sdb_plugin_info_t *info)
386 {
387 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
388 "backend accessing the system statistics collection daemon "
389 "throught the UNIXSOCK interface");
390 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
391 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
392 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
393 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
394 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
396 sdb_plugin_register_config(sdb_collectd_config);
397 return 0;
398 } /* sdb_version_extra */
400 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */