8d7a8ebbfd7778c10726b9a22018a01a03287ab0
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/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 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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
70 "store/update host '%s'.", host.host_name);
71 free(host.host_name);
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").", 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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
106 "store/update host attribute '%s' for host '%s'.",
107 attr.attr_name, 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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
135 "Failed to connect to the storeconfigs DB.");
136 return -1;
137 }
139 sdb_log(SDB_LOG_INFO, "puppet::store-configs backend: Successfully "
140 "connected to the storeconfigs DB.");
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
155 "Connection to storeconfigs DB failed.");
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
163 "retrieve hosts from the storeconfigs DB.");
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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Failed to "
181 "retrieve host attributes from the storeconfigs DB.");
182 return -1;
183 }
184 return 0;
185 } /* sdb_puppet_stcfg_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 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Connection "
204 "requires a single string argument\n"
205 "\tUsage: <Connection NAME>");
206 return -1;
207 }
209 for (i = 0; i < ci->children_num; ++i) {
210 oconfig_item_t *child = ci->children + i;
211 char *key = NULL, *value = NULL;
213 int status = 0;
215 if (! strcasecmp(child->key, "DBAdapter")) {
216 if (oconfig_get_string(child, &driver)) {
217 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
218 "DBAdapter requires a single string argument inside "
219 "<Connection %s>\n\tUsage: DBAdapter NAME",
220 name);
221 }
222 continue;
223 }
224 else if (! strcasecmp(child->key, "DBName")) {
225 if (oconfig_get_string(child, &database)) {
226 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
227 "DBName requires a single string argument inside "
228 "<Connection %s>\n\tUsage: DBName NAME",
229 name);
230 }
231 continue;
232 }
233 else if (! strcasecmp(child->key, "DBServer")) {
234 status = oconfig_get_string(child, &value);
235 key = "host";
236 }
237 else if (! strcasecmp(child->key, "DBPort")) {
238 status = oconfig_get_string(child, &value);
239 key = "port";
240 }
241 else if (! strcasecmp(child->key, "DBUser")) {
242 status = oconfig_get_string(child, &value);
243 key = "username";
244 }
245 else if (! strcasecmp(child->key, "DBPassword")) {
246 status = oconfig_get_string(child, &value);
247 key = "password";
248 }
249 else if (! strcasecmp(child->key, "DBIOption")) {
250 if ((child->values_num != 2)
251 || (child->values[0].type != OCONFIG_TYPE_STRING)
252 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
253 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
254 "DBIOption requires exactly two string arguments "
255 "inside <Connection %s>\n"
256 "\tUsage: DBIOption KEY VALUE", name);
257 continue;
258 }
260 status = 0;
261 key = child->values[0].value.string;
262 value = child->values[1].value.string;
263 }
264 else {
265 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
266 "Ignoring unknown config option '%s' inside "
267 "<Connection %s>.", child->key, name);
268 continue;
269 }
271 if (status) {
272 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: Option "
273 "'%s' requires a single string argument inside "
274 "<Connection %s>\n\tUsage: DBAdapter NAME",
275 child->key, name);
276 continue;
277 }
279 assert(key && value);
281 if (! options) {
282 if (! (options = sdb_dbi_options_create())) {
283 char errmsg[1024];
284 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
285 "Failed to create DBI options object: %s",
286 sdb_strerror(errno, errmsg, sizeof(errmsg)));
287 continue;
288 }
289 }
291 if (sdb_dbi_options_add(options, key, value)) {
292 char errmsg[1024];
293 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
294 "Failed to add option '%s': %s", key,
295 sdb_strerror(errno, errmsg, sizeof(errmsg)));
296 continue;
297 }
298 }
300 if (! driver) {
301 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
302 "Connection '%s' " "missing the 'DBAdapter' option.",
303 name);
304 return -1;
305 }
306 if (! database) {
307 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
308 "Connection '%s' missing the 'DBName' option.", name);
309 return -1;
310 }
312 snprintf(cb_name, sizeof(cb_name), "puppet::storeconfigs::%s", name);
313 cb_name[sizeof(cb_name) - 1] = '\0';
315 client = sdb_dbi_client_create(driver, database);
316 if (! client) {
317 char errbuf[1024];
318 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
319 "Failed to create DBI client: %s",
320 sdb_strerror(errno, errbuf, sizeof(errbuf)));
321 return -1;
322 }
324 sdb_dbi_client_set_options(client, options);
326 user_data = sdb_object_create_wrapper(client,
327 (void (*)(void *))sdb_dbi_client_destroy);
328 if (! user_data) {
329 sdb_dbi_client_destroy(client);
330 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: "
331 "Failed to allocate sdb_object_t");
332 return -1;
333 }
335 sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
336 sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
337 /* interval */ NULL, user_data);
339 /* pass control to the list */
340 sdb_object_deref(user_data);
341 return 0;
342 } /* sdb_puppet_stcfg_config_conn */
344 static int
345 sdb_puppet_stcfg_config(oconfig_item_t *ci)
346 {
347 int i;
349 for (i = 0; i < ci->children_num; ++i) {
350 oconfig_item_t *child = ci->children + i;
352 if (! strcasecmp(child->key, "Connection"))
353 sdb_puppet_stcfg_config_conn(child);
354 else
355 sdb_log(SDB_LOG_WARNING, "puppet::store-configs backend: "
356 "Ignoring unknown config option '%s'.", child->key);
357 }
358 return 0;
359 } /* sdb_puppet_stcfg_config */
361 int
362 sdb_module_init(sdb_plugin_info_t *info)
363 {
364 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet::store-configs");
365 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
366 "backend accessing the Puppet stored configuration database");
367 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
368 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
370 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
371 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
373 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
374 sdb_log(SDB_LOG_ERR, "puppet::store-configs backend: failed to "
375 "initialize DBI; possibly you don't have any drivers "
376 "installed.");
377 return -1;
378 }
380 sdb_plugin_register_config("puppet::store-configs",
381 sdb_puppet_stcfg_config);
382 return 0;
383 } /* sdb_version_extra */
385 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */