line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id: Intersplunk.pm,v e46bf13852d9 2015/10/03 10:12:28 gomor $ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
package Splunklib::Intersplunk; |
5
|
1
|
|
|
1
|
|
747
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
26
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use base qw(Exporter); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
124
|
|
9
|
|
|
|
|
|
|
our @EXPORT_OK = qw(readResults outputResults); |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
1106
|
use Data::Dumper; |
|
1
|
|
|
|
|
7856
|
|
|
1
|
|
|
|
|
73
|
|
12
|
1
|
|
|
1
|
|
1428
|
use Text::CSV_XS; |
|
1
|
|
|
|
|
12461
|
|
|
1
|
|
|
|
|
82
|
|
13
|
1
|
|
|
1
|
|
875
|
use URI::Escape; |
|
1
|
|
|
|
|
1600
|
|
|
1
|
|
|
|
|
1100
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# |
16
|
|
|
|
|
|
|
# Intersplunk format |
17
|
|
|
|
|
|
|
# |
18
|
|
|
|
|
|
|
# Converted from /opt/splunk/lib/python2.7/site-packages/splunk/Intersplunk.py |
19
|
|
|
|
|
|
|
# readResults() and outputResults() function. |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _csv_reader { |
23
|
0
|
|
|
0
|
|
|
my $csvr = Text::CSV_XS->new({ |
24
|
|
|
|
|
|
|
binary => 1, |
25
|
|
|
|
|
|
|
sep_char => ',', |
26
|
|
|
|
|
|
|
#allow_loose_escapes => 1, |
27
|
|
|
|
|
|
|
}); |
28
|
0
|
0
|
|
|
|
|
if (! defined($csvr)) { |
29
|
|
|
|
|
|
|
# XXX: error handler |
30
|
0
|
|
|
|
|
|
return; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
0
|
|
|
|
|
|
return $csvr; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _csv_writer { |
37
|
0
|
|
|
0
|
|
|
my ($ary, $stdout) = @_; |
38
|
|
|
|
|
|
|
|
39
|
0
|
|
|
|
|
|
my $results = $ary->[0]; |
40
|
0
|
|
|
|
|
|
my $header = $ary->[1]; |
41
|
0
|
|
|
|
|
|
my $lookup = $ary->[2]; |
42
|
|
|
|
|
|
|
|
43
|
0
|
0
|
|
|
|
|
my $csv = Text::CSV_XS->new({ |
44
|
|
|
|
|
|
|
binary => 1, |
45
|
|
|
|
|
|
|
sep_char => ',', |
46
|
|
|
|
|
|
|
}) or die "Cannot use CSV: ".Text::CSV->error_diag(); |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Header |
49
|
0
|
|
|
|
|
|
$csv->print($stdout, $header); |
50
|
0
|
|
|
|
|
|
print $stdout "\n"; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
# Content |
53
|
0
|
|
|
|
|
|
for my $result (@$results) { |
54
|
0
|
|
|
|
|
|
$csv->print($stdout, $result); |
55
|
0
|
|
|
|
|
|
print $stdout "\n"; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return 1; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub getEncodedMV { |
62
|
0
|
|
|
0
|
1
|
|
my ($s) = @_; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# XXX: TODO |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
return 1; |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub decodeMV { |
70
|
0
|
|
|
0
|
1
|
|
my ($s, $vals) = @_; |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# XXX: TODO |
73
|
|
|
|
|
|
|
|
74
|
0
|
0
|
|
|
|
|
if (! length($s)) { |
75
|
0
|
|
|
|
|
|
return; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
|
my $tok = ''; |
79
|
0
|
|
|
|
|
|
my $inval = 0; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# XXX: todo |
82
|
|
|
|
|
|
|
#my $i = 0; |
83
|
|
|
|
|
|
|
#while ($i < length($s)) { |
84
|
|
|
|
|
|
|
#if (! $inval) { |
85
|
|
|
|
|
|
|
#if ( |
86
|
|
|
|
|
|
|
#} |
87
|
|
|
|
|
|
|
#} |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
|
|
|
return 1; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub readResults { |
93
|
0
|
|
|
0
|
1
|
|
my ($stdin, $settings, $has_header) = @_; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
0
|
|
|
|
$settings ||= {}; # No settings by default |
96
|
0
|
|
0
|
|
|
|
$has_header ||= 1; # Header by default |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if ($has_header) { |
99
|
0
|
|
|
|
|
|
while (my $line = <$stdin>) { |
100
|
0
|
|
|
|
|
|
chomp($line); |
101
|
0
|
0
|
|
|
|
|
last if $line =~ /^\s*$/; |
102
|
0
|
|
|
|
|
|
$line = URI::Escape::uri_unescape($line); |
103
|
0
|
|
|
|
|
|
my ($k, $v) = split(/:/, $line); |
104
|
0
|
|
|
|
|
|
$settings->{$k} = $v; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
#print Dumper($settings)."\n"; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $csvr = _csv_reader(); |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
my $results = []; |
113
|
0
|
|
|
|
|
|
my $header = []; |
114
|
0
|
|
|
|
|
|
my $first = 1; |
115
|
0
|
|
|
|
|
|
my @mv_fields = (); |
116
|
0
|
|
|
|
|
|
my $lookup = {}; |
117
|
0
|
|
|
|
|
|
while (my $line = $csvr->getline($stdin)) { |
118
|
0
|
0
|
|
|
|
|
if ($first) { |
119
|
0
|
|
|
|
|
|
$header = $line; |
120
|
0
|
|
|
|
|
|
$first = 0; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Check which fields are multivalued (for a field 'foo', '__mv_foo' also exists) |
123
|
0
|
|
|
|
|
|
my %h_header = map { $_ => 1 } @$header; |
|
0
|
|
|
|
|
|
|
124
|
0
|
|
|
|
|
|
for my $field (@$header) { |
125
|
0
|
0
|
|
|
|
|
if (exists($h_header{"__mv_$field"})) { |
126
|
0
|
|
|
|
|
|
push @mv_fields, $field; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
0
|
|
|
|
|
|
next; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
# need to maintain field order |
134
|
|
|
|
|
|
|
#tie(my %result, 'Tie::IxHash'); |
135
|
|
|
|
|
|
|
#my %result, 'Tie::IxHash'; |
136
|
|
|
|
|
|
|
#my $i = 0; |
137
|
|
|
|
|
|
|
#for my $val (@$line) { |
138
|
|
|
|
|
|
|
#$result{$header->[$i]} = $val; |
139
|
|
|
|
|
|
|
#$i++; |
140
|
|
|
|
|
|
|
#} |
141
|
0
|
|
|
|
|
|
my $pos = 0; |
142
|
0
|
|
|
|
|
|
for my $hdr (@$header) { |
143
|
0
|
|
|
|
|
|
$lookup->{$hdr} = $pos; |
144
|
0
|
|
|
|
|
|
$pos++; |
145
|
|
|
|
|
|
|
} |
146
|
0
|
|
|
|
|
|
my $result = $line; |
147
|
|
|
|
|
|
|
|
148
|
0
|
|
|
|
|
|
for my $key (@mv_fields) { |
149
|
0
|
|
|
|
|
|
my $mv_key = "__mv_$key"; |
150
|
|
|
|
|
|
|
#if (exists($result{$key}) && exists($result{$mv_key})) { |
151
|
0
|
0
|
0
|
|
|
|
if (exists($result->[$lookup->{$key}]) && exists($result->[$lookup->{$mv_key}])) { |
152
|
|
|
|
|
|
|
# Expand the value of __mv_[key] to a list, store it in key, and delete __mv_[key] |
153
|
0
|
|
|
|
|
|
my $vals = []; |
154
|
|
|
|
|
|
|
#if (decodeMV($result{$mv_key}, $vals)) { |
155
|
0
|
0
|
|
|
|
|
if (decodeMV($result->[$lookup->{$mv_key}], $vals)) { |
156
|
|
|
|
|
|
|
#$result{$key} = $vals; |
157
|
|
|
|
|
|
|
#if (@{$result{$key}} == 1) { |
158
|
|
|
|
|
|
|
#$result{$key} = $result{$key}->[0]; |
159
|
|
|
|
|
|
|
#} |
160
|
|
|
|
|
|
|
#delete $result{$mv_key}; |
161
|
|
|
|
|
|
|
# XXX: todo |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
#print Dumper($line)."\n"; |
167
|
|
|
|
|
|
|
#print Dumper(\%result)."\n"; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
#push @$results, \%result; |
170
|
0
|
|
|
|
|
|
push @$results, $result; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
#print Dumper($header)."\n"; |
174
|
|
|
|
|
|
|
#print Dumper($results)."\n"; |
175
|
|
|
|
|
|
|
|
176
|
0
|
|
|
|
|
|
return [ $results, $header, $lookup ]; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub outputResults { |
180
|
0
|
|
|
0
|
1
|
|
my ($ary, $messages, $fields, $mvdelim, $stdout) = @_; |
181
|
|
|
|
|
|
|
|
182
|
0
|
|
0
|
|
|
|
$mvdelim ||= '\n'; |
183
|
|
|
|
|
|
|
|
184
|
0
|
|
|
|
|
|
my $results = $ary->[0]; |
185
|
0
|
|
|
|
|
|
my $header = $ary->[1]; |
186
|
0
|
|
|
|
|
|
my $lookup = $ary->[2]; |
187
|
|
|
|
|
|
|
|
188
|
0
|
0
|
|
|
|
|
if (defined($messages)) { |
189
|
|
|
|
|
|
|
# message header is everything before the first empty line, similar to the input |
190
|
|
|
|
|
|
|
# header format. also key = value, with stripping of whitespace |
191
|
0
|
|
|
|
|
|
for my $level (keys %$messages) { |
192
|
0
|
|
|
|
|
|
printf("%s=%s\n", $level, $messages->{$level}); |
193
|
|
|
|
|
|
|
} |
194
|
0
|
|
|
|
|
|
print "\n"; |
195
|
|
|
|
|
|
|
} |
196
|
|
|
|
|
|
|
|
197
|
0
|
0
|
|
|
|
|
if (@$results == 0) { |
198
|
0
|
|
|
|
|
|
return; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
my $s = {}; |
202
|
0
|
|
|
|
|
|
my $l = []; |
203
|
|
|
|
|
|
|
# Check each entry to see if it is a list (multivalued). |
204
|
|
|
|
|
|
|
# If so, set the multivalued key to the proper encoding. |
205
|
|
|
|
|
|
|
# Replace the list with a newline separated string of the values. |
206
|
0
|
|
|
|
|
|
for my $result (@$results) { |
207
|
|
|
|
|
|
|
#for my $key (keys %$result) { |
208
|
0
|
|
|
|
|
|
for my $key (@$result) { |
209
|
|
|
|
|
|
|
# XXX: todo |
210
|
|
|
|
|
|
|
#if (ref($result->{$key}) eq 'ARRAY') { |
211
|
|
|
|
|
|
|
#$result->{"__mv_$key"} = getEncodedMV($result->{$key}); |
212
|
|
|
|
|
|
|
#$result->{$key} = join($mvdelim, @{$result->{$key}}); |
213
|
|
|
|
|
|
|
#} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
#if (! exists($s->{$key})) { |
216
|
|
|
|
|
|
|
#$s->{$key} = 1; |
217
|
|
|
|
|
|
|
#push @$l, $key; |
218
|
|
|
|
|
|
|
#} |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
0
|
|
|
|
|
|
my $h; |
223
|
0
|
0
|
|
|
|
|
if (! $fields) { |
224
|
0
|
|
|
|
|
|
$h = $header; |
225
|
|
|
|
|
|
|
} |
226
|
|
|
|
|
|
|
else { |
227
|
0
|
|
|
|
|
|
$h = $fields; |
228
|
|
|
|
|
|
|
} |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
#print Dumper($h)."\n"; |
231
|
|
|
|
|
|
|
#print Dumper($results)."\n"; |
232
|
|
|
|
|
|
|
|
233
|
0
|
|
|
|
|
|
_csv_writer($ary, $stdout); |
234
|
|
|
|
|
|
|
|
235
|
0
|
|
|
|
|
|
return 1; |
236
|
|
|
|
|
|
|
} |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
1; |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
__END__ |