blib/lib/Net/Radiator/Monitor.pm

filter: Statementstmt 41.6% 25 / 60 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 50.0% 6 / 12 Podpod 100% 3 / 3 totaltotal 32.3% 34 / 105 CC 14 SCAR 29.5
CC
14
Cov
29%
new
42.0
__ANON__
25.0
_cmd
20.0
CRAP 19.2
↑ Summary
1 package Net::Radiator::Monitor;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.021';
11 2 our %METHODS = (
12 server_stats => { cmd => 'STATS',
13 args=> '.',
14 ml => 1
15 },
16 client_stats => {
17 cmd => 'STATS',
18 args=> '".Client." . $_[0]',
19 evl => 1,
20 ml => 1
21 },
22 list_clients => { cmd => 'LIST',
23 args=> 'Client',
24 ml => 1
25 },
26 list_realms => { cmd => 'LIST',
27 args=> 'Realm',
28 ml => 1
29 },
30 list_handlers => {
31 cmd => 'LIST',
32 args=> 'Handler',
33 ml => 1
34 }
35 );
36  
37 0 ▶sub new {
Subroutine new: not called CC=6 SCAR=37.4
Pod documented
38 0 my ($class,%args)= @_;
39 0 my $self = {};
40 0 bless $self, $class;
41 0 $self->{user} = $args{user};
42 0 $self->{passwd} = $args{passwd};
43 0 ▶ $self->{server} = (defined $args{server} ? $args{server} : croak 'Constructor failed: no server supplied');
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'server'} ? : 0 0 0
44 0 ▶ $self->{port} = $args{port} ||= 9048;
Condition0%0/2
$args{"port"} ||= 9048
l!l
00
MC/DC0%0/1
$args{"port"} ||= 9048
$args{"port"}
Truth table0%0/2
A
A= $args{"port"}
Aresult
11
00
45 0 ▶ $self->{timeout}= $args{timeout} ||= 5;
Condition0%0/2
$args{"timeout"} ||= 5
l!l
00
MC/DC0%0/1
$args{"timeout"} ||= 5
$args{"timeout"}
Truth table0%0/2
A
A= $args{"timeout"}
Aresult
11
00
46 $self->{sock}= IO::Socket::INET->new( PeerAddr => $self->{server},
47 PeerPort => $self->{port},
48 Proto => 'tcp',
49 Timeout => $self->{timeout}
50 0 ▶ ) or croak "Can't establish connection to server $args{server} on port $args{port}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"sock"} = "IO::Socket::INET"->new("PeerAddr", $self->{"server"}, "PeerPort", $self->{"port"}, "Proto", "tcp", "Timeout", $self->{"timeout"}) or croak(qq[Can't establish connection to server $args{"server"} on port $args{"port"}: $!\n]) 0 0 0
51  
52 0 $self->{sel}= IO::Select->new($self->{sock});
53
54 0 my @login = $self->_cmd(cmd => 'LOGIN', args => " $self->{user} $self->{passwd}");
55 return ( $login[0] eq 'LOGGEDIN'
56 ? $self
57 0 ▶ : do { print "Failed to log in: $login[0]\n"; return 0 } )
Branch: 1 branch
BranchTrueFalseTotal
$login[0] eq 'LOGGEDIN' ? : 0 0 0
58 }
59  
60 {
61 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 2 foreach my $m (keys %METHODS) {
64 10 ▶ *{ __PACKAGE__ . "::$m" } = sub {
Subroutine __ANON__: not called CC=5 SCAR=32.2
65 0 my $self = shift;
66 0 my %res;
67 0 ▶ my ($c1,$c2) = ($m =~ /^list/ ? (0,2) : (0,1));
Branch: 1 branch
BranchTrueFalseTotal
$m =~ /^list/ ? : 0 0 0
68
69 0 ▶ foreach my $line ($self->_cmd(cmd => $METHODS{$m}{cmd}, args => ($METHODS{$m}{evl} ? eval $METHODS{$m}{args} : $METHODS{$m}{args}), ml => $METHODS{$m}{ml})) {
Branch: 1 branch
BranchTrueFalseTotal
$Net::Radiator::Monitor::METHODS{$m}{'evl'} ? : 0 0 0
70 0 my ($var,$val) = (split /:/, $line)[$c1,$c2];
71 0 ▶ $res{$var} = $val || 0;
Condition0%0/2
$val || 0
l!l
00
MC/DC0%0/1
$val || 0
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
72 }
73
74 0 return %res
75 }
76 10 }
77 }
78  
79 0 ▶sub quit{ $_[0]->{sock}->send('QUIT') and $_[0]->{sock}->close }
Subroutine quit: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"sock"}->send("QUIT") and $_[0]{"sock"}->close 0 0 0
80  
81 0 ▶sub id { return $_[0]->_cmd(cmd => 'ID', args => '') }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
82  
83 0 ▶sub _trace {
Subroutine _trace: not called CC=1 SCAR=6.9
84 0 my ($self,$level) = @_;
85 0 return $_[0]->_cmd(cmd => 'ID', args => '')
86 }
87  
88 0 ▶sub _cmd {
Subroutine _cmd: not called CC=4 SCAR=30.0
89 0 my ($self,%args) = @_;
90 0 my $buf;
91 0 $self->{sock}->send("$args{cmd} $args{args}\n");
92  
93 0 ▶ if ($self->{sel}->can_read($self->{timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"sel"}->can_read($self->{"timeout"})) 0 0 0
94 0 $self->{sock}->recv($buf, 10240);
95 0 chomp $buf;
96 0 my @r = split '\001', $buf;
97 0 ▶ shift @r if ( $args{ml} and ( scalar @r > 1 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $args{"ml"} and scalar @r > 1 0 0 0
Condition0%0/3
$args{"ml"} and scalar @r > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"ml"} and scalar @r > 1
$args{"ml"} scalar @r > 1
Truth table0%0/3
A and B
A= $args{"ml"}B= scalar @r > 1
ABresult
0X0
100
111
98 0 return @r;
99 }
100  
101 return
102 0 }
103  
104 =head1 NAME
105  
106 Net::Radiator::Monitor - Perl interface to Radiator Monitor command language
107  
108 =head1 SYNOPSIS
109  
110 This module provides a Perl interface to Radiator Monitor command language.
111  
112 use strict;
113 use warnings;
114  
115 use Net::Radiator::Monitor;
116 use Carp qw(croak);
117  
118 my $monitor = Net::Radiator::Monitor->new(
119 user => $user,
120 passwd => $passwd,
121 server => $server,
122 port => 9084,
123 timeout => 5
124 ) or croak "Unable to create monitor: $!\n";
125  
126 print $monitor->id;
127  
128 $monitor->quit;
129  
130 =head1 METHODS
131  
132 =head2 new
133  
134 my $monitor = Net::Radiator::Monitor->new(
135 user => $user,
136 passwd => $passwd,
137 server => $server,
138 );
139
140  
141 Constructor - creates a new Net::Radiator::Monitor object using the specified parameters. This method
142 takes three mandatory and two optional parameters.
143  
144 =over 4
145  
146 =item user
147  
148 The username to use to connect to the monitor interface. This username must have the required
149 access to connect to the monitor.
150  
151 =item passwd
152  
153 The password for the username use to connect to the monitor interface.
154  
155 =item server
156  
157 The server to connect to - this should be either a resolvable hostname or an IP address.
158  
159 =item port
160  
161 The port on which to connect to the monitor interface - this parameter is optional and if
162 not specified will default to the Radiator default port of 9084.
163  
164 =item timeout
165  
166 The connection timeout value and recieve timeout value for the connection to the Radiator
167 server - this parameter is optional and if not specified will default to five seconds.
168  
169 =back
170  
171 =head2 quit
172  
173 $monitor->quit;
174  
175 Closes the monitor connection.
176  
177 =head2 id
178  
179 my $id = $monitor->id;
180  
181 Returns the Radiator server ID string. the string has the following format:
182  
183 ID <local_timestamp> Radiator <version> on <servername>
184  
185 Where:
186  
187 =over 4
188  
189 =item <local_timestamp>
190  
191 Is the current local time on the server given in seconds since epoch.
192  
193 =item <version>
194  
195 Is the Radiator server version.
196  
197 =item <servername>
198  
199 Is the configured server name.
200  
201 =back
202  
203 =head2 server_stats
204  
205 my %server_stats = $monitor->server_stats;
206  
207 foreach my $stats (sort keys %server_stats) {
208 print "$stats : $server_stats{$stats}\n"
209 }
210  
211 Returns a hash containing name,value pairs of collected server statistics.
212 Server statistics are culminative values of access and accounting across all
213 configured objects.
214  
215 The measured statistics (and the keys of the hash) are:
216  
217 Access challenges
218 Access rejects
219 Access requests
220 Accounting requests
221 Accounting responses
222 Average response time
223 Bad authenticators in accounting requests
224 Bad authenticators in authentication requests
225 Dropped access requests
226 Dropped accounting requests
227 Duplicate access requests
228 Duplicate accounting requests
229 Malformed access requests
230 Malformed accounting requests
231 Total Bad authenticators in requests
232 Total dropped requests
233 Total duplicate requests
234 Total proxied requests
235 Total proxied requests with no reply
236 Total requests
237  
238 =head2 client_stats ($client_id)
239  
240 my %client_stats = $monitor->client_stats($client_id);
241
242 Returns a hash containing name,value pairs of collected statistics for client
243 specified by the value of the client id. The available statistics are the same
244 as those listed for the B<server_stats> method.
245  
246 The B<list_clients> method can be sed to retrieve valid client IDs.
247  
248 =head2 list_clients
249  
250 while (($id, $name) = each $monitor->list-clients) {
251 print "Client : $name - ID : $id\n"
252 }
253  
254 Returns a hash containing all configured clients where the key is the numerical identifier
255 for the realm and the value is the client name or IP address (dependent on configuration).
256  
257 =head2 list_realms
258  
259 Returns a hash containing all configured realms where the key is the numerical identifier
260 for the realm and the value is the realm name.
261  
262 =head2 list_handlers
263  
264 Returns a hash containing all configured handlers where the key is the numerical identifier
265 for the handler and the value is the handler name.
266  
267 =head1 AUTHOR
268  
269 Luke Poskitt, C<< <ltp at cpan.org> >>
270  
271 =head1 BUGS
272  
273 Please report any bugs or feature requests to C<bug-net-radiator-monitor at rt.cpan.org>, or through
274 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Radiator-Monitor>. I will be notified, and then you'll
275 automatically be notified of progress on your bug as I make changes.
276  
277  
278  
279  
280 =head1 SUPPORT
281  
282 You can find documentation for this module with the perldoc command.
283  
284 perldoc Net::Radiator::Monitor
285  
286  
287 You can also look for information at:
288  
289 =over 4
290  
291 =item * RT: CPAN's request tracker (report bugs here)
292  
293 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Radiator-Monitor>
294  
295 =item * AnnoCPAN: Annotated CPAN documentation
296  
297 L<http://annocpan.org/dist/Net-Radiator-Monitor>
298  
299 =item * CPAN Ratings
300  
301 L<http://cpanratings.perl.org/d/Net-Radiator-Monitor>
302  
303 =item * Search CPAN
304  
305 L<http://search.cpan.org/dist/Net-Radiator-Monitor/>
306  
307 =back
308  
309  
310 =head1 ACKNOWLEDGEMENTS
311  
312  
313 =head1 LICENSE AND COPYRIGHT
314  
315 Copyright 2012 Luke Poskitt.
316  
317 This program is free software; you can redistribute it and/or modify it
318 under the terms of either: the GNU General Public License as published
319 by the Free Software Foundation; or the Artistic License.
320  
321 See http://dev.perl.org/licenses/ for more information.
322  
323  
324 =cut
325  
326 2 1;
↑ Summary