Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
jsonparser: New parser module for JSON parsing.
The new parser supports json-c only, it does not work with json-glib. For this reason, the configure script was modified a little, so that it uses --with-json only. When json-c is found or selected, then both the parser and the template function will be built. If json-glib is found or selected, then only the template function will be available. The summary after configure now lists the determined settings. The new parser is - so far - fairly simple: it parses a JSON with json-c (int, double and string values are supported so far, booleans, arrays and sub-objects aren't yet), and sets the appropriate name-value pair in the LogMessage. Users can configure a prefix to prepend before each key. Usage is as simple as: source s_json { tcp(port(21514) flags(no-parse)); }; destination d_json { file("/tmp/test.json" template("$(format-json --scope dot-nv-pairs)\n")); }; parser p_json { json-parser (prefix(".json.")); }; log { source(s_json); parser(p_json); destination(d_json); }; Signed-off-by: Gergely Nagy <algernon@balabit.hu>
- Loading branch information
Showing
10 changed files
with
456 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
SUBDIRS = afsocket afsql afstreams affile afprog afuser afmongodb csvparser confgen syslogformat pacctformat basicfuncs dbparser tfjson dummy | ||
SUBDIRS = afsocket afsql afstreams affile afprog afuser afmongodb csvparser confgen syslogformat pacctformat basicfuncs dbparser tfjson jsonparser dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
moduledir = @moduledir@ | ||
AM_CPPFLAGS = -I$(top_srcdir)/lib -I../../lib | ||
export top_srcdir | ||
|
||
if ENABLE_JSON_PARSE | ||
module_LTLIBRARIES := libjsonparser.la | ||
libjsonparser_la_SOURCES = \ | ||
jsonparser.c jsonparser.h \ | ||
jsonparser-grammar.y \ | ||
jsonparser-parser.c jsonparser-parser.h \ | ||
jsonparser-plugin.c | ||
|
||
libjsonparser_la_CPPFLAGS = $(AM_CPPFLAGS) | ||
libjsonparser_la_CFLAGS = $(JSON_CFLAGS) | ||
libjsonparser_la_LIBADD = $(MODULE_DEPS_LIBS) | ||
libjsonparser_la_LDFLAGS = $(MODULE_LDFLAGS) $(JSON_LIBS) | ||
|
||
BUILT_SOURCES = jsonparser-grammar.y jsonparser-grammar.c jsonparser-grammar.h | ||
EXTRA_DIST = $(BUILT_SOURCES) jsonparser-grammar.ym | ||
endif | ||
|
||
include $(top_srcdir)/build/lex-rules.am |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011 Gergely Nagy <algernon@balabit.hu> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
*/ | ||
|
||
%code top { | ||
#include "jsonparser-parser.h" | ||
|
||
} | ||
|
||
|
||
%code { | ||
|
||
#include "jsonparser.h" | ||
#include "cfg-parser.h" | ||
#include "jsonparser-grammar.h" | ||
#include "syslog-names.h" | ||
#include "messages.h" | ||
|
||
extern LogParser *last_parser; | ||
|
||
} | ||
|
||
%name-prefix "jsonparser_" | ||
|
||
/* this parameter is needed in order to instruct bison to use a complete | ||
* argument list for yylex/yyerror */ | ||
|
||
%lex-param {CfgLexer *lexer} | ||
%parse-param {CfgLexer *lexer} | ||
%parse-param {LogParser **instance} | ||
%parse-param {gpointer arg} | ||
|
||
/* INCLUDE_DECLS */ | ||
|
||
%token KW_JSON_PARSER | ||
%token KW_PREFIX | ||
|
||
%type <ptr> parser_expr_json | ||
|
||
%% | ||
|
||
start | ||
: LL_CONTEXT_PARSER parser_expr_json { YYACCEPT; } | ||
; | ||
|
||
|
||
parser_expr_json | ||
: KW_JSON_PARSER '(' | ||
{ | ||
last_parser = *instance = (LogParser *) log_json_parser_new(); | ||
} | ||
parser_json_opts | ||
')' { $$ = last_parser; } | ||
; | ||
|
||
parser_json_opts | ||
: parser_json_opt parser_json_opts | ||
| | ||
; | ||
|
||
parser_json_opt | ||
: KW_PREFIX '(' string ')' { log_json_parser_set_prefix(last_parser, $3); free ($3); } | ||
| | ||
; | ||
|
||
/* INCLUDE_RULES */ | ||
|
||
%% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011 Gergely Nagy <algernon@balabit.hu> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
*/ | ||
|
||
#include "jsonparser.h" | ||
#include "cfg-parser.h" | ||
#include "jsonparser-grammar.h" | ||
|
||
extern int jsonparser_debug; | ||
|
||
int jsonparser_parse(CfgLexer *lexer, LogParser **instance, gpointer arg); | ||
|
||
static CfgLexerKeyword jsonparser_keywords[] = | ||
{ | ||
{ "json_parser", KW_JSON_PARSER, }, | ||
{ "prefix", KW_PREFIX, }, | ||
{ NULL } | ||
}; | ||
|
||
CfgParser jsonparser_parser = | ||
{ | ||
#if ENABLE_DEBUG | ||
.debug_flag = &jsonparser_debug, | ||
#endif | ||
.name = "jsonparser", | ||
.keywords = jsonparser_keywords, | ||
.parse = (gint (*)(CfgLexer *, gpointer *, gpointer)) jsonparser_parse, | ||
.cleanup = (void (*)(gpointer)) log_pipe_unref, | ||
}; | ||
|
||
CFG_PARSER_IMPLEMENT_LEXER_BINDING(jsonparser_, LogParser **) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011 Gergely Nagy <algernon@balabit.hu> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
*/ | ||
|
||
#ifndef JSONPARSER_PARSER_H_INCLUDED | ||
#define JSONPARSER_PARSER_H_INCLUDED | ||
|
||
#include "cfg-parser.h" | ||
#include "cfg-lexer.h" | ||
#include "logparser.h" | ||
|
||
extern CfgParser jsonparser_parser; | ||
|
||
CFG_PARSER_DECLARE_LEXER_BINDING(jsonparser_, LogParser **) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright (c) 2011 BalaBit IT Ltd, Budapest, Hungary | ||
* Copyright (c) 2011 Gergely Nagy <algernon@balabit.hu> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published | ||
* by the Free Software Foundation, or (at your option) any later version. | ||
* | ||
* This library is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this library; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
* | ||
* As an additional exemption you are allowed to compile & link against the | ||
* OpenSSL libraries as published by the OpenSSL project. See the file | ||
* COPYING for details. | ||
*/ | ||
|
||
#include "cfg-parser.h" | ||
#include "plugin.h" | ||
#include "jsonparser.h" | ||
|
||
extern CfgParser jsonparser_parser; | ||
|
||
static Plugin jsonparser_plugins[] = | ||
{ | ||
{ | ||
.type = LL_CONTEXT_PARSER, | ||
.name = "json-parser", | ||
.parser = &jsonparser_parser, | ||
}, | ||
}; | ||
|
||
gboolean | ||
jsonparser_module_init(GlobalConfig *cfg, CfgArgs *args) | ||
{ | ||
plugin_register(cfg, jsonparser_plugins, G_N_ELEMENTS(jsonparser_plugins)); | ||
return TRUE; | ||
} | ||
|
||
const ModuleInfo module_info = | ||
{ | ||
.canonical_name = "jsonparser", | ||
.version = VERSION, | ||
.description = "The jsonparser module provides JSON parsing support for syslog-ng.", | ||
.core_revision = SOURCE_REVISION, | ||
.plugins = jsonparser_plugins, | ||
.plugins_len = G_N_ELEMENTS(jsonparser_plugins), | ||
}; |
Oops, something went wrong.