1 /*
2 * SysDB - 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 "sysdb.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 SDB_PLUGIN_MAGIC;
46 /*
47 * private helper functions
48 */
50 static int
51 sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
52 size_t n, sdb_data_t *data,
53 sdb_object_t __attribute__((unused)) *user_data)
54 {
55 sdb_host_t host = SDB_HOST_INIT;
57 int status;
59 assert(n == 2);
60 assert((data[0].type == SDB_TYPE_STRING)
61 && (data[1].type == SDB_TYPE_DATETIME));
63 host.host_name = strdup(data[0].data.string);
64 host.host_last_update = data[1].data.datetime;
66 status = sdb_store_host(&host);
68 if (status < 0) {
69 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
70 "host '%s'.\n", host.host_name);
71 free(host.host_name);
72 return -1;
73 }
74 else if (! status)
75 fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
76 "(last update timestamp = %"PRIscTIME").\n",
77 host.host_name, host.host_last_update);
78 free(host.host_name);
79 return 0;
80 } /* sdb_puppet_stcfg_get_hosts */
82 static int
83 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
84 size_t n, sdb_data_t *data,
85 sdb_object_t __attribute__((unused)) *user_data)
86 {
87 sdb_attribute_t attr = SDB_ATTR_INIT;
89 int status;
91 assert(n == 4);
92 assert((data[0].type == SDB_TYPE_STRING)
93 && (data[1].type == SDB_TYPE_STRING)
94 && (data[2].type == SDB_TYPE_STRING)
95 && (data[3].type == SDB_TYPE_DATETIME));
97 attr.hostname = strdup(data[0].data.string);
98 attr.attr_name = strdup(data[1].data.string);
99 attr.attr_value = strdup(data[2].data.string);
100 attr.attr_last_update = data[3].data.datetime;
102 status = sdb_store_attribute(&attr);
104 if (status < 0) {
105 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
106 "host attribute '%s' for host '%s'.\n", attr.attr_name,
107 attr.hostname);
108 free(attr.hostname);
109 free(attr.attr_name);
110 free(attr.attr_value);
111 return -1;
112 }
114 free(attr.hostname);
115 free(attr.attr_name);
116 free(attr.attr_value);
117 return 0;
118 } /* sdb_puppet_stcfg_get_attrs */
120 /*
121 * plugin API
122 */
124 static int
125 sdb_puppet_stcfg_init(sdb_object_t *user_data)
126 {
127 sdb_dbi_client_t *client;
129 if (! user_data)
130 return -1;
132 client = SDB_OBJ_WRAPPER(user_data)->data;
133 if (sdb_dbi_client_connect(client)) {
134 fprintf(stderr, "puppet storeconfigs backend: "
135 "Failed to connect to the storeconfigs DB.\n");
136 return -1;
137 }
139 fprintf(stderr, "puppet storeconfigs backend: Successfully "
140 "connected to the storeconfigs DB.\n");
141 return 0;
142 } /* sdb_puppet_stcfg_init */
144 static int
145 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
146 {
147 sdb_dbi_client_t *client;
149 if (! user_data)
150 return -1;
152 client = SDB_OBJ_WRAPPER(user_data)->data;
153 if (sdb_dbi_client_check_conn(client)) {
154 fprintf(stderr, "puppet storeconfigs backend: "
155 "Connection to storeconfigs DB failed.\n");
156 return -1;
157 }
159 if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
160 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
161 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
162 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
163 "hosts from the storeconfigs DB.\n");
164 return -1;
165 }
167 if (sdb_dbi_exec_query(client, "SELECT "
168 "hosts.name AS hostname, "
169 "fact_names.name AS name, "
170 "fact_values.value AS value, "
171 "fact_values.updated_at AS updated_at "
172 "FROM fact_values "
173 "INNER JOIN hosts "
174 "ON fact_values.host_id = hosts.id "
175 "INNER JOIN fact_names "
176 "ON fact_values.fact_name_id = fact_names.id;",
177 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
178 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
179 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
180 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
181 "host attributes from the storeconfigs DB.\n");
182 return -1;
183 }
184 return 0;
185 } /* sdb_collectd_collect */
187 static int
188 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
189 {
190 char *name = NULL;
191 char cb_name[1024];
193 sdb_object_t *user_data;
194 sdb_dbi_client_t *client;
195 sdb_dbi_options_t *options = NULL;
197 char *driver = NULL;
198 char *database = NULL;
200 int i;
202 if (oconfig_get_string(ci, &name)) {
203 fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
204 "single string argument\n\tUsage: <Connection NAME>\n");
205 return -1;
206 }
208 for (i = 0; i < ci->children_num; ++i) {
209 oconfig_item_t *child = ci->children + i;
210 char *key = NULL, *value = NULL;
212 int status = 0;
214 if (! strcasecmp(child->key, "DBAdapter")) {
215 if (oconfig_get_string(child, &driver)) {
216 fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
217 "requires a single string argument inside "
218 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
219 name);
220 }
221 continue;
222 }
223 else if (! strcasecmp(child->key, "DBName")) {
224 if (oconfig_get_string(child, &database)) {
225 fprintf(stderr, "puppet storeconfigs backend: DBName"
226 "requires a single string argument inside "
227 "<Connection %s>\n\tUsage: DBName NAME\n",
228 name);
229 }
230 continue;
231 }
232 else if (! strcasecmp(child->key, "DBServer")) {
233 status = oconfig_get_string(child, &value);
234 key = "host";
235 }
236 else if (! strcasecmp(child->key, "DBPort")) {
237 status = oconfig_get_string(child, &value);
238 key = "port";
239 }
240 else if (! strcasecmp(child->key, "DBUser")) {
241 status = oconfig_get_string(child, &value);
242 key = "username";
243 }
244 else if (! strcasecmp(child->key, "DBPassword")) {
245 status = oconfig_get_string(child, &value);
246 key = "password";
247 }
248 else if (! strcasecmp(child->key, "DBIOption")) {
249 if ((child->values_num != 2)
250 || (child->values[0].type != OCONFIG_TYPE_STRING)
251 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
252 fprintf(stderr, "puppet storeconfigs backend: DBIOption "
253 "requires exactly two string arguments inside "
254 "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
255 name);
256 continue;
257 }
259 status = 0;
260 key = child->values[0].value.string;
261 value = child->values[1].value.string;
262 }
263 else {
264 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
265 "config option '%s' inside <Connection %s>.\n",
266 child->key, name);
267 continue;
268 }
270 if (status) {
271 fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
272 "requires a single string argument inside "
273 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
274 child->key, name);
275 continue;
276 }
278 assert(key && value);
280 if (! options) {
281 if (! (options = sdb_dbi_options_create())) {
282 char errmsg[1024];
283 fprintf(stderr, "puppet storeconfigs backend: Failed to "
284 "create DBI options object: %s\n",
285 sdb_strerror(errno, errmsg, sizeof(errmsg)));
286 continue;
287 }
288 }
290 if (sdb_dbi_options_add(options, key, value)) {
291 char errmsg[1024];
292 fprintf(stderr, "puppet storeconfigs backend: Failed to add "
293 "option '%s': %s\n", key,
294 sdb_strerror(errno, errmsg, sizeof(errmsg)));
295 continue;
296 }
297 }
299 if (! driver) {
300 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
301 "missing the 'DBAdapter' option.\n", name);
302 return -1;
303 }
304 if (! database) {
305 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
306 "missing the 'DBName' option.\n", name);
307 return -1;
308 }
310 snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
311 cb_name[sizeof(cb_name) - 1] = '\0';
313 client = sdb_dbi_client_create(driver, database);
314 if (! client) {
315 char errbuf[1024];
316 fprintf(stderr, "puppet storeconfigs backend: "
317 "Failed to create DBI client: %s\n",
318 sdb_strerror(errno, errbuf, sizeof(errbuf)));
319 return -1;
320 }
322 sdb_dbi_client_set_options(client, options);
324 user_data = sdb_object_create_wrapper(client,
325 (void (*)(void *))sdb_dbi_client_destroy);
326 if (! user_data) {
327 sdb_dbi_client_destroy(client);
328 fprintf(stderr, "puppet storeconfigs backend: "
329 "Failed to allocate sdb_object_t\n");
330 return -1;
331 }
333 sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
334 sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
335 /* interval */ NULL, user_data);
337 /* pass control to the list */
338 sdb_object_deref(user_data);
339 return 0;
340 } /* sdb_puppet_stcfg_config_conn */
342 static int
343 sdb_puppet_stcfg_config(oconfig_item_t *ci)
344 {
345 int i;
347 for (i = 0; i < ci->children_num; ++i) {
348 oconfig_item_t *child = ci->children + i;
350 if (! strcasecmp(child->key, "Connection"))
351 sdb_puppet_stcfg_config_conn(child);
352 else
353 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
354 "config option '%s'.\n", child->key);
355 }
356 return 0;
357 } /* sdb_puppet_stcfg_config */
359 int
360 sdb_module_init(sdb_plugin_info_t *info)
361 {
362 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet-storeconfigs");
363 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
364 "backend accessing the Puppet stored configuration database");
365 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
366 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
367 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
368 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
371 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
372 fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
373 "DBI; possibly you don't have any drivers installed.\n");
374 return -1;
375 }
377 sdb_plugin_register_config("puppet-storeconfigs",
378 sdb_puppet_stcfg_config);
379 return 0;
380 } /* sdb_version_extra */
382 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */