6f70f4940d01867645c098b762bb4416e6332066
1 /*
2 * SysDB - src/backend/puppet/store-configs.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 "core/error.h"
32 #include "utils/dbi.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 const char *hostname;
56 sdb_time_t timestamp;
58 int status;
60 assert(n == 2);
61 assert((data[0].type == SDB_TYPE_STRING)
62 && (data[1].type == SDB_TYPE_DATETIME));
64 hostname = data[0].data.string;
65 timestamp = data[1].data.datetime;
67 status = sdb_store_host(hostname, timestamp);
69 if (status < 0) {
70 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
71 "store/update host '%s'.", hostname);
72 return -1;
73 }
74 else if (! status)
75 sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
76 "Added/updated host '%s' (last update timestamp = "
77 "%"PRIscTIME").", hostname, timestamp);
78 return 0;
79 } /* sdb_puppet_stcfg_get_hosts */
81 static int
82 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
83 size_t n, sdb_data_t *data,
84 sdb_object_t __attribute__((unused)) *user_data)
85 {
86 sdb_attribute_t attr = SDB_ATTR_INIT;
88 int status;
90 assert(n == 4);
91 assert((data[0].type == SDB_TYPE_STRING)
92 && (data[1].type == SDB_TYPE_STRING)
93 && (data[2].type == SDB_TYPE_STRING)
94 && (data[3].type == SDB_TYPE_DATETIME));
96 attr.hostname = strdup(data[0].data.string);
97 SDB_OBJ(&attr)->name = strdup(data[1].data.string);
98 attr.attr_value = strdup(data[2].data.string);
99 attr._last_update = data[3].data.datetime;
101 status = sdb_store_attribute(&attr);
103 if (status < 0) {
104 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
105 "store/update host attribute '%s' for host '%s'.",
106 SDB_OBJ(&attr)->name, attr.hostname);
107 free(attr.hostname);
108 free(SDB_OBJ(&attr)->name);
109 free(attr.attr_value);
110 return -1;
111 }
113 free(attr.hostname);
114 free(SDB_OBJ(&attr)->name);
115 free(attr.attr_value);
116 return 0;
117 } /* sdb_puppet_stcfg_get_attrs */
119 /*
120 * plugin API
121 */
123 static int
124 sdb_puppet_stcfg_init(sdb_object_t *user_data)
125 {
126 sdb_dbi_client_t *client;
128 if (! user_data)
129 return -1;
131 client = SDB_OBJ_WRAPPER(user_data)->data;
132 if (sdb_dbi_client_connect(client)) {
133 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
134 "Failed to connect to the storeconfigs DB.");
135 return -1;
136 }
138 sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
139 "connected to the storeconfigs DB.");
140 return 0;
141 } /* sdb_puppet_stcfg_init */
143 static int
144 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
145 {
146 sdb_dbi_client_t *client;
148 if (! user_data)
149 return -1;
151 client = SDB_OBJ_WRAPPER(user_data)->data;
152 if (sdb_dbi_client_check_conn(client)) {
153 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
154 "Connection to storeconfigs DB failed.");
155 return -1;
156 }
158 if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
159 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
160 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
161 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
162 "retrieve hosts from the storeconfigs DB.");
163 return -1;
164 }
166 if (sdb_dbi_exec_query(client, "SELECT "
167 "hosts.name AS hostname, "
168 "fact_names.name AS name, "
169 "fact_values.value AS value, "
170 "fact_values.updated_at AS updated_at "
171 "FROM fact_values "
172 "INNER JOIN hosts "
173 "ON fact_values.host_id = hosts.id "
174 "INNER JOIN fact_names "
175 "ON fact_values.fact_name_id = fact_names.id;",
176 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
177 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
178 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
179 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
180 "retrieve host attributes from the storeconfigs DB.");
181 return -1;
182 }
183 return 0;
184 } /* sdb_puppet_stcfg_collect */
186 static int
187 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
188 {
189 char *name = NULL;
190 char cb_name[1024];
192 sdb_object_t *user_data;
193 sdb_dbi_client_t *client;
194 sdb_dbi_options_t *options = NULL;
196 char *driver = NULL;
197 char *database = NULL;
199 int i;
201 if (oconfig_get_string(ci, &name)) {
202 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
203 "requires a single string argument\n"
204 "\tUsage: <Connection NAME>");
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
217 "DBAdapter requires a single string argument inside "
218 "<Connection %s>\n\tUsage: DBAdapter NAME",
219 name);
220 }
221 continue;
222 }
223 else if (! strcasecmp(child->key, "DBName")) {
224 if (oconfig_get_string(child, &database)) {
225 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
226 "DBName requires a single string argument inside "
227 "<Connection %s>\n\tUsage: DBName NAME",
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
253 "DBIOption requires exactly two string arguments "
254 "inside <Connection %s>\n"
255 "\tUsage: DBIOption KEY VALUE", 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 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
265 "Ignoring unknown config option '%s' inside "
266 "<Connection %s>.", child->key, name);
267 continue;
268 }
270 if (status) {
271 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
272 "'%s' requires a single string argument inside "
273 "<Connection %s>\n\tUsage: DBAdapter NAME",
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
284 "Failed to create DBI options object: %s",
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
293 "Failed to add option '%s': %s", key,
294 sdb_strerror(errno, errmsg, sizeof(errmsg)));
295 continue;
296 }
297 }
299 if (! driver) {
300 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
301 "Connection '%s' " "missing the 'DBAdapter' option.",
302 name);
303 return -1;
304 }
305 if (! database) {
306 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
307 "Connection '%s' missing the 'DBName' option.", name);
308 return -1;
309 }
311 snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
312 cb_name[sizeof(cb_name) - 1] = '\0';
314 client = sdb_dbi_client_create(driver, database);
315 if (! client) {
316 char errbuf[1024];
317 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
318 "Failed to create DBI client: %s",
319 sdb_strerror(errno, errbuf, sizeof(errbuf)));
320 return -1;
321 }
323 sdb_dbi_client_set_options(client, options);
325 user_data = sdb_object_create_wrapper("dbi-client", client,
326 (void (*)(void *))sdb_dbi_client_destroy);
327 if (! user_data) {
328 sdb_dbi_client_destroy(client);
329 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
330 "Failed to allocate sdb_object_t");
331 return -1;
332 }
334 sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
335 sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
336 /* interval */ NULL, user_data);
338 /* pass control to the list */
339 sdb_object_deref(user_data);
340 return 0;
341 } /* sdb_puppet_stcfg_config_conn */
343 static int
344 sdb_puppet_stcfg_config(oconfig_item_t *ci)
345 {
346 int i;
348 for (i = 0; i < ci->children_num; ++i) {
349 oconfig_item_t *child = ci->children + i;
351 if (! strcasecmp(child->key, "Connection"))
352 sdb_puppet_stcfg_config_conn(child);
353 else
354 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
355 "Ignoring unknown config option '%s'.", child->key);
356 }
357 return 0;
358 } /* sdb_puppet_stcfg_config */
360 int
361 sdb_module_init(sdb_plugin_info_t *info)
362 {
363 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
364 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
365 "backend accessing the Puppet stored configuration database");
366 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
367 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
368 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
370 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
372 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
373 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
374 "initialize DBI; possibly you don't have any drivers "
375 "installed.");
376 return -1;
377 }
379 sdb_plugin_register_config("puppet::store-configs",
380 sdb_puppet_stcfg_config);
381 return 0;
382 } /* sdb_version_extra */
384 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */