1 /*
2 * syscollector - src/include/core/store.h
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 #ifndef SC_CORE_STORE_H
29 #define SC_CORE_STORE_H 1
31 #include "syscollector.h"
32 #include "core/object.h"
33 #include "utils/time.h"
34 #include "utils/llist.h"
36 #include <stdio.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
42 typedef struct {
43 sc_object_t parent;
45 sc_time_t last_update;
46 char *name;
47 } sc_store_obj_t;
48 #define SC_STORE_OBJ_INIT { SC_OBJECT_INIT, 0, NULL }
49 #define SC_STORE_OBJ(obj) ((sc_store_obj_t *)(obj))
51 typedef struct {
52 sc_store_obj_t parent;
53 #define svc_last_update parent.last_update
54 #define svc_name parent.name
56 char *hostname;
57 } sc_service_t;
58 #define SC_SVC_INIT { SC_STORE_OBJ_INIT, NULL }
59 #define SC_SVC(obj) ((sc_service_t *)(obj))
61 typedef struct {
62 sc_store_obj_t parent;
63 #define attr_last_update parent.last_update
64 #define attr_name parent.name
66 char *attr_value;
67 char *hostname;
68 } sc_attribute_t;
69 #define SC_ATTR_INIT { SC_STORE_OBJ_INIT, NULL, NULL }
70 #define SC_ATTR(obj) ((sc_attribute_t *)(obj))
72 typedef struct {
73 sc_store_obj_t parent;
74 #define host_last_update parent.last_update
75 #define host_name parent.name
77 sc_llist_t *attributes;
78 sc_llist_t *services;
79 } sc_host_t;
80 #define SC_HOST_INIT { SC_STORE_OBJ_INIT, NULL, NULL }
81 #define SC_HOST(obj) ((sc_host_t *)(obj))
83 sc_host_t *
84 sc_host_create(const char *name);
86 sc_host_t *
87 sc_host_clone(const sc_host_t *host);
89 /*
90 * sc_store_host:
91 * Add/update a host in the store. If the host, identified by its name,
92 * already exists, it will be updated according to the specified 'host'
93 * object. Else, a new entry will be created in the store. Any memory required
94 * for storing the entry will be allocated an managed by the store itself. The
95 * specified host-object will not be referenced or further accessed.
96 *
97 * Returns:
98 * - 0 on success
99 * - a positive value if the new entry is older than the currently stored
100 * entry (in this case, no update will happen)
101 * - a negative value on error
102 */
103 int
104 sc_store_host(const sc_host_t *host);
106 const sc_host_t *
107 sc_store_get_host(const char *name);
109 sc_attribute_t *
110 sc_attribute_create(const char *hostname,
111 const char *name, const char *value);
113 sc_attribute_t *
114 sc_attribute_clone(const sc_attribute_t *attr);
116 /*
117 * sc_store_attribute:
118 * Add/update a host's attribute in the store. If the attribute, identified by
119 * its name, already exists for the specified host, it will be updated
120 * according to the specified 'attr' object. If the referenced host does not
121 * exist, an error will be reported. Else, a new entry will be created in the
122 * store. Any memory required for storing the entry will be allocated and
123 * managed by the store itself. The specified attribute-object will not be
124 * referenced or further accessed.
125 *
126 * Returns:
127 * - 0 on success
128 * - a positive value if the new entry is older than the currently stored
129 * entry (in this case, no update will happen)
130 * - a negative value on error
131 */
132 int
133 sc_store_attribute(const sc_attribute_t *attr);
135 sc_service_t *
136 sc_service_create(const char *hostname, const char *name);
138 sc_service_t *
139 sc_service_clone(const sc_service_t *svc);
141 /*
142 * sc_store_service:
143 * Add/update a store in the store. If the service, identified by its name,
144 * already exists for the specified host, it will be updated according to the
145 * specified 'service' object. If the referenced host does not exist, an error
146 * will be reported. Else, a new entry will be created in the store. Any
147 * memory required for storing the entry will be allocated an managed by the
148 * store itself. The specified service-object will not be referenced or
149 * further accessed.
150 *
151 * Returns:
152 * - 0 on success
153 * - a positive value if the new entry is older than the currently stored
154 * entry (in this case, no update will happen)
155 * - a negative value on error
156 */
157 int
158 sc_store_service(const sc_service_t *svc);
160 const sc_service_t *
161 sc_store_get_service(const sc_host_t *host, const char *name);
163 int
164 sc_store_dump(FILE *fh);
166 #ifdef __cplusplus
167 } /* extern "C" */
168 #endif
170 #endif /* ! SC_CORE_STORE_H */
172 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */