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(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, "store::network: Failed to connect "
197 "to SysDB at %s as user %s", ud->addr, ud->username);
198 return -1;
199 }
201 sdb_log(SDB_LOG_INFO, "store::network: Successfully connected "
202 "to SysDB 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, "store::network: Failed to allocate "
218 "a user-data object: %s",
219 sdb_strerror(errno, errbuf, sizeof(errbuf)));
220 return -1;
221 }
223 if (oconfig_get_string(ci, &ud->addr)) {
224 sdb_log(SDB_LOG_ERR, "store::network: Server requires "
225 "a single string argument\n\tUsage: <Server ADDRESS>");
226 user_data_destroy(ud);
227 return -1;
228 }
229 ud->addr = strdup(ud->addr);
230 if (! ud->addr) {
231 sdb_log(SDB_LOG_ERR, "store::network: Failed to duplicate "
232 "a string");
233 user_data_destroy(ud);
234 return -1;
235 }
237 ud->client = sdb_client_create(ud->addr);
238 if (! ud->client) {
239 char errbuf[1024];
240 sdb_log(SDB_LOG_ERR, "store::network: Failed to create client "
241 "connecting to '%s': %s", ud->addr,
242 sdb_strerror(errno, errbuf, sizeof(errbuf)));
243 user_data_destroy(ud);
244 return -1;
245 }
247 for (i = 0; i < ci->children_num; ++i) {
248 oconfig_item_t *child = ci->children + i;
249 char *tmp = NULL;
251 if (! strcasecmp(child->key, "Username")) {
252 if (oconfig_get_string(child, &tmp)) {
253 ret = -1;
254 break;
255 }
256 ud->username = strdup(tmp);
257 }
258 else if (! strcasecmp(child->key, "SSLCertificate")) {
259 if (oconfig_get_string(child, &tmp)) {
260 ret = -1;
261 break;
262 }
263 ud->ssl_opts.cert_file = strdup(tmp);
264 }
265 else if (! strcasecmp(child->key, "SSLCertificateKey")) {
266 if (oconfig_get_string(child, &tmp)) {
267 ret = -1;
268 break;
269 }
270 ud->ssl_opts.key_file = strdup(tmp);
271 }
272 else if (! strcasecmp(child->key, "SSLCACertificates")) {
273 if (oconfig_get_string(child, &tmp)) {
274 ret = -1;
275 break;
276 }
277 ud->ssl_opts.ca_file = strdup(tmp);
278 }
279 else
280 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
281 "unknown config option '%s' inside <Server %s>.",
282 child->key, ud->addr);
283 }
285 if (ret) {
286 user_data_destroy(ud);
287 return ret;
288 }
289 if (! ud->username)
290 ud->username = sdb_get_current_user();
292 if (sdb_client_set_ssl_options(ud->client, &ud->ssl_opts)) {
293 sdb_log(SDB_LOG_ERR, "store::network: Failed to apply SSL options");
294 user_data_destroy(ud);
295 return -1;
296 }
298 user_data = sdb_object_create_wrapper("store-network-userdata", ud,
299 user_data_destroy);
300 if (! user_data) {
301 char errbuf[1024];
302 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
303 "a user-data wrapper object: %s",
304 sdb_strerror(errno, errbuf, sizeof(errbuf)));
305 user_data_destroy(ud);
306 return -1;
307 }
309 sdb_plugin_register_init(ud->addr, store_init, user_data);
310 sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
311 sdb_object_deref(user_data);
312 return 0;
313 } /* store_config_server */
315 static int
316 store_config(oconfig_item_t *ci)
317 {
318 int i;
320 if (! ci) /* nothing to do to deconfigure this plugin */
321 return 0;
323 for (i = 0; i < ci->children_num; ++i) {
324 oconfig_item_t *child = ci->children + i;
326 if (! strcasecmp(child->key, "Server"))
327 store_config_server(child);
328 else
329 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
330 "unknown config option '%s'.", child->key);
331 }
332 return 0;
333 } /* store_config */
335 int
336 sdb_module_init(sdb_plugin_info_t *info)
337 {
338 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
339 "send stored objects to a remote SysDB instance");
340 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
341 "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
342 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
343 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
344 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
346 sdb_plugin_register_config(store_config);
347 return 0;
348 } /* sdb_module_init */
350 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */