Code

store.h: Reuse the sdb_metric_store_t type for a metric's store field.
[sysdb.git] / doc / sysdbql.7.txt
1 sysdbql(7)
2 ==========
3 :doctype: manpage
5 NAME
6 ----
7 sysdbql - the SysDB query language (SysQL)
9 SYNOPSIS
10 --------
12   LIST hosts;
13   LIST services;
15   FETCH host 'some.host.name';
17   LOOKUP hosts MATCHING attribute['architecture'] = 'amd64'
18                    AND 'backend::collectd::unixsock' in backend
19                FILTER age < 5 * interval;
21   STORE host attribute 'some.host.name'.'key' 123.45
22                        LAST UPDATE 2001-02-03 04:05:06;
24 DESCRIPTION
25 -----------
26 include::sysdb-description.txt[]
28 The SysDB query language (SysQL) is a human-readable format for describing a
29 request to retrieve data from a SysDB daemon. It is very remotely similar to
30 the Standard Query Language (SQL) supported by relational database management
31 systems (RDBMS) but specialized for SysDB's use-case.
33 Besides querying data, SysQL may also be used to store or update objects in
34 SysDB.
36 QUERY COMMANDS
37 --------------
38 Each command is terminated by a semicolon. The following commands are
39 available to retrieve information from SysDB:
41 *LIST* hosts|services|metrics [*FILTER* '<filter_condition>']::
42 Retrieve a sorted (by name) list of all objects of the specified type
43 currently stored in SysDB. The return value is a list of objects including
44 their names, the timestamp of the last update and an approximation of the
45 interval with which the host was updated. When listing services or metrics,
46 the respective objects will be grouped by host. If a filter condition is
47 specified, only objects matching that filter will be included in the reply.
48 See the section "FILTER clause" for more details about how to specify the
49 search and filter conditions.
51 *FETCH* host '<hostname>' [*FILTER* '<filter_condition>']::
52 *FETCH* service|metric '<hostname>'.'<name>' [*FILTER* '<filter_condition>']::
53 Retrieve detailed information about the specified object. The return value
54 includes the full object including all of its attributes and child objects.
55 If the named object does not exist, an error is returned. If a filter
56 condition is specified, only objects matching that filter will be included in
57 the reply. See the section "FILTER clause" for more details about how to
58 specify the search and filter conditions.
60 *LOOKUP* hosts|services|metrics [*MATCHING* '<search_condition>'] [*FILTER* '<filter_condition>']::
61 Retrieve detailed information about all objects matching the specified search
62 condition. The return value is a list of detailed information for each
63 matching object providing the same details as returned by the *FETCH* command.
64 If no object matches the search criteria, it's not considered an error.
65 Instead, an empty list is returned. If a filter condition is specified, only
66 objects matching that filter will be included in the reply. See the sections
67 "MATCHING clause" and "FILTER clause" for more details about how to specify
68 the search and filter conditions.
70 *TIMESERIES* '<hostname>'.'<metric>' [START '<datetime>'] [END '<datetime>']::
71 Retrieve a time-series for the specified host's metric. The data is retrieved
72 from a backend data-store based on information provided by the respective
73 query plugin. The return value includes the actual start and end time of the
74 time-series and one or multiple sequences of time-stamp / value pairs. If the
75 metric does not exist or if the backend data-store is not supported, an error
76 is returned.
78 MATCHING clause
79 ~~~~~~~~~~~~~~~
80 The *MATCHING* clause in a query specifies a boolean expression which is used
81 to match objects based on their fields, attributes, or child objects. Any
82 object for which the expression evaluates to true will be included in the
83 result set according to the rules set by the actual query.
85 FILTER clause
86 ~~~~~~~~~~~~~
87 The *FILTER* clause in a query specifies a boolean expression which is used to
88 filter objects included in the query's response. The filter is applied to
89 hosts, services, and attributes alike and, thus, will usually be based on the
90 core properties of the stored objects. The basic syntax for filter clauses is
91 the same as for matching clauses.
93 Expressions
94 ~~~~~~~~~~~
95 Expressions form the basic building block for all queries. Boolean expressions
96 select objects based on the values of arithmetic expressions which operate on
97 object specific values (fields) or constant values.
99 Boolean expressions may use the following operators:
101 '<expression>' *AND* '<expression>'::
102 '<expression>' *OR* '<expression>'::
103         Evaluates to the logical AND or logical OR of the specified
104         subexpressions.
106 *NOT* '<expression>'::
107         Invert the boolean result of the specified subexpression.
109 '<expression>' '<cmp>' '<expression>'::
110         Compare the values of two expressions using any compare operator (see
111         below). Evaluates to either true or false. Comparing any value with a NULL
112         value always evaluates to false. The data types of the two values have to
113         match except for a few cases as noted in the documentation of the
114         respective operator.
116 *ANY* '<iterable>' '<cmp>' '<expression>'::
117         Compares each element of an iterable using any compare operator. Evaluates
118         to true if any of the elements matches or false if no such elements exist.
119         Otherwise, the same rules as for other comparison operations apply.
120         Attributes, a host's services and metrics, and arrays are iterables.
122 *ALL* '<iterable>' '<cmp>' '<expression>'::
123         *ALL* is similar to the *ANY* operator but matches if all elements match
124         or if no elements exist.
126 '<expression>' *IS NULL*::
127 '<expression>' *IS NOT NULL*::
128         Check whether an expression evaluates to a NULL value (or not). An
129         expression evaluates to NULL if the queried object does not exist (e.g.,
130         when accessing an attribute value).
132 '<expression>' *IS TRUE*::
133 '<expression>' *IS NOT TRUE*::
134 '<expression>' *IS FALSE*::
135 '<expression>' *IS NOT FALSE*::
136         Check whether an expression evaluates to a boolean *true* or *false* value
137         (or not).
139 '<expression>' *IN* '<expression>'::
140 '<expression>' *NOT IN* '<expression>'::
141         Checks whether the value of the first expression is included in the value
142         of the second expression (or not). The second value has to be an array
143         value (e.g., *backend* field) and the type of the first value has to match
144         the array's element type. The first value may also be an array. In this
145         case, the expression evaluates to true if all elements of that array are
146         included in the second array where order does not matter.
148 Parentheses ('()') may be used around subexpressions to group them and enforce
149 precedence.
151 The following fields may be queried:
153 *name*::
154         The canonicalized name of the object. The type of this field is string.
156 *last_update*::
157         The timestamp of the last update of the object. This value is based on
158         information provided by the queried backend if possible. The type of this
159         field is date-time.
161 *age*::
162         The amount of time since the last update of the object. The type of this
163         field is date-time.
165 *interval*::
166         The interval with which the object gets updated. This value is determined
167         automatically based on a moving average determined from the update
168         timestamps of an object. It depends on the update timestamps as provided
169         by the backend (if available) and SysDB's query interval. The type of this
170         field is date-time.
172 *backend*::
173         The name of the backend (plugin) providing the data. The type of this
174         field is array of strings.
176 *attribute[*'<name>'*]*::
177         The value of the object's named attribute. If an attribute of the
178         specified name does not exist, each comparison is treated as if the value
179         does not match. See the documentation for the *IS NULL* and *IS NOT NULL*
180         operators for ways to check if an attribute exists. The value of an
181         attribute may be of any of the supported data types. There is no schema
182         specifying each attribute's data type which means that the parser cannot
183         do any type checks. Thus, in case the data types mismatch when comparing
184         an attribute value with some other value, the two values will be cast to
185         strings before comparing them.
187 *value*::
188         *(Attributes only)* The value of an attribute. Attributes may be accessed
189         by iterating the values of the parent object and this field provides
190         access to its value in that case. See *attribute[*'<name>'*]* above for
191         details about how to handle attribute values.
193 *timeseries*::
194         *(Metrics only)* A boolean value indicating whether a backend data-store
195         for fetching time-series information is known to SysDB. See the section
196         "Metrics and Time-Series" in manpage:sysdb[7] for details.
198 Field expressions may be applied to parent or child nodes. For example, a
199 host's services are child objects and the host is the parent of the service
200 objects. This is done using typed expressions:
202 host|service|metric.'<field>'::
203         Evaluate the field in the context of the respective parent or child.
204         Currently, this is limited to services or metrics referencing their parent
205         host.
207 The following logical operators are supported by SysDB. Unless otherwise
208 noted, the data types of the left hand and right hand side have to match.
210 *=*::
211         Checks two values for equality.
213 *!=*::
214         Checks two values for inequality.
216 *=~*::
217         Checks if a value matches a regular expression. The regex pattern has to
218         be specified as a string but the value may be of any type and will be cast
219         to a string before evaluating the regular expression. SysDB uses POSIX
220         extended regular expressions.
222 *!~*::
223         Checks if a value does not match a regular expression. The same rules
224         apply as for the *=~* operator.
226 *<*, *\<=*, *>=*, *>*::
227         Checks whether a value compares less than, less than or equal to, greater
228         than or equal, or greater than some other value.
230 The following arithmetic operators are supported by SysDB. Unless otherwise
231 noted, the data types of the left hand and right hand side have to match.
233 *+*::
234 *-*::
235         Add or subtract two numeric or date-time values.
237 ***::
238 */*::
239         Multiple or divide two numeric or data-time values. A date-time value may
240         be multiplied with a numeric value or divided by a numeric value. The
241         result will then be a date-time value.
243 *%*::
244         Modulo operator (remainder of division). Same rules for data types apply
245         as for division.
247 *||*::
248         Concatenate string or array values.
250 STORING DATA
251 ------------
252 The *STORE* command may be used to store or update an object in SysDB. Each
253 command is terminated by a semicolon. The following variants are available for
254 storing the different data types:
256 *STORE* host '<name>' [*LAST UPDATE* '<datetime>']::
257 *STORE* service|metric '<hostname>'.'<name>' [*LAST UPDATE* '<datetime>']::
258 *STORE* host attribute '<hostname>'.'<key>' '<value>' [*LAST UPDATE* '<datetime>']::
259 *STORE* service|metric attribute '<hostname>'.'<name>'.'<key>' '<value>' [*LAST UPDATE* '<datetime>']::
260         Store an object of the specified type and name. For services, metrics, and
261         attributes, the name is prepended with the parent object name separated by
262         a dot ('.'). Optionally, the time-stamp of the object's last update may be
263         provided as well. If omitted, the current time on the server will be used
264         instead.
266 *STORE* metric '<hostname>'.'<name>' STORE '<type>' '<id>' [*LAST UPDATE* '<datetime>']::
267         Store a metric and provide information about the metric store associated
268         with it. A metric store describes how to access a metric's data and can be
269         used to retrieve time-series information associated with the metric. See
270         the manpage:sysdb[7] manpage for details.
272         Note that the metric store information will be forwarded to the server
273         unmodified. That is, they need to be specified in a way such that the
274         server can make sense out of them. Else, retrieval of time-series data
275         will fail.
277 DATA TYPES
278 ----------
279 The SysDB query language natively supports various data-types. Constants of
280 all types may be used in any place where a value is expected.
282 *String constants*::
283         A string constant is an arbitrary sequence of characters enclosed in
284         single quotes ('''). Single quotes may be included in a string constant by
285         specifying two adjacent single quotes.
287 *Integer constants*::
288         An integer constant may be specified either as a sequence of digits or in
289         scientific notation written in the form "'a' E 'b'" (without spaces) where
290         'a' and 'b' are integers. A leading plus or minus sign specifies the sign
291         of the constant.
293 *Floating-point constants*::
294         A floating-point constant is a sequence of digits containing a decimal
295         point. Digits before or after the decimal point (but not both) are
296         optional. Floating-point constants may also be specified in scientific
297         notation by appending the letter "E" and a positive or negative integer
298         exponent. A leading plus or minus sign specifies the sign of the constant.
300 *Date and time constants*::
301         A date constant may be specified as 'YYYY-MM-DD' and time constants may be
302         specified as 'HH:MM:SS.nnnnnnnnn' where seconds and nanoseconds are
303         optional.
305 *Interval constants*::
306         An interval may be specified by one or multiple quantity and unit pairs.
307         The quantity may be any integer constant and the unit may be any of the
308         following: *Y* (years), *M* (months), *D* (days), *h* (hours), *m*
309         (minutes), *s* (seconds), *ms* (milliseconds), *us* (microseconds), or
310         *ns* (nanoseconds). Note that years and months are approximations.
312 *Array constants*::
313         An array stores of one or more values of the same type. It may be
314         specified as a comma-separated list of constant values enclosed in square
315         brackets ('[<elem1>,<elem2>,...]'). For each value, the same rules apply
316         as for a regular constant value of that type.
318 RESPONSE FORMAT
319 ---------------
320 The JavaScript Object Notation (JSON) format, as specified in RFC 4627, is
321 used in all query replies from the server. http://www.ietf.org/rfc/rfc4627.txt
323 For all other commands, the reply will be a message string.
325 EXAMPLES
326 --------
327 The following examples illustrate the use of the commands and what their
328 replies look like. The replies are pretty-printed to more easily follow them.
330   LIST hosts;
331   [{
332       "name": "host1.example.com",
333       "last_update": "2001-02-03 04:05:06 +0700",
334       "update_interval": "5m4s",
335       "backend": ['backend::mk-livestatus']
336     },{
337       "name": "host2.example.com",
338       "last_update": "2001-02-03 04:05:06 +0700",
339       "update_interval": "10s",
340       "backend": ['backend::mk-livestatus','backend::collectd::unixsock']
341     }]
343   FETCH host 'host1.example.com';
344   {
345       "name": "host1.example.com",
346       "last_update": "2001-02-03 04:05:06 +0700",
347       "update_interval": "5m4s",
348       "backend": ['backend::mk-livestatus'],
349       "attributes": [{
350           "name": "architecture",
351           "value": "amd64",
352           "last_update": "2001-02-03 04:05:06 +0700",
353           "update_interval": "5m4s",
354           "backend": ['backend::mk-livestatus']
355         },{
356           ...
357         }],
358       "services": [{
359           "name": "some service",
360           "last_update": "2001-02-03 04:05:06 +0700",
361           "update_interval": "5m4s",
362           "backend": ['backend::mk-livestatus']
363         },{
364           ...
365         }]}
367   LOOKUP hosts MATCHING attribute['architecture'] = 'amd64';
368   [{
369       "name": "host1.example.com",
370       "last_update": "2001-02-03 04:05:06 +0700",
371       "update_interval": "5m4s",
372       "backend": ['backend::mk-livestatus'],
373       "attributes": [{
374           "name": "architecture",
375           "value": "amd64",
376           "last_update": "2001-02-03 04:05:06 +0700",
377           "update_interval": "5m4s",
378           "backend": ['backend::mk-livestatus']
379         },{
380           ...
381         }],
382       "services": [{
383           "name": "some service",
384           "last_update": "2001-02-03 04:05:06 +0700",
385           "update_interval": "5m4s",
386           "backend": ['backend::mk-livestatus']
387         },{
388           ...
389     }]},{
390       ...
391     }]
393 SEE ALSO
394 --------
395 manpage:sysdb[1], manpage:sysdb[7]
397 RFC 4627 (JSON): http://www.ietf.org/rfc/rfc4627.txt
399 The SysDB homepage: https://sysdb.io/
401 AUTHOR
402 ------
403 SysDB was written by Sebastian "tokkee" Harl <sh@tokkee.org>.
405 COPYRIGHT
406 ---------
407 Copyright (C) 2012-2014 Sebastian "tokkee" Harl <sh@tokkee.org>
409 This is free software under the terms of the BSD license, see the source for
410 copying conditions. There is NO WARRANTY; not even for MERCHANTABILITY or
411 FITNESS FOR A PARTICULAR PURPOSE.
413 // vim: set tw=78 sw=4 ts=4 noexpandtab spell spelllang=en_us :