Code

When querying services/metrics skip hosts without such children.
[sysdb.git] / src / include / core / store.h
1 /*
2  * SysDB - 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 SDB_CORE_STORE_H
29 #define SDB_CORE_STORE_H 1
31 #include "sysdb.h"
32 #include "core/object.h"
33 #include "core/data.h"
34 #include "core/time.h"
35 #include "core/timeseries.h"
36 #include "utils/strbuf.h"
38 #include <stdio.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
44 /*
45  * Store object types.
46  */
47 enum {
48         SDB_HOST = 1,
49         SDB_SERVICE,
50         SDB_METRIC,
51         SDB_ATTRIBUTE,
52 };
53 #define SDB_STORE_TYPE_TO_NAME(t) \
54         (((t) == SDB_HOST) ? "host" \
55                 : ((t) == SDB_SERVICE) ? "service" \
56                 : ((t) == SDB_METRIC) ? "metric" \
57                 : ((t) == SDB_ATTRIBUTE) ? "attribute" : "unknown")
59 /*
60  * sdb_store_obj_t represents the super-class of any object stored in the
61  * database. It inherits from sdb_object_t and may safely be cast to a generic
62  * object to access its name.
63  */
64 struct sdb_store_obj;
65 typedef struct sdb_store_obj sdb_store_obj_t;
67 /*
68  * Queryable fields of a stored object.
69  */
70 enum {
71         SDB_FIELD_LAST_UPDATE = 1, /* datetime */
72         SDB_FIELD_AGE,             /* datetime */
73         SDB_FIELD_INTERVAL,        /* datetime */
74         SDB_FIELD_BACKEND,         /* string */
75 };
77 #define SDB_FIELD_TO_NAME(f) \
78         (((f) == SDB_FIELD_LAST_UPDATE) ? "last-update" \
79                 : ((f) == SDB_FIELD_AGE) ? "age" \
80                 : ((f) == SDB_FIELD_INTERVAL) ? "interval" \
81                 : ((f) == SDB_FIELD_BACKEND) ? "backend" : "unknown")
83 /*
84  * sdb_store_clear:
85  * Clear the entire store and remove all stored objects.
86  */
87 void
88 sdb_store_clear(void);
90 /*
91  * sdb_store_host:
92  * Add/update a host in the store. If the host, identified by its
93  * canonicalized name, already exists, it will be updated according to the
94  * specified name and timestamp. Else, a new entry will be created in the
95  * store. Any memory required for storing the entry will be allocated an
96  * managed by the store itself.
97  *
98  * Returns:
99  *  - 0 on success
100  *  - a positive value if the new entry is older than the currently stored
101  *    entry (in this case, no update will happen)
102  *  - a negative value on error
103  */
104 int
105 sdb_store_host(const char *name, sdb_time_t last_update);
107 /*
108  * sdb_store_has_host:
109  * sdb_store_get_host:
110  * Query the store for a host by its (canonicalized) name.
111  *
112  * sdb_store_get_host increments the ref count of the host object. The caller
113  * needs to deref it when no longer using it.
114  */
115 _Bool
116 sdb_store_has_host(const char *name);
118 sdb_store_obj_t *
119 sdb_store_get_host(const char *name);
121 /*
122  * sdb_store_attribute:
123  * Add/update a host's attribute in the store. If the attribute, identified by
124  * its key, already exists for the specified host, it will be updated to the
125  * specified values. If the referenced host does not exist, an error will be
126  * reported. Else, a new entry will be created in the store. Any memory
127  * required for storing the entry will be allocated and managed by the store
128  * itself.
129  *
130  * Returns:
131  *  - 0 on success
132  *  - a positive value if the new entry is older than the currently stored
133  *    entry (in this case, no update will happen)
134  *  - a negative value on error
135  */
136 int
137 sdb_store_attribute(const char *hostname,
138                 const char *key, const sdb_data_t *value,
139                 sdb_time_t last_update);
141 /*
142  * sdb_store_service:
143  * Add/update a service 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.
149  *
150  * Returns:
151  *  - 0 on success
152  *  - a positive value if the new entry is older than the currently stored
153  *    entry (in this case, no update will happen)
154  *  - a negative value on error
155  */
156 int
157 sdb_store_service(const char *hostname, const char *name,
158                 sdb_time_t last_update);
160 /*
161  * sdb_store_service_attr:
162  * Add/update a service's attribute in the store. If the attribute, identified
163  * by its key, already exists for the specified service, it will be updated to
164  * the specified value. If the references service (for the specified host)
165  * does not exist, an error will be reported. Any memory required for storing
166  * the entry will be allocated and managed by the store itself.
167  *
168  * Returns:
169  *  - 0 on success
170  *  - a positive value if the new entry is older than the currently stored
171  *    entry (in this case, no update will happen)
172  *  - a negative value on error
173  */
174 int
175 sdb_store_service_attr(const char *hostname, const char *service,
176                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
178 /*
179  * A metric store describes how to access a metric's data.
180  */
181 typedef struct {
182         const char *type;
183         const char *id;
184 } sdb_metric_store_t;
186 /*
187  * sdb_store_metric:
188  * Add/update a metric in the store. If the metric, identified by its name,
189  * already exists for the specified host, it will be updated according to the
190  * specified 'metric' object. If the referenced host does not exist, an error
191  * will be reported. Else, a new entry will be created in the store. Any
192  * memory required for storing the entry will be allocated an managed by the
193  * store itself.
194  *
195  * If specified, the metric store describes where to access the metric's data.
196  *
197  * Returns:
198  *  - 0 on success
199  *  - a positive value if the new entry is older than the currently stored
200  *    entry (in this case, no update will happen)
201  *  - a negative value on error
202  */
203 int
204 sdb_store_metric(const char *hostname, const char *name,
205                 sdb_metric_store_t *store, sdb_time_t last_update);
207 /*
208  * sdb_store_metric_attr:
209  * Add/update a metric's attribute in the store. If the attribute, identified
210  * by its key, already exists for the specified metric, it will be updated to
211  * the specified value. If the references metric (for the specified host)
212  * does not exist, an error will be reported. Any memory required for storing
213  * the entry will be allocated and managed by the store itself.
214  *
215  * Returns:
216  *  - 0 on success
217  *  - a positive value if the new entry is older than the currently stored
218  *    entry (in this case, no update will happen)
219  *  - a negative value on error
220  */
221 int
222 sdb_store_metric_attr(const char *hostname, const char *metric,
223                 const char *key, const sdb_data_t *value, sdb_time_t last_update);
225 /*
226  * sdb_store_fetch_timeseries:
227  * Fetch the time-series described by the specified host's metric and
228  * serialize it as JSON into the provided string buffer.
229  *
230  * Returns:
231  *  - 0 on success
232  *  - a negative value else
233  */
234 int
235 sdb_store_fetch_timeseries(const char *hostname, const char *metric,
236                 sdb_timeseries_opts_t *opts, sdb_strbuf_t *buf);
238 /*
239  * sdb_store_get_field:
240  * Get the value of a stored object's queryable field. The caller is
241  * responsible for freeing any dynamically allocated memory possibly stored in
242  * the returned value.
243  *
244  * Note: Retrieving the backend this way is not currently supported.
245  *
246  * Returns:
247  *  - 0 on success
248  *  - a negative value else
249  */
250 int
251 sdb_store_get_field(sdb_store_obj_t *obj, int field, sdb_data_t *res);
253 /*
254  * Expressions specify arithmetic expressions.
255  *
256  * A expression object inherits from sdb_object_t and, thus, may safely be
257  * cast to a generic object.
258  */
259 struct sdb_store_expr;
260 typedef struct sdb_store_expr sdb_store_expr_t;
261 #define SDB_STORE_EXPR(obj) ((sdb_store_expr_t *)(obj))
263 /*
264  * sdb_store_expr_create:
265  * Creates an arithmetic expression implementing the specified operator on the
266  * specified left and right operand.
267  *
268  * Returns:
269  *  - an expression object on success
270  *  - NULL else
271  */
272 sdb_store_expr_t *
273 sdb_store_expr_create(int op, sdb_store_expr_t *left, sdb_store_expr_t *right);
275 /*
276  * sdb_store_expr_fieldvalue:
277  * Creates an expression which evaluates to the value of the specified
278  * queryable field of a stored object.
279  *
280  * Returns:
281  *  - an expression object on success
282  *  - NULL else
283  */
284 sdb_store_expr_t *
285 sdb_store_expr_fieldvalue(int field);
287 /*
288  * sdb_store_expr_constvalue:
289  * Creates an expression which evaluates to the specified constant value.
290  *
291  * Returns:
292  *  - an expression object on success
293  *  - NULL else
294  */
295 sdb_store_expr_t *
296 sdb_store_expr_constvalue(const sdb_data_t *value);
298 /*
299  * sdb_store_expr_eval:
300  * Evaluate an expression for the specified stored object and stores the
301  * result in 'res'. The result's value will be allocated dynamically if
302  * necessary and, thus, should be free'd by the caller (e.g. using
303  * sdb_data_free_datum). The object may be NULL, in which case the expression
304  * needs to evaluate to a constant value.
305  *
306  * Returns:
307  *  - 0 on success
308  *  - a negative value else
309  */
310 int
311 sdb_store_expr_eval(sdb_store_expr_t *expr, sdb_store_obj_t *obj,
312                 sdb_data_t *res);
314 /*
315  * Conditionals may be used to lookup hosts from the store based on a
316  * conditional expression.
317  *
318  * A conditional object inherits from sdb_object_t and, thus, may safely be
319  * cast to a generic object.
320  */
321 struct sdb_store_cond;
322 typedef struct sdb_store_cond sdb_store_cond_t;
323 #define SDB_STORE_COND(obj) ((sdb_store_cond_t *)(obj))
325 /*
326  * sdb_store_attr_cond:
327  * Creates a conditional based on attribute values. The value of stored
328  * attributes is compared against the value the expression evaluates to. See
329  * sdb_data_cmp for details about the comparison.
330  */
331 sdb_store_cond_t *
332 sdb_store_attr_cond(const char *name, sdb_store_expr_t *expr);
334 /*
335  * sdb_store_obj_cond:
336  * Creates a conditional based on queryable object fields. The respective
337  * field of *any* object type is compared against the value the expression
338  * evaluates to.
339  */
340 sdb_store_cond_t *
341 sdb_store_obj_cond(int field, sdb_store_expr_t *expr);
343 /*
344  * Store matchers may be used to lookup hosts from the store based on their
345  * various attributes. Service and attribute matchers are applied to a host's
346  * services and attributes and evaluate to true if *any* service or attribute
347  * matches.
348  *
349  * A store matcher object inherits from sdb_object_t and, thus, may safely be
350  * cast to a generic object.
351  */
352 struct sdb_store_matcher;
353 typedef struct sdb_store_matcher sdb_store_matcher_t;
354 #define SDB_STORE_MATCHER(obj) ((sdb_store_matcher_t *)(obj))
356 /*
357  * sdb_store_name_matcher:
358  * Creates a matcher matching by the specified object type's name. If 're' is
359  * true, the specified name is treated as a POSIX extended regular expression.
360  * Else, the exact name has to match (case-insensitive).
361  */
362 sdb_store_matcher_t *
363 sdb_store_name_matcher(int type, const char *name, _Bool re);
365 /*
366  * sdb_store_attr_matcher:
367  * Creates a matcher matching attributes based on their value. If 're' is
368  * true, the specified name is treated as a POSIX extended regular expression.
369  * Else, the exact name has to match (case-insensitive).
370  */
371 sdb_store_matcher_t *
372 sdb_store_attr_matcher(const char *name, const char *value, _Bool re);
374 /*
375  * sdb_store_isnull_matcher:
376  * Creates a matcher matching "missing" attributes.
377  */
378 sdb_store_matcher_t *
379 sdb_store_isnull_matcher(const char *attr_name);
381 /*
382  * sdb_store_lt_matcher, sdb_store_le_matcher, sdb_store_eq_matcher,
383  * sdb_store_ge_matcher, sdb_store_gt_matcher:
384  * Creates a matcher based on a conditional. The matcher matches objects for
385  * which the conditional evaluates the object to compare less than, less or
386  * equal, equal, greater or equal, or greater than the conditional's value
387  * repsectively.
388  */
389 sdb_store_matcher_t *
390 sdb_store_lt_matcher(sdb_store_cond_t *cond);
391 sdb_store_matcher_t *
392 sdb_store_le_matcher(sdb_store_cond_t *cond);
393 sdb_store_matcher_t *
394 sdb_store_eq_matcher(sdb_store_cond_t *cond);
395 sdb_store_matcher_t *
396 sdb_store_ge_matcher(sdb_store_cond_t *cond);
397 sdb_store_matcher_t *
398 sdb_store_gt_matcher(sdb_store_cond_t *cond);
400 /*
401  * sdb_store_parse_object_type_plural:
402  * Parse the type name (plural) of a stored object.
403  *
404  * Returns:
405  *  - the object type on success
406  *  - a negative value else
407  */
408 int
409 sdb_store_parse_object_type_plural(const char *name);
411 /*
412  * sdb_store_parse_field_name:
413  * Parse the name of a stored object's queryable field.
414  *
415  * Returns:
416  *  - the field id on success
417  *  - a negative value else
418  */
419 int
420 sdb_store_parse_field_name(const char *name);
422 /*
423  * sdb_store_matcher_parse_cmp:
424  * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
425  *
426  * Returns:
427  *  - a matcher object on success
428  *  - NULL else
429  */
430 sdb_store_matcher_t *
431 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
432                 const char *op, sdb_store_expr_t *expr);
434 /*
435  * sdb_store_matcher_parse_field_cmp:
436  * Parse a simple compare expression for queryable object fields (<field> <op>
437  * <expression>).
438  *
439  * Returns:
440  *  - a matcher object on success
441  *  - NULL else
442  */
443 sdb_store_matcher_t *
444 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
445                 sdb_store_expr_t *expr);
447 /*
448  * sdb_store_dis_matcher:
449  * Creates a matcher matching the disjunction (logical OR) of two matchers.
450  */
451 sdb_store_matcher_t *
452 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
454 /*
455  * sdb_store_con_matcher:
456  * Creates a matcher matching the conjunction (logical AND) of two matchers.
457  */
458 sdb_store_matcher_t *
459 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
461 /*
462  * sdb_store_con_matcher::
463  * Creates a matcher matching the inverse (logical NOT) of a matcher.
464  */
465 sdb_store_matcher_t *
466 sdb_store_inv_matcher(sdb_store_matcher_t *m);
468 /*
469  * sdb_store_matcher_matches:
470  * Check whether the specified matcher matches the specified store object. If
471  * specified, the filter will be used to preselect objects for further
472  * evaluation. It is applied to any object that's used during the evaluation
473  * of the matcher. Only those objects matching the filter will be considered.
474  *
475  * Note that the filter is applied to all object types (hosts, service,
476  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
477  * for this purpose and, if used, may result in unexpected behavior.
478  *
479  * Returns:
480  *  - 1 if the object matches
481  *  - 0 else
482  */
483 int
484 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
485                 sdb_store_matcher_t *filter);
487 /*
488  * sdb_store_matcher_tostring:
489  * Format a matcher object as string. This is meant for logging or debugging
490  * purposes.
491  */
492 char *
493 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
495 /*
496  * sdb_store_lookup_cb:
497  * Lookup callback. It is called for each matching object when looking up data
498  * in the store. The lookup aborts if the callback returns non-zero.
499  */
500 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
502 /*
503  * sdb_store_scan:
504  * Look up objects in the store. The specified callback function is called for
505  * each object in the store matching 'm'. The function performs a full scan of
506  * all hosts stored in the database. If specified, the filter will be used to
507  * preselect objects for further evaluation. See the description of
508  * 'sdb_store_matcher_matches' for details.
509  *
510  * Returns:
511  *  - 0 on success
512  *  - a negative value else
513  */
514 int
515 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
516                 sdb_store_lookup_cb cb, void *user_data);
518 /*
519  * Flags for serialization functions.
520  *
521  * By default, the full host object will be included in the serialized output.
522  * When specifying any of the flags, the respective information will be left
523  * out. The SKIP_EMPTY flags may be used to skip host objects entirely.
524  */
525 enum {
526         SDB_SKIP_ATTRIBUTES         = 1 << 0,
527         SDB_SKIP_SERVICES           = 1 << 1,
528         SDB_SKIP_METRICS            = 1 << 2,
529         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
531         SDB_SKIP_ALL                = (1 << 8) - 1,
533         /* skip hosts if they do not reference any services/metrics */
534         SDB_SKIP_EMPTY_SERVICES     = 1 << 8,
535         SDB_SKIP_EMPTY_METRICS      = 1 << 9,
536 };
538 /*
539  * sdb_store_tojson:
540  * Serialize the entire store to JSON and append the result to the specified
541  * buffer. If specified, only objects matching the filter will be included in
542  * the result (see sdb_store_host_tojson for details).
543  *
544  * Returns:
545  *  - 0 on success
546  *  - a negative value on error
547  */
548 int
549 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
551 /*
552  * sdb_store_host_tojson:
553  * Serialize a host object to JSON and append the result to the specified
554  * buffer. If specified, only objects matching the filter will be included in
555  * the result. The filter is applied to each object individually and, thus,
556  * should not be of any object-type specific kind. The filter is never applied
557  * to the specified host object; the caller is responsible for this and for
558  * correctly handling skipped hosts.
559  *
560  * Returns:
561  *  - 0 on success
562  *  - a negative value on error
563  */
564 int
565 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
566                 sdb_store_matcher_t *filter, int flags);
568 /*
569  * sdb_store_iter_cb:
570  * Store iterator callback. Iteration stops if the callback returns non-zero.
571  */
572 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
574 /*
575  * sdb_store_iterate:
576  * Iterate the entire store, calling the specified callback for each object.
577  * The user_data pointer is passed on to each call of the callback.
578  *
579  * Returns:
580  *  - 0 on success
581  *  - a negative value else
582  */
583 int
584 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
586 #ifdef __cplusplus
587 } /* extern "C" */
588 #endif
590 #endif /* ! SDB_CORE_STORE_H */
592 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */