Code

4c92c151770c53a34b44b147df793658c3343752
[sysdb.git] / src / backend / puppet-storeconfigs.c
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/error.h"
32 #include "utils/dbi.h"
33 #include "utils/string.h"
35 #include "liboconfig/utils.h"
37 #include <dbi/dbi.h>
39 #include <assert.h>
40 #include <errno.h>
42 #include <string.h>
43 #include <strings.h>
45 SDB_PLUGIN_MAGIC;
47 /*
48  * private helper functions
49  */
51 static int
52 sdb_puppet_stcfg_get_hosts(sdb_dbi_client_t __attribute__((unused)) *client,
53                 size_t n, sdb_data_t *data,
54                 sdb_object_t __attribute__((unused)) *user_data)
55 {
56         sdb_host_t host = SDB_HOST_INIT;
58         int status;
60         assert(n == 2);
61         assert((data[0].type == SDB_TYPE_STRING)
62                         && (data[1].type == SDB_TYPE_DATETIME));
64         host.host_name = strdup(data[0].data.string);
65         host.host_last_update = data[1].data.datetime;
67         status = sdb_store_host(&host);
69         if (status < 0) {
70                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
71                                 "store/update host '%s'.\n", host.host_name);
72                 free(host.host_name);
73                 return -1;
74         }
75         else if (! status)
76                 sdb_error_set(SDB_LOG_DEBUG, "puppet storeconfigs backend: "
77                                 "Added/updated host '%s' (last update timestamp = "
78                                 "%"PRIscTIME").\n", host.host_name, host.host_last_update);
79         free(host.host_name);
80         return 0;
81 } /* sdb_puppet_stcfg_get_hosts */
83 static int
84 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
85                 size_t n, sdb_data_t *data,
86                 sdb_object_t __attribute__((unused)) *user_data)
87 {
88         sdb_attribute_t attr = SDB_ATTR_INIT;
90         int status;
92         assert(n == 4);
93         assert((data[0].type == SDB_TYPE_STRING)
94                         && (data[1].type == SDB_TYPE_STRING)
95                         && (data[2].type == SDB_TYPE_STRING)
96                         && (data[3].type == SDB_TYPE_DATETIME));
98         attr.hostname = strdup(data[0].data.string);
99         attr.attr_name = strdup(data[1].data.string);
100         attr.attr_value = strdup(data[2].data.string);
101         attr.attr_last_update = data[3].data.datetime;
103         status = sdb_store_attribute(&attr);
105         if (status < 0) {
106                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
107                                 "store/update host attribute '%s' for host '%s'.\n",
108                                 attr.attr_name, attr.hostname);
109                 free(attr.hostname);
110                 free(attr.attr_name);
111                 free(attr.attr_value);
112                 return -1;
113         }
115         free(attr.hostname);
116         free(attr.attr_name);
117         free(attr.attr_value);
118         return 0;
119 } /* sdb_puppet_stcfg_get_attrs */
121 /*
122  * plugin API
123  */
125 static int
126 sdb_puppet_stcfg_init(sdb_object_t *user_data)
128         sdb_dbi_client_t *client;
130         if (! user_data)
131                 return -1;
133         client = SDB_OBJ_WRAPPER(user_data)->data;
134         if (sdb_dbi_client_connect(client)) {
135                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
136                                 "Failed to connect to the storeconfigs DB.\n");
137                 return -1;
138         }
140         sdb_error_set(SDB_LOG_INFO, "puppet storeconfigs backend: Successfully "
141                         "connected to the storeconfigs DB.\n");
142         return 0;
143 } /* sdb_puppet_stcfg_init */
145 static int
146 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
148         sdb_dbi_client_t *client;
150         if (! user_data)
151                 return -1;
153         client = SDB_OBJ_WRAPPER(user_data)->data;
154         if (sdb_dbi_client_check_conn(client)) {
155                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
156                                 "Connection to storeconfigs DB failed.\n");
157                 return -1;
158         }
160         if (sdb_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
161                                 sdb_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
162                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
163                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
164                                 "retrieve hosts from the storeconfigs DB.\n");
165                 return -1;
166         }
168         if (sdb_dbi_exec_query(client, "SELECT "
169                                         "hosts.name AS hostname, "
170                                         "fact_names.name AS name, "
171                                         "fact_values.value AS value, "
172                                         "fact_values.updated_at AS updated_at "
173                                 "FROM fact_values "
174                                 "INNER JOIN hosts "
175                                         "ON fact_values.host_id = hosts.id "
176                                 "INNER JOIN fact_names "
177                                         "ON fact_values.fact_name_id = fact_names.id;",
178                                 sdb_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
179                                 /* col types = */ SDB_TYPE_STRING, SDB_TYPE_STRING,
180                                 SDB_TYPE_STRING, SDB_TYPE_DATETIME)) {
181                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Failed to "
182                                 "retrieve host attributes from the storeconfigs DB.\n");
183                 return -1;
184         }
185         return 0;
186 } /* sdb_collectd_collect */
188 static int
189 sdb_puppet_stcfg_config_conn(oconfig_item_t *ci)
191         char *name = NULL;
192         char cb_name[1024];
194         sdb_object_t *user_data;
195         sdb_dbi_client_t *client;
196         sdb_dbi_options_t *options = NULL;
198         char *driver = NULL;
199         char *database = NULL;
201         int i;
203         if (oconfig_get_string(ci, &name)) {
204                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Connection "
205                                 "requires a single string argument\n"
206                                 "\tUsage: <Connection NAME>\n");
207                 return -1;
208         }
210         for (i = 0; i < ci->children_num; ++i) {
211                 oconfig_item_t *child = ci->children + i;
212                 char *key = NULL, *value = NULL;
214                 int status = 0;
216                 if (! strcasecmp(child->key, "DBAdapter")) {
217                         if (oconfig_get_string(child, &driver)) {
218                                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
219                                                 "DBAdapter requires a single string argument inside "
220                                                 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
221                                                 name);
222                         }
223                         continue;
224                 }
225                 else if (! strcasecmp(child->key, "DBName")) {
226                         if (oconfig_get_string(child, &database)) {
227                                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
228                                                 "DBName requires a single string argument inside "
229                                                 "<Connection %s>\n\tUsage: DBName NAME\n",
230                                                 name);
231                         }
232                         continue;
233                 }
234                 else if (! strcasecmp(child->key, "DBServer")) {
235                         status = oconfig_get_string(child, &value);
236                         key = "host";
237                 }
238                 else if (! strcasecmp(child->key, "DBPort")) {
239                         status = oconfig_get_string(child, &value);
240                         key = "port";
241                 }
242                 else if (! strcasecmp(child->key, "DBUser")) {
243                         status = oconfig_get_string(child, &value);
244                         key = "username";
245                 }
246                 else if (! strcasecmp(child->key, "DBPassword")) {
247                         status = oconfig_get_string(child, &value);
248                         key = "password";
249                 }
250                 else if (! strcasecmp(child->key, "DBIOption")) {
251                         if ((child->values_num != 2)
252                                         || (child->values[0].type != OCONFIG_TYPE_STRING)
253                                         || (child->values[1].type != OCONFIG_TYPE_STRING)) {
254                                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
255                                                 "DBIOption requires exactly two string arguments "
256                                                 "inside <Connection %s>\n"
257                                                 "\tUsage: DBIOption KEY VALUE\n", name);
258                                 continue;
259                         }
261                         status = 0;
262                         key = child->values[0].value.string;
263                         value = child->values[1].value.string;
264                 }
265                 else {
266                         sdb_error_set(SDB_LOG_WARNING, "puppet storeconfigs backend: "
267                                         "Ignoring unknown config option '%s' inside "
268                                         "<Connection %s>.\n", child->key, name);
269                         continue;
270                 }
272                 if (status) {
273                         sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: Option "
274                                         "'%s' requires a single string argument inside "
275                                         "<Connection %s>\n\tUsage: DBAdapter NAME\n",
276                                         child->key, name);
277                         continue;
278                 }
280                 assert(key && value);
282                 if (! options) {
283                         if (! (options = sdb_dbi_options_create())) {
284                                 char errmsg[1024];
285                                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
286                                                 "Failed to create DBI options object: %s\n",
287                                                 sdb_strerror(errno, errmsg, sizeof(errmsg)));
288                                 continue;
289                         }
290                 }
292                 if (sdb_dbi_options_add(options, key, value)) {
293                         char errmsg[1024];
294                         sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
295                                         "Failed to add option '%s': %s\n", key,
296                                         sdb_strerror(errno, errmsg, sizeof(errmsg)));
297                         continue;
298                 }
299         }
301         if (! driver) {
302                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
303                                 "Connection '%s' " "missing the 'DBAdapter' option.\n",
304                                 name);
305                 return -1;
306         }
307         if (! database) {
308                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
309                                 "Connection '%s' missing the 'DBName' option.\n", name);
310                 return -1;
311         }
313         snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
314         cb_name[sizeof(cb_name) - 1] = '\0';
316         client = sdb_dbi_client_create(driver, database);
317         if (! client) {
318                 char errbuf[1024];
319                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
320                                 "Failed to create DBI client: %s\n",
321                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
322                 return -1;
323         }
325         sdb_dbi_client_set_options(client, options);
327         user_data = sdb_object_create_wrapper(client,
328                         (void (*)(void *))sdb_dbi_client_destroy);
329         if (! user_data) {
330                 sdb_dbi_client_destroy(client);
331                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: "
332                                 "Failed to allocate sdb_object_t\n");
333                 return -1;
334         }
336         sdb_plugin_register_init(cb_name, sdb_puppet_stcfg_init, user_data);
337         sdb_plugin_register_collector(cb_name, sdb_puppet_stcfg_collect,
338                         /* interval */ NULL, user_data);
340         /* pass control to the list */
341         sdb_object_deref(user_data);
342         return 0;
343 } /* sdb_puppet_stcfg_config_conn */
345 static int
346 sdb_puppet_stcfg_config(oconfig_item_t *ci)
348         int i;
350         for (i = 0; i < ci->children_num; ++i) {
351                 oconfig_item_t *child = ci->children + i;
353                 if (! strcasecmp(child->key, "Connection"))
354                         sdb_puppet_stcfg_config_conn(child);
355                 else
356                         sdb_error_set(SDB_LOG_WARNING, "puppet storeconfigs backend: "
357                                         "Ignoring unknown config option '%s'.\n", child->key);
358         }
359         return 0;
360 } /* sdb_puppet_stcfg_config */
362 int
363 sdb_module_init(sdb_plugin_info_t *info)
365         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_NAME, "puppet-storeconfigs");
366         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
367                         "backend accessing the Puppet stored configuration database");
368         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
369                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
370         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
371         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
372         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
374         if (dbi_initialize(/* driver dir = */ NULL) < 0) {
375                 sdb_error_set(SDB_LOG_ERR, "puppet storeconfigs backend: failed to "
376                                 "initialize DBI; possibly you don't have any drivers "
377                                 "installed.\n");
378                 return -1;
379         }
381         sdb_plugin_register_config("puppet-storeconfigs",
382                         sdb_puppet_stcfg_config);
383         return 0;
384 } /* sdb_version_extra */
386 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */