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 "utils/dbi.h"
32 #include "utils/error.h"
34 #include "liboconfig/utils.h"
36 #include <assert.h>
37 #include <errno.h>
39 #include <string.h>
40 #include <strings.h>
42 SDB_PLUGIN_MAGIC;
44 /*
45 * private helper functions
46 */
48 static int
49 sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
50 size_t n, sdb_data_t *data,
51 sdb_object_t __attribute__((unused)) *user_data)
52 {
53 const char *hostname;
54 sdb_time_t timestamp;
56 int status;
58 assert(n == 2);
59 assert((data[0].type == SDB_TYPE_STRING)
60 && (data[1].type == SDB_TYPE_DATETIME));
62 hostname = data[0].data.string;
63 timestamp = data[1].data.datetime;
65 status = sdb_store_host(hostname, timestamp);
67 if (status < 0) {
68 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
69 "store/update host '%s'.", hostname);
70 return -1;
71 }
72 else if (! status)
73 sdb_log(SDB_LOG_DEBUG, "puppet::store-configs backend: "
74 "Added/updated host '%s' (last update timestamp = "
75 "%"PRIsdbTIME").", hostname, timestamp);
76 return 0;
77 } /* sdb_puppet_stcfg_get_hosts */
79 static int
80 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
81 size_t n, sdb_data_t *data,
82 sdb_object_t __attribute__((unused)) *user_data)
83 {
84 int status;
86 const char *hostname;
87 const char *key;
88 sdb_data_t value;
89 sdb_time_t last_update;
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 hostname = data[0].data.string;
98 key = data[1].data.string;
99 value.type = SDB_TYPE_STRING;
100 value.data.string = data[2].data.string;
101 last_update = data[3].data.datetime;
103 status = sdb_store_attribute(hostname, key, &value, last_update);
105 if (status < 0) {
106 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
107 "store/update host attribute '%s' for host '%s'.",
108 key, hostname);
109 return -1;
110 }
112 return 0;
113 } /* sdb_puppet_stcfg_get_attrs */
115 /*
116 * plugin API
117 */
119 static int
120 sdb_puppet_stcfg_init(sdb_object_t *user_data)
121 {
122 sdb_dbi_client_t *client;
124 if (! user_data)
125 return -1;
127 client = SDB_OBJ_WRAPPER(user_data)->data;
128 if (sdb_dbi_client_connect(client)) {
129 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
130 "Failed to connect to the storeconfigs DB.");
131 return -1;
132 }
134 sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
135 "connected to the storeconfigs DB.");
136 return 0;
137 } /* sdb_puppet_stcfg_init */
139 static int
140 sdb_puppet_stcfg_shutdown(sdb_object_t *user_data)
141 {
142 if (! user_data)
143 return -1;
145 sdb_dbi_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
146 SDB_OBJ_WRAPPER(user_data)->data = NULL;
147 return 0;
148 } /* sdb_puppet_stcfg_shutdown */
150 static int
151 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
152 {
153 sdb_dbi_client_t *client;
155 if (! user_data)
156 return -1;
158 client = SDB_OBJ_WRAPPER(user_data)->data;
159 if (sdb_dbi_client_check_conn(client)) {
160 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
161 "Connection to storeconfigs DB failed.");
162 return -1;
163 }
165 if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
166 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
167 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
168 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
169 "retrieve hosts from the storeconfigs DB.");
170 return -1;
171 }
173 if (sdb_dbi_exec_query(client, "SELECT "
174 "hosts.name AS hostname, "
175 "fact_names.name AS name, "
176 "fact_values.value AS value, "
177 "fact_values.updated_at AS updated_at "
178 "FROM fact_values "
179 "INNER JOIN hosts "
180 "ON fact_values.host_id = hosts.id "
181 "INNER JOIN fact_names "
182 "ON fact_values.fact_name_id = fact_names.id;",
183 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
184 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
185 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
186 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
187 "retrieve host attributes from the storeconfigs DB.");
188 return -1;
189 }
190 return 0;
191 } /* sdb_puppet_stcfg_collect */
193 static int
194 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
195 {
196 char *name = NULL;
198 sdb_object_t *user_data;
199 sdb_dbi_client_t *client;
200 sdb_dbi_options_t *options = NULL;
202 char *driver = NULL;
203 char *database = NULL;
205 int i;
207 if (oconfig_get_string(ci, &name)) {
208 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
209 "requires a single string argument\n"
210 "\tUsage: <Connection NAME>");
211 return -1;
212 }
214 for (i = 0; i < ci->children_num; ++i) {
215 oconfig_item_t *child = ci->children + i;
216 char *key = NULL, *value = NULL;
218 int status = 0;
220 if (! strcasecmp(child->key, "DBAdapter")) {
221 if (oconfig_get_string(child, &driver)) {
222 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
223 "DBAdapter requires a single string argument inside "
224 "<Connection %s>\n\tUsage: DBAdapter NAME",
225 name);
226 }
227 continue;
228 }
229 else if (! strcasecmp(child->key, "DBName")) {
230 if (oconfig_get_string(child, &database)) {
231 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
232 "DBName requires a single string argument inside "
233 "<Connection %s>\n\tUsage: DBName NAME",
234 name);
235 }
236 continue;
237 }
238 else if (! strcasecmp(child->key, "DBServer")) {
239 status = oconfig_get_string(child, &value);
240 key = "host";
241 }
242 else if (! strcasecmp(child->key, "DBPort")) {
243 status = oconfig_get_string(child, &value);
244 key = "port";
245 }
246 else if (! strcasecmp(child->key, "DBUser")) {
247 status = oconfig_get_string(child, &value);
248 key = "username";
249 }
250 else if (! strcasecmp(child->key, "DBPassword")) {
251 status = oconfig_get_string(child, &value);
252 key = "password";
253 }
254 else if (! strcasecmp(child->key, "DBIOption")) {
255 if ((child->values_num != 2)
256 || (child->values[0].type != OCONFIG_TYPE_STRING)
257 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
258 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
259 "DBIOption requires exactly two string arguments "
260 "inside <Connection %s>\n"
261 "\tUsage: DBIOption KEY VALUE", name);
262 continue;
263 }
265 status = 0;
266 key = child->values[0].value.string;
267 value = child->values[1].value.string;
268 }
269 else {
270 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
271 "Ignoring unknown config option '%s' inside "
272 "<Connection %s>.", child->key, name);
273 continue;
274 }
276 if (status) {
277 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
278 "'%s' requires a single string argument inside "
279 "<Connection %s>\n\tUsage: DBAdapter NAME",
280 child->key, name);
281 continue;
282 }
284 assert(key && value);
286 if (! options) {
287 if (! (options = sdb_dbi_options_create())) {
288 char errmsg[1024];
289 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
290 "Failed to create DBI options object: %s",
291 sdb_strerror(errno, errmsg, sizeof(errmsg)));
292 continue;
293 }
294 }
296 if (sdb_dbi_options_add(options, key, value)) {
297 char errmsg[1024];
298 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
299 "Failed to add option '%s': %s", key,
300 sdb_strerror(errno, errmsg, sizeof(errmsg)));
301 continue;
302 }
303 }
305 if (! driver) {
306 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
307 "Connection '%s' " "missing the 'DBAdapter' option.",
308 name);
309 return -1;
310 }
311 if (! database) {
312 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
313 "Connection '%s' missing the 'DBName' option.", name);
314 return -1;
315 }
317 client = sdb_dbi_client_create(driver, database);
318 if (! client) {
319 char errbuf[1024];
320 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
321 "Failed to create DBI client: %s",
322 sdb_strerror(errno, errbuf, sizeof(errbuf)));
323 return -1;
324 }
326 sdb_dbi_client_set_options(client, options);
328 user_data = sdb_object_create_wrapper("dbi-client", client,
329 (void (*)(void *))sdb_dbi_client_destroy);
330 if (! user_data) {
331 sdb_dbi_client_destroy(client);
332 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
333 "Failed to allocate sdb_object_t");
334 return -1;
335 }
337 sdb_plugin_register_init(name, sdb_puppet_stcfg_init, user_data);
338 sdb_plugin_register_shutdown(name, sdb_puppet_stcfg_shutdown,
339 user_data);
340 sdb_plugin_register_collector(name, sdb_puppet_stcfg_collect,
341 /* interval */ NULL, user_data);
343 /* pass control to the list */
344 sdb_object_deref(user_data);
345 return 0;
346 } /* sdb_puppet_stcfg_config_conn */
348 static int
349 sdb_puppet_stcfg_config(oconfig_item_t *ci)
350 {
351 int i;
353 if (! ci) /* nothing to do to deconfigure this plugin */
354 return 0;
356 for (i = 0; i < ci->children_num; ++i) {
357 oconfig_item_t *child = ci->children + i;
359 if (! strcasecmp(child->key, "Connection"))
360 sdb_puppet_stcfg_config_conn(child);
361 else
362 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
363 "Ignoring unknown config option '%s'.", child->key);
364 }
365 return 0;
366 } /* sdb_puppet_stcfg_config */
368 int
369 sdb_module_init(sdb_plugin_info_t *info)
370 {
371 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
372 "backend accessing the Puppet stored configuration database");
373 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
374 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
375 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
376 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
377 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
379 sdb_plugin_register_config(sdb_puppet_stcfg_config);
380 return 0;
381 } /* sdb_version_extra */
383 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */