Code

store: Added sdb_store_fetch_timeseries().
[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_field_name:
402  * Parse the name of a stored object's queryable field.
403  *
404  * Returns:
405  *  - the field id on success
406  *  - a negative value else
407  */
408 int
409 sdb_store_parse_field_name(const char *name);
411 /*
412  * sdb_store_matcher_parse_cmp:
413  * Parse a simple compare expression (<obj_type>.<attr> <op> <expression>).
414  *
415  * Returns:
416  *  - a matcher object on success
417  *  - NULL else
418  */
419 sdb_store_matcher_t *
420 sdb_store_matcher_parse_cmp(const char *obj_type, const char *attr,
421                 const char *op, sdb_store_expr_t *expr);
423 /*
424  * sdb_store_matcher_parse_field_cmp:
425  * Parse a simple compare expression for queryable object fields (<field> <op>
426  * <expression>).
427  *
428  * Returns:
429  *  - a matcher object on success
430  *  - NULL else
431  */
432 sdb_store_matcher_t *
433 sdb_store_matcher_parse_field_cmp(const char *name, const char *op,
434                 sdb_store_expr_t *expr);
436 /*
437  * sdb_store_dis_matcher:
438  * Creates a matcher matching the disjunction (logical OR) of two matchers.
439  */
440 sdb_store_matcher_t *
441 sdb_store_dis_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
443 /*
444  * sdb_store_con_matcher:
445  * Creates a matcher matching the conjunction (logical AND) of two matchers.
446  */
447 sdb_store_matcher_t *
448 sdb_store_con_matcher(sdb_store_matcher_t *left, sdb_store_matcher_t *right);
450 /*
451  * sdb_store_con_matcher::
452  * Creates a matcher matching the inverse (logical NOT) of a matcher.
453  */
454 sdb_store_matcher_t *
455 sdb_store_inv_matcher(sdb_store_matcher_t *m);
457 /*
458  * sdb_store_matcher_matches:
459  * Check whether the specified matcher matches the specified store object. If
460  * specified, the filter will be used to preselect objects for further
461  * evaluation. It is applied to any object that's used during the evaluation
462  * of the matcher. Only those objects matching the filter will be considered.
463  *
464  * Note that the filter is applied to all object types (hosts, service,
465  * metric, attribute). Thus, any object-specific matchers are mostly unsuited
466  * for this purpose and, if used, may result in unexpected behavior.
467  *
468  * Returns:
469  *  - 1 if the object matches
470  *  - 0 else
471  */
472 int
473 sdb_store_matcher_matches(sdb_store_matcher_t *m, sdb_store_obj_t *obj,
474                 sdb_store_matcher_t *filter);
476 /*
477  * sdb_store_matcher_tostring:
478  * Format a matcher object as string. This is meant for logging or debugging
479  * purposes.
480  */
481 char *
482 sdb_store_matcher_tostring(sdb_store_matcher_t *m, char *buf, size_t buflen);
484 /*
485  * sdb_store_lookup_cb:
486  * Lookup callback. It is called for each matching object when looking up data
487  * in the store. The lookup aborts if the callback returns non-zero.
488  */
489 typedef int (*sdb_store_lookup_cb)(sdb_store_obj_t *obj, void *user_data);
491 /*
492  * sdb_store_scan:
493  * Look up objects in the store. The specified callback function is called for
494  * each object in the store matching 'm'. The function performs a full scan of
495  * all hosts stored in the database. If specified, the filter will be used to
496  * preselect objects for further evaluation. See the description of
497  * 'sdb_store_matcher_matches' for details.
498  *
499  * Returns:
500  *  - 0 on success
501  *  - a negative value else
502  */
503 int
504 sdb_store_scan(sdb_store_matcher_t *m, sdb_store_matcher_t *filter,
505                 sdb_store_lookup_cb cb, void *user_data);
507 /*
508  * Flags for serialization functions.
509  *
510  * By default, the full object will be included in the serialized output. When
511  * specifying any of the flags, the respective information will be left out.
512  */
513 enum {
514         SDB_SKIP_ATTRIBUTES         = 1 << 0,
515         SDB_SKIP_SERVICES           = 1 << 1,
516         SDB_SKIP_METRICS            = 1 << 2,
517         SDB_SKIP_SERVICE_ATTRIBUTES = 1 << 3,
519         SDB_SKIP_ALL                = 0xffff,
520 };
522 /*
523  * sdb_store_tojson:
524  * Serialize the entire store to JSON and append the result to the specified
525  * buffer. If specified, only objects matching the filter will be included in
526  * the result (see sdb_store_host_tojson for details).
527  *
528  * Returns:
529  *  - 0 on success
530  *  - a negative value on error
531  */
532 int
533 sdb_store_tojson(sdb_strbuf_t *buf, sdb_store_matcher_t *filter, int flags);
535 /*
536  * sdb_store_host_tojson:
537  * Serialize a host object to JSON and append the result to the specified
538  * buffer. If specified, only objects matching the filter will be included in
539  * the result. The filter is applied to each object individually and, thus,
540  * should not be of any object-type specific kind. The filter is never applied
541  * to the specified host object; the caller is responsible for this and for
542  * correctly handling skipped hosts.
543  *
544  * Returns:
545  *  - 0 on success
546  *  - a negative value on error
547  */
548 int
549 sdb_store_host_tojson(sdb_store_obj_t *host, sdb_strbuf_t *buf,
550                 sdb_store_matcher_t *filter, int flags);
552 /*
553  * sdb_store_iter_cb:
554  * Store iterator callback. Iteration stops if the callback returns non-zero.
555  */
556 typedef int (*sdb_store_iter_cb)(sdb_store_obj_t *obj, void *user_data);
558 /*
559  * sdb_store_iterate:
560  * Iterate the entire store, calling the specified callback for each object.
561  * The user_data pointer is passed on to each call of the callback.
562  *
563  * Returns:
564  *  - 0 on success
565  *  - a negative value else
566  */
567 int
568 sdb_store_iterate(sdb_store_iter_cb cb, void *user_data);
570 #ifdef __cplusplus
571 } /* extern "C" */
572 #endif
574 #endif /* ! SDB_CORE_STORE_H */
576 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */