1 /*
2 * SysDB - src/plugins/backend/store/network.c
3 * Copyright (C) 2015 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 #if HAVE_CONFIG_H
29 # include "config.h"
30 #endif
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "client/sock.h"
35 #include "utils/error.h"
36 #include "utils/proto.h"
37 #include "utils/os.h"
38 #include "utils/ssl.h"
40 #include "liboconfig/utils.h"
42 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51 * private data types
52 */
54 typedef struct {
55 sdb_client_t *client;
56 char *addr;
57 char *username;
58 sdb_ssl_options_t ssl_opts;
59 } user_data_t;
60 #define UD(obj) SDB_OBJ_WRAPPER(obj)->data
62 static void
63 user_data_destroy(void *obj)
64 {
65 user_data_t *ud = obj;
67 if (! ud)
68 return;
70 if (ud->client)
71 sdb_client_destroy(ud->client);
72 ud->client = NULL;
73 if (ud->addr)
74 free(ud->addr);
75 ud->addr = NULL;
76 if (ud->username)
77 free(ud->username);
78 ud->username = NULL;
80 sdb_ssl_free_options(&ud->ssl_opts);
82 free(ud);
83 } /* user_data_destroy */
85 /*
86 * store writer implementation
87 */
89 static int
90 store_rpc(user_data_t *ud, const char *msg, size_t msg_len)
91 {
92 sdb_strbuf_t *buf = sdb_strbuf_create(128);
93 uint32_t rstatus = 0;
94 ssize_t status;
96 if (sdb_client_eof(ud->client)) {
97 sdb_client_close(ud->client);
98 if (sdb_client_connect(ud->client, ud->username)) {
99 sdb_log(SDB_LOG_ERR, "store::network: Failed to reconnect "
100 "to SysDB at %s as user %s", ud->addr, ud->username);
101 return -1;
102 }
103 sdb_log(SDB_LOG_INFO, "store::network: Successfully reconnected "
104 "to SysDB at %s as user %s", ud->addr, ud->username);
105 }
107 status = sdb_client_rpc(ud->client, SDB_CONNECTION_STORE,
108 (uint32_t)msg_len, msg, &rstatus, buf);
109 if (status < 0)
110 sdb_log(SDB_LOG_ERR, "store::network: %s", sdb_strbuf_string(buf));
111 else if (rstatus != SDB_CONNECTION_OK) {
112 sdb_log(SDB_LOG_ERR, "store::network: Failed to send object: %s",
113 sdb_strbuf_string(buf));
114 status = -1;
115 }
117 sdb_strbuf_destroy(buf);
118 if (status < 0)
119 return -1;
120 return 0;
121 } /* store_rpc */
123 static int
124 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
125 {
126 sdb_proto_host_t host = { last_update, name };
127 size_t len = sdb_proto_marshal_host(NULL, 0, &host);
128 char buf[len];
130 sdb_proto_marshal_host(buf, len, &host);
131 return store_rpc(UD(user_data), buf, len);
132 } /* store_host */
134 static int
135 store_service(const char *hostname, const char *name, sdb_time_t last_update,
136 sdb_object_t *user_data)
137 {
138 sdb_proto_service_t svc = { last_update, hostname, name };
139 ssize_t len = sdb_proto_marshal_service(NULL, 0, &svc);
140 char buf[len];
142 sdb_proto_marshal_service(buf, len, &svc);
143 return store_rpc(UD(user_data), buf, len);
144 } /* store_service */
146 static int
147 store_metric(const char *hostname, const char *name,
148 sdb_metric_store_t *store, sdb_time_t last_update,
149 sdb_object_t *user_data)
150 {
151 sdb_proto_metric_t metric = {
152 last_update, hostname, name,
153 store ? store->type : NULL, store ? store->id : NULL,
154 };
155 size_t len = sdb_proto_marshal_metric(NULL, 0, &metric);
156 char buf[len];
158 sdb_proto_marshal_metric(buf, len, &metric);
159 return store_rpc(UD(user_data), buf, len);
160 } /* store_metric */
162 static int
163 store_attr(const char *hostname, const char *key, const sdb_data_t *value,
164 sdb_time_t last_update, sdb_object_t *user_data)
165 {
166 sdb_proto_attribute_t attr = {
167 last_update, SDB_HOST, NULL, hostname, key, *value,
168 };
169 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
170 char buf[len];
172 sdb_proto_marshal_attribute(buf, len, &attr);
173 return store_rpc(UD(user_data), buf, len);
174 } /* store_attr */
176 static int
177 store_service_attr(const char *hostname, const char *service,
178 const char *key, const sdb_data_t *value, sdb_time_t last_update,
179 sdb_object_t *user_data)
180 {
181 sdb_proto_attribute_t attr = {
182 last_update, SDB_SERVICE, hostname, service, key, *value,
183 };
184 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
185 char buf[len];
187 sdb_proto_marshal_attribute(buf, len, &attr);
188 return store_rpc(UD(user_data), buf, len);
189 } /* store_service_attr */
191 static int
192 store_metric_attr(const char *hostname, const char *metric,
193 const char *key, const sdb_data_t *value, sdb_time_t last_update,
194 sdb_object_t *user_data)
195 {
196 sdb_proto_attribute_t attr = {
197 last_update, SDB_METRIC, hostname, metric, key, *value,
198 };
199 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
200 char buf[len];
202 sdb_proto_marshal_attribute(buf, len, &attr);
203 return store_rpc(UD(user_data), buf, len);
204 } /* store_metric_attr */
206 static sdb_store_writer_t store_impl = {
207 store_host, store_service, store_metric,
208 store_attr, store_service_attr, store_metric_attr,
209 };
211 /*
212 * plugin API
213 */
215 static int
216 store_init(sdb_object_t *user_data)
217 {
218 user_data_t *ud;
220 if (! user_data)
221 return -1;
223 ud = SDB_OBJ_WRAPPER(user_data)->data;
224 if (sdb_client_connect(ud->client, ud->username)) {
225 sdb_log(SDB_LOG_ERR, "store::network: Failed to connect "
226 "to SysDB at %s as user %s", ud->addr, ud->username);
227 return -1;
228 }
230 sdb_log(SDB_LOG_INFO, "store::network: Successfully connected "
231 "to SysDB at %s as user %s", ud->addr, ud->username);
232 return 0;
233 } /* store_init */
235 static int
236 store_config_server(oconfig_item_t *ci)
237 {
238 sdb_object_t *user_data;
239 user_data_t *ud;
240 int ret = 0;
241 int i;
243 ud = calloc(1, sizeof(*ud));
244 if (! ud) {
245 char errbuf[1024];
246 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
247 "a user-data object: %s",
248 sdb_strerror(errno, errbuf, sizeof(errbuf)));
249 return -1;
250 }
252 if (oconfig_get_string(ci, &ud->addr)) {
253 sdb_log(SDB_LOG_ERR, "store::network: Server requires "
254 "a single string argument\n\tUsage: <Server ADDRESS>");
255 user_data_destroy(ud);
256 return -1;
257 }
258 ud->addr = strdup(ud->addr);
259 if (! ud->addr) {
260 sdb_log(SDB_LOG_ERR, "store::network: Failed to duplicate "
261 "a string");
262 user_data_destroy(ud);
263 return -1;
264 }
266 ud->client = sdb_client_create(ud->addr);
267 if (! ud->client) {
268 char errbuf[1024];
269 sdb_log(SDB_LOG_ERR, "store::network: Failed to create client "
270 "connecting to '%s': %s", ud->addr,
271 sdb_strerror(errno, errbuf, sizeof(errbuf)));
272 user_data_destroy(ud);
273 return -1;
274 }
276 for (i = 0; i < ci->children_num; ++i) {
277 oconfig_item_t *child = ci->children + i;
278 char *tmp = NULL;
280 if (! strcasecmp(child->key, "Username")) {
281 if (oconfig_get_string(child, &tmp)) {
282 ret = -1;
283 break;
284 }
285 ud->username = strdup(tmp);
286 }
287 else if (! strcasecmp(child->key, "SSLCertificate")) {
288 if (oconfig_get_string(child, &tmp)) {
289 ret = -1;
290 break;
291 }
292 ud->ssl_opts.cert_file = strdup(tmp);
293 }
294 else if (! strcasecmp(child->key, "SSLCertificateKey")) {
295 if (oconfig_get_string(child, &tmp)) {
296 ret = -1;
297 break;
298 }
299 ud->ssl_opts.key_file = strdup(tmp);
300 }
301 else if (! strcasecmp(child->key, "SSLCACertificates")) {
302 if (oconfig_get_string(child, &tmp)) {
303 ret = -1;
304 break;
305 }
306 ud->ssl_opts.ca_file = strdup(tmp);
307 }
308 else
309 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
310 "unknown config option '%s' inside <Server %s>.",
311 child->key, ud->addr);
312 }
314 if (ret) {
315 user_data_destroy(ud);
316 return ret;
317 }
318 if (! ud->username)
319 ud->username = sdb_get_current_user();
321 if (sdb_client_set_ssl_options(ud->client, &ud->ssl_opts)) {
322 sdb_log(SDB_LOG_ERR, "store::network: Failed to apply SSL options");
323 user_data_destroy(ud);
324 return -1;
325 }
327 user_data = sdb_object_create_wrapper("store-network-userdata", ud,
328 user_data_destroy);
329 if (! user_data) {
330 char errbuf[1024];
331 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
332 "a user-data wrapper object: %s",
333 sdb_strerror(errno, errbuf, sizeof(errbuf)));
334 user_data_destroy(ud);
335 return -1;
336 }
338 sdb_plugin_register_init(ud->addr, store_init, user_data);
339 sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
340 sdb_object_deref(user_data);
341 return 0;
342 } /* store_config_server */
344 static int
345 store_config(oconfig_item_t *ci)
346 {
347 int i;
349 if (! ci) /* nothing to do to deconfigure this plugin */
350 return 0;
352 for (i = 0; i < ci->children_num; ++i) {
353 oconfig_item_t *child = ci->children + i;
355 if (! strcasecmp(child->key, "Server"))
356 store_config_server(child);
357 else
358 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
359 "unknown config option '%s'.", child->key);
360 }
361 return 0;
362 } /* store_config */
364 int
365 sdb_module_init(sdb_plugin_info_t *info)
366 {
367 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
368 "send stored objects to a remote SysDB instance");
369 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
370 "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
371 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
372 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
373 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
375 sdb_plugin_register_config(store_config);
376 return 0;
377 } /* sdb_module_init */
379 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */