Code

Puppet storeconfigs backend: Query facts and store them as host attributes.
[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_hosts(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_hosts */
82 static int
83 sc_puppet_stcfg_get_attrs(sc_dbi_client_t __attribute__((unused)) *client,
84                 size_t n, sc_data_t *data,
85                 sc_object_t __attribute__((unused)) *user_data)
86 {
87         sc_attribute_t attr = SC_ATTR_INIT;
89         int status;
91         assert(n == 4);
92         assert((data[0].type == SC_TYPE_STRING)
93                         && (data[1].type == SC_TYPE_STRING)
94                         && (data[2].type == SC_TYPE_STRING)
95                         && (data[3].type == SC_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 = sc_store_attribute(&attr);
104         if (status < 0) {
105                 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
106                                 "host attribute '%s' for host '%s'.\n", attr.attr_name,
107                                 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 } /* sc_puppet_stcfg_get_attrs */
120 /*
121  * plugin API
122  */
124 static int
125 sc_puppet_stcfg_init(sc_object_t *user_data)
127         sc_dbi_client_t *client;
129         if (! user_data)
130                 return -1;
132         client = SC_OBJ_WRAPPER(user_data)->data;
133         if (sc_dbi_client_connect(client)) {
134                 fprintf(stderr, "puppet storeconfigs backend: "
135                                 "Failed to connect to the storeconfigs DB.\n");
136                 return -1;
137         }
139         fprintf(stderr, "puppet storeconfigs backend: Successfully "
140                         "connected to the storeconfigs DB.\n");
141         return 0;
142 } /* sc_collectd_init */
144 static int
145 sc_puppet_stcfg_collect(sc_object_t *user_data)
147         sc_dbi_client_t *client;
149         if (! user_data)
150                 return -1;
152         client = SC_OBJ_WRAPPER(user_data)->data;
153         if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
154                                 sc_puppet_stcfg_get_hosts, NULL, /* #columns = */ 2,
155                                 /* col types = */ SC_TYPE_STRING, SC_TYPE_DATETIME)) {
156                 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
157                                 "hosts from the storeconfigs DB.\n");
158                 return -1;
159         }
161         if (sc_dbi_exec_query(client, "SELECT "
162                                         "hosts.name AS hostname, "
163                                         "fact_names.name AS name, "
164                                         "fact_values.value AS value, "
165                                         "fact_values.updated_at AS updated_at "
166                                 "FROM fact_values "
167                                 "INNER JOIN hosts "
168                                         "ON fact_values.host_id = hosts.id "
169                                 "INNER JOIN fact_names "
170                                         "ON fact_values.fact_name_id = fact_names.id;",
171                                 sc_puppet_stcfg_get_attrs, NULL, /* #columns = */ 4,
172                                 /* col types = */ SC_TYPE_STRING, SC_TYPE_STRING,
173                                 SC_TYPE_STRING, SC_TYPE_DATETIME)) {
174                 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
175                                 "host attributes from the storeconfigs DB.\n");
176                 return -1;
177         }
178         return 0;
179 } /* sc_collectd_collect */
181 static int
182 sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
184         char *name = NULL;
185         char cb_name[1024];
187         sc_object_t *user_data;
188         sc_dbi_client_t *client;
189         sc_dbi_options_t *options = NULL;
191         char *driver = NULL;
192         char *database = NULL;
194         int i;
196         if (oconfig_get_string(ci, &name)) {
197                 fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
198                                 "single string argument\n\tUsage: <Connection NAME>\n");
199                 return -1;
200         }
202         for (i = 0; i < ci->children_num; ++i) {
203                 oconfig_item_t *child = ci->children + i;
204                 char *key = NULL, *value = NULL;
206                 int status = 0;
208                 if (! strcasecmp(child->key, "DBAdapter")) {
209                         if (oconfig_get_string(child, &driver)) {
210                                 fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
211                                                 "requires a single string argument inside "
212                                                 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
213                                                 name);
214                         }
215                         continue;
216                 }
217                 else if (! strcasecmp(child->key, "DBName")) {
218                         if (oconfig_get_string(child, &database)) {
219                                 fprintf(stderr, "puppet storeconfigs backend: DBName"
220                                                 "requires a single string argument inside "
221                                                 "<Connection %s>\n\tUsage: DBName NAME\n",
222                                                 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                                 fprintf(stderr, "puppet storeconfigs backend: DBIOption "
247                                                 "requires exactly two string arguments inside "
248                                                 "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
249                                                 name);
250                                 continue;
251                         }
253                         status = 0;
254                         key = child->values[0].value.string;
255                         value = child->values[1].value.string;
256                 }
257                 else {
258                         fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
259                                         "config option '%s' inside <Connection %s>.\n",
260                                         child->key, name);
261                         continue;
262                 }
264                 if (status) {
265                         fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
266                                         "requires a single string argument inside "
267                                         "<Connection %s>\n\tUsage: DBAdapter NAME\n",
268                                         child->key, name);
269                         continue;
270                 }
272                 assert(key && value);
274                 if (! options) {
275                         if (! (options = sc_dbi_options_create())) {
276                                 char errmsg[1024];
277                                 fprintf(stderr, "puppet storeconfigs backend: Failed to "
278                                                 "create DBI options object: %s\n",
279                                                 sc_strerror(errno, errmsg, sizeof(errmsg)));
280                                 continue;
281                         }
282                 }
284                 if (sc_dbi_options_add(options, key, value)) {
285                         char errmsg[1024];
286                         fprintf(stderr, "puppet storeconfigs backend: Failed to add "
287                                         "option '%s': %s\n", key,
288                                         sc_strerror(errno, errmsg, sizeof(errmsg)));
289                         continue;
290                 }
291         }
293         if (! driver) {
294                 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
295                                 "missing the 'DBAdapter' option.\n", name);
296                 return -1;
297         }
298         if (! database) {
299                 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
300                                 "missing the 'DBName' option.\n", name);
301                 return -1;
302         }
304         snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
305         cb_name[sizeof(cb_name) - 1] = '\0';
307         client = sc_dbi_client_create(driver, database);
308         if (! client) {
309                 char errbuf[1024];
310                 fprintf(stderr, "puppet storeconfigs backend: "
311                                 "Failed to create DBI client: %s\n",
312                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
313                 return -1;
314         }
316         sc_dbi_client_set_options(client, options);
318         user_data = sc_object_create_wrapper(client,
319                         (void (*)(void *))sc_dbi_client_destroy);
320         if (! user_data) {
321                 sc_dbi_client_destroy(client);
322                 fprintf(stderr, "puppet storeconfigs backend: "
323                                 "Failed to allocate sc_object_t\n");
324                 return -1;
325         }
327         sc_plugin_register_init(cb_name, sc_puppet_stcfg_init, user_data);
328         sc_plugin_register_collector(cb_name, sc_puppet_stcfg_collect,
329                         /* interval */ NULL, user_data);
331         /* pass control to the list */
332         sc_object_deref(user_data);
333         return 0;
334 } /* sc_puppet_stcfg_config_conn */
336 static int
337 sc_puppet_stcfg_config(oconfig_item_t *ci)
339         int i;
341         for (i = 0; i < ci->children_num; ++i) {
342                 oconfig_item_t *child = ci->children + i;
344                 if (! strcasecmp(child->key, "Connection"))
345                         sc_puppet_stcfg_config_conn(child);
346                 else
347                         fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
348                                         "config option '%s'.\n", child->key);
349         }
350         return 0;
351 } /* sc_puppet_stcfg_config */
353 int
354 sc_module_init(sc_plugin_info_t *info)
356         sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "puppet-storeconfigs");
357         sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
358                         "backend accessing the Puppet stored configuration database");
359         sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
360                         "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
361         sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
362         sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
363         sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
365         if (dbi_initialize(/* driver dir = */ NULL) < 0) {
366                 fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
367                                 "DBI; possibly you don't have any drivers installed.\n");
368                 return -1;
369         }
371         sc_plugin_register_config("puppet-storeconfigs", sc_puppet_stcfg_config);
372         return 0;
373 } /* sc_version_extra */
375 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */