hi David,
in order to give some idea how JSON can be handled with the current
alpha version, here is a ruleset which parses arbitrary single line
json messages. Fortunately, there is an excellent JSON module in CPAN
which is also a standard part of recent Perl installation. One feature
it lacks, though, is the flattening mechanism, so I've written this
myself. Hopefully some flattening routine will be part of the JSON
module in the future, and then the below ruleset would become really
short. In order to make it look more compact, the flatten() routine
could be put into a separate Perl module file and included with
'require' like JSON modul itself.
type=single
ptype=regexp
pattern=(SEC_STARTUP|SEC_RESTART)
desc=load JSON module and define flattening function
action=eval %o ( require JSON ); eval %o ( \
\
sub flatten { \
my($ref) = $_[0]; \
my($ret) = $_[1]; \
my($prefix) = $_[2]; \
my($key, $i); \
\
if (ref($ref) eq "HASH") { \
\
foreach $key (keys %{$ref}) { \
if (ref($ref->{$key}) eq "") { \
$ret->{$prefix . $key} = $ref->{$key}; \
} elsif (ref($ref->{$key}) eq "HASH") { \
flatten($ref->{$key}, $ret, $prefix . $key . "!"); \
} elsif (ref($ref->{$key}) eq "ARRAY") { \
flatten($ref->{$key}, $ret, $prefix . $key . "!"); \
} else { \
$ret->{$prefix . $key} = ${$ref->{$key}}; \
} \
} \
\
} elsif (ref($ref) eq "ARRAY") { \
\
for ($i = 0; $i < scalar(@{$ref}); ++$i) { \
if (ref($ref->[$i]) eq "") { \
$ret->{$prefix . $i} = $ref->[$i]; \
} elsif (ref($ref->[$i]) eq "HASH") { \
flatten($ref->[$i], $ret, $prefix . $i . "!"); \
} elsif (ref($ref->[$i]) eq "ARRAY") { \
flatten($ref->[$i], $ret, $prefix . $i . "!"); \
} else { \
$ret->{$prefix . $i} = ${$ref->[$i]}; \
} \
} \
\
} \
} \
)
type=single
ptype=PerlFunc
pattern=sub { \
chomp($_[0]); my($ptr) = JSON::decode_json($_[0]); \
my(%hash) = (); flatten($ptr, \%hash, ""); \
return \%hash; }
desc=find hostname and first 2 IP's from json structure
action=write - $+{host!ip!0} $+{host!ip!1} $+{host!dns}
If I feed SEC with the following structure
{"host":{"ip":["127.0.0.1","127.0.0.2"],"dns":"localhost"}}
the following line is echoed back:
127.0.0.1 127.0.0.2 localhost
Of course, since the above parsing is done without any knowledge about
the field names, the user has to know which field names are available.
with kind regards,
risto
2012/11/4 <[email protected]>:
> On Sun, 4 Nov 2012, [email protected] wrote:
>
>> On Sun, 4 Nov 2012, Risto Vaarandi wrote:
>>
>>> 2012/10/20 <[email protected]>:
>>>>
>>>> On Sat, 20 Oct 2012, Risto Vaarandi wrote:
>>>>
>>> hi David,
>>> I have completed some work on the alpha version of the next release,
>>> and it supports returning hashes from PerlFunc patterns. If the user
>>> defines a custom parsing scheme inside PerlFunc pattern, keyword=value
>>> pairs can now be returned as named match variables with a single hash.
>>> However, there are still few things I'd like to clarify. Perl regexp
>>> named captures assume that a name consist of alphanumerals and
>>> underscores (i.e., each name character must match \w) which is also
>>> the case for LumberJack. Nevertheless, Perl also assumes that named
>>> capture never begins with a number. Does LumberJack impose a similar
>>> restriction? This restriction allows for one nice optimization --
>>> internally, named and numbered match variables can be stored in one
>>> hash table (for the latter, numbers are used as keywords).
>>> As a separator, I think it is quite easy to allow the use of ! inside
>>> variable names.
>>
>>
>> Lumberjack hasn't specified this, but since this is a very common
>> limitation for variable names, I think it's a very reasonable thing to do.
>>
>> Nothing that Lumberjack has specified would violate this.
>>
>> I'll post to that list?
>
>
> The initial reaction I'm gettig back is that this wasn't discussed, but they
> have are very strongly in favor of prohibiting leading numbers in field
> names.
>
> David Lang
------------------------------------------------------------------------------
LogMeIn Central: Instant, anywhere, Remote PC access and management.
Stay in control, update software, and manage PCs from one command center
Diagnose problems and improve visibility into emerging IT issues
Automate, monitor and manage. Do more in less time with Central
http://p.sf.net/sfu/logmein12331_d2d
_______________________________________________
Simple-evcorr-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/simple-evcorr-users