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) ((user_data_t *)(obj))
61 #define CLIENT(obj) UD(SDB_OBJ_WRAPPER(obj)->data)->client
63 static void
64 user_data_destroy(void *obj)
65 {
66 user_data_t *ud = UD(obj);
68 if (! ud)
69 return;
71 if (ud->client)
72 sdb_client_destroy(ud->client);
73 ud->client = NULL;
74 if (ud->addr)
75 free(ud->addr);
76 ud->addr = NULL;
77 if (ud->username)
78 free(ud->username);
79 ud->username = NULL;
81 sdb_ssl_free_options(&ud->ssl_opts);
83 free(ud);
84 } /* user_data_destroy */
86 /*
87 * store writer implementation
88 */
90 static int
91 store_rpc(sdb_client_t *client, const char *msg, size_t msg_len)
92 {
93 sdb_strbuf_t *buf = sdb_strbuf_create(128);
94 uint32_t rstatus = 0;
95 ssize_t status;
97 status = sdb_client_rpc(client, SDB_CONNECTION_STORE,
98 (uint32_t)msg_len, msg, &rstatus, buf);
99 if (status < 0)
100 sdb_log(SDB_LOG_ERR, "store::network: %s", sdb_strbuf_string(buf));
101 else if (rstatus != SDB_CONNECTION_OK) {
102 sdb_log(SDB_LOG_ERR, "store::network: Failed to send object: %s",
103 sdb_strbuf_string(buf));
104 status = -1;
105 }
107 sdb_strbuf_destroy(buf);
108 if (status < 0)
109 return -1;
110 return 0;
111 } /* store_rpc */
113 static int
114 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
115 {
116 sdb_proto_host_t host = { last_update, name };
117 size_t len = sdb_proto_marshal_host(NULL, 0, &host);
118 char buf[len];
120 sdb_proto_marshal_host(buf, len, &host);
121 return store_rpc(CLIENT(user_data), buf, len);
122 } /* store_host */
124 static int
125 store_service(const char *hostname, const char *name, sdb_time_t last_update,
126 sdb_object_t *user_data)
127 {
128 sdb_proto_service_t svc = { last_update, hostname, name };
129 ssize_t len = sdb_proto_marshal_service(NULL, 0, &svc);
130 char buf[len];
132 sdb_proto_marshal_service(buf, len, &svc);
133 return store_rpc(CLIENT(user_data), buf, len);
134 } /* store_service */
136 static int
137 store_metric(const char *hostname, const char *name,
138 sdb_metric_store_t *store, sdb_time_t last_update,
139 sdb_object_t *user_data)
140 {
141 sdb_proto_metric_t metric = {
142 last_update, hostname, name,
143 store ? store->type : NULL, store ? store->id : NULL,
144 };
145 size_t len = sdb_proto_marshal_metric(NULL, 0, &metric);
146 char buf[len];
148 sdb_proto_marshal_metric(buf, len, &metric);
149 return store_rpc(CLIENT(user_data), buf, len);
150 } /* store_metric */
152 static int
153 store_attr(const char *hostname, const char *key, const sdb_data_t *value,
154 sdb_time_t last_update, sdb_object_t *user_data)
155 {
156 sdb_proto_attribute_t attr = {
157 last_update, SDB_HOST, NULL, hostname, key, *value,
158 };
159 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
160 char buf[len];
162 sdb_proto_marshal_attribute(buf, len, &attr);
163 return store_rpc(CLIENT(user_data), buf, len);
164 } /* store_attr */
166 static int
167 store_service_attr(const char *hostname, const char *service,
168 const char *key, const sdb_data_t *value, sdb_time_t last_update,
169 sdb_object_t *user_data)
170 {
171 sdb_proto_attribute_t attr = {
172 last_update, SDB_SERVICE, hostname, service, key, *value,
173 };
174 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
175 char buf[len];
177 sdb_proto_marshal_attribute(buf, len, &attr);
178 return store_rpc(CLIENT(user_data), buf, len);
179 } /* store_service_attr */
181 static int
182 store_metric_attr(const char *hostname, const char *metric,
183 const char *key, const sdb_data_t *value, sdb_time_t last_update,
184 sdb_object_t *user_data)
185 {
186 sdb_proto_attribute_t attr = {
187 last_update, SDB_METRIC, hostname, metric, key, *value,
188 };
189 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
190 char buf[len];
192 sdb_proto_marshal_attribute(buf, len, &attr);
193 return store_rpc(CLIENT(user_data), buf, len);
194 } /* store_metric_attr */
196 static sdb_store_writer_t store_impl = {
197 store_host, store_service, store_metric,
198 store_attr, store_service_attr, store_metric_attr,
199 };
201 /*
202 * plugin API
203 */
205 static int
206 store_init(sdb_object_t *user_data)
207 {
208 user_data_t *ud;
210 if (! user_data)
211 return -1;
213 ud = SDB_OBJ_WRAPPER(user_data)->data;
214 if (sdb_client_connect(ud->client, ud->username)) {
215 sdb_log(SDB_LOG_ERR, "store::network: Failed to connect "
216 "to SysDB at %s as user %s", ud->addr, ud->username);
217 return -1;
218 }
220 sdb_log(SDB_LOG_INFO, "store::network: Successfully connected "
221 "to SysDB at %s as user %s", ud->addr, ud->username);
222 return 0;
223 } /* store_init */
225 static int
226 store_config_server(oconfig_item_t *ci)
227 {
228 sdb_object_t *user_data;
229 user_data_t *ud;
230 int ret = 0;
231 int i;
233 ud = calloc(1, sizeof(*ud));
234 if (! ud) {
235 char errbuf[1024];
236 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
237 "a user-data object: %s",
238 sdb_strerror(errno, errbuf, sizeof(errbuf)));
239 return -1;
240 }
242 if (oconfig_get_string(ci, &ud->addr)) {
243 sdb_log(SDB_LOG_ERR, "store::network: Server requires "
244 "a single string argument\n\tUsage: <Server ADDRESS>");
245 user_data_destroy(ud);
246 return -1;
247 }
248 ud->addr = strdup(ud->addr);
249 if (! ud->addr) {
250 sdb_log(SDB_LOG_ERR, "store::network: Failed to duplicate "
251 "a string");
252 user_data_destroy(ud);
253 return -1;
254 }
256 ud->client = sdb_client_create(ud->addr);
257 if (! ud->client) {
258 char errbuf[1024];
259 sdb_log(SDB_LOG_ERR, "store::network: Failed to create client "
260 "connecting to '%s': %s", ud->addr,
261 sdb_strerror(errno, errbuf, sizeof(errbuf)));
262 user_data_destroy(ud);
263 return -1;
264 }
266 for (i = 0; i < ci->children_num; ++i) {
267 oconfig_item_t *child = ci->children + i;
268 char *tmp = NULL;
270 if (! strcasecmp(child->key, "Username")) {
271 if (oconfig_get_string(child, &tmp)) {
272 ret = -1;
273 break;
274 }
275 ud->username = strdup(tmp);
276 }
277 else if (! strcasecmp(child->key, "SSLCertificate")) {
278 if (oconfig_get_string(child, &tmp)) {
279 ret = -1;
280 break;
281 }
282 ud->ssl_opts.cert_file = strdup(tmp);
283 }
284 else if (! strcasecmp(child->key, "SSLCertificateKey")) {
285 if (oconfig_get_string(child, &tmp)) {
286 ret = -1;
287 break;
288 }
289 ud->ssl_opts.key_file = strdup(tmp);
290 }
291 else if (! strcasecmp(child->key, "SSLCACertificates")) {
292 if (oconfig_get_string(child, &tmp)) {
293 ret = -1;
294 break;
295 }
296 ud->ssl_opts.ca_file = strdup(tmp);
297 }
298 else
299 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
300 "unknown config option '%s' inside <Server %s>.",
301 child->key, ud->addr);
302 }
304 if (ret) {
305 user_data_destroy(ud);
306 return ret;
307 }
308 if (! ud->username)
309 ud->username = sdb_get_current_user();
311 if (sdb_client_set_ssl_options(ud->client, &ud->ssl_opts)) {
312 sdb_log(SDB_LOG_ERR, "store::network: Failed to apply SSL options");
313 user_data_destroy(ud);
314 return -1;
315 }
317 user_data = sdb_object_create_wrapper("store-network-userdata", ud,
318 user_data_destroy);
319 if (! user_data) {
320 char errbuf[1024];
321 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
322 "a user-data wrapper object: %s",
323 sdb_strerror(errno, errbuf, sizeof(errbuf)));
324 user_data_destroy(ud);
325 return -1;
326 }
328 sdb_plugin_register_init(ud->addr, store_init, user_data);
329 sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
330 sdb_object_deref(user_data);
331 return 0;
332 } /* store_config_server */
334 static int
335 store_config(oconfig_item_t *ci)
336 {
337 int i;
339 if (! ci) /* nothing to do to deconfigure this plugin */
340 return 0;
342 for (i = 0; i < ci->children_num; ++i) {
343 oconfig_item_t *child = ci->children + i;
345 if (! strcasecmp(child->key, "Server"))
346 store_config_server(child);
347 else
348 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
349 "unknown config option '%s'.", child->key);
350 }
351 return 0;
352 } /* store_config */
354 int
355 sdb_module_init(sdb_plugin_info_t *info)
356 {
357 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
358 "send stored objects to a remote SysDB instance");
359 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
360 "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
361 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
362 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
363 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
365 sdb_plugin_register_config(store_config);
366 return 0;
367 } /* sdb_module_init */
369 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */