2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
1 |
#!/usr/bin/env perl
|
2 |
||
3 |
use strict; |
|
4 |
use warnings; |
|
5 |
use English qw(-no_match_vars); |
|
6 |
||
7 |
my ($file, $attrib, $cmp, $val, $val2) = @ARGV; |
|
8 |
if ( !$file || !$attrib ) { |
|
9 |
warn "Usage: check-query-log-values.pl FILE ATTRIBUTE [CMP VALUE [VALUE]]\n"; |
|
10 |
exit 1; |
|
11 |
}
|
|
12 |
||
13 |
# Slurp the file. There should be only 1 event.
|
|
14 |
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR"; |
|
15 |
my $event_text = ''; |
|
16 |
{
|
|
17 |
local $INPUT_RECORD_SEPARATOR = "\n#\n"; |
|
18 |
while ( defined(my $event = <$fh>) ) { |
|
19 |
$event_text = $event; |
|
20 |
}
|
|
21 |
}
|
|
22 |
close $fh; |
|
23 |
my @event_lines = split /\n/, $event_text; |
|
24 |
||
25 |
my $event = parse_event(@event_lines); |
|
26 |
||
27 |
my @attribs = split /,/, $attrib; |
|
28 |
my @vals = split /,/, $val if $val; |
|
29 |
my @vals2 = split /,/, $val2 if $val2; |
|
30 |
||
31 |
if ( $val ) { |
|
32 |
die "The number of ATTRIBUTEs and VALUEs does not match" |
|
33 |
if $#attribs != $#vals; |
|
34 |
}
|
|
35 |
||
36 |
print "Checking attributes and values of query $event->{arg}\n"; |
|
37 |
||
38 |
for my $i (0..$#attribs) { |
|
39 |
my $attrib = lc $attribs[$i]; |
|
40 |
my $val = $vals[$i]; |
|
41 |
my $val2 = $vals2[$i]; |
|
42 |
||
43 |
if ( $attrib eq 'all' ) { |
|
44 |
dump_event($event); |
|
45 |
}
|
|
46 |
else { |
|
47 |
die "A CMP argument is required if ATTRIBUTE is not ALL" |
|
48 |
unless $cmp; |
|
49 |
die "A VALUE argument is required if ATTRIBUTE is not ALL" |
|
50 |
unless defined $val; |
|
51 |
||
52 |
print "ERROR: attribute $attrib does not exist.\n" |
|
53 |
unless exists $event->{$attrib}; |
|
54 |
||
55 |
$cmp = lc $cmp; |
|
56 |
||
57 |
my $ok = 0; |
|
58 |
my $event_val = $event->{$attrib}; |
|
59 |
if ( $cmp eq '=' || $cmp eq 'equals' ) { |
|
60 |
$ok = 1 if defined $event_val && $event_val eq $val; |
|
61 |
}
|
|
62 |
elsif ( $cmp eq 'matches' ) { |
|
63 |
$ok = 1 if defined $event_val && $event_val =~ m/$val/; |
|
64 |
}
|
|
65 |
elsif ( $cmp eq 'between' ) { |
|
66 |
die "I need a second VALUE argument if CMP is BETWEEN" |
|
67 |
unless $val2; |
|
68 |
$ok = 1 if defined $event_val |
|
69 |
&& $val <= $event_val && $event_val <= $val2; |
|
70 |
}
|
|
71 |
else { |
|
72 |
die "Unknown CMP: $cmp"; |
|
73 |
}
|
|
74 |
||
75 |
if ( $ok ) { |
|
76 |
# Don't print the matches pattern becaues it's probably some
|
|
77 |
# variable value like a timestamp.
|
|
78 |
print "$attrib value $cmp " |
|
79 |
. ($cmp eq 'matches' ? "" : "$val ") |
|
80 |
. ($cmp eq 'between' ? "and $val2 " : "") |
|
81 |
. "OK\n"; |
|
82 |
}
|
|
83 |
else { |
|
84 |
print "$attrib value $event_val does not '$cmp' $val" |
|
85 |
. ($cmp eq 'between' ? " and $val2" : "") . "\n" |
|
86 |
. "Event dump:\n"; |
|
87 |
dump_event($event); |
|
88 |
}
|
|
89 |
}
|
|
90 |
}
|
|
91 |
||
92 |
sub parse_event { |
|
93 |
my (@event_lines) = @_; |
|
94 |
die "I need a event_lines argument" unless @event_lines; |
|
95 |
||
96 |
my $rs = pop @event_lines; |
|
97 |
if ( $rs ne "#" ) { |
|
98 |
print "ERROR: Event does not end with the # record separator.\n"; |
|
99 |
}
|
|
100 |
||
101 |
my @props; |
|
102 |
my $arg = ''; |
|
103 |
my $lineno = 1; |
|
104 |
foreach my $line ( @event_lines ) { |
|
105 |
next if $line =~ m/^$/; |
|
2420.1.1
by Daniel
Make first event line start_ts=TS. Update and expand docu; fix a typo (wrong version). |
106 |
if ( $lineno <= 4 ) { # ts, ints, floats and bools |
2311.1.1
by Daniel Nichter
Add query_log plugin. It's tested and documented. |
107 |
push @props, $line =~ m/([a-z_]+)=(\S+)/g; |
108 |
}
|
|
109 |
elsif ( $lineno == 5 ) { # strings |
|
110 |
push @props, $line =~ m/([a-z_]+)="([^"]*)"/; |
|
111 |
}
|
|
112 |
else { # query |
|
113 |
$arg .= $line; |
|
114 |
}
|
|
115 |
$lineno++; |
|
116 |
}
|
|
117 |
||
118 |
chomp $arg; |
|
119 |
push @props, 'arg', $arg; |
|
120 |
||
121 |
my $event = { @props }; |
|
122 |
||
123 |
return $event; |
|
124 |
}
|
|
125 |
||
126 |
sub dump_event { |
|
127 |
my ($event) = @_; |
|
128 |
foreach my $attrib ( sort keys %$event ) { |
|
129 |
print "$attrib=$event->{$attrib}\n"; |
|
130 |
}
|
|
131 |
}
|