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"
39 #include "liboconfig/utils.h"
41 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <strings.h>
47 SDB_PLUGIN_MAGIC;
49 /*
50 * private data types
51 */
53 typedef struct {
54 sdb_client_t *client;
55 char *addr;
56 char *username;
57 } user_data_t;
58 #define UD(obj) ((user_data_t *)(obj))
59 #define CLIENT(obj) UD(SDB_OBJ_WRAPPER(obj)->data)->client
61 static void
62 user_data_destroy(void *obj)
63 {
64 user_data_t *ud = UD(obj);
66 if (! ud)
67 return;
69 if (ud->client)
70 sdb_client_destroy(ud->client);
71 ud->client = NULL;
72 if (ud->addr)
73 free(ud->addr);
74 ud->addr = NULL;
75 if (ud->username)
76 free(ud->username);
77 ud->username = NULL;
78 } /* user_data_destroy */
80 /*
81 * store writer implementation
82 */
84 static int
85 store_rpc(sdb_client_t *client, const char *msg, size_t msg_len)
86 {
87 sdb_strbuf_t *buf = sdb_strbuf_create(128);
88 uint32_t rstatus = 0;
89 ssize_t status;
91 status = sdb_client_rpc(client, SDB_CONNECTION_STORE,
92 (uint32_t)msg_len, msg, &rstatus, buf);
93 if (status < 0)
94 sdb_log(SDB_LOG_ERR, "store::network: %s", sdb_strbuf_string(buf));
95 else if (rstatus != SDB_CONNECTION_OK) {
96 sdb_log(SDB_LOG_ERR, "store::network: Failed to send object: %s",
97 sdb_strbuf_string(buf));
98 status = -1;
99 }
101 sdb_strbuf_destroy(buf);
102 if (status < 0)
103 return -1;
104 return 0;
105 } /* store_rpc */
107 static int
108 store_host(const char *name, sdb_time_t last_update, sdb_object_t *user_data)
109 {
110 sdb_proto_host_t host = { last_update, name };
111 size_t len = sdb_proto_marshal_host(NULL, 0, &host);
112 char buf[len];
114 sdb_proto_marshal_host(buf, len, &host);
115 return store_rpc(CLIENT(user_data), buf, len);
116 } /* store_host */
118 static int
119 store_service(const char *hostname, const char *name, sdb_time_t last_update,
120 sdb_object_t *user_data)
121 {
122 sdb_proto_service_t svc = { last_update, hostname, name };
123 ssize_t len = sdb_proto_marshal_service(NULL, 0, &svc);
124 char buf[len];
126 sdb_proto_marshal_service(buf, len, &svc);
127 return store_rpc(CLIENT(user_data), buf, len);
128 } /* store_service */
130 static int
131 store_metric(const char *hostname, const char *name,
132 sdb_metric_store_t *store, sdb_time_t last_update,
133 sdb_object_t *user_data)
134 {
135 sdb_proto_metric_t metric = {
136 last_update, hostname, name,
137 store ? store->type : NULL, store ? store->id : NULL,
138 };
139 size_t len = sdb_proto_marshal_metric(NULL, 0, &metric);
140 char buf[len];
142 sdb_proto_marshal_metric(buf, len, &metric);
143 return store_rpc(CLIENT(user_data), buf, len);
144 } /* store_metric */
146 static int
147 store_attr(const char *hostname, const char *key, const sdb_data_t *value,
148 sdb_time_t last_update, sdb_object_t *user_data)
149 {
150 sdb_proto_attribute_t attr = {
151 last_update, SDB_HOST, NULL, hostname, key, *value,
152 };
153 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
154 char buf[len];
156 sdb_proto_marshal_attribute(buf, len, &attr);
157 return store_rpc(CLIENT(user_data), buf, len);
158 } /* store_attr */
160 static int
161 store_service_attr(const char *hostname, const char *service,
162 const char *key, const sdb_data_t *value, sdb_time_t last_update,
163 sdb_object_t *user_data)
164 {
165 sdb_proto_attribute_t attr = {
166 last_update, SDB_SERVICE, hostname, service, key, *value,
167 };
168 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
169 char buf[len];
171 sdb_proto_marshal_attribute(buf, len, &attr);
172 return store_rpc(CLIENT(user_data), buf, len);
173 } /* store_service_attr */
175 static int
176 store_metric_attr(const char *hostname, const char *metric,
177 const char *key, const sdb_data_t *value, sdb_time_t last_update,
178 sdb_object_t *user_data)
179 {
180 sdb_proto_attribute_t attr = {
181 last_update, SDB_METRIC, hostname, metric, key, *value,
182 };
183 size_t len = sdb_proto_marshal_attribute(NULL, 0, &attr);
184 char buf[len];
186 sdb_proto_marshal_attribute(buf, len, &attr);
187 return store_rpc(CLIENT(user_data), buf, len);
188 } /* store_metric_attr */
190 static sdb_store_writer_t store_impl = {
191 store_host, store_service, store_metric,
192 store_attr, store_service_attr, store_metric_attr,
193 };
195 /*
196 * plugin API
197 */
199 static int
200 store_init(sdb_object_t *user_data)
201 {
202 user_data_t *ud;
204 if (! user_data)
205 return -1;
207 ud = SDB_OBJ_WRAPPER(user_data)->data;
208 if (sdb_client_connect(ud->client, ud->username)) {
209 sdb_log(SDB_LOG_ERR, "store::network: Failed to connect "
210 "to SysDB at %s as user %s", ud->addr, ud->username);
211 return -1;
212 }
214 sdb_log(SDB_LOG_ERR, "store::network: Successfully connected "
215 "to SysDB at %s as user %s", ud->addr, ud->username);
216 return 0;
217 } /* store_init */
219 static int
220 store_config_server(oconfig_item_t *ci)
221 {
222 sdb_object_t *user_data;
223 user_data_t *ud;
225 int i;
227 ud = calloc(1, sizeof(*ud));
228 if (! ud) {
229 char errbuf[1024];
230 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
231 "a user-data object: %s",
232 sdb_strerror(errno, errbuf, sizeof(errbuf)));
233 return -1;
234 }
236 if (oconfig_get_string(ci, &ud->addr)) {
237 sdb_log(SDB_LOG_ERR, "store::network: Server requires "
238 "a single string argument\n\tUsage: <Server ADDRESS>");
239 user_data_destroy(ud);
240 return -1;
241 }
242 ud->addr = strdup(ud->addr);
243 if (! ud->addr) {
244 sdb_log(SDB_LOG_ERR, "store::network: Failed to duplicate "
245 "a string");
246 user_data_destroy(ud);
247 return -1;
248 }
250 ud->client = sdb_client_create(ud->addr);
251 if (! ud->client) {
252 char errbuf[1024];
253 sdb_log(SDB_LOG_ERR, "store::network: Failed to create client "
254 "connecting to '%s': %s", ud->addr,
255 sdb_strerror(errno, errbuf, sizeof(errbuf)));
256 user_data_destroy(ud);
257 return -1;
258 }
260 for (i = 0; i < ci->children_num; ++i) {
261 oconfig_item_t *child = ci->children + i;
263 if (! strcasecmp(child->key, "Username"))
264 oconfig_get_string(child, &ud->username);
265 else
266 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
267 "unknown config option '%s' inside <Server %s>.",
268 child->key, ud->addr);
269 }
271 if (ud->username)
272 ud->username = strdup(ud->username);
273 if (! ud->username)
274 ud->username = sdb_get_current_user();
276 user_data = sdb_object_create_wrapper("store-network-userdata", ud,
277 user_data_destroy);
278 if (! user_data) {
279 char errbuf[1024];
280 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
281 "a user-data wrapper object: %s",
282 sdb_strerror(errno, errbuf, sizeof(errbuf)));
283 user_data_destroy(ud);
284 return -1;
285 }
287 sdb_plugin_register_init(ud->addr, store_init, user_data);
288 sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
289 sdb_object_deref(user_data);
290 return 0;
291 } /* store_config_server */
293 static int
294 store_config(oconfig_item_t *ci)
295 {
296 int i;
298 if (! ci) /* nothing to do to deconfigure this plugin */
299 return 0;
301 for (i = 0; i < ci->children_num; ++i) {
302 oconfig_item_t *child = ci->children + i;
304 if (! strcasecmp(child->key, "Server"))
305 store_config_server(child);
306 else
307 sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
308 "unknown config option '%s'.", child->key);
309 }
310 return 0;
311 } /* store_config */
313 int
314 sdb_module_init(sdb_plugin_info_t *info)
315 {
316 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
317 "send stored objects to a remote SysDB instance");
318 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
319 "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
320 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
321 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
322 sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
324 sdb_plugin_register_config(store_config);
325 return 0;
326 } /* sdb_module_init */
328 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */