Code

9a16e23a4593f58f7ca582728e4828b6d68205aa
[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>' *IN* '<expression>'::
133 '<expression>' *NOT IN* '<expression>'::
134         Checks whether the value of the first expression is included in the value
135         of the second expression (or not). The second value has to be an array
136         value (e.g., *backend* field) and the type of the first value has to match
137         the array's element type. The first value may also be an array. In this
138         case, the expression evaluates to true if all elements of that array are
139         included in the second array where order does not matter.
141 Parentheses ('()') may be used around subexpressions to group them and enforce
142 precedence.
144 The following fields may be queried:
146 *name*::
147         The canonicalized name of the object. The type of this field is string.
149 *last_update*::
150         The timestamp of the last update of the object. This value is based on
151         information provided by the queried backend if possible. The type of this
152         field is date-time.
154 *age*::
155         The amount of time since the last update of the object. The type of this
156         field is date-time.
158 *interval*::
159         The interval with which the object gets updated. This value is determined
160         automatically based on a moving average determined from the update
161         timestamps of an object. It depends on the update timestamps as provided
162         by the backend (if available) and SysDB's query interval. The type of this
163         field is date-time.
165 *backend*::
166         The name of the backend (plugin) providing the data. The type of this
167         field is array of strings.
169 *attribute[*'<name>'*]*::
170         The value of the object's named attribute. If an attribute of the
171         specified name does not exist, each comparison is treated as if the value
172         does not match. See the documentation for the *IS NULL* and *IS NOT NULL*
173         operators for ways to check if an attribute exists. The value of an
174         attribute may be of any of the supported data types. There is no schema
175         specifying each attribute's data type which means that the parser cannot
176         do any type checks. Thus, in case the data types mismatch when comparing
177         an attribute value with some other value, the two values will be cast to
178         strings before comparing them.
180 Field expressions may be applied to parent or child nodes. For example, a
181 host's services are child objects and the host is the parent of the service
182 objects. This is done using typed expressions:
184 host|service|metric.'<field>'::
185         Evaluate the field in the context of the respective parent or child.
186         Currently, this is limited to services or metrics referencing their parent
187         host.
189 The following logical operators are supported by SysDB. Unless otherwise
190 noted, the data types of the left hand and right hand side have to match.
192 *=*::
193         Checks two values for equality.
195 *!=*::
196         Checks two values for inequality.
198 *=~*::
199         Checks if a value matches a regular expression. The regex pattern has to
200         be specified as a string but the value may be of any type and will be cast
201         to a string before evaluating the regular expression. SysDB uses POSIX
202         extended regular expressions.
204 *!~*::
205         Checks if a value does not match a regular expression. The same rules
206         apply as for the *=~* operator.
208 *<*, *\<=*, *>=*, *>*::
209         Checks whether a value compares less than, less than or equal to, greater
210         than or equal, or greater than some other value.
212 The following arithmetic operators are supported by SysDB. Unless otherwise
213 noted, the data types of the left hand and right hand side have to match.
215 *+*::
216 *-*::
217         Add or subtract two numeric or date-time values.
219 ***::
220 */*::
221         Multiple or divide two numeric or data-time values. A date-time value may
222         be multiplied with a numeric value or divided by a numeric value. The
223         result will then be a date-time value.
225 *%*::
226         Modulo operator (remainder of division). Same rules for data types apply
227         as for division.
229 *||*::
230         Concatenate string or array values.
232 STORING DATA
233 ------------
234 The *STORE* command may be used to store or update an object in SysDB. Each
235 command is terminated by a semicolon. The following variants are available for
236 storing the different data types:
238 *STORE* host '<name>' [*LAST UPDATE* '<datetime>']::
239 *STORE* service|metric '<hostname>'.'<name>' [*LAST UPDATE* '<datetime>']::
240 *STORE* host attribute '<hostname>'.'<key>' '<value>' [*LAST UPDATE* '<datetime>']::
241 *STORE* service|metric attribute '<hostname>'.'<name>'.'<key>' '<value>' [*LAST UPDATE* '<datetime>']::
242         Store an object of the specified type and name. For services, metrics, and
243         attributes, the name is prepended with the parent object name separated by
244         a dot ('.'). Optionally, the time-stamp of the object's last update may be
245         provided as well. If omitted, the current time on the server will be used
246         instead.
248 *STORE* metric '<hostname>'.'<name>' STORE '<type>' '<id>' [*LAST UPDATE* '<datetime>']::
249         Store a metric and provide information about the metric store associated
250         with it. A metric store describes how to access a metric's data and can be
251         used to retrieve time-series information associated with the metric. See
252         the manpage:sysdb[7] manpage for details.
254         Note that the metric store information will be forwarded to the server
255         unmodified. That is, they need to be specified in a way such that the
256         server can make sense out of them. Else, retrieval of time-series data
257         will fail.
259 DATA TYPES
260 ----------
261 The SysDB query language natively supports various data-types. Constants of
262 all types may be used in any place where a value is expected.
264 *String constants*::
265         A string constant is an arbitrary sequence of characters enclosed in
266         single quotes ('''). Single quotes may be included in a string constant by
267         specifying two adjacent single quotes.
269 *Integer constants*::
270         An integer constant may be specified either as a sequence of digits or in
271         scientific notation written in the form "'a' E 'b'" (without spaces) where
272         'a' and 'b' are integers. A leading plus or minus sign specifies the sign
273         of the constant.
275 *Floating-point constants*::
276         A floating-point constant is a sequence of digits containing a decimal
277         point. Digits before or after the decimal point (but not both) are
278         optional. Floating-point constants may also be specified in scientific
279         notation by appending the letter "E" and a positive or negative integer
280         exponent. A leading plus or minus sign specifies the sign of the constant.
282 *Date and time constants*::
283         A date constant may be specified as 'YYYY-MM-DD' and time constants may be
284         specified as 'HH:MM:SS.nnnnnnnnn' where seconds and nanoseconds are
285         optional.
287 *Interval constants*::
288         An interval may be specified by one or multiple quantity and unit pairs.
289         The quantity may be any integer constant and the unit may be any of the
290         following: *Y* (years), *M* (months), *D* (days), *h* (hours), *m*
291         (minutes), *s* (seconds), *ms* (milliseconds), *us* (microseconds), or
292         *ns* (nanoseconds). Note that years and months are approximations.
294 *Array constants*::
295         An array stores of one or more values of the same type. It may be
296         specified as a comma-separated list of constant values enclosed in square
297         brackets ('[<elem1>,<elem2>,...]'). For each value, the same rules apply
298         as for a regular constant value of that type.
300 RESPONSE FORMAT
301 ---------------
302 The JavaScript Object Notation (JSON) format, as specified in RFC 4627, is
303 used in all query replies from the server. http://www.ietf.org/rfc/rfc4627.txt
305 For all other commands, the reply will be a message string.
307 EXAMPLES
308 --------
309 The following examples illustrate the use of the commands and what their
310 replies look like. The replies are pretty-printed to more easily follow them.
312   LIST hosts;
313   [{
314       "name": "host1.example.com",
315       "last_update": "2001-02-03 04:05:06 +0700",
316       "update_interval": "5m4s",
317       "backend": ['backend::mk-livestatus']
318     },{
319       "name": "host2.example.com",
320       "last_update": "2001-02-03 04:05:06 +0700",
321       "update_interval": "10s",
322       "backend": ['backend::mk-livestatus','backend::collectd::unixsock']
323     }]
325   FETCH host 'host1.example.com';
326   {
327       "name": "host1.example.com",
328       "last_update": "2001-02-03 04:05:06 +0700",
329       "update_interval": "5m4s",
330       "backend": ['backend::mk-livestatus'],
331       "attributes": [{
332           "name": "architecture",
333           "value": "amd64",
334           "last_update": "2001-02-03 04:05:06 +0700",
335           "update_interval": "5m4s",
336           "backend": ['backend::mk-livestatus']
337         },{
338           ...
339         }],
340       "services": [{
341           "name": "some service",
342           "last_update": "2001-02-03 04:05:06 +0700",
343           "update_interval": "5m4s",
344           "backend": ['backend::mk-livestatus']
345         },{
346           ...
347         }]}
349   LOOKUP hosts MATCHING attribute['architecture'] = 'amd64';
350   [{
351       "name": "host1.example.com",
352       "last_update": "2001-02-03 04:05:06 +0700",
353       "update_interval": "5m4s",
354       "backend": ['backend::mk-livestatus'],
355       "attributes": [{
356           "name": "architecture",
357           "value": "amd64",
358           "last_update": "2001-02-03 04:05:06 +0700",
359           "update_interval": "5m4s",
360           "backend": ['backend::mk-livestatus']
361         },{
362           ...
363         }],
364       "services": [{
365           "name": "some service",
366           "last_update": "2001-02-03 04:05:06 +0700",
367           "update_interval": "5m4s",
368           "backend": ['backend::mk-livestatus']
369         },{
370           ...
371     }]},{
372       ...
373     }]
375 SEE ALSO
376 --------
377 manpage:sysdb[1], manpage:sysdb[7]
379 RFC 4627 (JSON): http://www.ietf.org/rfc/rfc4627.txt
381 The SysDB homepage: https://sysdb.io/
383 AUTHOR
384 ------
385 SysDB was written by Sebastian "tokkee" Harl <sh@tokkee.org>.
387 COPYRIGHT
388 ---------
389 Copyright (C) 2012-2014 Sebastian "tokkee" Harl <sh@tokkee.org>
391 This is free software under the terms of the BSD license, see the source for
392 copying conditions. There is NO WARRANTY; not even for MERCHANTABILITY or
393 FITNESS FOR A PARTICULAR PURPOSE.
395 // vim: set tw=78 sw=4 ts=4 noexpandtab spell spelllang=en_us :