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 int status;
88 const char *hostname;
89 const char *key;
90 const char *value;
91 sdb_time_t last_update;
93 assert(n == 4);
94 assert((data[0].type == SDB_TYPE_STRING)
95 && (data[1].type == SDB_TYPE_STRING)
96 && (data[2].type == SDB_TYPE_STRING)
97 && (data[3].type == SDB_TYPE_DATETIME));
99 hostname = data[0].data.string;
100 key = data[1].data.string;
101 value = data[2].data.string;
102 last_update = data[3].data.datetime;
104 status = sdb_store_attribute(hostname, key, value, last_update);
106 if (status < 0) {
107 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
108 "store/update host attribute '%s' for host '%s'.",
109 key, hostname);
110 return -1;
111 }
113 return 0;
114 } /* sdb_puppet_stcfg_get_attrs */
116 /*
117 * plugin API
118 */
120 static int
121 sdb_puppet_stcfg_init(sdb_object_t *user_data)
122 {
123 sdb_dbi_client_t *client;
125 if (! user_data)
126 return -1;
128 client = SDB_OBJ_WRAPPER(user_data)->data;
129 if (sdb_dbi_client_connect(client)) {
130 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
131 "Failed to connect to the storeconfigs DB.");
132 return -1;
133 }
135 sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
136 "connected to the storeconfigs DB.");
137 return 0;
138 } /* sdb_puppet_stcfg_init */
140 static int
141 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
142 {
143 sdb_dbi_client_t *client;
145 if (! user_data)
146 return -1;
148 client = SDB_OBJ_WRAPPER(user_data)->data;
149 if (sdb_dbi_client_check_conn(client)) {
150 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
151 "Connection to storeconfigs DB failed.");
152 return -1;
153 }
155 if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
156 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
157 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
158 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
159 "retrieve hosts from the storeconfigs DB.");
160 return -1;
161 }
163 if (sdb_dbi_exec_query(client, "SELECT "
164 "hosts.name AS hostname, "
165 "fact_names.name AS name, "
166 "fact_values.value AS value, "
167 "fact_values.updated_at AS updated_at "
168 "FROM fact_values "
169 "INNER JOIN hosts "
170 "ON fact_values.host_id = hosts.id "
171 "INNER JOIN fact_names "
172 "ON fact_values.fact_name_id = fact_names.id;",
173 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
174 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
175 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
176 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
177 "retrieve host attributes from the storeconfigs DB.");
178 return -1;
179 }
180 return 0;
181 } /* sdb_puppet_stcfg_collect */
183 static int
184 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
185 {
186 char *name = NULL;
187 char cb_name[1024];
189 sdb_object_t *user_data;
190 sdb_dbi_client_t *client;
191 sdb_dbi_options_t *options = NULL;
193 char *driver = NULL;
194 char *database = NULL;
196 int i;
198 if (oconfig_get_string(ci, &name)) {
199 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
200 "requires a single string argument\n"
201 "\tUsage: <Connection NAME>");
202 return -1;
203 }
205 for (i = 0; i < ci->children_num; ++i) {
206 oconfig_item_t *child = ci->children + i;
207 char *key = NULL, *value = NULL;
209 int status = 0;
211 if (! strcasecmp(child->key, "DBAdapter")) {
212 if (oconfig_get_string(child, &driver)) {
213 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
214 "DBAdapter requires a single string argument inside "
215 "<Connection %s>\n\tUsage: DBAdapter NAME",
216 name);
217 }
218 continue;
219 }
220 else if (! strcasecmp(child->key, "DBName")) {
221 if (oconfig_get_string(child, &database)) {
222 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
223 "DBName requires a single string argument inside "
224 "<Connection %s>\n\tUsage: DBName NAME",
225 name);
226 }
227 continue;
228 }
229 else if (! strcasecmp(child->key, "DBServer")) {
230 status = oconfig_get_string(child, &value);
231 key = "host";
232 }
233 else if (! strcasecmp(child->key, "DBPort")) {
234 status = oconfig_get_string(child, &value);
235 key = "port";
236 }
237 else if (! strcasecmp(child->key, "DBUser")) {
238 status = oconfig_get_string(child, &value);
239 key = "username";
240 }
241 else if (! strcasecmp(child->key, "DBPassword")) {
242 status = oconfig_get_string(child, &value);
243 key = "password";
244 }
245 else if (! strcasecmp(child->key, "DBIOption")) {
246 if ((child->values_num != 2)
247 || (child->values[0].type != OCONFIG_TYPE_STRING)
248 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
249 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
250 "DBIOption requires exactly two string arguments "
251 "inside <Connection %s>\n"
252 "\tUsage: DBIOption KEY VALUE", name);
253 continue;
254 }
256 status = 0;
257 key = child->values[0].value.string;
258 value = child->values[1].value.string;
259 }
260 else {
261 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
262 "Ignoring unknown config option '%s' inside "
263 "<Connection %s>.", child->key, name);
264 continue;
265 }
267 if (status) {
268 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
269 "'%s' requires a single string argument inside "
270 "<Connection %s>\n\tUsage: DBAdapter NAME",
271 child->key, name);
272 continue;
273 }
275 assert(key && value);
277 if (! options) {
278 if (! (options = sdb_dbi_options_create())) {
279 char errmsg[1024];
280 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
281 "Failed to create DBI options object: %s",
282 sdb_strerror(errno, errmsg, sizeof(errmsg)));
283 continue;
284 }
285 }
287 if (sdb_dbi_options_add(options, key, value)) {
288 char errmsg[1024];
289 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
290 "Failed to add option '%s': %s", key,
291 sdb_strerror(errno, errmsg, sizeof(errmsg)));
292 continue;
293 }
294 }
296 if (! driver) {
297 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
298 "Connection '%s' " "missing the 'DBAdapter' option.",
299 name);
300 return -1;
301 }
302 if (! database) {
303 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
304 "Connection '%s' missing the 'DBName' option.", name);
305 return -1;
306 }
308 snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
309 cb_name[sizeof(cb_name) - 1] = '\0';
311 client = sdb_dbi_client_create(driver, database);
312 if (! client) {
313 char errbuf[1024];
314 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
315 "Failed to create DBI client: %s",
316 sdb_strerror(errno, errbuf, sizeof(errbuf)));
317 return -1;
318 }
320 sdb_dbi_client_set_options(client, options);
322 user_data = sdb_object_create_wrapper("dbi-client", client,
323 (void (*)(void *))sdb_dbi_client_destroy);
324 if (! user_data) {
325 sdb_dbi_client_destroy(client);
326 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
327 "Failed to allocate sdb_object_t");
328 return -1;
329 }
331 sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
332 sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
333 /* interval */ NULL, user_data);
335 /* pass control to the list */
336 sdb_object_deref(user_data);
337 return 0;
338 } /* sdb_puppet_stcfg_config_conn */
340 static int
341 sdb_puppet_stcfg_config(oconfig_item_t *ci)
342 {
343 int i;
345 for (i = 0; i < ci->children_num; ++i) {
346 oconfig_item_t *child = ci->children + i;
348 if (! strcasecmp(child->key, "Connection"))
349 sdb_puppet_stcfg_config_conn(child);
350 else
351 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
352 "Ignoring unknown config option '%s'.", child->key);
353 }
354 return 0;
355 } /* sdb_puppet_stcfg_config */
357 int
358 sdb_module_init(sdb_plugin_info_t *info)
359 {
360 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
361 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
362 "backend accessing the Puppet stored configuration database");
363 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
364 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
365 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
366 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
367 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
369 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
370 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
371 "initialize DBI; possibly you don't have any drivers "
372 "installed.");
373 return -1;
374 }
376 sdb_plugin_register_config("puppet::store-configs",
377 sdb_puppet_stcfg_config);
378 return 0;
379 } /* sdb_version_extra */
381 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */