blib/lib/WWW/Splunk/API.pm

filter: Statementstmt 19.6% 20 / 102 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.0% 6 / 15 Podpod 100% 7 / 7 totaltotal 16.2% 33 / 203 CC 37 SCAR 49.1
CC
37
Cov
12%
request
342.0
new
56.0
__ANON__
42.0
CRAP 136.2
↑ Summary blib/lib/WWW/Splunk.pm »
1 =encoding utf8
2  
3 =head1 NAME
4  
5 WWW::Splunk::API - Splunk REST client
6  
7 =head1 DESCRIPTION
8  
9 L<WWW::Splunk::API> is a low-level interface to Splunk
10 log search engine. It deals with HTTP communication as well as
11 working around certain interface glitches.
12  
13 See L<http://www.splunk.com/base/Documentation/latest/Developer/RESTSearch>
14 for API definition.
15  
16 This module is designed to be Splunk API version agnostic.
17  
18 =cut
19  
20 package WWW::Splunk::API;
21  
22 4 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use WWW::Splunk::XMLParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 4 our $VERSION = '2.08';
31 4 our $prefix = '/services';
32  
33 =head2 B<new> (F<params>)
34  
35 A constructor.
36  
37 my $splunk = WWW::Splunk::API->new({
38 host => $host,
39 port => $port,
40 login => $login,
41 password => $password,
42 unsafe_ssl => 0,
43 verbose => 0,
44 });
45  
46 =cut
47  
48 sub new {
49 0 ▶ my ($class, $self) = @_;
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
50  
51 0 ▶ $self->{port} ||= 8089;
Condition0%0/2
$self->{"port"} ||= 8089
l!l
00
MC/DC0%0/1
$self->{"port"} ||= 8089
$self->{"port"}
Truth table0%0/2
A
A= $self->{"port"}
Aresult
11
00
52 0 ▶ $self->{host} ||= 'localhost';
Condition0%0/2
$self->{"host"} ||= "localhost"
l!l
00
MC/DC0%0/1
$self->{"host"} ||= "localhost"
$self->{"host"}
Truth table0%0/2
A
A= $self->{"host"}
Aresult
11
00
53 0 ▶ $self->{url} ||= 'https://'.$self->{host}.':'.$self->{port};
Condition0%0/2
$self->{"url"} ||= 'https://' . $self->{"host"} . ':' . $self->{"port"}
l!l
00
MC/DC0%0/1
$self->{"url"} ||= 'https://' . $self->{"host"} . ':' . $self->{"port"}
$self->{"url"}
Truth table0%0/2
A
A= $self->{"url"}
Aresult
11
00
54 0 ▶ $self->{verbose} ||= 0;
Condition0%0/2
$self->{"verbose"} ||= 0
l!l
00
MC/DC0%0/1
$self->{"verbose"} ||= 0
$self->{"verbose"}
Truth table0%0/2
A
A= $self->{"verbose"}
Aresult
11
00
55  
56 # Set up user agent unless an existing one was passed
57 0 ▶ unless ($self->{agent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"agent"}) 0 0 0
58 $self->{agent} = new LWP::UserAgent
59 0 (ssl_opts => {verify_hostname => (not $self->{unsafe_ssl})});
60 0 $self->{agent}->cookie_jar ({});
61 $self->{agent}->credentials (
62 delete ($self->{host}).':'.(delete $self->{port}),
63 '/splunk',
64 delete $self->{login},
65 delete $self->{password},
66 0 ▶ ) if exists $self->{login};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"login"} 0 0 0
67 0 $self->{agent}->agent ("$class/$VERSION ");
68 }
69  
70 0 return bless $self, $class;
71 }
72  
73 =head2 B<delete> (F<parameters>)
74  
75 Wrapper around HTTP::Request::Common::DELETE().
76  
77 =cut
78  
79 sub delete {
80 0 ▶ my ($self, @args) = @_;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
81  
82 0 ▶ print "DELETE" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
83 0 $self->request (\&DELETE, @args);
84 }
85  
86 =head2 B<post> (F<parameters>)
87  
88 Wrapper around HTTP::Request::Common::POST().
89  
90 =cut
91  
92 sub post {
93 0 ▶ my ($self, @args) = @_;
Subroutine post: not called CC=2 SCAR=17.9
Pod documented
94  
95 0 ▶ print "POST" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
96 0 $self->request (\&POST, @args);
97 }
98  
99 =head2 B<get> (F<parameters>)
100  
101 Wrapper around HTTP::Request::Common::GET().
102  
103 =cut
104  
105 sub get {
106 0 ▶ my ($self, @args) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
107  
108 0 ▶ print "GET" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
109 0 $self->request (\&GET, @args);
110 }
111  
112 =head2 B<head> (F<parameters>)
113  
114 Wrapper around HTTP::Request::Common::HEAD().
115 Not used anywhere in splunk API
116  
117 =cut
118  
119 sub head {
120 0 ▶ my ($self, @args) = @_;
Subroutine head: not called CC=2 SCAR=17.9
Pod documented
121  
122 0 ▶ print "HEAD" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
123 0 $self->request (\&HEAD, @args);
124 }
125  
126 =head2 B<put> (F<parameters>)
127  
128 Wrapper around HTTP::Request::Common::PUT().
129 Not used anywhere in splunk API
130  
131 =cut
132  
133 sub put {
134 0 ▶ my ($self, @args) = @_;
Subroutine put: not called CC=2 SCAR=17.9
Pod documented
135  
136 0 ▶ print "PUT" if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 0 0
137 0 $self->request (\&PUT, @args);
138 }
139  
140 =head2 B<request> (F<method>, F<location>, [F<data>], [F<callback>])
141  
142 Request a Splunk api and deal with the results.
143  
144 Method can be either a L<HTTP::Request> instance (see L<HTTP::Request::Common>
145 for useful ones), or a plain string, such as "GET" or "DELETE."
146  
147 Optional F<data> is has reference gets serialized into a request body for POST
148 request. Use I<undef> in case you don't have any data to send, but need to
149 specify a callback function in subsequent argument.
150  
151 Call-back function can be specified for a single special case, where a XML stream
152 of <results> elements is expected.
153  
154 =cut
155  
156 sub request {
157 0 ▶ my ($self, $method, $location, $data, $callback) = @_;
Subroutine request: not called CC=18 SCAR=58.3
Pod documented
158  
159 0 my $url = $self->{url}.$prefix.$location;
160 0 ▶ if ($self->{verbose}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"verbose"}) 0 0 0
161 0 print " $url\n";
162 0 ▶ if (defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $data) 0 0 0
163 0 foreach my $key (sort keys %$data) {
164 0 my $value = $data->{$key};
165 0 $value =~ s/\n/ /msg;
166 0 print "- $key => $value\n";
167 }
168 }
169 }
170  
171 # Construct the request
172 0 my $request;
173 0 ▶ if (ref $method and ref $method eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method and ref $method eq 'CODE') { } 0 0 0
Condition0%0/3
ref $method and ref $method eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $method and ref $method eq "CODE"
ref $method ref $method eq "CODE"
Truth table0%0/3
A and B
A= ref $methodB= ref $method eq "CODE"
ABresult
0X0
100
111
174 # Most likely a HTTP::Request::Common
175 0 ▶ if (! defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data) { } 0 0 0
176 0 $request = $method->($url);
177 } else {
178 0 $request = $method->($url, $data);
179 }
180 } else {
181 # A method string
182 0 $request = new HTTP::Request ($method, $url);
183 }
184  
185 0 my $content_type = '';
186 0 my $buffer;
187  
188 0 $self->{agent}->remove_handler ('response_header');
189 $self->{agent}->add_handler (response_header => sub {
190 0 ▶ my($response, $ua, $h) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
191  
192 # Do not think of async processing of error responses
193 0 ▶ return 0 unless $response->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $response->is_success 0 0 0
194  
195 0 ▶ my $content_type_header = $response->header('Content-Type') // '';
Condition0%0/2
$response->header("Content-Type") // ""
l!l
00
MC/DC0%0/1
$response->header("Content-Type") // ""
$response->header("Content-Type")
Truth table0%0/2
A
A= $response->header("Content-Type")
Aresult
11
00
196 0 ▶ if ($content_type_header =~ /^([^\s;]+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_type_header =~ /^([^\s;]+)/) { } 0 0 0
elsif ($response->code ne 204) { } 0 0 0
197 0 $content_type = $1;
198 } elsif ($response->code ne 204) {
199 # Sometimes splunk return HTTP 204 NO CONTENT during poll_search() call,
200 # Content-Type header is empty in this case. We must not croak in this case.
201 0 croak "Missing or invalid Content-Type: $content_type_header";
202 }
203  
204 0 ▶ if ($callback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 0 0 0
205 0 $response->{default_add_content} = 0;
206 0 $buffer = "";
207 }
208 0 });
209  
210 0 $self->{agent}->remove_handler ('response_data');
211 $self->{agent}->add_handler (response_data => sub {
212 0 ▶ my ($response, $ua, $h, $data) = @_;
Subroutine __ANON__: not called CC=4 SCAR=30.0
213  
214 0 ▶ return 1 unless defined $buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buffer 0 0 0
215 0 $buffer .= $data;
216 0 foreach (split /<\/results>\K/, $buffer) {
217 0 ▶ unless (/<\/results>$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (m[</results>$]) 0 0 0
218 0 $buffer = $_;
219 0 last;
220 }
221  
222 0 my $xml = XML::LibXML->load_xml (string => $_);
223 0 $callback->(WWW::Splunk::XMLParser::parse ($xml));
224 }
225  
226 0 return 1;
227 0 ▶ }) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
228  
229 # Run it
230 0 my $response = $self->{agent}->request ($request);
231 0 ▶ croak $response->header ('X-Died') if $response->header ('X-Died');
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("X-Died") 0 0 0
232  
233 # Deal with HTTP errors
234 0 ▶ unless ($response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
235 0 ▶ my $content = WWW::Splunk::XMLParser::parse ($response->content)
Branch: 1 branch
BranchTrueFalseTotal
if $response->header("Content-Type") =~ /xml/ 0 0 0
236 if $response->header ('Content-Type') =~ /xml/;
237 0 my $error = "HTTP Error: ".$response->status_line;
238 $error .= sprintf "\n%s: %s",
239 $content->findvalue ('/response/messages/msg/@type'),
240 $content->findvalue ('/response/messages/msg')
241 0 ▶ if eval { $content->isa ('XML::LibXML::Document') }
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $content->isa("XML::LibXML::Document") } } and $content->documentElement->nodeName eq "response" 0 0 0
Condition0%0/3
eval { do { $content->isa("XML::LibXML::Document") } } and $content->documentElement->nodeName eq "response"
!ll&&!rl&&r
000
MC/DC0%0/2
eval { do { $content->isa("XML::LibXML::Document") } } and $content->documentElement->nodeName eq "response"
eval { do { $content->isa("XML::LibXML::Document") } } $content->documentElement->nodeName eq "response"
Truth table0%0/3
A and B
A= eval { do { $content->isa("XML::LibXML::Document") } }B= $content->documentElement->nodeName eq "response"
ABresult
0X0
100
111
242 and $content->documentElement->nodeName eq 'response';
243 0 croak $error;
244 }
245  
246 # We've gotten the response already
247 0 ▶ return if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
248  
249 # Parse content from synchronous responses
250 # TODO: use callback and m_media_type matchspecs
251 0 ▶ if ($content_type eq 'text/xml') {
Branch: 3 branches
BranchTrueFalseTotal
if ($content_type eq 'text/xml') { } 0 0 0
elsif ($response->code eq 204) { } 0 0 0
elsif ($content_type eq 'text/plain') { } 0 0 0
252 0 my $xml = XML::LibXML->load_xml (string => $response->content);
253 0 my @ret = WWW::Splunk::XMLParser::parse ($xml);
254 0 ▶ return $#ret ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
$#ret ? : 0 0 0
255 } elsif ($response->code eq 204) {
256 # "No content"
257 # Happens when events are requested immediately
258 # after the job is enqueued. With a text/plain content type
259 # Empty array is the least disturbing thing to return here
260 0 return ();
261 } elsif ($content_type eq 'text/plain') {
262 # Sometimes an empty text/plain body is sent
263 # even without 204 return code.
264 0 return ();
265 } else {
266 # TODO: We probably can't do much about RAW
267 # format, yet we could parse at least JSON
268 0 croak "Unknown content type: $content_type";
269 }
270 }
271  
272 =head1 SEE ALSO
273  
274 L<WWW::Splunk>, L<sc>
275  
276 =head1 AUTHORS
277  
278 Lubomir Rintel, L<< <lkundrak@v3.sk> >>,
279 Michal Josef Špaček L<< <skim@cpan.org> >>
280  
281 The code is hosted on GitHub L<http://github.com/tupinek/perl-WWW-Splunk>.
282 Bug fixes and feature enhancements are always welcome.
283  
284 =head1 LICENSE
285  
286 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
287  
288 =cut
↑ Summary blib/lib/WWW/Splunk.pm »