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 variables
45 */
47 static sc_llist_t *host_list = NULL;
48 static pthread_rwlock_t host_lock = PTHREAD_RWLOCK_INITIALIZER;
50 /*
51 * private helper functions
52 */
54 static int
55 sc_store_obj_cmp_by_name(const sc_object_t *a, const sc_object_t *b)
56 {
57 const sc_store_obj_t *h1 = (const sc_store_obj_t *)a;
58 const sc_store_obj_t *h2 = (const sc_store_obj_t *)b;
60 assert(h1 && h2);
61 return strcasecmp(h1->name, h2->name);
62 } /* sc_store_obj_cmp_by_name */
64 static int
65 sc_host_init(sc_object_t *obj, va_list ap)
66 {
67 char *name = va_arg(ap, char *);
69 SC_HOST(obj)->host_name = strdup(name);
70 if (! SC_HOST(obj)->host_name)
71 return -1;
73 SC_HOST(obj)->host_last_update = sc_gettime();
74 /* ignore errors -> last_update will be updated later */
76 SC_HOST(obj)->services = sc_llist_create();
77 if (! SC_HOST(obj)->services)
78 return -1;
79 return 0;
80 } /* sc_host_init */
82 static void
83 sc_host_destroy(sc_object_t *obj)
84 {
85 assert(obj);
87 if (SC_HOST(obj)->host_name)
88 free(SC_HOST(obj)->host_name);
90 if (SC_HOST(obj)->services)
91 sc_llist_destroy(SC_HOST(obj)->services);
92 } /* sc_host_destroy */
94 static int
95 sc_svc_init(sc_object_t *obj, va_list ap)
96 {
97 char *hostname = va_arg(ap, char *);
98 char *name = va_arg(ap, char *);
100 SC_SVC(obj)->hostname = strdup(hostname);
101 SC_SVC(obj)->svc_name = strdup(name);
102 if ((! SC_SVC(obj)->hostname) || (! SC_SVC(obj)->svc_name))
103 return -1;
105 SC_SVC(obj)->svc_last_update = sc_gettime();
106 /* ignore errors -> last_update will be updated later */
107 return 0;
108 } /* sc_svc_init */
110 static void
111 sc_svc_destroy(sc_object_t *obj)
112 {
113 assert(obj);
115 if (SC_SVC(obj)->hostname)
116 free(SC_SVC(obj)->hostname);
117 if (SC_SVC(obj)->svc_name)
118 free(SC_SVC(obj)->svc_name);
119 } /* sc_svc_destroy */
121 /*
122 * public API
123 */
125 sc_host_t *
126 sc_host_create(char *name)
127 {
128 sc_object_t *obj;
130 if (! name)
131 return NULL;
133 obj = sc_object_create(sizeof(sc_host_t), sc_host_init,
134 sc_host_destroy, name);
135 if (! obj)
136 return NULL;
137 return SC_HOST(obj);
138 } /* sc_host_create */
140 sc_host_t *
141 sc_host_clone(const sc_host_t *host)
142 {
143 sc_host_t *clone;
145 clone = sc_host_create(host->host_name);
146 if (! clone)
147 return NULL;
149 clone->host_last_update = host->host_last_update;
150 if (host->services) {
151 clone->services = sc_llist_clone(host->services);
152 if (! clone->services) {
153 sc_object_deref(SC_OBJ(clone));
154 return NULL;
155 }
156 }
157 else
158 clone->services = NULL;
159 return clone;
160 } /* sc_host_clone */
162 int
163 sc_store_host(const sc_host_t *host)
164 {
165 sc_time_t last_update;
167 sc_host_t *old;
168 int status = 0;
170 if ((! host) || (! host->host_name))
171 return -1;
173 last_update = host->host_last_update;
174 if (last_update <= 0)
175 last_update = sc_gettime();
177 pthread_rwlock_wrlock(&host_lock);
179 if (! host_list) {
180 if (! (host_list = sc_llist_create())) {
181 pthread_rwlock_unlock(&host_lock);
182 return -1;
183 }
184 }
186 old = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)host,
187 sc_store_obj_cmp_by_name));
189 if (old) {
190 if (old->host_last_update > last_update) {
191 fprintf(stderr, "store: Cannot update host '%s' - "
192 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
193 host->host_name, last_update, old->host_last_update);
194 status = -1;
195 }
196 else {
197 old->host_last_update = last_update;
198 }
199 }
200 else {
201 sc_host_t *new = sc_host_clone(host);
202 if (! new) {
203 char errbuf[1024];
204 fprintf(stderr, "store: Failed to clone host object: %s\n",
205 sc_strerror(errno, errbuf, sizeof(errbuf)));
206 return -1;
207 }
209 if (! new->services) {
210 if (! (new->services = sc_llist_create())) {
211 char errbuf[1024];
212 fprintf(stderr, "store: Failed to initialize "
213 "host object '%s': %s\n", host->host_name,
214 sc_strerror(errno, errbuf, sizeof(errbuf)));
215 sc_object_deref(SC_OBJ(new));
216 return -1;
217 }
218 }
220 status = sc_llist_insert_sorted(host_list, SC_OBJ(new),
221 sc_store_obj_cmp_by_name);
223 /* pass control to the list or destroy in case of an error */
224 sc_object_deref(SC_OBJ(new));
225 }
227 pthread_rwlock_unlock(&host_lock);
228 return status;
229 } /* sc_store_host */
231 const sc_host_t *
232 sc_store_get_host(char *name)
233 {
234 sc_host_t tmp = SC_HOST_INIT;
235 sc_host_t *host;
237 if (! name)
238 return NULL;
240 tmp.host_name = name;
241 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
242 sc_store_obj_cmp_by_name));
244 if (! host)
245 return NULL;
246 return host;
247 } /* sc_store_get_host */
249 sc_service_t *
250 sc_service_create(char *hostname, char *name)
251 {
252 sc_object_t *obj;
254 if ((! hostname) || (! name))
255 return NULL;
257 obj = sc_object_create(sizeof(sc_service_t), sc_svc_init,
258 sc_svc_destroy, hostname, name);
259 if (! obj)
260 return NULL;
261 return SC_SVC(obj);
262 } /* sc_service_create */
264 sc_service_t *
265 sc_service_clone(const sc_service_t *svc)
266 {
267 sc_service_t *clone;
269 clone = sc_service_create(svc->hostname, svc->svc_name);
270 if (! clone)
271 return NULL;
273 clone->svc_last_update = svc->svc_last_update;
274 return clone;
275 } /* sc_service_clone */
277 int
278 sc_store_service(const sc_service_t *svc)
279 {
280 sc_host_t tmp = SC_HOST_INIT;
281 sc_host_t *host;
283 sc_service_t *old;
285 sc_time_t last_update;
287 int status = 0;
289 if (! svc)
290 return -1;
292 last_update = svc->svc_last_update;
293 if (last_update <= 0)
294 last_update = sc_gettime();
296 if (! host_list)
297 return -1;
299 pthread_rwlock_wrlock(&host_lock);
301 tmp.host_name = svc->hostname;
302 host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
303 sc_store_obj_cmp_by_name));
305 if (! host)
306 return -1;
308 old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)svc,
309 sc_store_obj_cmp_by_name));
311 if (old) {
312 if (old->host_last_update > last_update) {
313 fprintf(stderr, "store: Cannot update service '%s/%s' - "
314 "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
315 svc->hostname, svc->svc_name, last_update,
316 old->host_last_update);
317 status = -1;
318 }
319 else {
320 old->svc_last_update = last_update;
321 }
322 }
323 else {
324 sc_service_t *new = sc_service_clone(svc);
325 if (! new) {
326 char errbuf[1024];
327 fprintf(stderr, "store: Failed to clone service object: %s\n",
328 sc_strerror(errno, errbuf, sizeof(errbuf)));
329 return -1;
330 }
332 status = sc_llist_insert_sorted(host->services, SC_OBJ(new),
333 sc_store_obj_cmp_by_name);
335 /* pass control to the list or destroy in case of an error */
336 sc_object_deref(SC_OBJ(new));
337 }
339 pthread_rwlock_unlock(&host_lock);
340 return status;
341 } /* sc_store_service */
343 const sc_service_t *
344 sc_store_get_service(const sc_host_t *host, char *name)
345 {
346 sc_service_t tmp = SC_SVC_INIT;
347 sc_service_t *svc;
349 if ((! host) || (! name))
350 return NULL;
352 tmp.svc_name = name;
353 svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&tmp,
354 sc_store_obj_cmp_by_name));
356 if (! svc)
357 return NULL;
358 return svc;
359 } /* sc_store_get_service */
361 int
362 sc_store_dump(FILE *fh)
363 {
364 sc_llist_iter_t *host_iter;
366 if (! fh)
367 return -1;
369 pthread_rwlock_rdlock(&host_lock);
371 host_iter = sc_llist_get_iter(host_list);
372 if (! host_iter)
373 return -1;
375 while (sc_llist_iter_has_next(host_iter)) {
376 sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter));
377 sc_llist_iter_t *svc_iter;
379 char time_str[64];
381 assert(host);
383 if (! sc_strftime(time_str, sizeof(time_str),
384 "%F %T %z", host->host_last_update))
385 snprintf(time_str, sizeof(time_str), "<error>");
386 time_str[sizeof(time_str) - 1] = '\0';
388 fprintf(fh, "Host '%s' (last updated: %s):\n",
389 host->host_name, time_str);
391 svc_iter = sc_llist_get_iter(host->services);
392 if (! svc_iter) {
393 char errbuf[1024];
394 fprintf(fh, "Failed to retrieve services: %s\n",
395 sc_strerror(errno, errbuf, sizeof(errbuf)));
396 continue;
397 }
399 while (sc_llist_iter_has_next(svc_iter)) {
400 sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter));
401 assert(svc);
403 if (! sc_strftime(time_str, sizeof(time_str),
404 "%F %T %z", host->host_last_update))
405 snprintf(time_str, sizeof(time_str), "<error>");
406 time_str[sizeof(time_str) - 1] = '\0';
408 fprintf(fh, "\tService '%s' (last updated: %s)\n",
409 svc->svc_name, time_str);
410 }
412 sc_llist_iter_destroy(svc_iter);
413 }
415 sc_llist_iter_destroy(host_iter);
416 return 0;
417 } /* sc_store_dump */
419 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */