blib/lib/IPsonar.pm

filter: Statementstmt 79.7% 150 / 188 Branchbran 86.9% 40 / 46 Conditioncond 76.4% 26 / 34 MC/DCmcdc 52.6% 10 / 19 Subroutinesub 75.0% 21 / 28 Podpod 100% 7 / 7 totaltotal 78.8% 254 / 322 CC 36 SCAR 19.2
CC
36
Cov
81%
next_result
13.5
query
13.0
_new_with_file
5.0
CRAP 6.8
↑ Summary
1 package IPsonar;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 our $VERSION;
8 9 $VERSION = "0.32";
9  
10 9 ▶use Net::SSLeay qw(make_headers get_https);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use XML::Simple qw(:strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 HTTPS_TCP_PORT => 443,
19 DEFAULT_PAGE_SIZE => 100,
20 9 };
21  
22 # "IPsonar" and "Lumeta" are both registered marks of the Lumeta Corporation.
23  
24 =head1 NAME
25  
26 IPsonar - Wrapper to interact with the Lumeta IPsonar API
27  
28 =head1 VERSION
29  
30 Version 0.32
31 (Mercurial Revision ID: 8cc8c5b56c62+)
32  
33 =cut
34  
35 =head1 SYNOPSIS
36  
37 This module wraps the IPsonar RESTful API.
38 It handles the paging and https stuff so you can concentrate on extracting
39 information from reports.
40  
41 "Lumeta" and "IPsonar" are both registered trademarks of the Lumeta Coporation
42  
43 =head1 EXAMPLE
44  
45 # Script to get all the IP address for all the devices that have
46 # port 23 open:
47  
48 my $rsn = IPsonar->new('rsn_address_or_name','username','password');
49 my $test_report = 23;
50  
51 my $results = $rsn->query('detail.devices',
52 {
53 'q.f.report.id' => $test_report,
54 'q.f.servicediscovery.ports' => 23
55 }) or die "Problem ".$rsn->error;
56  
57 while (my $x = $rsn->next_result) {
58 print "IP: $x->{ip}\n";
59 }
60  
61  
62 =head1 CONSTRUCTORS
63  
64 =cut
65  
66 #-----------------------------------------------------------
67 # new(rsn, username, password)
68  
69 =over 8
70  
71 =item B<new (rsn, username, password)>
72  
73 =back
74  
75 Setup a connection to a report server using username / password
76 Note: This doesn't actually initiate a connection until you issue
77 a query. The I<rsn> can either be a hostname or IP address. The I<username>
78 and I<password> are for one of the GUI users.
79  
80 =cut
81  
82 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
83 2 my $class = shift;
84 2 my $rsn = shift;
85 2 my $username = shift;
86 2 my $password = shift;
87 2 my $self = {};
88 2 ▶ $self->{request} = sub { #request(query, parameters)
Subroutine __ANON__: called CC=1 SCAR=0.0
89 2 my $query = shift;
90 2 my $params = shift;
91 2 _request_using_password( $rsn, $query, $params, $username, $password );
92 2 };
93 2 bless $self, $class;
94 2 return $self;
95 }
96  
97 #-----------------------------------------------------------
98  
99 =over 8
100  
101 =item B<new_with_cert (rsn, path_to_cert, password)>
102  
103 =back
104  
105 Setup a connection to a report server using SSL certificate
106 Note: This doesn't actually initiate a connection until you issue
107 a query. The I<rsn> can either be a hostname or IP address. The
108 I<password> is the password required to unlock your certificate (as required).
109  
110 =cut
111  
112 0 ▶sub new_with_cert {
Subroutine new_with_cert: not called CC=1 SCAR=6.9
Pod documented
113 0 my $class = shift;
114 0 my $self = {};
115 0 my $rsn = shift;
116 0 my $cert_path = shift;
117 0 my $password = shift;
118 0 ▶ $self->{request} = sub { #request(query, parameters)
Subroutine __ANON__: not called CC=1 SCAR=6.9
119 0 my $query = shift;
120 0 my $params = shift;
121 0 _request_using_certificate( $rsn, $query, $params, $cert_path,
122 $password );
123 0 };
124 0 bless $self, $class;
125 0 return $self;
126 }
127  
128 # This function is to allow us to run unit tests without having
129 # access to a live RSN or the exact same reports I originally tested
130 # against. We're reading from a file with pre-canned requests and data.
131 6 ▶sub _new_with_file { # _new_with_file(file)
Subroutine _new_with_file: called CC=5 SCAR=16.1
132 6 my $class = shift;
133 6 my $file = shift;
134 6 my $self = {};
135 6 $self->{pages} = {};
136  
137 # Fill in $self->{pages}
138 6 ▶ open my $testfile, "<", $file or croak "Couldn't open $file";
Branch: 1 branch
BranchTrueFalseTotal
open my $testfile, "<", $file or croak("Couldn't open $file") 6 0 6
139 6 my $page = q{};
140 6 my $request;
141 6 ▶ while (<$testfile>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $testfile)) 892 6 898
142 892 ▶ if (/^URL: (.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^URL: (.*)$/) { } 346 546 892
143 346 ▶ $self->{pages}->{$request} = $page if $page;
Branch: 1 branch
BranchTrueFalseTotal
if $page 340 6 346
144 346 $request = _normalize_path($1);
145 346 $page = q{};
146 }
147 else {
148 546 $page .= $_;
149 }
150 }
151 6 $self->{pages}->{$request} = $page;
152  
153 70 ▶ $self->{request} = sub { #request(query, parameters)
Subroutine __ANON__: called CC=2 SCAR=7.0
154 70 my $query = shift;
155 70 my $params = shift;
156 70 my $path = _normalize_path( _get_path( $query, $params ) );
157 70 ▶ return $self->{pages}->{"$path"}
Condition50%1/2
$self->{'pages'}{"$path"} || croak("Couldn't find $path in file")
l!l
700
MC/DC0%0/1
$self->{'pages'}{"$path"} || croak("Couldn't find $path in file")
$self->{'pages'}{"$path"}
Truth table50%1/2
A
A= $self->{'pages'}{"$path"}
Aresult
11
00
158 || croak "Couldn't find $path in file";
159 6 };
160 6 bless $self, $class;
161 6 return $self;
162 }
163  
164 #-----------------------------------------------------------
165 # Normalize path exists to force the path we're looking for into
166 # a specific order. This fixes the issue I ran into at in perl 5.18
167 # where hash order is now effectively random (which is a generally
168 # a good thing but causes my code to fail)
169 #
170 # /reporting/api/service/detail.devices?q.f.report.id=23&q.f.servicediscovery.ports=2300&fmt=xml&q.pageSize=100&q.page=0 should become
171 # /reporting/api/service/detail.devices?fmt=xml&q.f.report.id=23&q.f.servicediscovery.ports=2300&q.pageSize=100&q.page=0
172 416 ▶sub _normalize_path {
Subroutine _normalize_path: called CC=1 SCAR=0.0
173 416 my $path = shift;
174 416 my ( $start, $rest ) = $path =~ /^(.+)\?(.*)$/;
175 416 my @params = split /&/, $rest;
176 416 return $start . '?' . join( '&', sort(@params) );
177 }
178  
179 #-----------------------------------------------------------
180  
181 =over 8
182  
183 =item B<new_localhost>
184  
185 =back
186  
187 Setup a connection to the report server you're on
188  
189 =cut
190  
191 0 ▶sub new_localhost {
Subroutine new_localhost: not called CC=1 SCAR=6.9
Pod documented
192 0 my $class = shift;
193 0 my $self = {};
194 0 ▶ $self->{request} = sub { #request(query, parameters)
Subroutine __ANON__: not called CC=1 SCAR=6.9
195 0 my $query = shift;
196 0 my $params = shift;
197 0 _request_localhost( $query, $params );
198 0 };
199 0 bless $self, $class;
200 0 return $self;
201 }
202  
203 #-----------------------------------------------------------
204  
205 =head1 METHODS
206  
207 =over 8
208  
209 =item B<$rsn-E<gt>query ( method, hashref_of_parameters)>
210  
211 =back
212  
213 Issue a query (get results for non-paged queries).
214 If you're getting back paged data we'll return the number of items
215 available in the query. If we're getting back a single result we
216 return a hashref to those results.
217  
218 If the query fails we'll leave the reason in $rsn->error
219  
220 =cut
221  
222 72 ▶sub query {
Subroutine query: called CC=13 SCAR=25.7
Pod documented
223 72 my $self = shift;
224 72 $self->{query} = shift;
225 72 $self->{params} = shift;
226  
227 # Set default parameters (over-riding fmt, it must be XML).
228 72 ▶ $self->{params}->{'q.page'} ||= 0;
Condition100%2/2
$self->{"params"}{"q.page"} ||= 0
l!l
4527
MC/DC100%1/1
$self->{"params"}{"q.page"} ||= 0
$self->{"params"}{"q.page"}
Truth table100%2/2
A
A= $self->{"params"}{"q.page"}
Aresult
11
00
229  
230 72 ▶ if ( !defined( $self->{params}->{'q.pageSize'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"params"}{"q.pageSize"}) 8 64 72
231 8 $self->{params}->{'q.pageSize'} = DEFAULT_PAGE_SIZE;
232 }
233  
234 72 $self->{params}->{fmt} = 'xml';
235  
236 #-----------------------------------------------------------
237 # instance variables
238 #
239 # total The total number of items we could iterate over
240 # request A funcref to the underlying function that gets
241 # our XML back from the server. It's a funcref
242 # because it can either be password or PKI authentication
243 # query The API call we're making (e.g. "config.reports"
244 # params The API parameters we're passing
245 # error The XML error we got back from IPsonar (if any)
246 # page_size The number of items we expect per page
247 # paged Is the result paged (or are we getting a single value)
248 # max_page Maximum page we'll be able to retrieve
249 # max_row Maximum row on this page (0-n).
250 #-----------------------------------------------------------
251  
252 72 my $res = $self->{request}( $self->{query}, $self->{params} );
253  
254 # KeyAttr => [] because otherwise XML::Simple tries to be clever
255 # and hand back a hashref keyed on "id" or "name" instead of an
256 # arrayref of items.
257 70 my $xml = XMLin( $res, KeyAttr => [], ForceArray => [] );
258  
259 70 ▶ if ( $xml->{status} ne 'SUCCESS' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{"status"} ne "SUCCESS") 1 69 70
260  
261 1 print Dumper($xml) . "\n";
262 1 $self->{error} = $xml->{error}->{detail};
263 1 croak $self->{error};
264 }
265  
266 69 $self->{xml} = $xml;
267 69 $self->{page_row} = 0;
268  
269 69 ▶ if ( defined( $xml->{total} ) and $xml->{total} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"total"} and $xml->{"total"} == 0) 2 67 69
Condition100%3/3
defined $xml->{"total"} and $xml->{"total"} == 0
!ll&&!rl&&r
1662
MC/DC100%2/2
defined $xml->{"total"} and $xml->{"total"} == 0
defined $xml->{"total"} $xml->{"total"} == 0
Truth table100%3/3
A and B
A= defined $xml->{"total"}B= $xml->{"total"} == 0
ABresult
0X0
100
111
270 2 $self->{total} = 0;
271 2 $self->{paged} = 1;
272 2 return $xml;
273 }
274  
275 67 ▶ if ( $xml->{total} && $self->{params}->{'q.pageSize'} ) { # Paged Data
Branch: 1 branch
BranchTrueFalseTotal
if ($xml->{'total'} and $self->{'params'}{'q.pageSize'}) { } 66 1 67
Condition66%2/3
$xml->{"total"} and $self->{"params"}{"q.pageSize"}
!ll&&!rl&&r
1066
MC/DC50%1/2
$xml->{"total"} and $self->{"params"}{"q.pageSize"}
$xml->{"total"} $self->{"params"}{"q.pageSize"}
Truth table66%2/3
A and B
A= $xml->{"total"}B= $self->{"params"}{"q.pageSize"}
ABresult
0X0
100
111
276 66 $self->{total} = $xml->{total};
277 66 my $page_size = $self->{params}->{'q.pageSize'};
278  
279 # Figure out what the key to the array data is
280 66 my $temp = XMLin( $res, NoAttr => 1, KeyAttr => [], ForceArray => 1 );
281 66 my $key = ( keys %{$temp} )[0];
282 66 $self->{pagedata} = $self->{xml}->{$key};
283 66 ▶ warn "Key = $key, Self = " . Dumper($self) if !$self->{xml}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"xml"}{$key} 0 66 66
284  
285 # Setup paging information
286 #TODO this is a honking mess, too many special conditions.
287 66 $self->{paged} = 1;
288 66 $self->{max_page} = int( ( $self->{total} - 1 ) / $page_size );
289  
290 $self->{max_row} =
291 $self->{params}->{'q.page'} < $self->{max_page}
292 ? $page_size - 1
293 66 ▶ : ( ( $self->{total} % $page_size ) || $page_size ) - 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'params'}{'q.page'} < $self->{'max_page'} ? : 45 21 66
Condition66%2/3
$self->{'total'} % $page_size || $page_size
l!l&&r!l&&!r
1650
MC/DC0%0/2
$self->{'total'} % $page_size || $page_size
$self->{'total'} % $page_size $page_size
Truth table66%2/3
A || B
A= $self->{'total'} % $page_sizeB= $page_size
ABresult
1X1
011
000
294  
295 # There's only one page with $self->{total} items
296 66 ▶ if ( $self->{params}->{'q.pageSize'} == $self->{total} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}{"q.pageSize"} == $self->{"total"}) 1 65 66
297 1 $self->{max_row} = $self->{total} - 1;
298 }
299  
300 # We're looking at things with pagesize 1
301 66 ▶ if ( $self->{params}->{'q.pageSize'} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}{"q.pageSize"} == 1) 16 50 66
302 16 $self->{max_row} = 0;
303 }
304  
305 66 return $self->{total};
306 }
307 else { # Not paged data
308 1 $self->{total} = 0;
309 1 $self->{paged} = 0;
310 1 delete( $self->{key} );
311 1 return $xml;
312 }
313 }
314  
315 #-----------------------------------------------------------
316  
317 =over 8
318  
319 =item B<$rsn-E<gt>next_result ()>
320  
321 =back
322  
323 Get next paged results as a hashref. Returns 0 when we've got no more
324 results.
325  
326 Note: Currently, we always return a hashref to the same (only) non-paged
327 results.
328  
329 =cut
330  
331 325 ▶sub next_result {
Subroutine next_result: called CC=13 SCAR=26.0
Pod documented
332 325 my $self = shift;
333  
334 #print "page_row: $self->{page_row}, max_row: $self->{max_row}, ".
335 # "page: $self->{params}->{'q.page'}, max_page: $self->{max_page}\n";
336  
337 #No results
338 325 ▶ return 0 if $self->{total} == 0 && $self->{paged};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"total"} == 0 and $self->{"paged"} 2 323 325
Condition66%2/3
$self->{"total"} == 0 and $self->{"paged"}
!ll&&!rl&&r
32302
MC/DC50%1/2
$self->{"total"} == 0 and $self->{"paged"}
$self->{"total"} == 0 $self->{"paged"}
Truth table66%2/3
A and B
A= $self->{"total"} == 0B= $self->{"paged"}
ABresult
0X0
100
111
339  
340 #Not paged data
341 323 ▶ return $self->{xml} if !$self->{paged};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"paged"} 0 323 323
342  
343 #End of Data
344 323 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"params"}{"q.page"} == $self->{"max_page"} and $self->{"page_row"} > $self->{"max_row"} || ref $self->{"pagedata"} eq "ARRAY" && !$self->{"pagedata"}[$self->{"page_row"}]) 19 304 323
Condition77%7/9
$self->{"params"}{"q.page"} == $self->{"max_page"} and $self->{"page_row"} > $self->{"max_row"} || ref $self->{"pagedata"} eq "ARRAY" && !$self->{"pagedata"}[$self->{"page_row"}]
!ll&&!rl&&r
20410019
$self->{"page_row"} > $self->{"max_row"} or ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]
l!l&&r!l&&!r
190100
ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]
!ll&&!rl&&r
1990
MC/DC50%2/4
$self->{"params"}{"q.page"} == $self->{"max_page"} and $self->{"page_row"} > $self->{"max_row"} || ref $self->{"pagedata"} eq "ARRAY" && !$self->{"pagedata"}[$self->{"page_row"}]
$self->{"params"}{"q.page"} == $self->{"max_page"} $self->{"page_row"} > $self->{"max_row"} ref $self->{"pagedata"} eq "ARRAY" not $self->{"pagedata"}[$self->{"page_row"}]
Truth table80%4/5
A and B or C and not(D)
A= $self->{"params"}{"q.page"} == $self->{"max_page"}B= $self->{"page_row"} > $self->{"max_row"}C= ref $self->{"pagedata"} eq "ARRAY"D= not $self->{"pagedata"}[$self->{"page_row"}]
ABCDresult
0XXX0
100X0
10100
11XX1
10111
345 $self->{params}->{'q.page'} == $self->{max_page}
346 && (
347 $self->{page_row} > $self->{max_row}
348 || ( ref( $self->{pagedata} ) eq 'ARRAY'
349 && !$self->{pagedata}[ $self->{page_row} ] )
350 )
351 )
352 {
353 19 return;
354 }
355  
356 #End of Page
357 # The pagedata of this test handles cases where IPsonar doesn't
358 # return all the rows it's supposed to (rare, but it happens)
359 304 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"page_row"} > $self->{"max_row"} or ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]) 45 259 304
Condition66%4/6
$self->{"page_row"} > $self->{"max_row"} or ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]
l!l&&r!l&&!r
450259
ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]
!ll&&!rl&&r
22570
MC/DC33%1/3
$self->{"page_row"} > $self->{"max_row"} or ref $self->{"pagedata"} eq "ARRAY" and not $self->{"pagedata"}[$self->{"page_row"}]
$self->{"page_row"} > $self->{"max_row"} ref $self->{"pagedata"} eq "ARRAY" not $self->{"pagedata"}[$self->{"page_row"}]
Truth table75%3/4
A or B and not(C)
A= $self->{"page_row"} > $self->{"max_row"}B= ref $self->{"pagedata"} eq "ARRAY"C= not $self->{"pagedata"}[$self->{"page_row"}]
ABCresult
1XX1
0111
00X0
0100
360 $self->{page_row} > $self->{max_row}
361 || ( ref( $self->{pagedata} ) eq 'ARRAY'
362 && !$self->{pagedata}[ $self->{page_row} ] )
363 )
364 {
365 45 $self->{params}->{'q.page'}++;
366 45 $self->query( $self->{query}, $self->{params} );
367 }
368  
369 #Single item on last page
370 304 ▶ if ( $self->{page_row} == 0 and $self->{max_row} == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"page_row"} == 0 and $self->{"max_row"} == 0) 20 284 304
Condition100%3/3
$self->{"page_row"} == 0 and $self->{"max_row"} == 0
!ll&&!rl&&r
2394520
MC/DC100%2/2
$self->{"page_row"} == 0 and $self->{"max_row"} == 0
$self->{"page_row"} == 0 $self->{"max_row"} == 0
Truth table100%3/3
A and B
A= $self->{"page_row"} == 0B= $self->{"max_row"} == 0
ABresult
0X0
100
111
371 20 $self->{page_row}++;
372 20 return $self->{pagedata};
373 }
374  
375 284 return $self->{pagedata}[ $self->{page_row}++ ];
376  
377 }
378  
379 #-----------------------------------------------------------
380  
381 =over 8
382  
383 =item B<$rsn-E<gt>error>
384  
385 =back
386  
387 Get error information
388  
389 =cut
390  
391 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
392 0 my $self = shift;
393 0 return $self->{error};
394 }
395  
396 #===========================================================
397 # From API cookbook
398 ###
399  
400 ### These can already be defined in your environment, or you can get
401 ### them from the user on the command line or from stdin. It's
402 ### probably best to get the password from stdin.
403  
404 ###
405 ### Routine to run a query using authentication via PKI certificate
406 ### Inputs:
407 ### server - the IPsonar report server
408 ### method - the method or query name, e.g., getReports
409 ### params - reference to a hash of parameter name / value pairs
410 ### Output:
411 ### The page in XML format returned by IPsonar
412 ###
413 0 ▶sub _request_using_certificate {
Subroutine _request_using_certificate: not called CC=1 SCAR=6.9
414 0 my ( $server, $method, $params, $cert, $passwd ) = @_;
415  
416 0 my $path = _get_path( $method, $params ); # See "Constructing URLs";
417 0 my $url = "https://${server}${path}";
418  
419 0 local $ENV{HTTPS_PKCS12_FILE} = $cert;
420 0 local $ENV{HTTPS_PKCS12_PASSWORD} = $passwd;
421 0 my $ua = LWP::UserAgent->new;
422 0 my $req = HTTP::Request->new( 'GET', $url );
423 0 my $res = $ua->request($req);
424  
425 0 return $res->content;
426 }
427  
428 #===========================================================
429 # From API cookbook
430 ###
431 ### Routine to run a query using authentication via user name and password.
432 ### Inputs:
433 ### server - the IPsonar report server
434 ### method - the method or query name, e.g., initiateScan
435 ### params - reference to a hash of parameter name / value pairs
436 ### uname - the IPsonar user name
437 ### passwd - the IPsonar user's password
438 ### Output:
439 ### The page in XML format returned by IPsonar
440 ###
441 2 ▶sub _request_using_password {
Subroutine _request_using_password: called CC=2 SCAR=7.1
442 2 my ( $server, $method, $params, $uname, $passwd ) = @_;
443 2 my $port = HTTPS_TCP_PORT; # The usual port for https
444 2 my $path = _get_path( $method, $params ); # See "Constructing URLs"
445 #print "URL: https://$server$path\n";
446  
447 2 my $authstring = MIME::Base64::encode( "$uname:$passwd", q() );
448 2 my ( $page, $result, %headers ) = # we're only interested in $page
449 Net::SSLeay::get_https( $server, $port, $path,
450 Net::SSLeay::make_headers( Authorization => 'Basic ' . $authstring ) );
451 2 ▶ if ( !( $result =~ /OK$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result =~ /OK$/) 2 0 2
452 2 croak $result;
453 }
454 0 return ($page);
455 }
456  
457 0 ▶sub _request_localhost {
Subroutine _request_localhost: not called CC=1 SCAR=6.9
458 0 my ( $method, $params ) = @_;
459 0 my $path = _get_path( $method, $params ); # See "Constructing URLs"
460 0 my $url = "http://127.0.0.1:8081${path}";
461  
462 0 my $ua = LWP::UserAgent->new;
463 0 my $req = HTTP::Request->new( 'GET', $url );
464 0 my $res = $ua->request($req);
465  
466 0 return $res->content;
467 }
468  
469 #===========================================================
470 # From API cookbook
471 ###
472 ### Routine to encode the path part of an API call's URL. The
473 ### path is everything after "https://server".
474 ### Inputs:
475 ### method - the method or query name, e.g., initiateScan
476 ### params - reference to a hash of parameter name /value pairs
477 ### Output:
478 ### The query path with the special characters properly encoded
479 ###
480 72 ▶sub _get_path {
Subroutine _get_path: called CC=4 SCAR=13.9
481 72 my ( $method, $params ) = @_;
482 72 my $path_start = '/reporting/api/service/';
483 72 my $path = $path_start . $method . q(?); # all API calls start this way
484 # Now add parameters
485 72 ▶ if ( defined $params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params) 72 0 72
486 72 ▶ while ( my ( $p, $v ) = each %{$params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 348 72 420
487 348 ▶ if ( $path !~ /[?]$/xms ) { # ... if this isn't the first param
Branch: 1 branch
BranchTrueFalseTotal
unless ($path =~ /[?]$/msx) 276 72 348
488 276 $path .= q(&); # params are separated by &
489 }
490 348 $path .= "$p=$v";
491 }
492 }
493 72 my $encoded = URI->new($path); # encode the illegal characters
494 # (eg, space => %20)
495 72 return ( $encoded->as_string );
496 }
497  
498 #-----------------------------------------------------------
499  
500 =head1 METHODS
501  
502 =over 8
503  
504 =item B<$rsn-E<gt>reports ()>
505  
506 =back
507  
508 Returns an array representing the reports on this RSN.
509 This array is sorted by ascending report id.
510 Do not run this while you're iterating through another query as
511 it will reset its internal state.
512 Timestamps are converted to epoch time.
513  
514 An example of how you might use this:
515  
516 #!/usr/bin/perl
517  
518 use strict;
519 use warnings;
520  
521 use IPsonar;
522  
523 my $rsn = IPsonar->new('s2','username','password');
524 my @reports = $rsn->reports;
525  
526  
527 =cut
528  
529 2 ▶sub reports {
Subroutine reports: called CC=3 SCAR=11.0
Pod documented
530 2 my $self = shift;
531 2 my @reports;
532 2 ▶ my $results = $self->query( 'config.reports', {} )
Branch: 1 branch
BranchTrueFalseTotal
my $results = $self->query("config.reports", {}) or croak('Problem ' . $self->error) 2 0 2
533 or croak "Problem " . $self->error;
534  
535 2 ▶ while ( my $r = $self->next_result ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->next_result) 31 2 33
536 31 $r->{timestamp} = int( $r->{timestamp} / 1000 );
537 31 push @reports, $r;
538 }
539  
540 2 @reports = sort { $a->{id} <=> $b->{id} } @reports;
541 2 return @reports;
542 }
543 9 1;
544  
545 =head1 USAGE
546  
547 The way I've settled on using this is to build the query I want using
548 the built-in IPsonar query builder. Once I've got that fine tuned I
549 translate the url into a query.
550  
551 For example, if I build a query to get all the routers from report 49
552 (showing port information), I'd wind up with the following URL:
553  
554 https://s2/reporting/api/service/detail.devices?fmt=xml&q.page=0&q.pageSize=100&q.details=Ports&q.f.report&q.f.report.id=49&q.f.router&q.f.router.router=true
555  
556 This module takes care of the I<fmt>, I<q.page>, and I<q.pageSize> parameters
557 for you (you can specify I<q.pageSize> if you really want).
558 I might translate that into the following code:
559  
560 #!/usr/bin/perl
561  
562 use strict;
563 use warnings;
564  
565 use IPsonar;
566 use Data::Dumper;
567  
568 my $rsn = IPsonar->new('s2','username','password');
569  
570 my $results = $rsn->query('detail.devices',
571 {
572 'q.details' => 'Ports',
573 'q.f.report.id' => 49,
574 'q.f.router.router' => 'true',
575 }) or die "Problem ".$rsn->error;
576  
577 while ( my $x = $rsn->next_result ) {
578 print Dumper($x);
579 my $ports = $x->{ports}->{closedPorts}->{integer};
580 print ref($ports) eq 'ARRAY' ? join ',' , @{$ports} : $ports ;
581 }
582  
583 And get this as a result:
584  
585 $VAR1 = {
586 'ports' => {
587 'openPorts' => {
588 'integer' => '23'
589 },
590 'closedPorts' => {
591 'integer' => [
592 '21',
593 '22',
594 '25',
595 ]
596 }
597 },
598 'ip' => '10.2.0.2'
599 };
600 21,23,25
601  
602 Note that things like ports might come back as an Arrayref or might
603 come back as a single item. I find there's some tweaking involved as you
604 figure out how the data is laid out.
605  
606 =cut
↑ Summary