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, "Failed to reconnect to SysDB "
100 "at %s as user %s", ud->addr, ud->username);
101 return -1;
102 }
103 sdb_log(SDB_LOG_INFO, "Successfully reconnected to SysDB "
104 "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, "%s", sdb_strbuf_string(buf));
111 else if (rstatus != SDB_CONNECTION_OK) {
112 sdb_log(SDB_LOG_ERR, "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(sdb_store_host_t *host, sdb_object_t *user_data)
125 {
126 sdb_proto_host_t h = { host->last_update, host->name };
127 size_t len = sdb_proto_marshal_host(NULL, 0, &h);
128 char buf[len];
130 sdb_proto_marshal_host(buf, len, &h);
131 return store_rpc(UD(user_data), buf, len);
132 } /* store_host */
134 static int
135 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
136 {
137 sdb_proto_service_t s = {
138 service->last_update, service->hostname, service->name,
139 };
140 ssize_t len = sdb_proto_marshal_service(NULL, 0, &s);
141 char buf[len];
143 sdb_proto_marshal_service(buf, len, &s);
144 return store_rpc(UD(user_data), buf, len);
145 } /* store_service */
147 static int
148 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
149 {
150 sdb_proto_metric_t m = {
151 metric->last_update, metric->hostname, metric->name,
152 /* TODO: Add support for sending all data stores. */
153 metric->stores_num ? metric->stores[0].type : NULL,
154 metric->stores_num ? metric->stores[0].id : NULL,
155 metric->stores_num ? metric->stores[0].last_update : 0,
156 };
157 size_t len = sdb_proto_marshal_metric(NULL, 0, &m);
158 char buf[len];
160 sdb_proto_marshal_metric(buf, len, &m);
161 return store_rpc(UD(user_data), buf, len);
162 } /* store_metric */
164 static int
165 store_attr(sdb_store_attribute_t *attr, sdb_object_t *user_data)
166 {
167 sdb_proto_attribute_t a = {
168 attr->last_update, attr->parent_type, attr->hostname, attr->parent,
169 attr->key, attr->value,
170 };
171 size_t len = sdb_proto_marshal_attribute(NULL, 0, &a);
172 char buf[len];
174 sdb_proto_marshal_attribute(buf, len, &a);
175 return store_rpc(UD(user_data), buf, len);
176 } /* store_attr */
178 static sdb_store_writer_t store_impl = {
179 store_host, store_service, store_metric, store_attr,
180 };
182 /*
183 * plugin API
184 */
186 static int
187 store_init(sdb_object_t *user_data)
188 {
189 user_data_t *ud;
191 if (! user_data)
192 return -1;
194 ud = SDB_OBJ_WRAPPER(user_data)->data;
195 if (sdb_client_connect(ud->client, ud->username)) {
196 sdb_log(SDB_LOG_ERR, "Failed to connect to SysDB "
197 "at %s as user %s", ud->addr, ud->username);
198 return -1;
199 }
201 sdb_log(SDB_LOG_INFO, "Successfully connected to SysDB "
202 "at %s as user %s", ud->addr, ud->username);
203 return 0;
204 } /* store_init */
206 static int
207 store_config_server(oconfig_item_t *ci)
208 {
209 sdb_object_t *user_data;
210 user_data_t *ud;
211 int ret = 0;
212 int i;
214 ud = calloc(1, sizeof(*ud));
215 if (! ud) {
216 char errbuf[1024];
217 sdb_log(SDB_LOG_ERR, "Failed to allocate a user-data object: %s",
218 sdb_strerror(errno, errbuf, sizeof(errbuf)));
219 return -1;
220 }
222 if (oconfig_get_string(ci, &ud->addr)) {
223 sdb_log(SDB_LOG_ERR, "Server requires a single string argument\n"
224 "\tUsage: <Server ADDRESS>");
225 user_data_destroy(ud);
226 return -1;
227 }
228 ud->addr = strdup(ud->addr);
229 if (! ud->addr) {
230 sdb_log(SDB_LOG_ERR, "Failed to duplicate a string");
231 user_data_destroy(ud);
232 return -1;
233 }
235 ud->client = sdb_client_create(ud->addr);
236 if (! ud->client) {
237 char errbuf[1024];
238 sdb_log(SDB_LOG_ERR, "Failed to create client connecting to '%s': %s",
239 ud->addr, sdb_strerror(errno, errbuf, sizeof(errbuf)));
240 user_data_destroy(ud);
241 return -1;
242 }
244 for (i = 0; i < ci->children_num; ++i) {
245 oconfig_item_t *child = ci->children + i;
246 char *tmp = NULL;
248 if (! strcasecmp(child->key, "Username")) {
249 if (oconfig_get_string(child, &tmp)) {
250 ret = -1;
251 break;
252 }
253 ud->username = strdup(tmp);
254 }
255 else if (! strcasecmp(child->key, "SSLCertificate")) {
256 if (oconfig_get_string(child, &tmp)) {
257 ret = -1;
258 break;
259 }
260 ud->ssl_opts.cert_file = strdup(tmp);
261 }
262 else if (! strcasecmp(child->key, "SSLCertificateKey")) {
263 if (oconfig_get_string(child, &tmp)) {
264 ret = -1;
265 break;
266 }
267 ud->ssl_opts.key_file = strdup(tmp);
268 }
269 else if (! strcasecmp(child->key, "SSLCACertificates")) {
270 if (oconfig_get_string(child, &tmp)) {
271 ret = -1;
272 break;
273 }
274 ud->ssl_opts.ca_file = strdup(tmp);
275 }
276 else
277 sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s' "
278 "inside <Server %s>.", child->key, ud->addr);
279 }
281 if (ret) {
282 user_data_destroy(ud);
283 return ret;
284 }
285 if (! ud->username)
286 ud->username = sdb_get_current_user();
288 if (sdb_client_set_ssl_options(ud->client, &ud->ssl_opts)) {
289 sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
290 user_data_destroy(ud);
291 return -1;
292 }
294 user_data = sdb_object_create_wrapper("store-network-userdata", ud,
295 user_data_destroy);
296 if (! user_data) {
297 char errbuf[1024];
298 sdb_log(SDB_LOG_ERR, "Failed to allocate a user-data wrapper object: %s",
299 sdb_strerror(errno, errbuf, sizeof(errbuf)));
300 user_data_destroy(ud);
301 return -1;
302 }
304 sdb_plugin_register_init(ud->addr, store_init, user_data);
305 sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
306 sdb_object_deref(user_data);
307 return 0;
308 } /* store_config_server */
310 static int
311 store_config(oconfig_item_t *ci)
312 {
313 int i;
315 if (! ci) /* nothing to do to deconfigure this plugin */
316 return 0;
318 for (i = 0; i < ci->children_num; ++i) {
319 oconfig_item_t *child = ci->children + i;
321 if (! strcasecmp(child->key, "Server"))
322 store_config_server(child);
323 else
324 sdb_log(SDB_LOG_WARNING, "Ignoring unknown config option '%s'.",
325 child->key);
326 }
327 return 0;
328 } /* store_config */
330 int
331 sdb_module_init(sdb_plugin_info_t *info)
332 {
333 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
334 "send stored objects to a remote SysDB instance");
335 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
336 "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
337 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
338 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
339 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
341 sdb_plugin_register_config(store_config);
342 return 0;
343 } /* sdb_module_init */
345 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */