Code

Automatically prefix all log messages with the plugin name (if available).
[sysdb.git] / src / plugins / backend / puppet / store-configs.c
1 /*
2  * SysDB - src/plugins/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_plugin_store_host(hostname, timestamp);
67         if (status < 0) {
68                 sdb_log(SDB_LOG_ERR, "Failed to store/update host '%s'.", hostname);
69                 return -1;
70         }
71         else if (! status)
72                 sdb_log(SDB_LOG_DEBUG, "Added/updated host '%s' "
73                                 "(last update timestamp = %"PRIsdbTIME").",
74                                 hostname, timestamp);
75         return 0;
76 } /* sdb_puppet_stcfg_get_hosts */
78 static int
79 sdb_puppet_stcfg_get_attrs(sdb_dbi_client_t __attribute__((unused)) *client,
80                 size_t n, sdb_data_t *data,
81                 sdb_object_t __attribute__((unused)) *user_data)
82 {
83         int status;
85         const char *hostname;
86         const char *key;
87         sdb_data_t  value;
88         sdb_time_t  last_update;
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         hostname = data[0].data.string;
97         key = data[1].data.string;
98         value.type = SDB_TYPE_STRING;
99         value.data.string = data[2].data.string;
100         last_update = data[3].data.datetime;
102         status = sdb_plugin_store_attribute(hostname, key, &value, last_update);
104         if (status < 0) {
105                 sdb_log(SDB_LOG_ERR, "Failed to store/update host attribute "
106                                 "'%s' for host '%s'.", key, hostname);
107                 return -1;
108         }
110         return 0;
111 } /* sdb_puppet_stcfg_get_attrs */
113 /*
114  * plugin API
115  */
117 static int
118 sdb_puppet_stcfg_init(sdb_object_t *user_data)
120         sdb_dbi_client_t *client;
122         if (! user_data)
123                 return -1;
125         client = SDB_OBJ_WRAPPER(user_data)->data;
126         if (sdb_dbi_client_connect(client)) {
127                 sdb_log(SDB_LOG_ERR, "Failed to connect to the storeconfigs DB.");
128                 return -1;
129         }
131         sdb_log(SDB_LOG_INFO, "Successfully connected to the storeconfigs DB.");
132         return 0;
133 } /* sdb_puppet_stcfg_init */
135 static int
136 sdb_puppet_stcfg_shutdown(sdb_object_t *user_data)
138         if (! user_data)
139                 return -1;
141         sdb_dbi_client_destroy(SDB_OBJ_WRAPPER(user_data)->data);
142         SDB_OBJ_WRAPPER(user_data)->data = NULL;
143         return 0;
144 } /* sdb_puppet_stcfg_shutdown */
146 static int
147 sdb_puppet_stcfg_collect(sdb_object_t *user_data)
149         sdb_dbi_client_t *client;
151         if (! user_data)
152                 return -1;
154         client = SDB_OBJ_WRAPPER(user_data)->data;
155         if (sdb_dbi_client_check_conn(client)) {
156                 sdb_log(SDB_LOG_ERR, "Connection to storeconfigs DB failed.");
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_log(SDB_LOG_ERR, "Failed to 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, "Failed to 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)
189         char *name = NULL;
191         sdb_object_t *user_data;
192         sdb_dbi_client_t *client;
193         sdb_dbi_options_t *options = NULL;
195         char *driver = NULL;
196         char *database = NULL;
198         int i;
200         if (oconfig_get_string(ci, &name)) {
201                 sdb_log(SDB_LOG_ERR, "Connection requires a single string argument\n"
202                                 "\tUsage: <Connection NAME>");
203                 return -1;
204         }
206         for (i = 0; i < ci->children_num; ++i) {
207                 oconfig_item_t *child = ci->children + i;
208                 char *key = NULL, *value = NULL;
210                 int status = 0;
212                 if (! strcasecmp(child->key, "DBAdapter")) {
213                         if (oconfig_get_string(child, &driver)) {
214                                 sdb_log(SDB_LOG_ERR, "DBAdapter requires a single string argument "
215                                                 "inside <Connection %s>\n\tUsage: DBAdapter NAME", name);
216                         }
217                         continue;
218                 }
219                 else if (! strcasecmp(child->key, "DBName")) {
220                         if (oconfig_get_string(child, &database)) {
221                                 sdb_log(SDB_LOG_ERR, "DBName requires a single string argument "
222                                                 "inside <Connection %s>\n\tUsage: DBName NAME", name);
223                         }
224                         continue;
225                 }
226                 else if (! strcasecmp(child->key, "DBServer")) {
227                         status = oconfig_get_string(child, &value);
228                         key = "host";
229                 }
230                 else if (! strcasecmp(child->key, "DBPort")) {
231                         status = oconfig_get_string(child, &value);
232                         key = "port";
233                 }
234                 else if (! strcasecmp(child->key, "DBUser")) {
235                         status = oconfig_get_string(child, &value);
236                         key = "username";
237                 }
238                 else if (! strcasecmp(child->key, "DBPassword")) {
239                         status = oconfig_get_string(child, &value);
240                         key = "password";
241                 }
242                 else if (! strcasecmp(child->key, "DBIOption")) {
243                         if ((child->values_num != 2)
244                                         || (child->values[0].type != OCONFIG_TYPE_STRING)
245                                         || (child->values[1].type != OCONFIG_TYPE_STRING)) {
246                                 sdb_log(SDB_LOG_ERR, "DBIOption requires exactly two string arguments "
247                                                 "inside <Connection %s>\n\tUsage: DBIOption KEY VALUE", name);
248                                 continue;
249                         }
251                         status = 0;
252                         key = child->values[0].value.string;
253                         value = child->values[1].value.string;
254                 }
255                 else {
256                         sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s' "
257                                         "inside <Connection %s>.", child->key, name);
258                         continue;
259                 }
261                 if (status) {
262                         sdb_log(SDB_LOG_ERR, "Option '%s' requires a single string argument "
263                                         "inside <Connection %s>\n\tUsage: DBAdapter NAME",
264                                         child->key, name);
265                         continue;
266                 }
268                 assert(key && value);
270                 if (! options) {
271                         if (! (options = sdb_dbi_options_create())) {
272                                 char errmsg[1024];
273                                 sdb_log(SDB_LOG_ERR, "Failed to create DBI options object: %s",
274                                                 sdb_strerror(errno, errmsg, sizeof(errmsg)));
275                                 continue;
276                         }
277                 }
279                 if (sdb_dbi_options_add(options, key, value)) {
280                         char errmsg[1024];
281                         sdb_log(SDB_LOG_ERR, "Failed to add option '%s': %s", key,
282                                         sdb_strerror(errno, errmsg, sizeof(errmsg)));
283                         continue;
284                 }
285         }
287         if (! driver) {
288                 sdb_log(SDB_LOG_ERR, "Connection '%s' " "missing the 'DBAdapter' option.", name);
289                 return -1;
290         }
291         if (! database) {
292                 sdb_log(SDB_LOG_ERR, "Connection '%s' missing the 'DBName' option.", name);
293                 return -1;
294         }
296         client = sdb_dbi_client_create(driver, database);
297         if (! client) {
298                 char errbuf[1024];
299                 sdb_log(SDB_LOG_ERR, "Failed to create DBI client: %s",
300                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
301                 return -1;
302         }
304         sdb_dbi_client_set_options(client, options);
306         user_data = sdb_object_create_wrapper("dbi-client", client,
307                         (void (*)(void *))sdb_dbi_client_destroy);
308         if (! user_data) {
309                 sdb_dbi_client_destroy(client);
310                 sdb_log(SDB_LOG_ERR, "Failed to allocate sdb_object_t");
311                 return -1;
312         }
314         sdb_plugin_register_init(name, sdb_puppet_stcfg_init, user_data);
315         sdb_plugin_register_shutdown(name, sdb_puppet_stcfg_shutdown,
316                         user_data);
317         sdb_plugin_register_collector(name, sdb_puppet_stcfg_collect,
318                         /* interval */ NULL, user_data);
320         /* pass control to the list */
321         sdb_object_deref(user_data);
322         return 0;
323 } /* sdb_puppet_stcfg_config_conn */
325 static int
326 sdb_puppet_stcfg_config(oconfig_item_t *ci)
328         int i;
330         if (! ci) /* nothing to do to deconfigure this plugin */
331                 return 0;
333         for (i = 0; i < ci->children_num; ++i) {
334                 oconfig_item_t *child = ci->children + i;
336                 if (! strcasecmp(child->key, "Connection"))
337                         sdb_puppet_stcfg_config_conn(child);
338                 else
339                         sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s'.", child->key);
340         }
341         return 0;
342 } /* sdb_puppet_stcfg_config */
344 int
345 sdb_module_init(sdb_plugin_info_t *info)
347         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
348                         "backend accessing the Puppet stored configuration database");
349         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
350                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
351         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
352         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
353         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
355         sdb_plugin_register_config(sdb_puppet_stcfg_config);
356         return 0;
357 } /* sdb_version_extra */
359 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */