File Coverage

lib/Slaughter/Info/freebsd.pm
Criterion Covered Total %
statement 42 52 80.7
branch 10 14 71.4
condition 1 3 33.3
subroutine 4 4 100.0
pod 2 2 100.0
total 59 75 78.6


line stmt bran cond sub pod time code
1             #!/usr/bin/perl -w
2              
3             =head1 NAME
4            
5             Slaughter::Info::freebsd - Determine information about a FreeBSD host.
6            
7             =cut
8              
9             =head1 SYNOPSIS
10            
11             This module is the FreeBSD version of the Slaughter information-gathering
12             module.
13            
14             Modules beneath the C<Slaughter::Info> namespace are loaded when slaughter
15             is executed, they are used to populate a hash with information about
16             the current host.
17            
18             This module is loaded only on FreeBSD systems, and will determine such details
19             as the local hostname, the free RAM, any IP addresses, etc.
20            
21             The correct information-gathering module is loaded at run-time via the use of the C<$^O> variable, and if no system-specific module is available then the generic L<Slaughter::Info::generic> module is used as a fall-back.
22            
23             The information discovered can be dumped by running C<slaughter>
24            
25             =for example begin
26            
27             ~# slaughter --dump
28            
29             =for example end
30            
31             Usage of this module is as follows:
32            
33             =for example begin
34            
35             use Slaughter::Info::freebsd;
36            
37             my $obj = Slaughter::Info::freebsd->new();
38             my $data = $obj->getInformation();
39            
40             # use info now ..
41             print "We have $data->{'ip_count'} IPv4 addresses.\n";
42             print "We have $data->{'ip6_count'} IPv6 addresses.\n";
43            
44             =for example end
45            
46             When this module is used an attempt is also made to load the module
47             C<Slaughter::Info::Local::freebsd> and if that succeeds it will be used to
48             augment the information discovered and made available to slaughter
49             policies.
50            
51             =cut
52              
53              
54             =head1 METHODS
55            
56             Now follows documentation on the available methods.
57            
58             =cut
59              
60              
61 1     1   653 use strict;
  1         1  
  1         24  
62 1     1   4 use warnings;
  1         1  
  1         406  
63              
64              
65             package Slaughter::Info::freebsd;
66              
67              
68             #
69             # The version of our release.
70             #
71             our $VERSION = "3.0.5";
72              
73              
74              
75             =head2 new
76            
77             Create a new instance of this object.
78            
79             =cut
80              
81             sub new
82             {
83 1     1 1 437     my ( $proto, %supplied ) = (@_);
84 1   33     5     my $class = ref($proto) || $proto;
85              
86 1         1     my $self = {};
87 1         2     bless( $self, $class );
88 1         2     return $self;
89              
90             }
91              
92              
93             =head2 getInformation
94            
95             This function retrieves meta-information about the current host.
96            
97             The return value is a hash-reference of data determined dynamically.
98            
99             =cut
100              
101             sub getInformation
102             {
103 1     1 1 709     my ($self) = (@_);
104              
105             #
106             # The data we will return.
107             #
108 1         2     my $ref;
109              
110             #
111             # Call "hostname" to determine the local hostname.
112             #
113 1         2130     $ref->{ 'fqdn' } = `hostname`;
114 1         9     chomp( $ref->{ 'fqdn' } );
115              
116             #
117             # Get the hostname and domain name as seperate strings.
118             #
119 1 50       12     if ( $ref->{ 'fqdn' } =~ /^([^.]+)\.(.*)$/ )
120                 {
121 0         0         $ref->{ 'hostname' } = $1;
122 0         0         $ref->{ 'domain' } = $2;
123                 }
124                 else
125                 {
126              
127             #
128             # Better than nothing, right?
129             #
130 1         4         $ref->{ 'hostname' } = $ref->{ 'fqdn' };
131 1         4         $ref->{ 'domain' } = $ref->{ 'fqdn' };
132                 }
133              
134              
135             #
136             # Kernel version.
137             #
138 1         1689     $ref->{ 'release' } = `uname -r`;
139 1         10     chomp( $ref->{ 'release' } );
140              
141             #
142             # Are we i386/amd64?
143             #
144 1         1578     $ref->{ 'arch' } = `uname -p`;
145 1         9     chomp( $ref->{ 'arch' } );
146              
147             #
148             # This should be portable.
149             #
150 1         10     $ref->{ 'path' } = $ENV{ 'PATH' };
151              
152             #
153             # Count of IPv4/IPv6 addresses.
154             #
155 1         3     my $ipv4 = 1;
156 1         3     my $ipv6 = 1;
157              
158             #
159             # Parse the output of /sbin/ifconfig.
160             #
161 1         2071     foreach my $line ( split( /[\r\n]/, `ifconfig` ) )
162                 {
163 17         19         chomp($line);
164 17 100       43         next unless ( $line =~ /(inet|inet6)/ );
165              
166 4 100       15         if ( $line =~ /inet ([^ \t]+)/ )
167                     {
168 2         9             my $addr = $1;
169 2 50       6             next if ( $addr =~ /^127\./i );
170 2         9             $ref->{ 'ip_' . $ipv4 } = $addr;
171 2         5             $ipv4 += 1;
172                     }
173 4 100       8         if ( $line =~ /inet6 ([^ \t]+)/ )
174                     {
175 2         4             my $addr = $1;
176 2 50       16             next if ( $addr =~ /fe80/i );
177 2         7             $ref->{ 'ip6_' . $ipv6 } = $addr;
178 2         3             $ipv6 += 1;
179                     }
180                 }
181              
182             # counts of addresses
183 1         4     $ref->{ 'ip_count' } = $ipv4;
184 1         3     $ref->{ 'ip6_count' } = $ipv6;
185              
186             #
187             # Load Average - This test will always succeed on an FreeBSD
188             # system, but it is here to allow the module to be loaded/tested
189             # upon a GNU/Linux host
190             #
191 1 50       7     if ( $^O =~ /freebsd/ )
192                 {
193 0         0         $ref->{ 'load_average' } = `sysctl -n vm.loadavg`;
194              
195             # remove newline.
196 0         0         chomp( $ref->{ 'load_average' } );
197              
198             # remove "{" and "}" wrappers
199 0         0         $ref->{ 'load_average' } =~ s/[\{\}]//g;
200              
201             # remove leading/trailing whitespace.
202 0         0         $ref->{ 'load_average' } =~ s/^\s+|\s+$//g;
203              
204             #
205             # Split into per-minute values.
206             #
207 0         0         my @avg = split( /[ \t]/, $ref->{ 'load_average' } );
208 0         0         $ref->{ 'load_average_1' } = $avg[0];
209 0         0         $ref->{ 'load_average_5' } = $avg[1];
210 0         0         $ref->{ 'load_average_15' } = $avg[2];
211              
212              
213                 }
214              
215 1         10     return ($ref);
216             }
217              
218              
219              
220             1;
221              
222              
223              
224             =head1 AUTHOR
225            
226             Steve Kemp <steve@steve.org.uk>
227            
228             =cut
229              
230             =head1 LICENSE
231            
232             Copyright (c) 2010-2015 by Steve Kemp. All rights reserved.
233            
234             This module is free software;
235             you can redistribute it and/or modify it under
236             the same terms as Perl itself.
237             The LICENSE file contains the full text of the license.
238            
239             =cut
240