Splunklib/Intersplunk.pm

filter: Statementstmt 13.8% 21 / 152 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 17 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 42.8% 6 / 14 Podpod 100% 6 / 6 totaltotal 13.2% 33 / 249 CC 42 SCAR 52.9
CC
42
Cov
10%
readResults
462.0
outputResults
72.0
_csv_writer
56.0
CRAP 198.0
↑ Summary Splunklib.pm »
1 #
2 # $Id: Intersplunk.pm,v 927d5bf7d37e 2015/10/03 12:57:38 gomor $
3 #
4 package Splunklib::Intersplunk;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 our @EXPORT_OK = qw(readResults outputResults isGetInfo outputGetInfo);
10  
11 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 my $Debug = 0;
16  
17 #
18 # Intersplunk format
19 #
20 # Converted from /opt/splunk/lib/python2.7/site-packages/splunk/Intersplunk.py
21 # readResults() and outputResults() function.
22 #
23  
24 0 ▶sub _csv_reader {
Subroutine _csv_reader: not called CC=2 SCAR=17.9
25 0 my $csvr = Text::CSV_XS->new({
26 binary => 1,
27 sep_char => ',',
28 #allow_loose_escapes => 1,
29 });
30 0 ▶ if (! defined($csvr)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $csvr) 0 0 0
31 # XXX: error handler
32 0 return;
33 }
34  
35 0 return $csvr;
36 }
37  
38 0 ▶sub _csv_writer {
Subroutine _csv_writer: not called CC=7 SCAR=40.3
39 0 my ($ary, $stdout) = @_;
40  
41 0 my $results = $ary->[0];
42 0 my $header = $ary->[1];
43 0 my $lookup = $ary->[2];
44  
45 0 my $output_debug;
46 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) 0 0 0
47 0 open(my $output_debug, '>>', '/tmp/output-ta-base64pl-debug.txt');
48 }
49  
50 0 ▶ my $csv = Text::CSV_XS->new({
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV_XS"->new({"binary", 1, "sep_char", ","}) or die 'Cannot use CSV: ' . "Text::CSV"->error_diag 0 0 0
51 binary => 1,
52 sep_char => ',',
53 }) or die "Cannot use CSV: ".Text::CSV->error_diag();
54  
55 # Header
56 0 $csv->print($stdout, $header);
57 0 print $stdout "\r\n";
58 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) 0 0 0
59 0 $csv->print($output_debug, $header);
60 0 print $output_debug "\r\n";
61 }
62  
63 # Content
64 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) { } 0 0 0
65 0 for my $result (@$results) {
66 0 $csv->print($stdout, $result);
67 0 print $stdout "\r\n";
68 0 $csv->print($output_debug, $result);
69 0 print $output_debug "\r\n";
70 }
71 }
72 else {
73 0 for my $result (@$results) {
74 0 $csv->print($stdout, $result);
75 0 print $stdout "\r\n";
76 }
77 }
78  
79 0 return 1;
80 }
81  
82 0 ▶sub getEncodedMV {
Subroutine getEncodedMV: not called CC=1 SCAR=6.9
Pod documented
83 0 my ($s) = @_;
84  
85 # XXX: TODO
86  
87 0 return 1;
88 }
89  
90 0 ▶sub decodeMV {
Subroutine decodeMV: not called CC=2 SCAR=17.9
Pod documented
91 0 my ($s, $vals) = @_;
92  
93 # XXX: TODO
94  
95 0 ▶ if (! length($s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $s) 0 0 0
96 0 return;
97 }
98  
99 0 my $tok = '';
100 0 my $inval = 0;
101  
102 # XXX: todo
103 #my $i = 0;
104 #while ($i < length($s)) {
105 #if (! $inval) {
106 #if (
107 #}
108 #}
109  
110 0 return 1;
111 }
112  
113 #
114 # Raw intersplunk input example:
115 #
116 # splunkVersion:6.2.6
117 # allowStream:1
118 # keywords:%22%22%22index%3A%3Alocal_system%22%22%20%22
119 # search:search%20index%3Dlocal_system%20%7C%20fields%20_raw%20%7C%20base64pl%20field%3D_raw
120 # sid:1443870176.14
121 # realtime:0
122 # preview:0
123 # truncated:0
124 #
125 # "_bkt","_cd","_indextime","_raw","_serial","_si","_sourcetype","_time"
126 # "local_system~17~5004F7D1-06C9-44F3-A726-29190625C311","17:207",1443870169,"Oct 3 13:02:48 messiah sudo: pam_unix(sudo:session): session closed for user root",0,"messiah
127 # local_system",syslog,1443870168
128  
129 0 ▶sub readResults {
Subroutine readResults: not called CC=21 SCAR=61.4
Pod documented
130 0 my ($stdin, $settings, $has_header) = @_;
131  
132 0 ▶ $settings ||= {}; # No settings by default
Condition0%0/2
$settings ||= {}
l!l
00
MC/DC0%0/1
$settings ||= {}
$settings
Truth table0%0/2
A
A= $settings
Aresult
11
00
133 0 ▶ $has_header ||= 1; # Header by default
Condition0%0/2
$has_header ||= 1
l!l
00
MC/DC0%0/1
$has_header ||= 1
$has_header
Truth table0%0/2
A
A= $has_header
Aresult
11
00
134  
135 0 my $input_debug;
136 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) 0 0 0
137 0 open(my $input_debug, '>', '/tmp/input-intersplunk-debug.txt');
138 }
139  
140 0 ▶ if ($has_header) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_header) 0 0 0
141 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) { } 0 0 0
142 0 ▶ while (my $line = <$stdin>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $stdin)) 0 0 0
143 0 print $input_debug $line;
144 0 chomp($line);
145 0 ▶ last if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
146 0 $line = URI::Escape::uri_unescape($line);
147 0 my ($k, $v) = split(/:/, $line);
148 0 $settings->{$k} = $v;
149 }
150 }
151 else {
152 0 ▶ while (my $line = <$stdin>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $stdin)) 0 0 0
153 0 chomp($line);
154 0 ▶ last if $line =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
155 0 $line = URI::Escape::uri_unescape($line);
156 0 my ($k, $v) = split(/:/, $line);
157 0 $settings->{$k} = $v;
158 }
159 }
160 }
161  
162 0 my $csvr = _csv_reader();
163  
164 0 my $csvw;
165 0 ▶ if ($Debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Debug) 0 0 0
166 0 ▶ $csvw = Text::CSV_XS->new({
Branch: 1 branch
BranchTrueFalseTotal
$csvw = "Text::CSV_XS"->new({"binary", 1, "sep_char", ","}) or die 'Cannot use CSV: ' . "Text::CSV"->error_diag 0 0 0
167 binary => 1,
168 sep_char => ',',
169 }) or die "Cannot use CSV: ".Text::CSV->error_diag();
170 }
171  
172 0 my $results = [];
173 0 my $header = [];
174 0 my $first = 1;
175 0 my @mv_fields = ();
176 0 my $lookup = {};
177 0 ▶ while (my $line = $csvr->getline($stdin)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $csvr->getline($stdin)) 0 0 0
178 # Activate when $Debug=1
179 # Off by default for perf issues.
180 #$csvw->print($output_debug, $line);
181 #print $output_debug "\r\n";
182 0 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 0 0 0
183 0 $header = $line;
184 0 $first = 0;
185  
186 # Check which fields are multivalued (for a field 'foo', '__mv_foo' also exists)
187 0 my %h_header = map { $_ => 1 } @$header;
188 0 for my $field (@$header) {
189 0 ▶ if (exists($h_header{"__mv_$field"})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $h_header{"__mv_$field"}) 0 0 0
190 0 push @mv_fields, $field;
191 }
192 }
193  
194 0 next;
195 }
196  
197 # We must maintain field order
198 0 my $pos = 0;
199 0 for my $hdr (@$header) {
200 0 $lookup->{$hdr} = $pos;
201 0 $pos++;
202 }
203 0 my $result = $line;
204  
205 0 for my $key (@mv_fields) {
206 0 my $mv_key = "__mv_$key";
207 0 ▶ if (exists($result->[$lookup->{$key}]) && exists($result->[$lookup->{$mv_key}])) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$result[$lookup->{$key}] and exists $$result[$lookup->{$mv_key}]) 0 0 0
Condition0%0/3
exists $$result[$lookup->{$key}] and exists $$result[$lookup->{$mv_key}]
!ll&&!rl&&r
000
MC/DC0%0/2
exists $$result[$lookup->{$key}] and exists $$result[$lookup->{$mv_key}]
exists $$result[$lookup->{$key}] exists $$result[$lookup->{$mv_key}]
Truth table0%0/3
A and B
A= exists $$result[$lookup->{$key}]B= exists $$result[$lookup->{$mv_key}]
ABresult
0X0
100
111
208 # Expand the value of __mv_[key] to a list, store it in key, and delete __mv_[key]
209 0 my $vals = [];
210 0 ▶ if (decodeMV($result->[$lookup->{$mv_key}], $vals)) {
Branch: 1 branch
BranchTrueFalseTotal
if (decodeMV($$result[$lookup->{$mv_key}], $vals)) 0 0 0
211 #$result{$key} = $vals;
212 #if (@{$result{$key}} == 1) {
213 #$result{$key} = $result{$key}->[0];
214 #}
215 #delete $result{$mv_key};
216 # XXX: todo
217 }
218 }
219 }
220  
221 0 push @$results, $result;
222 }
223  
224 0 return [ $results, $header, $lookup ];
225 }
226  
227 0 ▶sub isGetInfo {
Subroutine isGetInfo: not called CC=5 SCAR=34.0
Pod documented
228 0 my ($args) = @_;
229  
230 0 ▶ if (@$args >= 1 && $args->[0] eq '__GETINFO__') {
Branch: 2 branches
BranchTrueFalseTotal
if (@$args >= 1 and $args->[0] eq '__GETINFO__') { } 0 0 0
elsif (@$args >= 1 and $args->[0] eq '__EXECUTE__') { } 0 0 0
Condition0%0/3
@$args >= 1 and $args->[0] eq "__GETINFO__"
!ll&&!rl&&r
000
MC/DC0%0/2
@$args >= 1 and $args->[0] eq "__GETINFO__"
@$args >= 1 $args->[0] eq "__GETINFO__"
Truth table0%0/3
A and B
A= @$args >= 1B= $args->[0] eq "__GETINFO__"
ABresult
0X0
100
111
231 0 shift @$args; # Strip it
232 0 return 1;
233 }
234 ▶ elsif (@$args >= 1 && $args->[0] eq '__EXECUTE__') {
Condition0%0/3
@$args >= 1 and $args->[0] eq "__EXECUTE__"
!ll&&!rl&&r
000
MC/DC0%0/2
@$args >= 1 and $args->[0] eq "__EXECUTE__"
@$args >= 1 $args->[0] eq "__EXECUTE__"
Truth table0%0/3
A and B
A= @$args >= 1B= $args->[0] eq "__EXECUTE__"
ABresult
0X0
100
111
235 0 shift @$args; # Strip it
236 0 return 0;
237 }
238 else {
239 # XXX: error handling
240 0 exit(0);
241 }
242  
243 0 return 0;
244 }
245  
246 0 ▶sub outputGetInfo {
Subroutine outputGetInfo: not called CC=3 SCAR=24.8
Pod documented
247 0 my ($settings, $stdout) = @_;
248  
249 # Below is the correct field order to use.
250 # We currently don't follow on the output, but it seems to be ok.
251 0 ▶ $settings ||= {
Condition0%0/2
$settings ||= {"changes_colorder", 1, "clear_required_fields", 0, "enableheader", 1, "generating", 0, "local", 0, "maxinputs", 0, "needs_empty_results", 1, "outputheader", 1, "overrides_timeorder", 0, "passauth", 0, "perf_warn_limit", 0, "required_fields", "", "requires_srinfo", 0, "retainsevents", 1, "run_in_preview", 1, "stderr_dest", "log", "streaming", 1, "supports_multivalues", 1, "supports_rawargs", 1, "__mv_changes_colorder", "", "__mv_clear_required_fields", "", "__mv_enableheader", "", "__mv_generating", "", "__mv_local", "", "__mv_maxinputs", "", "__mv_needs_empty_results", "", "__mv_outputheader", "", "__mv_overrides_timeorder", "", "__mv_passauth", "", "__mv_perf_warn_limit", "", "__mv_required_fields", "", "__mv_requires_srinfo", "", "__mv_retainsevents", "", "__mv_run_in_preview", "", "__mv_stderr_dest", "", "__mv_streaming", "", "__mv_supports_multivalues", "", "__mv_supports_rawargs", ""}
l!l
00
MC/DC0%0/1
$settings ||= {"changes_colorder", 1, "clear_required_fields", 0, "enableheader", 1, "generating", 0, "local", 0, "maxinputs", 0, "needs_empty_results", 1, "outputheader", 1, "overrides_timeorder", 0, "passauth", 0, "perf_warn_limit", 0, "required_fields", "", "requires_srinfo", 0, "retainsevents", 1, "run_in_preview", 1, "stderr_dest", "log", "streaming", 1, "supports_multivalues", 1, "supports_rawargs", 1, "__mv_changes_colorder", "", "__mv_clear_required_fields", "", "__mv_enableheader", "", "__mv_generating", "", "__mv_local", "", "__mv_maxinputs", "", "__mv_needs_empty_results", "", "__mv_outputheader", "", "__mv_overrides_timeorder", "", "__mv_passauth", "", "__mv_perf_warn_limit", "", "__mv_required_fields", "", "__mv_requires_srinfo", "", "__mv_retainsevents", "", "__mv_run_in_preview", "", "__mv_stderr_dest", "", "__mv_streaming", "", "__mv_supports_multivalues", "", "__mv_supports_rawargs", ""}
$settings
Truth table0%0/2
A
A= $settings
Aresult
11
00
252 changes_colorder => 1,
253 clear_required_fields => 0,
254 enableheader => 1,
255 generating => 0,
256 local => 0,
257 maxinputs => 0,
258 needs_empty_results => 1,
259 outputheader => 1,
260 overrides_timeorder => 0,
261 passauth => 0,
262 perf_warn_limit => 0,
263 required_fields => '',
264 requires_srinfo => 0,
265 retainsevents => 1,
266 run_in_preview => 1,
267 stderr_dest => 'log',
268 streaming => 1,
269 supports_multivalues => 1,
270 supports_rawargs => 1,
271 __mv_changes_colorder => '',
272 __mv_clear_required_fields => '',
273 __mv_enableheader => '',
274 __mv_generating => '',
275 __mv_local => '',
276 __mv_maxinputs => '',
277 __mv_needs_empty_results => '',
278 __mv_outputheader => '',
279 __mv_overrides_timeorder => '',
280 __mv_passauth => '',
281 __mv_perf_warn_limit => '',
282 __mv_required_fields => '',
283 __mv_requires_srinfo => '',
284 __mv_retainsevents => '',
285 __mv_run_in_preview => '',
286 __mv_stderr_dest => '',
287 __mv_streaming => '',
288 __mv_supports_multivalues => '',
289 __mv_supports_rawargs => '',
290 };
291  
292 0 my $header = '';
293 0 my $values = '';
294 0 for my $k (sort { $a cmp $b } keys %$settings) {
295 0 $header .= "$k,";
296 0 $values .= $settings->{$k}.",";
297 }
298 0 $header =~ s/,$//;
299 0 $values =~ s/,$//;
300  
301 0 print $stdout "\r\n";
302 0 print $stdout "$header\r\n";
303 0 print $stdout "$values\r\n";
304  
305 0 return 1;
306 }
307  
308 0 ▶sub outputResults {
Subroutine outputResults: not called CC=8 SCAR=42.8
Pod documented
309 0 my ($ary, $messages, $fields, $mvdelim, $stdout) = @_;
310  
311 0 ▶ $mvdelim ||= '\n';
Condition0%0/2
$mvdelim ||= "\\n"
l!l
00
MC/DC0%0/1
$mvdelim ||= "\\n"
$mvdelim
Truth table0%0/2
A
A= $mvdelim
Aresult
11
00
312  
313 0 my $results = $ary->[0];
314 0 my $header = $ary->[1];
315 0 my $lookup = $ary->[2];
316  
317 #
318 # Example message header
319 #
320 # $messages = {
321 # streaming_preop' => '0',
322 # streaming' => '0',
323 # generating' => '0',
324 # retainsevents' => '0',
325 # requires_preop' => '0',
326 # generates_timeorder' => '0',
327 # overrides_timeorder' => '0',
328 # clear_required_fields' => '0',
329 # };
330  
331 0 ▶ if (defined($messages)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $messages) 0 0 0
332 # message header is everything before the first empty line, similar to the input
333 # header format. also key = value, with stripping of whitespace
334 0 for my $level (sort { $a <=> $b } keys %$messages) {
335 0 print $stdout $level."=".$messages->{$level}."\r\n";
336 }
337 0 print $stdout "\r\n";
338 }
339  
340 0 ▶ if (@$results == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$results == 0) 0 0 0
341 0 return;
342 }
343  
344 0 my $s = {};
345 0 my $l = [];
346 # Check each entry to see if it is a list (multivalued).
347 # If so, set the multivalued key to the proper encoding.
348 # Replace the list with a newline separated string of the values.
349 0 for my $result (@$results) {
350 #for my $key (keys %$result) {
351 0 for my $key (@$result) {
352 # XXX: todo
353 #if (ref($result->{$key}) eq 'ARRAY') {
354 #$result->{"__mv_$key"} = getEncodedMV($result->{$key});
355 #$result->{$key} = join($mvdelim, @{$result->{$key}});
356 #}
357  
358 #if (! exists($s->{$key})) {
359 #$s->{$key} = 1;
360 #push @$l, $key;
361 #}
362 }
363 }
364  
365 0 my $h;
366 0 ▶ if (! $fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $fields) { } 0 0 0
367 0 $h = $header;
368 }
369 else {
370 0 $h = $fields;
371 }
372  
373 0 _csv_writer($ary, $stdout);
374  
375 0 return 1;
376 }
377  
378 2 1;
379  
380 __END__
↑ Summary Splunklib.pm »