File Coverage

blib/lib/Net/Radiator/Monitor.pm
Criterion Covered Total %
statement 18 53 33.9
branch 0 16 0.0
condition 0 9 0.0
subroutine 6 12 50.0
pod 3 3 100.0
total 27 93 29.0


line stmt bran cond sub pod time code
1             package Net::Radiator::Monitor;
2              
3 1     1   19188 use strict;
  1         2  
  1         38  
4 1     1   5 use warnings;
  1         3  
  1         27  
5              
6 1     1   384330 use IO::Socket::INET;
  1         29933  
  1         9  
7 1     1   489264 use IO::Select;
  1         2181  
  1         80  
8 1     1   9 use Carp qw(croak);
  1         3  
  1         416  
9              
10             our $VERSION = '0.021';
11             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             sub new {
38 0     0 1   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 0         $self->{server} = (defined $args{server} ? $args{server} : croak 'Constructor failed: no server supplied');
44 0   0       $self->{port} = $args{port} ||= 9048;
45 0   0       $self->{timeout}= $args{timeout} ||= 5;
46 0 0         $self->{sock}= IO::Socket::INET->new( PeerAddr => $self->{server},
47             PeerPort => $self->{port},
48             Proto => 'tcp',
49             Timeout => $self->{timeout}
50             ) or croak "Can't establish connection to server $args{server} on port $args{port}: $!\n";
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 0         : do { print "Failed to log in: $login[0]\n"; return 0 } )
  0            
  0            
58             }
59              
60             {
61 1     1   6 no strict 'refs';
  1         2  
  1         603  
62              
63             foreach my $m (keys %METHODS) {
64             *{ __PACKAGE__ . "::$m" } = sub {
65 0     0     my $self = shift;
66 0           my %res;
67 0 0         my ($c1,$c2) = ($m =~ /^list/ ? (0,2) : (0,1));
68            
69 0 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})) {
70 0           my ($var,$val) = (split /:/, $line)[$c1,$c2];
71 0   0       $res{$var} = $val || 0;
72             }
73            
74 0           return %res
75             }
76             }
77             }
78              
79 0 0   0 1   sub quit{ $_[0]->{sock}->send('QUIT') and $_[0]->{sock}->close }
80              
81 0     0 1   sub id { return $_[0]->_cmd(cmd => 'ID', args => '') }
82              
83             sub _trace {
84 0     0     my ($self,$level) = @_;
85 0           return $_[0]->_cmd(cmd => 'ID', args => '')
86             }
87              
88             sub _cmd {
89 0     0     my ($self,%args) = @_;
90 0           my $buf;
91 0           $self->{sock}->send("$args{cmd} $args{args}\n");
92              
93 0 0         if ($self->{sel}->can_read($self->{timeout})) {
94 0           $self->{sock}->recv($buf, 10240);
95 0           chomp $buf;
96 0           my @r = split '\001', $buf;
97 0 0 0       shift @r if ( $args{ml} and ( scalar @r > 1 ) );
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 Radiator on
184              
185             Where:
186              
187             =over 4
188              
189             =item
190              
191             Is the current local time on the server given in seconds since epoch.
192              
193             =item
194              
195             Is the Radiator server version.
196              
197             =item
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 method.
245              
246             The B 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<< >>
270              
271             =head1 BUGS
272              
273             Please report any bugs or feature requests to C, or through
274             the web interface at L. 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
294              
295             =item * AnnoCPAN: Annotated CPAN documentation
296              
297             L
298              
299             =item * CPAN Ratings
300              
301             L
302              
303             =item * Search CPAN
304              
305             L
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             1;