File Coverage

blib/lib/DBD/Sys/Plugin/Any/NetInterface.pm
Criterion Covered Total %
statement 13 64 20.3
branch 0 28 0.0
condition 0 5 0.0
subroutine 5 9 55.5
pod 4 4 100.0
total 22 110 20.0


line stmt bran cond sub pod time code
1             package DBD::Sys::Plugin::Any::NetInterface;
2              
3 3     3   2662 use strict;
  3         6  
  3         112  
4 3     3   16 use warnings;
  3         4  
  3         142  
5 3     3   14 use vars qw($VERSION @colNames);
  3         6  
  3         172  
6              
7 3     3   15 use base qw(DBD::Sys::Table);
  3         6  
  3         2514  
8              
9             =pod
10              
11             =head1 NAME
12              
13             DBD::Sys::Plugin::Any::NetInterface - provides a table containing the known network interfaces
14              
15             =head1 SYNOPSIS
16              
17             $netifs = $dbh->selectall_hashref("select * from netint", "interface");
18              
19             $VERSION = "0.02";
20             =head1 ISA
21              
22             DBD::Sys::Plugin::Any::NetInterface
23             ISA DBD::Sys::Table
24              
25             =cut
26              
27             my $haveNetInterface;
28              
29             @colNames =
30             qw(interface address_family address netmask broadcast hwaddress flags_bin flags mtu metric);
31              
32             =head1 DESCRIPTION
33              
34             This module provides the table C which contains the network
35             interfaces configured on a host and it's assigned addresses.
36              
37             =head2 COLUMNS
38              
39             =head3 interface
40              
41             Interface name (e.g. eth0, em0, ...)
42              
43             =head3 address_family
44              
45             Address family of following address
46              
47             =head3 address
48              
49             The address of the interface (addresses are unique, interfaces can have
50             multiple addresses).
51              
52             =head3 netmask
53              
54             Netmask of the address above.
55              
56             =head3 broadcast
57              
58             Broadcast address for network address
59              
60             =head3 hwaddress
61              
62             Hardware address (MAC number) of the interface NIC.
63              
64             =head3 flags_bin
65              
66             Binary representation of the interface flags (at least I or I).
67              
68             =head3 flags
69              
70             Comma separated list of the flags.
71              
72             =head3 mtu
73              
74             MTU for this address in this interface.
75              
76             =head3 metric
77              
78             Metric for the interface/address.
79              
80             =head1 METHODS
81              
82             =head2 get_table_name
83              
84             Returns 'netint'.
85              
86             =cut
87              
88 4     4 1 16 sub get_table_name() { return 'netint'; }
89              
90             =head2 get_col_names
91              
92             Returns the column names of the table as named in L
93              
94             =cut
95              
96 0     0 1   sub get_col_names() { @colNames }
97              
98             =head2 get_primary_key
99              
100             Returns 'address'.
101              
102             =cut
103              
104 0     0 1   sub get_primary_key() { return [qw(interface address_family address)]; }
105              
106             my %flagconsts;
107              
108             sub _getflags
109             {
110 0   0 0     my $flags = $_[0] || 0;
111 0 0         my $txt = ( $flags & $flagconsts{up} ) ? '
112 0           foreach my $nm ( keys %flagconsts )
113             {
114 0 0         $nm eq 'up' and next;
115 0 0         $flags & $flagconsts{$nm} and $txt .= ' ' . $nm;
116             }
117 0           $txt .= '>';
118             }
119              
120             =head2 collect_data
121              
122             Retrieves the data from L and put it into fetchable rows.
123              
124             =cut
125              
126             sub collect_data()
127             {
128 0     0 1   my @data;
129              
130 0 0         unless ( defined($haveNetInterface) )
131             {
132 0           $haveNetInterface = 0;
133 0           eval {
134 0           require Net::Interface;
135 0           require Socket6;
136 0           $haveNetInterface = 1;
137             };
138              
139 0 0         if ($haveNetInterface)
140             {
141 0           foreach my $iffname ( sort @{ $Net::Interface::EXPORT_TAGS{iffs} } )
  0            
142             {
143 0           my $iffn = Net::Interface->can($iffname);
144 0           my $val = &{$iffn}() + 0;
  0            
145 0           my $nm = &{$iffn}();
  0            
146 0           $flagconsts{$nm} = $val;
147             }
148             }
149             }
150              
151 0 0         if ($haveNetInterface)
152             {
153 0           my @ifaces = interfaces Net::Interface();
154 0           my $num = @ifaces;
155              
156 0           foreach my $hvp (@ifaces)
157             {
158 0           my $if = $hvp->info();
159 0           my $flags = _getflags( $if->{flags} );
160 0 0 0       unless ( defined $if->{flags}
161             && $if->{flags} & Net::Interface::IFF_UP() ) # no flags found
162             {
163 0           push(
164             @data,
165             [
166             $if->{name}, undef, undef, undef, undef, undef,
167             $if->{flags}, $flags, undef, undef,
168             ]
169             );
170             }
171             else # flags found
172             {
173 0 0         my $mac =
174             ( defined $if->{mac} ) ? Net::Interface::mac_bin2hex( $if->{mac} ) : undef;
175 0 0         my $mtu = $if->{mtu} ? $if->{mtu} : undef;
176 0 0         my $metric = ( defined $if->{metric} ) ? $if->{metric} : undef;
177              
178 0           foreach my $afname ( sort @{ $Net::Interface::EXPORT_TAGS{afs} } )
  0            
179             {
180 0           my $affn = Net::Interface->can($afname);
181 0 0         my $af = $affn ? &{$affn}() + 0 : undef;
  0            
182              
183 0 0         next unless ( defined($af) );
184              
185 0 0         if ( exists( $if->{$af} ) )
186             {
187 0           my @address = $hvp->address($af);
188 0           my @netmask = $hvp->netmask($af);
189 0           my @broadcast = $hvp->broadcast($af);
190              
191 0           foreach my $i ( 0 .. $#address )
192             {
193 0           my $addr_str = Socket6::inet_ntop( $af, $address[$i] );
194 0           my $netm_str = Socket6::inet_ntop( $af, $netmask[$i] );
195 0 0         my $broad_str = Socket6::inet_ntop( $af, $broadcast[$i] )
196             if ( defined( $broadcast[$i] ) );
197              
198 0           push(
199             @data,
200             [
201             $if->{name}, $afname, $addr_str, $netm_str,
202             $broad_str, $mac, $if->{flags}, $flags,
203             $if->{mtu}, $if->{metric},
204             ]
205             );
206             }
207             }
208             }
209             }
210             }
211             }
212              
213 0           return \@data;
214             }
215              
216             =head1 PREREQUISITES
217              
218             The module L is required to provide data for the table.
219              
220             =head1 AUTHOR
221              
222             Jens Rehsack Alexander Breibach
223             CPAN ID: REHSACK
224             rehsack@cpan.org alexander.breibach@googlemail.com
225             http://www.rehsack.de/
226              
227             =head1 COPYRIGHT
228              
229             This program is free software; you can redistribute
230             it and/or modify it under the same terms as Perl itself.
231              
232             The full text of the license can be found in the
233             LICENSE file included with this module.
234              
235             =head1 SUPPORT
236              
237             Free support can be requested via regular CPAN bug-tracking system. There is
238             no guaranteed reaction time or solution time, but it's always tried to give
239             accept or reject a reported ticket within a week. It depends on business load.
240             That doesn't mean that ticket via rt aren't handles as soon as possible,
241             that means that soon depends on how much I have to do.
242              
243             Business and commercial support should be acquired from the authors via
244             preferred freelancer agencies.
245              
246             =cut
247              
248             1;