Code

store: Don't return an error if an updated host is too old.
[sysdb.git] / src / core / store.c
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)
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)
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)
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)
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                         /* don't report an error; the host may be updated by multiple
195                          * backends */
196                         status = 0;
197                 }
198                 else {
199                         old->host_last_update = last_update;
200                 }
201         }
202         else {
203                 sc_host_t *new = sc_host_clone(host);
204                 if (! new) {
205                         char errbuf[1024];
206                         fprintf(stderr, "store: Failed to clone host object: %s\n",
207                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
208                         return -1;
209                 }
211                 if (! new->services) {
212                         if (! (new->services = sc_llist_create())) {
213                                 char errbuf[1024];
214                                 fprintf(stderr, "store: Failed to initialize "
215                                                 "host object '%s': %s\n", host->host_name,
216                                                 sc_strerror(errno, errbuf, sizeof(errbuf)));
217                                 sc_object_deref(SC_OBJ(new));
218                                 return -1;
219                         }
220                 }
222                 status = sc_llist_insert_sorted(host_list, SC_OBJ(new),
223                                 sc_store_obj_cmp_by_name);
225                 /* pass control to the list or destroy in case of an error */
226                 sc_object_deref(SC_OBJ(new));
227         }
229         pthread_rwlock_unlock(&host_lock);
230         return status;
231 } /* sc_store_host */
233 const sc_host_t *
234 sc_store_get_host(char *name)
236         sc_host_t  tmp = SC_HOST_INIT;
237         sc_host_t *host;
239         if (! name)
240                 return NULL;
242         tmp.host_name = name;
243         host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
244                                 sc_store_obj_cmp_by_name));
246         if (! host)
247                 return NULL;
248         return host;
249 } /* sc_store_get_host */
251 sc_service_t *
252 sc_service_create(char *hostname, char *name)
254         sc_object_t *obj;
256         if ((! hostname) || (! name))
257                 return NULL;
259         obj = sc_object_create(sizeof(sc_service_t), sc_svc_init,
260                         sc_svc_destroy, hostname, name);
261         if (! obj)
262                 return NULL;
263         return SC_SVC(obj);
264 } /* sc_service_create */
266 sc_service_t *
267 sc_service_clone(const sc_service_t *svc)
269         sc_service_t *clone;
271         clone = sc_service_create(svc->hostname, svc->svc_name);
272         if (! clone)
273                 return NULL;
275         clone->svc_last_update = svc->svc_last_update;
276         return clone;
277 } /* sc_service_clone */
279 int
280 sc_store_service(const sc_service_t *svc)
282         sc_host_t  tmp = SC_HOST_INIT;
283         sc_host_t *host;
285         sc_service_t *old;
287         sc_time_t last_update;
289         int status = 0;
291         if (! svc)
292                 return -1;
294         last_update = svc->svc_last_update;
295         if (last_update <= 0)
296                 last_update = sc_gettime();
298         if (! host_list)
299                 return -1;
301         pthread_rwlock_wrlock(&host_lock);
303         tmp.host_name = svc->hostname;
304         host = SC_HOST(sc_llist_search(host_list, (const sc_object_t *)&tmp,
305                                 sc_store_obj_cmp_by_name));
307         if (! host)
308                 return -1;
310         old = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)svc,
311                                 sc_store_obj_cmp_by_name));
313         if (old) {
314                 if (old->host_last_update > last_update) {
315                         fprintf(stderr, "store: Cannot update service '%s/%s' - "
316                                         "value too old (%"PRIscTIME" < %"PRIscTIME")\n",
317                                         svc->hostname, svc->svc_name, last_update,
318                                         old->host_last_update);
319                         status = -1;
320                 }
321                 else {
322                         old->svc_last_update = last_update;
323                 }
324         }
325         else {
326                 sc_service_t *new = sc_service_clone(svc);
327                 if (! new) {
328                         char errbuf[1024];
329                         fprintf(stderr, "store: Failed to clone service object: %s\n",
330                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
331                         return -1;
332                 }
334                 status = sc_llist_insert_sorted(host->services, SC_OBJ(new),
335                                 sc_store_obj_cmp_by_name);
337                 /* pass control to the list or destroy in case of an error */
338                 sc_object_deref(SC_OBJ(new));
339         }
341         pthread_rwlock_unlock(&host_lock);
342         return status;
343 } /* sc_store_service */
345 const sc_service_t *
346 sc_store_get_service(const sc_host_t *host, char *name)
348         sc_service_t  tmp = SC_SVC_INIT;
349         sc_service_t *svc;
351         if ((! host) || (! name))
352                 return NULL;
354         tmp.svc_name = name;
355         svc = SC_SVC(sc_llist_search(host->services, (const sc_object_t *)&tmp,
356                                 sc_store_obj_cmp_by_name));
358         if (! svc)
359                 return NULL;
360         return svc;
361 } /* sc_store_get_service */
363 int
364 sc_store_dump(FILE *fh)
366         sc_llist_iter_t *host_iter;
368         if (! fh)
369                 return -1;
371         pthread_rwlock_rdlock(&host_lock);
373         host_iter = sc_llist_get_iter(host_list);
374         if (! host_iter)
375                 return -1;
377         while (sc_llist_iter_has_next(host_iter)) {
378                 sc_host_t *host = SC_HOST(sc_llist_iter_get_next(host_iter));
379                 sc_llist_iter_t *svc_iter;
381                 char time_str[64];
383                 assert(host);
385                 if (! sc_strftime(time_str, sizeof(time_str),
386                                         "%F %T %z", host->host_last_update))
387                         snprintf(time_str, sizeof(time_str), "<error>");
388                 time_str[sizeof(time_str) - 1] = '\0';
390                 fprintf(fh, "Host '%s' (last updated: %s):\n",
391                                 host->host_name, time_str);
393                 svc_iter = sc_llist_get_iter(host->services);
394                 if (! svc_iter) {
395                         char errbuf[1024];
396                         fprintf(fh, "Failed to retrieve services: %s\n",
397                                         sc_strerror(errno, errbuf, sizeof(errbuf)));
398                         continue;
399                 }
401                 while (sc_llist_iter_has_next(svc_iter)) {
402                         sc_service_t *svc = SC_SVC(sc_llist_iter_get_next(svc_iter));
403                         assert(svc);
405                         if (! sc_strftime(time_str, sizeof(time_str),
406                                                 "%F %T %z", host->host_last_update))
407                                 snprintf(time_str, sizeof(time_str), "<error>");
408                         time_str[sizeof(time_str) - 1] = '\0';
410                         fprintf(fh, "\tService '%s' (last updated: %s)\n",
411                                         svc->svc_name, time_str);
412                 }
414                 sc_llist_iter_destroy(svc_iter);
415         }
417         sc_llist_iter_destroy(host_iter);
418         return 0;
419 } /* sc_store_dump */
421 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */