diff --git a/src/frontend/grammar.y b/src/frontend/grammar.y
index 1cf068538bc13f7686fc12df2d0ba22247f9d553..3a78d2490960a4f20cbe130f34e133852c08036f 100644 (file)
--- a/src/frontend/grammar.y
+++ b/src/frontend/grammar.y
#include "core/store.h"
#include "core/store-private.h"
+#include "core/time.h"
#include "utils/error.h"
#include "utils/llist.h"
%locations
%error-verbose
%expect 0
-%name-prefix="sdb_fe_yy"
+%name-prefix "sdb_fe_yy"
%union {
+ const char *sstr; /* static string */
char *str;
+ sdb_data_t data;
+ sdb_time_t datetime;
+
sdb_llist_t *list;
sdb_conn_node_t *node;
sdb_store_matcher_t *m;
+ sdb_store_expr_t *expr;
}
%start statements
%token SCANNER_ERROR
-%token AND OR NOT WHERE
-
+%token AND OR IS NOT MATCHING FILTER
%token CMP_EQUAL CMP_NEQUAL CMP_REGEX CMP_NREGEX
+%token CMP_LT CMP_LE CMP_GE CMP_GT
+%token CONCAT
+
+%token START END
+
+/* NULL token */
+%token NULL_T
+
+%token FETCH LIST LOOKUP TIMESERIES
%token <str> IDENTIFIER STRING
-%token <node> FETCH LIST LOOKUP
+
+%token <data> INTEGER FLOAT
+
+%token <datetime> DATE TIME
/* Precedence (lowest first): */
%left OR
%left AND
-%left NOT
+%right NOT
%left CMP_EQUAL CMP_NEQUAL
-%left CMP_REGEX CMP_NREGEX
+%left CMP_LT CMP_LE CMP_GE CMP_GT
+%nonassoc CMP_REGEX CMP_NREGEX
+%left CONCAT
+%nonassoc IS
+%left '+' '-'
+%left '*' '/' '%'
+%left '[' ']'
%left '(' ')'
%left '.'
fetch_statement
list_statement
lookup_statement
- expression
+ timeseries_statement
+ matching_clause
+ filter_clause
+ condition
%type <m> matcher
compare_matcher
+%type <expr> expression
+
+%type <sstr> cmp
+
+%type <data> data
+ interval interval_elem
+
+%type <datetime> datetime
+ start_clause end_clause
+
+%destructor { free($$); } <str>
+%destructor { sdb_object_deref(SDB_OBJ($$)); } <node> <m> <expr>
+
%%
statements:
if (parser_mode != SDB_PARSE_DEFAULT) {
sdb_fe_yyerror(&yylloc, scanner,
YY_("syntax error, unexpected statement, "
- "expecting expression"));
+ "expecting condition"));
sdb_object_deref(SDB_OBJ($3));
YYABORT;
}
if (parser_mode != SDB_PARSE_DEFAULT) {
sdb_fe_yyerror(&yylloc, scanner,
YY_("syntax error, unexpected statement, "
- "expecting expression"));
+ "expecting condition"));
sdb_object_deref(SDB_OBJ($1));
YYABORT;
}
}
}
|
- expression
+ condition
{
- /* only accept this in expression parse mode */
- if (! (parser_mode & SDB_PARSE_EXPR)) {
+ /* only accept this in condition parse mode */
+ if (! (parser_mode & SDB_PARSE_COND)) {
sdb_fe_yyerror(&yylloc, scanner,
- YY_("syntax error, unexpected expression, "
+ YY_("syntax error, unexpected condition, "
"expecting statement"));
sdb_object_deref(SDB_OBJ($1));
YYABORT;
|
lookup_statement
|
+ timeseries_statement
+ |
/* empty */
{
$$ = NULL;
;
/*
- * FETCH <hostname>;
+ * FETCH <type> <hostname> [FILTER <condition>];
*
* Retrieve detailed information about a single host.
*/
fetch_statement:
- FETCH STRING
+ FETCH IDENTIFIER STRING filter_clause
{
+ /* TODO: support other types as well */
+ if (strcasecmp($2, "host")) {
+ char errmsg[strlen($2) + 32];
+ snprintf(errmsg, sizeof(errmsg),
+ YY_("unknown data-source %s"), $2);
+ sdb_fe_yyerror(&yylloc, scanner, errmsg);
+ free($2); $2 = NULL;
+ free($3); $3 = NULL;
+ sdb_object_deref(SDB_OBJ($4));
+ YYABORT;
+ }
+
$$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
conn_fetch_t, conn_fetch_destroy));
- CONN_FETCH($$)->name = strdup($2);
+ CONN_FETCH($$)->type = SDB_HOST;
+ CONN_FETCH($$)->name = $3;
+ CONN_FETCH($$)->filter = CONN_MATCHER($4);
$$->cmd = CONNECTION_FETCH;
free($2); $2 = NULL;
}
;
/*
- * LIST;
+ * LIST <type> [FILTER <condition>];
*
* Returns a list of all hosts in the store.
*/
list_statement:
- LIST
+ LIST IDENTIFIER filter_clause
{
- $$ = SDB_CONN_NODE(sdb_object_create_T(/* name = */ NULL,
- sdb_conn_node_t));
+ int type = sdb_store_parse_object_type_plural($2);
+ if (type < 0) {
+ char errmsg[strlen($2) + 32];
+ snprintf(errmsg, sizeof(errmsg),
+ YY_("unknown data-source %s"), $2);
+ sdb_fe_yyerror(&yylloc, scanner, errmsg);
+ free($2); $2 = NULL;
+ sdb_object_deref(SDB_OBJ($3));
+ YYABORT;
+ }
+
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_list_t, conn_list_destroy));
+ CONN_LIST($$)->type = type;
+ CONN_LIST($$)->filter = CONN_MATCHER($3);
$$->cmd = CONNECTION_LIST;
+ free($2); $2 = NULL;
}
;
/*
- * LOOKUP <type> WHERE <expression>;
+ * LOOKUP <type> MATCHING <condition> [FILTER <condition>];
*
- * Returns detailed information about <type> matching expression.
+ * Returns detailed information about <type> matching condition.
*/
lookup_statement:
- LOOKUP IDENTIFIER WHERE expression
+ LOOKUP IDENTIFIER matching_clause filter_clause
{
/* TODO: support other types as well */
if (strcasecmp($2, "hosts")) {
char errmsg[strlen($2) + 32];
snprintf(errmsg, sizeof(errmsg),
- YY_("unknown table %s"), $2);
+ YY_("unknown data-source %s"), $2);
sdb_fe_yyerror(&yylloc, scanner, errmsg);
free($2); $2 = NULL;
+ sdb_object_deref(SDB_OBJ($3));
sdb_object_deref(SDB_OBJ($4));
YYABORT;
}
$$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
conn_lookup_t, conn_lookup_destroy));
- CONN_LOOKUP($$)->matcher = CONN_MATCHER($4);
+ CONN_LOOKUP($$)->type = SDB_HOST;
+ CONN_LOOKUP($$)->matcher = CONN_MATCHER($3);
+ CONN_LOOKUP($$)->filter = CONN_MATCHER($4);
$$->cmd = CONNECTION_LOOKUP;
free($2); $2 = NULL;
}
;
-expression:
+matching_clause:
+ MATCHING condition { $$ = $2; }
+ |
+ /* empty */ { $$ = NULL; }
+
+filter_clause:
+ FILTER condition { $$ = $2; }
+ |
+ /* empty */ { $$ = NULL; }
+
+/*
+ * TIMESERIES <host>.<metric> [START <datetime>] [END <datetime>];
+ *
+ * Returns a time-series for the specified host's metric.
+ */
+timeseries_statement:
+ TIMESERIES STRING '.' STRING start_clause end_clause
+ {
+ $$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
+ conn_ts_t, conn_ts_destroy));
+ CONN_TS($$)->hostname = $2;
+ CONN_TS($$)->metric = $4;
+ CONN_TS($$)->opts.start = $5;
+ CONN_TS($$)->opts.end = $6;
+ $$->cmd = CONNECTION_TIMESERIES;
+ }
+ ;
+
+start_clause:
+ START datetime { $$ = $2; }
+ |
+ /* empty */ { $$ = sdb_gettime() - SDB_INTERVAL_HOUR; }
+
+end_clause:
+ END datetime { $$ = $2; }
+ |
+ /* empty */ { $$ = sdb_gettime(); }
+
+/*
+ * Basic expressions.
+ */
+
+condition:
matcher
{
- sdb_store_matcher_t *m = $1;
- if (! m) {
+ if (! $1) {
/* TODO: improve error reporting */
sdb_fe_yyerror(&yylloc, scanner,
- YY_("syntax error, invalid expression"));
+ YY_("syntax error, invalid condition"));
YYABORT;
}
$$ = SDB_CONN_NODE(sdb_object_create_dT(/* name = */ NULL,
- conn_node_matcher_t, conn_matcher_destroy));
+ conn_matcher_t, conn_matcher_destroy));
$$->cmd = CONNECTION_EXPR;
-
- if ((M(m)->type == MATCHER_HOST)
- || (M(m)->type == MATCHER_AND)
- || (M(m)->type == MATCHER_OR)
- || (M(m)->type == MATCHER_NOT))
- CONN_MATCHER($$)->matcher = m;
- else if (M(m)->type == MATCHER_SERVICE)
- CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
- /* name_re = */ NULL, /* service = */ m,
- /* attr = */ NULL);
- else if (M(m)->type == MATCHER_ATTR)
- CONN_MATCHER($$)->matcher = sdb_store_host_matcher(NULL,
- /* name_re = */ NULL, /* service = */ NULL,
- /* attr = */ m);
- else {
- char errbuf[1024];
- snprintf(errbuf, sizeof(errbuf),
- YY_("syntax error, unexpected matcher type %d"),
- M(m)->type);
- sdb_object_deref(SDB_OBJ($$));
- sdb_fe_yyerror(&yylloc, scanner, errbuf);
- YYABORT;
- }
+ CONN_MATCHER($$)->matcher = $1;
}
;
matcher AND matcher
{
$$ = sdb_store_con_matcher($1, $3);
+ sdb_object_deref(SDB_OBJ($1));
+ sdb_object_deref(SDB_OBJ($3));
}
|
matcher OR matcher
{
$$ = sdb_store_dis_matcher($1, $3);
+ sdb_object_deref(SDB_OBJ($1));
+ sdb_object_deref(SDB_OBJ($3));
}
|
NOT matcher
{
$$ = sdb_store_inv_matcher($2);
+ sdb_object_deref(SDB_OBJ($2));
}
|
compare_matcher
;
/*
- * <object_type>.<object_attr> <op> <value>
+ * <object_type>.<object_attr> <cmp> <value>
*
* Parse matchers comparing object attributes with a value.
*/
compare_matcher:
- IDENTIFIER '.' IDENTIFIER CMP_EQUAL STRING
+ '.' IDENTIFIER cmp expression
+ {
+ $$ = sdb_store_matcher_parse_field_cmp($2, $3, $4);
+ free($2); $2 = NULL;
+ sdb_object_deref(SDB_OBJ($4));
+ }
+ |
+ IDENTIFIER cmp expression
{
- $$ = sdb_store_matcher_parse_cmp($1, $3, "=", $5);
- /* TODO: simplify memory management in the parser */
+ $$ = sdb_store_matcher_parse_cmp($1, NULL, $2, $3);
free($1); $1 = NULL;
- free($3); $3 = NULL;
- free($5); $5 = NULL;
+ sdb_object_deref(SDB_OBJ($3));
}
|
- IDENTIFIER '.' IDENTIFIER CMP_NEQUAL STRING
+ IDENTIFIER '[' IDENTIFIER ']' cmp expression
{
- $$ = sdb_store_matcher_parse_cmp($1, $3, "!=", $5);
- /* TODO: simplify memory management in the parser */
+ $$ = sdb_store_matcher_parse_cmp($1, $3, $5, $6);
free($1); $1 = NULL;
free($3); $3 = NULL;
- free($5); $5 = NULL;
+ sdb_object_deref(SDB_OBJ($6));
}
|
- IDENTIFIER '.' IDENTIFIER CMP_REGEX STRING
+ IDENTIFIER '[' IDENTIFIER ']' IS NULL_T
{
- $$ = sdb_store_matcher_parse_cmp($1, $3, "=~", $5);
+ $$ = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
free($1); $1 = NULL;
free($3); $3 = NULL;
- free($5); $5 = NULL;
}
|
- IDENTIFIER '.' IDENTIFIER CMP_NREGEX STRING
+ IDENTIFIER '[' IDENTIFIER ']' IS NOT NULL_T
{
- $$ = sdb_store_matcher_parse_cmp($1, $3, "!~", $5);
+ sdb_store_matcher_t *m;
+ m = sdb_store_matcher_parse_cmp($1, $3, "IS", NULL);
free($1); $1 = NULL;
free($3); $3 = NULL;
- free($5); $5 = NULL;
+
+ /* sdb_store_inv_matcher return NULL if m==NULL */
+ $$ = sdb_store_inv_matcher(m);
+ sdb_object_deref(SDB_OBJ(m));
+ }
+ ;
+
+expression:
+ '(' expression ')'
+ {
+ $$ = $2;
+ }
+ |
+ expression '+' expression
+ {
+ $$ = sdb_store_expr_create(SDB_DATA_ADD, $1, $3);
+ sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+ sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+ }
+ |
+ expression '-' expression
+ {
+ $$ = sdb_store_expr_create(SDB_DATA_SUB, $1, $3);
+ sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+ sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+ }
+ |
+ expression '*' expression
+ {
+ $$ = sdb_store_expr_create(SDB_DATA_MUL, $1, $3);
+ sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+ sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+ }
+ |
+ expression '/' expression
+ {
+ $$ = sdb_store_expr_create(SDB_DATA_DIV, $1, $3);
+ sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+ sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+ }
+ |
+ expression '%' expression
+ {
+ $$ = sdb_store_expr_create(SDB_DATA_MOD, $1, $3);
+ sdb_object_deref(SDB_OBJ($1)); $1 = NULL;
+ sdb_object_deref(SDB_OBJ($3)); $3 = NULL;
+ }
+ |
+ '.' IDENTIFIER
+ {
+ int field = sdb_store_parse_field_name($2);
+ free($2); $2 = NULL;
+ $$ = sdb_store_expr_fieldvalue(field);
+ }
+ |
+ data
+ {
+ $$ = sdb_store_expr_constvalue(&$1);
+ sdb_data_free_datum(&$1);
+ }
+ ;
+
+cmp:
+ CMP_EQUAL { $$ = "="; }
+ |
+ CMP_NEQUAL { $$ = "!="; }
+ |
+ CMP_REGEX { $$ = "=~"; }
+ |
+ CMP_NREGEX { $$ = "!~"; }
+ |
+ CMP_LT { $$ = "<"; }
+ |
+ CMP_LE { $$ = "<="; }
+ |
+ CMP_GE { $$ = ">="; }
+ |
+ CMP_GT { $$ = ">"; }
+ ;
+
+data:
+ STRING { $$.type = SDB_TYPE_STRING; $$.data.string = $1; }
+ |
+ INTEGER { $$ = $1; }
+ |
+ FLOAT { $$ = $1; }
+ |
+ datetime { $$.type = SDB_TYPE_DATETIME; $$.data.datetime = $1; }
+ |
+ interval { $$ = $1; }
+ ;
+
+datetime:
+ DATE TIME { $$ = $1 + $2; }
+ |
+ DATE { $$ = $1; }
+ |
+ TIME { $$ = $1; }
+ ;
+
+interval:
+ interval interval_elem
+ {
+ $$.data.datetime = $1.data.datetime + $2.data.datetime;
+ }
+ |
+ interval_elem { $$ = $1; }
+ ;
+
+interval_elem:
+ INTEGER IDENTIFIER
+ {
+ sdb_time_t unit = 1;
+
+ unit = sdb_strpunit($2);
+ if (! unit) {
+ char errmsg[strlen($2) + 32];
+ snprintf(errmsg, sizeof(errmsg),
+ YY_("invalid time unit %s"), $2);
+ sdb_fe_yyerror(&yylloc, scanner, errmsg);
+ free($2); $2 = NULL;
+ YYABORT;
+ }
+ free($2); $2 = NULL;
+
+ $$.type = SDB_TYPE_DATETIME;
+ $$.data.datetime = (sdb_time_t)$1.data.integer * unit;
+
+ if ($1.data.integer < 0) {
+ sdb_fe_yyerror(&yylloc, scanner,
+ YY_("syntax error, negative intervals not supported"));
+ YYABORT;
+ }
}
;