1 /*
2 * syscollector - src/core/store.c
3 * Copyright (C) 2012 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 #include "syscollector.h"
29 #include "core/store.h"
30 #include "utils/llist.h"
31 #include "utils/string.h"
33 #include <assert.h>
35 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include <pthread.h>
43 /*
44 * private data types
45 */
47 /* type used for looking up named objects */
48 typedef struct {
49 sc_object_t parent;
50 const char *obj_name;
51 } sc_store_lookup_obj_t;
52 #define SC_STORE_LOOKUP_OBJ_INIT { SC_OBJECT_INIT, NULL }
54 /*
55 * private variables
56 */
58 static sc_llist_t *host_list = NULL;
59 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
61 /*
62 * private helper functions
63 */
65 static int
66 sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b)
67 {
68 const sc_store_obj_t *h1 = (const sc_store_obj_t *)a;
69 const sc_store_obj_t *h2 = (const sc_store_obj_t *)b;
71 assert(h1 && h2);
72 return strcasecmp(h1->name, h2->name);
73 } /* sc_store_obj_cmp_by_name */
75 static int
76 sc_cmp_store_obj_with_name(const sc_object_t *a, const sc_object_t *b)
77 {
78 const sc_store_obj_t *obj = (const sc_store_obj_t *)a;
79 const sc_store_lookup_obj_t *lookup = (const sc_store_lookup_obj_t *)b;
81 assert(obj && lookup);
82 return strcasecmp(obj->name, lookup->obj_name);
83 } /* sc_cmp_store_obj_with_name */
85 static int
86 sc_host_init(sc_object_t *obj, va_list ap)
87 {
88 const char *name = va_arg(ap, const char *);
90 SC_HOST(obj)->host_name = strdup(name);
91 if (! SC_HOST(obj)->host_name)
92 return -1;
94 SC_HOST(obj)->host_last_update = sc_gettime();
95 /* ignore errors -> last_update will be updated later */
97 SC_HOST(obj)->services = sc_llist_create();
98 if (! SC_HOST(obj)->services)
99 return -1;
100 return 0;
101 } /* sc_host_init */
103 static void
104 sc_host_destroy(sc_object_t *obj)
105 {
106 assert(obj);
108 if (SC_HOST(obj)->host_name)
109 free(SC_HOST(obj)->host_name);
111 if (SC_HOST(obj)->services)
112 sc_llist_destroy(SC_HOST(obj)->services);
113 } /* sc_host_destroy */
115 static int
116 sc_svc_init(sc_object_t *obj, va_list ap)
117 {
118 const char *hostname = va_arg(ap, const char *);
119 const char *name = va_arg(ap, const char *);
121 SC_SVC(obj)->hostname = strdup(hostname);
122 SC_SVC(obj)->svc_name = strdup(name);
123 if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name))
124 return -1;
126 SC_SVC(obj)->svc_last_update = sc_gettime();
127 /* ignore errors -> last_update will be updated later */
128 return 0;
129 } /* sc_svc_init */
131 static void
132 sc_svc_destroy(sc_object_t *obj)
133 {
134 assert(obj);
136 if (SC_SVC(obj)->hostname)
137 free(SC_SVC(obj)->hostname);
138 if (SC_SVC(obj)->svc_name)
139 free(SC_SVC(obj)->svc_name);
140 } /* sc_svc_destroy */
142 /*
143 * public API
144 */
146 sc_host_t *
147 sc_host_create(const char *name)
148 {
149 sc_object_t *obj;
151 if (! name)
152 return NULL;
154 obj = sc_object_create(sizeof(sc_host_t), sc_host_init,
155 sc_host_destroy, name);
156 if (! obj)
157 return NULL;
158 return SC_HOST(obj);
159 } /* sc_host_create */
161 sc_host_t *
162 sc_host_clone(const sc_host_t *host)
163 {
164 sc_host_t *clone;
166 clone = sc_host_create(host->host_name);
167 if (! clone)
168 return NULL;
170 clone->host_last_update = host->host_last_update;
171 if (host->services) {
172 clone->services = sc_llist_clone(host->services);
173 if (! clone->services) {
174 sc_object_deref(SC_OBJ(clone));
175 return NULL;
176 }
177 }
178 else
179 clone->services = NULL;
180 return clone;
181 } /* sc_host_clone */
183 int
184 sc_store_host(const sc_host_t *host)
185 {
186 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
187 sc_time_t last_update;
189 sc_host_t *old;
190 int status = 0;
192 if ((! host) || (! host->host_name))
193 return -1;
195 last_update = host->host_last_update;
196 if (last_update <= 0)
197 last_update = sc_gettime();
199 pthread_rwlock_wrlock(&host_lock);
201 if (! host_list) {
202 if (! (host_list = sc_llist_create())) {
203 pthread_rwlock_unlock(&host_lock);
204 return -1;
205 }
206 }
208 lookup.obj_name = host->host_name;
209 old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
210 sc_cmp_store_obj_with_name));
212 if (old) {
213 if (old->host_last_update > last_update) {
214 fprintf(stderr, "store: Cannot update host '%s' - "
215 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
216 host->host_name, last_update, old->host_last_update);
217 /* don't report an error; the host may be updated by multiple
218 * backends */
219 status = 1;
220 }
221 else {
222 old->host_last_update = last_update;
223 }
224 }
225 else {
226 sc_host_t *new = sc_host_clone(host);
227 if (! new) {
228 char errbuf[1024];
229 fprintf(stderr, "store: Failed to clone host object: %s\n",
230 sc_strerror(errno, errbuf, sizeof(errbuf)));
231 return -1;
232 }
234 if (! new->services) {
235 if (! (new->services = sc_llist_create())) {
236 char errbuf[1024];
237 fprintf(stderr, "store: Failed to initialize "
238 "host object '%s': %s\n", host->host_name,
239 sc_strerror(errno, errbuf, sizeof(errbuf)));
240 sc_object_deref(SC_OBJ(new));
241 return -1;
242 }
243 }
245 status = sc_llist_insert_sorted(host_list, SC_OBJ(new),
246 sc_store_obj_cmp_by_name);
248 /* pass control to the list or destroy in case of an error */
249 sc_object_deref(SC_OBJ(new));
250 }
252 pthread_rwlock_unlock(&host_lock);
253 return status;
254 } /* sc_store_host */
256 const sc_host_t *
257 sc_store_get_host(const char *name)
258 {
259 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
260 sc_host_t *host;
262 if (! name)
263 return NULL;
265 lookup.obj_name = name;
266 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
267 sc_cmp_store_obj_with_name));
269 if (! host)
270 return NULL;
271 return host;
272 } /* sc_store_get_host */
274 sc_service_t *
275 sc_service_create(const char *hostname, const char *name)
276 {
277 sc_object_t *obj;
279 if ((! hostname) || (! name))
280 return NULL;
282 obj = sc_object_create(sizeof(sc_service_t), sc_svc_init,
283 sc_svc_destroy, hostname, name);
284 if (! obj)
285 return NULL;
286 return SC_SVC(obj);
287 } /* sc_service_create */
289 sc_service_t *
290 sc_service_clone(const sc_service_t *svc)
291 {
292 sc_service_t *clone;
294 clone = sc_service_create(svc->hostname, svc->svc_name);
295 if (! clone)
296 return NULL;
298 clone->svc_last_update = svc->svc_last_update;
299 return clone;
300 } /* sc_service_clone */
302 int
303 sc_store_service(const sc_service_t *svc)
304 {
305 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
306 sc_host_t *host;
308 sc_service_t *old;
310 sc_time_t last_update;
312 int status = 0;
314 if (! svc)
315 return -1;
317 last_update = svc->svc_last_update;
318 if (last_update <= 0)
319 last_update = sc_gettime();
321 if (! host_list)
322 return -1;
324 pthread_rwlock_wrlock(&host_lock);
326 lookup.obj_name = svc->hostname;
327 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&lookup,
328 sc_cmp_store_obj_with_name));
330 if (! host)
331 return -1;
333 lookup.obj_name = svc->svc_name;
334 old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
335 sc_cmp_store_obj_with_name));
337 if (old) {
338 if (old->host_last_update > last_update) {
339 fprintf(stderr, "store: Cannot update service '%s/%s' - "
340 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
341 svc->hostname, svc->svc_name, last_update,
342 old->host_last_update);
343 status = 1;
344 }
345 else {
346 old->svc_last_update = last_update;
347 }
348 }
349 else {
350 sc_service_t *new = sc_service_clone(svc);
351 if (! new) {
352 char errbuf[1024];
353 fprintf(stderr, "store: Failed to clone service object: %s\n",
354 sc_strerror(errno, errbuf, sizeof(errbuf)));
355 return -1;
356 }
358 status = sc_llist_insert_sorted(host->services, SC_OBJ(new),
359 sc_store_obj_cmp_by_name);
361 /* pass control to the list or destroy in case of an error */
362 sc_object_deref(SC_OBJ(new));
363 }
365 pthread_rwlock_unlock(&host_lock);
366 return status;
367 } /* sc_store_service */
369 const sc_service_t *
370 sc_store_get_service(const sc_host_t *host, const char *name)
371 {
372 sc_store_lookup_obj_t lookup = SC_STORE_LOOKUP_OBJ_INIT;
373 sc_service_t *svc;
375 if ((! host) || (! name))
376 return NULL;
378 lookup.obj_name = name;
379 svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&lookup,
380 sc_cmp_store_obj_with_name));
382 if (! svc)
383 return NULL;
384 return svc;
385 } /* sc_store_get_service */
387 int
388 sc_store_dump(FILE *fh)
389 {
390 sc_llist_iter_t *host_iter;
392 if (! fh)
393 return -1;
395 pthread_rwlock_rdlock(&host_lock);
397 host_iter = sc_llist_get_iter(host_list);
398 if (! host_iter)
399 return -1;
401 while (sc_llist_iter_has_next(host_iter)) {
402 sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter));
403 sc_llist_iter_t *svc_iter;
405 char time_str[64];
407 assert(host);
409 if (! sc_strftime(time_str, sizeof(time_str),
410 "%F %T %z", host->host_last_update))
411 snprintf(time_str, sizeof(time_str), "<error>");
412 time_str[sizeof(time_str) - 1] = '\0';
414 fprintf(fh, "Host '%s' (last updated: %s):\n",
415 host->host_name, time_str);
417 svc_iter = sc_llist_get_iter(host->services);
418 if (! svc_iter) {
419 char errbuf[1024];
420 fprintf(fh, "Failed to retrieve services: %s\n",
421 sc_strerror(errno, errbuf, sizeof(errbuf)));
422 continue;
423 }
425 while (sc_llist_iter_has_next(svc_iter)) {
426 sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter));
427 assert(svc);
429 if (! sc_strftime(time_str, sizeof(time_str),
430 "%F %T %z", host->host_last_update))
431 snprintf(time_str, sizeof(time_str), "<error>");
432 time_str[sizeof(time_str) - 1] = '\0';
434 fprintf(fh, "\tService '%s' (last updated: %s)\n",
435 svc->svc_name, time_str);
436 }
438 sc_llist_iter_destroy(svc_iter);
439 }
441 sc_llist_iter_destroy(host_iter);
442 return 0;
443 } /* sc_store_dump */
445 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */