blib/lib/WWW/Splunk.pm

filter: Statementstmt 37.7% 20 / 53 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 46.1% 6 / 13 Podpod 100% 6 / 6 totaltotal 28.5% 32 / 112 CC 15 SCAR 35.5
CC
15
Cov
24%
start_search
90.0
rt_search
20.0
poll_search
6.0
CRAP 34.7
« blib/lib/WWW/Splunk/API.pm ↑ Summary blib/lib/WWW/Splunk/XMLParser.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 WWW::Splunk - Client library for Splunk log search engine
6  
7 =head1 SYNOPSIS
8  
9 use WWW::Splunk;
10  
11 my $splunk = WWW::Splunk->new({
12 host => $host,
13 port => $port,
14 login => $login,
15 password => $password,
16 unsafe_ssl => 1,
17 verbose => 0,
18 });
19  
20 my $sid = $splunk->start_search('selinux avc');
21 $splunk->poll_search($sid);
22 until ($splunk->results_read($sid)) {
23 print scalar $splunk->search_results($sid);
24 }
25 print " results found\n";
26  
27 =head1 DESCRIPTION
28  
29 This module contains utility functions for Splunk API, implementing
30 version 4.1 API, verified to work with 4.2, 4.3 and 5.0.3 versions.
31  
32 =cut
33  
34 package WWW::Splunk;
35  
36 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 our $VERSION = '2.08';
40  
41 2 ▶use WWW::Splunk::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use base qw/WWW::Splunk::API/;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 =head2 B<start_search> (F<string>) [(F<since>)] [(F<until>)]
48  
49 Initiate a search, return a SID (Search ID) string.
50  
51 =cut
52  
53 sub start_search {
54 0 ▶ my ($self, $string, $since, $until) = @_;
Subroutine start_search: not called CC=9 SCAR=45.0
Pod documented
55  
56 0 delete $self->{last_read};
57  
58 # Format dates
59 0 ▶ ($since, $until) = map { defined $_ ? scalar UnixDate (ParseDate ($_) || $_, '%O') || $_ : undef }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
Condition0%0/6
scalar UnixDate(ParseDate($_) || $_, "%O") or $_
l!l&&r!l&&!r
000
ParseDate($_) || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar UnixDate(ParseDate($_) || $_, "%O") or $_
scalar UnixDate(ParseDate($_) || $_, "%O") $_
MC/DC0%0/2
ParseDate($_) || $_
ParseDate($_) $_
Truth table0%0/3
A or B
A= scalar UnixDate(ParseDate($_) || $_, "%O")B= $_
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= ParseDate($_)B= $_
ABresult
1X1
011
000
60 ($since, $until);
61  
62 0 $self->{results_consumed} = 0;
63 0 ▶ my $response = $self->post ('/search/jobs', {
Branch: 2 branches
BranchTrueFalseTotal
defined $since ? : 0 0 0
defined $until ? : 0 0 0
64 search => "search $string",
65 (defined $since ? (earliest_time => $since) : ()),
66 (defined $until ? (latest_time => $until) : ()),
67 });
68 0 ▶ die 'Unexpected response format '
Branch: 1 branch
BranchTrueFalseTotal
unless $response and ref $response eq "XML::LibXML::Document" 0 0 0
Condition0%0/3
$response and ref $response eq "XML::LibXML::Document"
!ll&&!rl&&r
000
MC/DC0%0/2
$response and ref $response eq "XML::LibXML::Document"
$response ref $response eq "XML::LibXML::Document"
Truth table0%0/3
A and B
A= $responseB= ref $response eq "XML::LibXML::Document"
ABresult
0X0
100
111
69 unless $response and ref $response eq 'XML::LibXML::Document';
70 0 my $sid = $response->findvalue ('/response/sid');
71 0 ▶ croak 'Bad response' unless defined $sid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sid 0 0 0
72  
73 0 return $sid;
74 }
75  
76 =head2 B<rt_search> (F<string>) (F<callback>) [(F<since>)] [(F<until>)]
77  
78 Initiate a real-time search, calling a callback for each line matched.
79  
80 Finishes only if connection terminates (potentially never), returning number of
81 results consumed.
82  
83 =cut
84  
85 sub rt_search {
86 0 ▶ my ($self, $string, $callback, $since, $until, $counter) = @_;
Subroutine rt_search: not called CC=4 SCAR=30.0
Pod documented
87  
88 0 ▶ $since ||= 'rt';
Condition0%0/2
$since ||= "rt"
l!l
00
MC/DC0%0/1
$since ||= "rt"
$since
Truth table0%0/2
A
A= $since
Aresult
11
00
89 0 ▶ $until ||= 'rt';
Condition0%0/2
$until ||= "rt"
l!l
00
MC/DC0%0/1
$until ||= "rt"
$until
Truth table0%0/2
A
A= $until
Aresult
11
00
90 0 ▶ $counter ||= 0;
Condition0%0/2
$counter ||= 0
l!l
00
MC/DC0%0/1
$counter ||= 0
$counter
Truth table0%0/2
A
A= $counter
Aresult
11
00
91  
92 $self->post ('/search/jobs/export', {
93 search => "search $string",
94 earliest_time => $since,
95 latest_time => $until,
96 search_mode => 'realtime',
97 }, sub {
98 0 ▶ $callback->(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 $counter++;
100 0 });
101  
102 0 return $counter;
103 }
104  
105 =head2 B<search_done> (F<sid>)
106  
107 Return true if the search is finished.
108  
109 =cut
110  
111 sub search_done {
112 0 ▶ my ($self, $sid) = @_;
Subroutine search_done: not called CC=1 SCAR=6.9
Pod documented
113  
114 0 my $search = $self->get ('/search/jobs/'.$sid);
115  
116 0 return $search->{isDone};
117 }
118  
119 =head2 B<poll_search> (F<sid>)
120  
121 Wait for a search to finish.
122  
123 =cut
124  
125 sub poll_search {
126 0 ▶ my ($self, $sid) = @_;
Subroutine poll_search: not called CC=2 SCAR=17.9
Pod documented
127  
128 0 ▶ until ($self->search_done ($sid)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->search_done($sid)) 0 0 0
129 0 sleep 1;
130 }
131 }
132  
133 =head2 B<search_results> (F<sid>)
134  
135 Return an array of the matched events.
136 If called multiple times, it only returns events which
137 were added from the time of the last call.
138 Oh, and you can't run multiple search concurrently
139 with single L<WWW::Splunk> instance. Otherwise,
140 L<WWW::Splunk> is perfectly thread-safe.
141  
142 =cut
143  
144 sub search_results {
145 0 ▶ my ($self, $sid) = @_;
Subroutine search_results: not called CC=2 SCAR=17.9
Pod documented
146  
147 0 my $done = $self->search_done ($sid);
148 my @results = $self->get ('/search/jobs/'.$sid.'/results?count=1024&offset='.
149 0 $self->{results_consumed});
150 0 $self->{results_consumed} += scalar @results;
151 0 ▶ $self->{last_read} = scalar @results if $done;
Branch: 1 branch
BranchTrueFalseTotal
if $done 0 0 0
152  
153 0 return @results;
154 }
155  
156 =head2 B<results_read> (F<sid>)
157  
158 Return true if search is finished and all there are no
159 more results to read (everything was fetched with L<search_results>).
160  
161 =cut
162  
163 sub results_read {
164 0 ▶ my ($self, $sid) = @_;
Subroutine results_read: not called CC=2 SCAR=17.9
Pod documented
165  
166 0 ▶ return undef if not defined $self->{last_read};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"last_read"} 0 0 0
167 0 return $self->{last_read} eq 0;
168 }
169  
170 =head1 AUTHORS
171  
172 Lubomir Rintel, L<< <lkundrak@v3.sk> >>,
173 Michal Josef Špaček L<< <skim@cpan.org> >>
174  
175 The code is hosted on GitHub L<http://github.com/tupinek/perl-WWW-Splunk>.
176 Bug fixes and feature enhancements are always welcome.
177  
178 =head1 LICENSE
179  
180 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
181  
182 =cut
183  
184 2 1;
« blib/lib/WWW/Splunk/API.pm ↑ Summary blib/lib/WWW/Splunk/XMLParser.pm »