5
use English qw(-no_match_vars);
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";
13
# Slurp the file. There should be only 1 event.
14
open my $fh, "<", $file or die "Cannot open $file: $OS_ERROR";
17
local $INPUT_RECORD_SEPARATOR = "\n#\n";
18
while ( defined(my $event = <$fh>) ) {
23
my @event_lines = split /\n/, $event_text;
25
my $event = parse_event(@event_lines);
27
my @attribs = split /,/, $attrib;
28
my @vals = split /,/, $val if $val;
29
my @vals2 = split /,/, $val2 if $val2;
32
die "The number of ATTRIBUTEs and VALUEs does not match"
33
if $#attribs != $#vals;
36
print "Checking attributes and values of query $event->{arg}\n";
38
for my $i (0..$#attribs) {
39
my $attrib = lc $attribs[$i];
41
my $val2 = $vals2[$i];
43
if ( $attrib eq 'all' ) {
47
die "A CMP argument is required if ATTRIBUTE is not ALL"
49
die "A VALUE argument is required if ATTRIBUTE is not ALL"
52
print "ERROR: attribute $attrib does not exist.\n"
53
unless exists $event->{$attrib};
58
my $event_val = $event->{$attrib};
59
if ( $cmp eq '=' || $cmp eq 'equals' ) {
60
$ok = 1 if defined $event_val && $event_val eq $val;
62
elsif ( $cmp eq 'matches' ) {
63
$ok = 1 if defined $event_val && $event_val =~ m/$val/;
65
elsif ( $cmp eq 'between' ) {
66
die "I need a second VALUE argument if CMP is BETWEEN"
68
$ok = 1 if defined $event_val
69
&& $val <= $event_val && $event_val <= $val2;
72
die "Unknown CMP: $cmp";
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 " : "")
84
print "$attrib value $event_val does not '$cmp' $val"
85
. ($cmp eq 'between' ? " and $val2" : "") . "\n"
93
my (@event_lines) = @_;
94
die "I need a event_lines argument" unless @event_lines;
96
my $rs = pop @event_lines;
98
print "ERROR: Event does not end with the # record separator.\n";
104
foreach my $line ( @event_lines ) {
105
next if $line =~ m/^$/;
106
if ( $lineno == 1 ) { # timestamp
107
push @props, 'ts', $line =~ m/^# (\S+)/;
109
elsif ( $lineno >= 2 && $lineno <= 4 ) { # ints, floats and bools
110
push @props, $line =~ m/([a-z_]+)=(\S+)/g;
112
elsif ( $lineno == 5 ) { # strings
113
push @props, $line =~ m/([a-z_]+)="([^"]*)"/;
122
push @props, 'arg', $arg;
124
my $event = { @props };
131
foreach my $attrib ( sort keys %$event ) {
132
print "$attrib=$event->{$attrib}\n";