5ebd16f61b8dd5d0fd77cd93076833260a2d5c7b
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_dbi_data_t *data)
53 {
54 sc_host_t host = SC_HOST_INIT;
56 assert(n == 2);
57 assert((data[0].type == DBI_TYPE_STRING)
58 && (data[1].type == DBI_TYPE_DATETIME));
60 host.host_name = strdup(data[0].data.string);
61 host.host_last_update = data[1].data.datetime;
63 if (sc_store_host(&host)) {
64 fprintf(stderr, "puppet storeconfigs backend: Failed to store/update "
65 "host '%s'.\n", host.host_name);
66 free(host.host_name);
67 return -1;
68 }
70 fprintf(stderr, "puppet storeconfigs backend: Added/updated host '%s' "
71 "(last update timestamp = %"PRIscTIME").\n",
72 host.host_name, host.host_last_update);
73 free(host.host_name);
74 return 0;
75 } /* sc_puppet_stcfg_get_data */
77 /*
78 * plugin API
79 */
81 static int
82 sc_puppet_stcfg_init(sc_object_t *user_data)
83 {
84 sc_dbi_client_t *client;
86 if (! user_data)
87 return -1;
89 client = SC_OBJ_WRAPPER(user_data)->data;
90 if (sc_dbi_client_connect(client)) {
91 fprintf(stderr, "puppet storeconfigs backend: "
92 "Failed to connect to the storeconfigs DB.\n");
93 return -1;
94 }
96 fprintf(stderr, "puppet storeconfigs backend: Successfully "
97 "connected to the storeconfigs DB.\n");
98 return 0;
99 } /* sc_collectd_init */
101 static int
102 sc_puppet_stcfg_collect(sc_object_t *user_data)
103 {
104 sc_dbi_client_t *client;
106 if (! user_data)
107 return -1;
109 client = SC_OBJ_WRAPPER(user_data)->data;
110 if (sc_dbi_exec_query(client, "SELECT name, updated_at FROM hosts;",
111 sc_puppet_stcfg_get_data, /* #columns = */ 2,
112 /* col types = */ DBI_TYPE_STRING, DBI_TYPE_DATETIME)) {
113 fprintf(stderr, "puppet storeconfigs backend: Failed to retrieve "
114 "hosts from the storeconfigs DB.\n");
115 return -1;
116 }
117 return 0;
118 } /* sc_collectd_collect */
120 static int
121 sc_puppet_stcfg_config_conn(oconfig_item_t *ci)
122 {
123 char *name = NULL;
124 char cb_name[1024];
126 sc_object_t *user_data;
127 sc_dbi_client_t *client;
128 sc_dbi_options_t *options = NULL;
130 char *driver = NULL;
131 char *database = NULL;
133 int i;
135 if (oconfig_get_string(ci, &name)) {
136 fprintf(stderr, "puppet storeconfigs backend: Connection requires a "
137 "single string argument\n\tUsage: <Connection NAME>\n");
138 return -1;
139 }
141 for (i = 0; i < ci->children_num; ++i) {
142 oconfig_item_t *child = ci->children + i;
143 char *key = NULL, *value = NULL;
145 int status = 0;
147 if (! strcasecmp(child->key, "DBAdapter")) {
148 if (oconfig_get_string(child, &driver)) {
149 fprintf(stderr, "puppet storeconfigs backend: DBAdapter "
150 "requires a single string argument inside "
151 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
152 name);
153 }
154 continue;
155 }
156 else if (! strcasecmp(child->key, "DBName")) {
157 if (oconfig_get_string(child, &database)) {
158 fprintf(stderr, "puppet storeconfigs backend: DBName"
159 "requires a single string argument inside "
160 "<Connection %s>\n\tUsage: DBName NAME\n",
161 name);
162 }
163 continue;
164 }
165 else if (! strcasecmp(child->key, "DBServer")) {
166 status = oconfig_get_string(child, &value);
167 key = "host";
168 }
169 else if (! strcasecmp(child->key, "DBPort")) {
170 status = oconfig_get_string(child, &value);
171 key = "port";
172 }
173 else if (! strcasecmp(child->key, "DBUser")) {
174 status = oconfig_get_string(child, &value);
175 key = "username";
176 }
177 else if (! strcasecmp(child->key, "DBPassword")) {
178 status = oconfig_get_string(child, &value);
179 key = "password";
180 }
181 else if (! strcasecmp(child->key, "DBIOption")) {
182 if ((child->values_num != 2)
183 || (child->values[0].type != OCONFIG_TYPE_STRING)
184 || (child->values[1].type != OCONFIG_TYPE_STRING)) {
185 fprintf(stderr, "puppet storeconfigs backend: DBIOption "
186 "requires exactly two string arguments inside "
187 "<Connection %s>\n\tUsage: DBIOption KEY VALUE\n",
188 name);
189 continue;
190 }
192 status = 0;
193 key = child->values[0].value.string;
194 value = child->values[1].value.string;
195 }
196 else {
197 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
198 "config option '%s' inside <Connection %s>.\n",
199 child->key, name);
200 continue;
201 }
203 if (status) {
204 fprintf(stderr, "puppet storeconfigs backend: Option '%s' "
205 "requires a single string argument inside "
206 "<Connection %s>\n\tUsage: DBAdapter NAME\n",
207 child->key, name);
208 continue;
209 }
211 assert(key && value);
213 if (! options) {
214 if (! (options = sc_dbi_options_create())) {
215 char errmsg[1024];
216 fprintf(stderr, "puppet storeconfigs backend: Failed to "
217 "create DBI options object: %s\n",
218 sc_strerror(errno, errmsg, sizeof(errmsg)));
219 continue;
220 }
221 }
223 if (sc_dbi_options_add(options, key, value)) {
224 char errmsg[1024];
225 fprintf(stderr, "puppet storeconfigs backend: Failed to add "
226 "option '%s': %s\n", key,
227 sc_strerror(errno, errmsg, sizeof(errmsg)));
228 continue;
229 }
230 }
232 if (! driver) {
233 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
234 "missing the 'DBAdapter' option.\n", name);
235 return -1;
236 }
237 if (! database) {
238 fprintf(stderr, "puppet storeconfigs backend: Connection '%s' "
239 "missing the 'DBName' option.\n", name);
240 return -1;
241 }
243 snprintf(cb_name, sizeof(cb_name), "puppet-storeconfigs-%s", name);
244 cb_name[sizeof(cb_name) - 1] = '\0';
246 client = sc_dbi_client_create(driver, database);
247 if (! client) {
248 char errbuf[1024];
249 fprintf(stderr, "puppet storeconfigs backend: "
250 "Failed to create DBI client: %s\n",
251 sc_strerror(errno, errbuf, sizeof(errbuf)));
252 return -1;
253 }
255 sc_dbi_client_set_options(client, options);
257 user_data = sc_object_create_wrapper(client,
258 (void (*)(void *))sc_dbi_client_destroy);
259 if (! user_data) {
260 sc_dbi_client_destroy(client);
261 fprintf(stderr, "puppet storeconfigs backend: "
262 "Failed to allocate sc_object_t\n");
263 return -1;
264 }
266 sc_plugin_register_init(cb_name, sc_puppet_stcfg_init, user_data);
267 sc_plugin_register_collector(cb_name, sc_puppet_stcfg_collect,
268 /* interval */ NULL, user_data);
270 /* pass control to the list */
271 sc_object_deref(user_data);
272 return 0;
273 } /* sc_puppet_stcfg_config_conn */
275 static int
276 sc_puppet_stcfg_config(oconfig_item_t *ci)
277 {
278 int i;
280 for (i = 0; i < ci->children_num; ++i) {
281 oconfig_item_t *child = ci->children + i;
283 if (! strcasecmp(child->key, "Connection"))
284 sc_puppet_stcfg_config_conn(child);
285 else
286 fprintf(stderr, "puppet storeconfigs backend: Ignoring unknown "
287 "config option '%s'.\n", child->key);
288 }
289 return 0;
290 } /* sc_puppet_stcfg_config */
292 int
293 sc_module_init(sc_plugin_info_t *info)
294 {
295 sc_plugin_set_info(info, SC_PLUGIN_INFO_NAME, "puppet-storeconfigs");
296 sc_plugin_set_info(info, SC_PLUGIN_INFO_DESC,
297 "backend accessing the Puppet stored configuration database");
298 sc_plugin_set_info(info, SC_PLUGIN_INFO_COPYRIGHT,
299 "Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>");
300 sc_plugin_set_info(info, SC_PLUGIN_INFO_LICENSE, "BSD");
301 sc_plugin_set_info(info, SC_PLUGIN_INFO_VERSION, SC_VERSION);
302 sc_plugin_set_info(info, SC_PLUGIN_INFO_PLUGIN_VERSION, SC_VERSION);
304 if (dbi_initialize(/* driver dir = */ NULL) < 0) {
305 fprintf(stderr, "puppet storeconfigs backend: failed to initialize "
306 "DBI; possibly you don't have any drivers installed.\n");
307 return -1;
308 }
310 sc_plugin_register_config("puppet-storeconfigs", sc_puppet_stcfg_config);
311 return 0;
312 } /* sc_version_extra */
314 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */