Code

utils dbi + unixsock: Pass user-data object to data processing callbacks.
[sysdb.git] / src / backend / puppet-storeconfigs.c
1 /*
2  * syscollector - 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 "syscollector.h"
29 #include "core/plugin.h"
30 #include "core/store.h"
31 #include "utils/dbi.h"
32 #include "utils/string.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 SC_PLUGIN_MAGIC;
46 /*
47  * private helper functions
48  */
50 static int
51 sc_puppet_stcfg_get_data(sc_dbi_client_t __attribute__((unused)) *client,
52                 size_t n, sc_data_t *data,
53                 sc_object_t __attribute__((unused)) *user_data)
54 {
55         sc_host_t host = SC_HOST_INIT;
57         int status;
59         assert(n == 2);
60         assert((data[0].type == SC_TYPE_STRING)
61                         && (data[1].type == SC_TYPE_DATETIME));
63         host.host_name = strdup(data[0].data.string);
64         host.host_last_update = data[1].data.datetime;
66         status = sc_store_host(&host);
68         if (status < 0) {
69                 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
70                                 "host '%s'.\n", host.host_name);
71                 free(host.host_name);
72                 return -1;
73         }
74         else if (! status)
75                 fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
76                                 "(last update timestamp = %"PRIscTIME").\n",
77                                 host.host_name, host.host_last_update);
78         free(host.host_name);
79         return 0;
80 } /* sc_puppet_stcfg_get_data */
82 /*
83  * plugin API
84  */
86 static int
87 sc_puppet_stcfg_init(sc_object_t *user_data)
88 {
89         sc_dbi_client_t *client;
91         if (! user_data)
92                 return -1;
94         client = SC_OBJ_WRAPPER(user_data)->data;
95         if (sc_dbi_client_connect(client)) {
96                 fprintf(stderr, "puppet storeconfigs backend: "
97                                 "Failed to connect to the storeconfigs DB.\n");
98                 return -1;
99         }
101         fprintf(stderr, "puppet storeconfigs backend: Successfully "
102                         "connected to the storeconfigs DB.\n");
103         return 0;
104 } /* sc_collectd_init */
106 static int
107 sc_puppet_stcfg_collect(sc_object_t *user_data)
109         sc_dbi_client_t *client;
111         if (! user_data)
112                 return -1;
114         client = SC_OBJ_WRAPPER(user_data)->data;
115         if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
116                                 sc_puppet_stcfg_get_data, NULL, /* #columns = */ 2,
117                                 /* col types = */ SC_TYPE_STRING, SC_TYPE_DATETIME)) {
118                 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
119                                 "hosts from the storeconfigs DB.\n");
120                 return -1;
121         }
122         return 0;
123 } /* sc_collectd_collect */
125 static int
126 sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
128         char *name = NULL;
129         char cb_name[1024];
131         sc_object_t *user_data;
132         sc_dbi_client_t *client;
133         sc_dbi_options_t *options = NULL;
135         char *driver = NULL;
136         char *database = NULL;
138         int i;
140         if (oconfig_get_string(ci, &name)) {
141                 fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
142                                 "single string argument\n\tUsage: <Connection NAME>\n");
143                 return -1;
144         }
146         for (i = 0; i < ci->children_num; ++i) {
147                 oconfig_item_t *child = ci->children + i;
148                 char *key = NULL, *value = NULL;
150                 int status = 0;
152                 if (! strcasecmp(child->key, "DBAdapter")) {
153                         if (oconfig_get_string(child, &driver)) {
154                                 fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
155                                                 "requires a single string argument inside "
156                                                 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
157                                                 name);
158                         }
159                         continue;
160                 }
161                 else if (! strcasecmp(child->key, "DBName")) {
162                         if (oconfig_get_string(child, &database)) {
163                                 fprintf(stderr, "puppet storeconfigs backend: DBName"
164                                                 "requires a single string argument inside "
165                                                 "<Connection %s>\n\tUsage: DBName NAME\n",
166                                                 name);
167                         }
168                         continue;
169                 }
170                 else if (! strcasecmp(child->key, "DBServer")) {
171                         status = oconfig_get_string(child, &value);
172                         key = "host";
173                 }
174                 else if (! strcasecmp(child->key, "DBPort")) {
175                         status = oconfig_get_string(child, &value);
176                         key = "port";
177                 }
178                 else if (! strcasecmp(child->key, "DBUser")) {
179                         status = oconfig_get_string(child, &value);
180                         key = "username";
181                 }
182                 else if (! strcasecmp(child->key, "DBPassword")) {
183                         status = oconfig_get_string(child, &value);
184                         key = "password";
185                 }
186                 else if (! strcasecmp(child->key, "DBIOption")) {
187                         if ((child->values_num != 2)
188                                         || (child->values[0].type != OCONFIG_TYPE_STRING)
189                                         || (child->values[1].type != OCONFIG_TYPE_STRING)) {
190                                 fprintf(stderr, "puppet storeconfigs backend: DBIOption "
191                                                 "requires exactly two string arguments inside "
192                                                 "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
193                                                 name);
194                                 continue;
195                         }
197                         status = 0;
198                         key = child->values[0].value.string;
199                         value = child->values[1].value.string;
200                 }
201                 else {
202                         fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
203                                         "config option '%s' inside <Connection %s>.\n",
204                                         child->key, name);
205                         continue;
206                 }
208                 if (status) {
209                         fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
210                                         "requires a single string argument inside "
211                                         "<Connection %s>\n\tUsage: DBAdapter NAME\n",
212                                         child->key, name);
213                         continue;
214                 }
216                 assert(key && value);
218                 if (! options) {
219                         if (! (options = sc_dbi_options_create())) {
220                                 char errmsg[1024];
221                                 fprintf(stderr, "puppet storeconfigs backend: Failed to "
222                                                 "create DBI options object: %s\n",
223                                                 sc_strerror(errno, errmsg, sizeof(errmsg)));
224                                 continue;
225                         }
226                 }
228                 if (sc_dbi_options_add(options, key, value)) {
229                         char errmsg[1024];
230                         fprintf(stderr, "puppet storeconfigs backend: Failed to add "
231                                         "option '%s': %s\n", key,
232                                         sc_strerror(errno, errmsg, sizeof(errmsg)));
233                         continue;
234                 }
235         }
237         if (! driver) {
238                 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
239                                 "missing the 'DBAdapter' option.\n", name);
240                 return -1;
241         }
242         if (! database) {
243                 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
244                                 "missing the 'DBName' option.\n", name);
245                 return -1;
246         }
248         snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
249         cb_name[sizeof(cb_name) - 1] = '\0';
251         client = sc_dbi_client_create(driver, database);
252         if (! client) {
253                 char errbuf[1024];
254                 fprintf(stderr, "puppet storeconfigs backend: "
255                                 "Failed to create DBI client: %s\n",
256                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
257                 return -1;
258         }
260         sc_dbi_client_set_options(client, options);
262         user_data = sc_object_create_wrapper(client,
263                         (void (*)(void *))sc_dbi_client_destroy);
264         if (! user_data) {
265                 sc_dbi_client_destroy(client);
266                 fprintf(stderr, "puppet storeconfigs backend: "
267                                 "Failed to allocate sc_object_t\n");
268                 return -1;
269         }
271         sc_plugin_register_init(cb_name, sc_puppet_stcfg_init, user_data);
272         sc_plugin_register_collector(cb_name, sc_puppet_stcfg_collect,
273                         /* interval */ NULL, user_data);
275         /* pass control to the list */
276         sc_object_deref(user_data);
277         return 0;
278 } /* sc_puppet_stcfg_config_conn */
280 static int
281 sc_puppet_stcfg_config(oconfig_item_t *ci)
283         int i;
285         for (i = 0; i < ci->children_num; ++i) {
286                 oconfig_item_t *child = ci->children + i;
288                 if (! strcasecmp(child->key, "Connection"))
289                         sc_puppet_stcfg_config_conn(child);
290                 else
291                         fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
292                                         "config option '%s'.\n", child->key);
293         }
294         return 0;
295 } /* sc_puppet_stcfg_config */
297 int
298 sc_module_init(sc_plugin_info_t *info)
300         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "puppet-storeconfigs");
301         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
302                         "backend accessing the Puppet stored configuration database");
303         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
304                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
305         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
306         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
307         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
309         if (dbi_initialize(/* driver dir = */ NULL) < 0) {
310                 fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
311                                 "DBI; possibly you don't have any drivers installed.\n");
312                 return -1;
313         }
315         sc_plugin_register_config("puppet-storeconfigs", sc_puppet_stcfg_config);
316         return 0;
317 } /* sc_version_extra */
319 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */