1 /*
2 * syscollector - src/backend/puppet-storeconfigs.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/dbi.h"
32 #include "utils/string.h"
34 #include "liboconfig/utils.h"
36 #include <dbi/dbi.h>
38 #include <assert.h>
39 #include <errno.h>
41 #include <string.h>
42 #include <strings.h>
44 SC_PLUGIN_MAGIC;
46 /*
47 * private helper functions
48 */
50 static int
51 sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
52 size_t n, sc_dbi_data_t *data)
53 {
54 sc_host_t host = SC_HOST_INIT;
56 int status;
58 assert(n == 2);
59 assert((data[0].type == DBI_TYPE_STRING)
60 && (data[1].type == DBI_TYPE_DATETIME));
62 host.host_name = strdup(data[0].data.string);
63 host.host_last_update = data[1].data.datetime;
65 status = sc_store_host(&host);
67 if (status < 0) {
68 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
69 "host '%s'.\n", host.host_name);
70 free(host.host_name);
71 return -1;
72 }
73 else if (! status)
74 fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
75 "(last update timestamp = %"PRIscTIME").\n",
76 host.host_name, host.host_last_update);
77 free(host.host_name);
78 return 0;
79 } /* sc_puppet_stcfg_get_data */
81 /*
82 * plugin API
83 */
85 static int
86 sc_puppet_stcfg_init(sc_object_t *user_data)
87 {
88 sc_dbi_client_t *client;
90 if (! user_data)
91 return -1;
93 client = SC_OBJ_WRAPPER(user_data)->data;
94 if (sc_dbi_client_connect(client)) {
95 fprintf(stderr, "puppet storeconfigs backend: "
96 "Failed to connect to the storeconfigs DB.\n");
97 return -1;
98 }
100 fprintf(stderr, "puppet storeconfigs backend: Successfully "
101 "connected to the storeconfigs DB.\n");
102 return 0;
103 } /* sc_collectd_init */
105 static int
106 sc_puppet_stcfg_collect(sc_object_t *user_data)
107 {
108 sc_dbi_client_t *client;
110 if (! user_data)
111 return -1;
113 client = SC_OBJ_WRAPPER(user_data)->data;
114 if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
115 sc_puppet_stcfg_get_data, /* #columns = */ 2,
116 /* col types = */ DBI_TYPE_STRING, DBI_TYPE_DATETIME)) {
117 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
118 "hosts from the storeconfigs DB.\n");
119 return -1;
120 }
121 return 0;
122 } /* sc_collectd_collect */
124 static int
125 sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
126 {
127 char *name = NULL;
128 char cb_name[1024];
130 sc_object_t *user_data;
131 sc_dbi_client_t *client;
132 sc_dbi_options_t *options = NULL;
134 char *driver = NULL;
135 char *database = NULL;
137 int i;
139 if (oconfig_get_string(ci, &name)) {
140 fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
141 "single string argument\n\tUsage: <Connection NAME>\n");
142 return -1;
143 }
145 for (i = 0; i < ci->children_num; ++i) {
146 oconfig_item_t *child = ci->children + i;
147 char *key = NULL, *value = NULL;
149 int status = 0;
151 if (! strcasecmp(child->key, "DBAdapter")) {
152 if (oconfig_get_string(child, &driver)) {
153 fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
154 "requires a single string argument inside "
155 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
156 name);
157 }
158 continue;
159 }
160 else if (! strcasecmp(child->key, "DBName")) {
161 if (oconfig_get_string(child, &database)) {
162 fprintf(stderr, "puppet storeconfigs backend: DBName"
163 "requires a single string argument inside "
164 "<Connection %s>\n\tUsage: DBName NAME\n",
165 name);
166 }
167 continue;
168 }
169 else if (! strcasecmp(child->key, "DBServer")) {
170 status = oconfig_get_string(child, &value);
171 key = "host";
172 }
173 else if (! strcasecmp(child->key, "DBPort")) {
174 status = oconfig_get_string(child, &value);
175 key = "port";
176 }
177 else if (! strcasecmp(child->key, "DBUser")) {
178 status = oconfig_get_string(child, &value);
179 key = "username";
180 }
181 else if (! strcasecmp(child->key, "DBPassword")) {
182 status = oconfig_get_string(child, &value);
183 key = "password";
184 }
185 else if (! strcasecmp(child->key, "DBIOption")) {
186 if ((child->values_num != 2)
187 || (child->values[0].type != OCONFIG_TYPE_STRING)
188 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
189 fprintf(stderr, "puppet storeconfigs backend: DBIOption "
190 "requires exactly two string arguments inside "
191 "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
192 name);
193 continue;
194 }
196 status = 0;
197 key = child->values[0].value.string;
198 value = child->values[1].value.string;
199 }
200 else {
201 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
202 "config option '%s' inside <Connection %s>.\n",
203 child->key, name);
204 continue;
205 }
207 if (status) {
208 fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
209 "requires a single string argument inside "
210 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
211 child->key, name);
212 continue;
213 }
215 assert(key && value);
217 if (! options) {
218 if (! (options = sc_dbi_options_create())) {
219 char errmsg[1024];
220 fprintf(stderr, "puppet storeconfigs backend: Failed to "
221 "create DBI options object: %s\n",
222 sc_strerror(errno, errmsg, sizeof(errmsg)));
223 continue;
224 }
225 }
227 if (sc_dbi_options_add(options, key, value)) {
228 char errmsg[1024];
229 fprintf(stderr, "puppet storeconfigs backend: Failed to add "
230 "option '%s': %s\n", key,
231 sc_strerror(errno, errmsg, sizeof(errmsg)));
232 continue;
233 }
234 }
236 if (! driver) {
237 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
238 "missing the 'DBAdapter' option.\n", name);
239 return -1;
240 }
241 if (! database) {
242 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
243 "missing the 'DBName' option.\n", name);
244 return -1;
245 }
247 snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
248 cb_name[sizeof(cb_name) - 1] = '\0';
250 client = sc_dbi_client_create(driver, database);
251 if (! client) {
252 char errbuf[1024];
253 fprintf(stderr, "puppet storeconfigs backend: "
254 "Failed to create DBI client: %s\n",
255 sc_strerror(errno, errbuf, sizeof(errbuf)));
256 return -1;
257 }
259 sc_dbi_client_set_options(client, options);
261 user_data = sc_object_create_wrapper(client,
262 (void (*)(void *))sc_dbi_client_destroy);
263 if (! user_data) {
264 sc_dbi_client_destroy(client);
265 fprintf(stderr, "puppet storeconfigs backend: "
266 "Failed to allocate sc_object_t\n");
267 return -1;
268 }
270 sc_plugin_register_init(cb_name, sc_puppet_stcfg_init, user_data);
271 sc_plugin_register_collector(cb_name, sc_puppet_stcfg_collect,
272 /* interval */ NULL, user_data);
274 /* pass control to the list */
275 sc_object_deref(user_data);
276 return 0;
277 } /* sc_puppet_stcfg_config_conn */
279 static int
280 sc_puppet_stcfg_config(oconfig_item_t *ci)
281 {
282 int i;
284 for (i = 0; i < ci->children_num; ++i) {
285 oconfig_item_t *child = ci->children + i;
287 if (! strcasecmp(child->key, "Connection"))
288 sc_puppet_stcfg_config_conn(child);
289 else
290 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
291 "config option '%s'.\n", child->key);
292 }
293 return 0;
294 } /* sc_puppet_stcfg_config */
296 int
297 sc_module_init(sc_plugin_info_t *info)
298 {
299 sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "puppet-storeconfigs");
300 sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
301 "backend accessing the Puppet stored configuration database");
302 sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
303 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
304 sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
305 sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
306 sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
308 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
309 fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
310 "DBI; possibly you don't have any drivers installed.\n");
311 return -1;
312 }
314 sc_plugin_register_config("puppet-storeconfigs", sc_puppet_stcfg_config);
315 return 0;
316 } /* sc_version_extra */
318 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */