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 const char *hostname;
139 const char *plugin;
140 const char *type;
141 sdb_time_t last_update;
143 assert(user_data);
145 assert(n == 4);
146 assert((data[0].type == SDB_TYPE_DATETIME)
147 && (data[1].type == SDB_TYPE_STRING)
148 && (data[2].type == SDB_TYPE_STRING)
149 && (data[3].type == SDB_TYPE_STRING));
151 last_update = data[0].data.datetime;
152 hostname = data[1].data.string;
153 plugin = data[2].data.string;
154 type = data[3].data.string;
156 state = SDB_OBJ_WRAPPER(user_data)->data;
157 if (sdb_collectd_store_host(state, hostname, last_update))
158 return -1;
160 if (sdb_collectd_add_svc(hostname, plugin, type, last_update))
161 ++state->svc_failed;
162 else
163 ++state->svc_updated;
164 return 0;
165 } /* sdb_collectd_get_data */
167 /*
168 * plugin API
169 */
171 static int
172 sdb_collectd_init(sdb_object_t *user_data)
173 {
174 sdb_unixsock_client_t *client;
176 if (! user_data)
177 return -1;
179 client = SDB_OBJ_WRAPPER(user_data)->data;
180 if (sdb_unixsock_client_connect(client)) {
181 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: "
182 "Failed to connect to collectd.");
183 return -1;
184 }
186 sdb_log(SDB_LOG_INFO, "collectd::unixsock backend: Successfully "
187 "connected to collectd @ %s.",
188 sdb_unixsock_client_path(client));
189 return 0;
190 } /* sdb_collectd_init */
192 static int
193 sdb_collectd_shutdown(__attribute__((unused)) sdb_object_t *user_data)
194 {
195 return 0;
196 } /* sdb_collectd_shutdown */
198 static int
199 sdb_collectd_collect(sdb_object_t *user_data)
200 {
201 sdb_unixsock_client_t *client;
203 char buffer[1024];
204 char *line;
205 char *msg;
207 char *endptr = NULL;
208 long int count;
210 sdb_collectd_state_t state = SDB_COLLECTD_STATE_INIT;
211 sdb_object_wrapper_t state_obj = SDB_OBJECT_WRAPPER_STATIC(&state,
212 /* destructor = */ NULL);
214 if (! user_data)
215 return -1;
217 client = SDB_OBJ_WRAPPER(user_data)->data;
219 if (sdb_unixsock_client_send(client, "LISTVAL") <= 0) {
220 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to send "
221 "LISTVAL command to collectd @ %s.",
222 sdb_unixsock_client_path(client));
223 return -1;
224 }
226 line = sdb_unixsock_client_recv(client, buffer, sizeof(buffer));
227 if (! line) {
228 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to read "
229 "status of LISTVAL command from collectd @ %s.",
230 sdb_unixsock_client_path(client));
231 return -1;
232 }
234 msg = strchr(line, ' ');
235 if (msg) {
236 *msg = '\0';
237 ++msg;
238 }
240 errno = 0;
241 count = strtol(line, &endptr, /* base */ 0);
242 if (errno || (line == endptr)) {
243 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to parse "
244 "status of LISTVAL command from collectd @ %s.",
245 sdb_unixsock_client_path(client));
246 return -1;
247 }
249 if (count < 0) {
250 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to get "
251 "value list from collectd @ %s: %s",
252 sdb_unixsock_client_path(client),
253 msg ? msg : line);
254 return -1;
255 }
257 if (sdb_unixsock_client_process_lines(client, sdb_collectd_get_data,
258 SDB_OBJ(&state_obj), count, /* delim */ " /",
259 /* column count = */ 4,
260 SDB_TYPE_DATETIME, SDB_TYPE_STRING,
261 SDB_TYPE_STRING, SDB_TYPE_STRING)) {
262 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed "
263 "to read response from collectd @ %s.",
264 sdb_unixsock_client_path(client));
265 return -1;
266 }
268 if (state.current_host) {
269 sdb_log(SDB_LOG_DEBUG, "collectd::unixsock backend: Added/updated "
270 "%i service%s (%i failed) for host '%s'.",
271 state.svc_updated, state.svc_updated == 1 ? "" : "s",
272 state.svc_failed, state.current_host);
273 }
274 return 0;
275 } /* sdb_collectd_collect */
277 static int
278 sdb_collectd_config_instance(oconfig_item_t *ci)
279 {
280 char *name = NULL;
281 char *socket_path = NULL;
283 char cb_name[1024];
285 sdb_object_t *user_data;
286 sdb_unixsock_client_t *client;
288 int i;
290 if (oconfig_get_string(ci, &name)) {
291 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance requires "
292 "a single string argument\n\tUsage: <Instance NAME>");
293 return -1;
294 }
296 for (i = 0; i < ci->children_num; ++i) {
297 oconfig_item_t *child = ci->children + i;
299 if (! strcasecmp(child->key, "Socket"))
300 oconfig_get_string(child, &socket_path);
301 else
302 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
303 "unknown config option '%s' inside <Instance %s>.",
304 child->key, name);
305 }
307 if (! socket_path) {
308 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Instance '%s' "
309 "missing the 'Socket' option.", name);
310 return -1;
311 }
313 snprintf(cb_name, sizeof(cb_name), "collectd::unixsock::%s", name);
314 cb_name[sizeof(cb_name) - 1] = '\0';
316 client = sdb_unixsock_client_create(socket_path);
317 if (! client) {
318 char errbuf[1024];
319 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to create "
320 "unixsock client: %s",
321 sdb_strerror(errno, errbuf, sizeof(errbuf)));
322 return -1;
323 }
325 user_data = sdb_object_create_wrapper("unixsock-client", client,
326 (void (*)(void *))sdb_unixsock_client_destroy);
327 if (! user_data) {
328 sdb_unixsock_client_destroy(client);
329 sdb_log(SDB_LOG_ERR, "collectd::unixsock backend: Failed to allocate "
330 "sdb_object_t");
331 return -1;
332 }
334 sdb_plugin_register_init(cb_name, sdb_collectd_init, user_data);
335 sdb_plugin_register_shutdown(cb_name, sdb_collectd_shutdown, user_data);
337 sdb_plugin_register_collector(cb_name, sdb_collectd_collect,
338 /* interval */ NULL, user_data);
340 /* pass control to the list */
341 sdb_object_deref(user_data);
342 return 0;
343 } /* sdb_collectd_config_instance */
345 static int
346 sdb_collectd_config(oconfig_item_t *ci)
347 {
348 int i;
350 if (! ci) /* nothing to do to deconfigure this plugin */
351 return 0;
353 for (i = 0; i < ci->children_num; ++i) {
354 oconfig_item_t *child = ci->children + i;
356 if (! strcasecmp(child->key, "Instance"))
357 sdb_collectd_config_instance(child);
358 else
359 sdb_log(SDB_LOG_WARNING, "collectd::unixsock backend: Ignoring "
360 "unknown config option '%s'.", child->key);
361 }
362 return 0;
363 } /* sdb_collectd_config */
365 int
366 sdb_module_init(sdb_plugin_info_t *info)
367 {
368 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "collectd::unixsock");
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
370 "backend accessing the system statistics collection daemon "
371 "throught the UNIXSOCK interface");
372 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
373 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
374 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
375 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
376 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
378 sdb_plugin_register_config("collectd::unixsock", sdb_collectd_config);
379 return 0;
380 } /* sdb_version_extra */
382 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */