blib/lib/Net/Int/Stats.pm

filter: Statementstmt 20.3% 11 / 54 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 22.2% 2 / 9 Podpod 0.0% 0 / 7 totaltotal 11.4% 13 / 114 CC 19 SCAR 50.9
CC
19
Cov
12%
data
272.0
extract
6.0
build
6.0
CRAP 162.8
↑ Summary
1 package Net::Int::Stats;
2  
3 1 our $VERSION = '2.1';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 ############ Global Declarations ##############
9  
10 # hash of hashes
11 # key1 - interface, key2 - type ex: rx_packets, values ex: 'packets:12345'
12 1 my %interface_values;
13  
14 # tmp array to store string tokens
15 my @tmp;
16  
17 # interface name
18 1 my $key1;
19  
20 # value types
21 1 my @key2;
22  
23 ########## End Global Declarations ###########
24  
25 # generate ifconfig values
26 0 ▶sub data {
Subroutine data: not called CC=16 SCAR=56.1
Pod undocumented
27  
28 # store ifconfig output
29 0 my @ifconfig_out = `/sbin/ifconfig`;
30  
31 # loop through each line of ifconfig output
32 0 foreach (@ifconfig_out){
33  
34 # skip if blank line
35 0 ▶ next if /^$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^$/ 0 0 0
36  
37 # get interface name if not white space
38 0 ▶ if (!/^\s/){
Branch: 1 branch
BranchTrueFalseTotal
unless (/^\s/) 0 0 0
39  
40 # extract values
41 0 extract($_);
42  
43 # store first token of interface name
44 0 $key1 = shift(@tmp);
45 }
46  
47 # get inet address, RX, TX, collisions and txqueuelen values
48 # look for 'inet addr' or 'RX' or 'TX' or 'collisions' text
49 0 ▶ if (/RX/ || /TX/ || /collisions/ || /inet addr/){
Branch: 1 branch
BranchTrueFalseTotal
if (/RX/ or /TX/ or /collisions/ or /inet addr/) 0 0 0
Condition0%0/9
/RX/ or /TX/ or /collisions/ or /inet addr/
l!l&&r!l&&!r
000
/RX/ or /TX/ or /collisions/
l!l&&r!l&&!r
000
/RX/ or /TX/
l!l&&r!l&&!r
000
MC/DC0%0/4
/RX/ or /TX/ or /collisions/ or /inet addr/
/RX/ /TX/ /collisions/ /inet addr/
Truth table0%0/5
A or B or C or D
A= /RX/B= /TX/C= /collisions/D= /inet addr/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
50  
51 # key2 values
52 0 ▶ @key2 = qw(inet_addr) if (/inet addr/);
Branch: 1 branch
BranchTrueFalseTotal
if /inet addr/ 0 0 0
53 0 ▶ @key2 = qw(rx_packets rx_errors rx_dropped rx_overruns rx_frame) if (/RX packets/);
Branch: 1 branch
BranchTrueFalseTotal
if /RX packets/ 0 0 0
54 0 ▶ @key2 = qw(tx_packets tx_errors tx_dropped tx_overruns tx_carrier) if (/TX packets/);
Branch: 1 branch
BranchTrueFalseTotal
if /TX packets/ 0 0 0
55 0 ▶ @key2 = qw(rx_bytes tx_bytes) if (/RX bytes/);
Branch: 1 branch
BranchTrueFalseTotal
if /RX bytes/ 0 0 0
56 0 ▶ @key2 = qw(collisions txqueuelen) if (/collisions/);
Branch: 1 branch
BranchTrueFalseTotal
if /collisions/ 0 0 0
57  
58 # extract values
59 0 extract($_);
60  
61 # shift first token of 'inet' or 'RX' or 'TX'
62 0 ▶ shift(@tmp) if (/inet addr/ || /RX packets/ || /TX packets/);
Branch: 1 branch
BranchTrueFalseTotal
if /inet addr/ or /RX packets/ or /TX packets/ 0 0 0
Condition0%0/6
/inet addr/ or /RX packets/ or /TX packets/
l!l&&r!l&&!r
000
/inet addr/ or /RX packets/
l!l&&r!l&&!r
000
MC/DC0%0/3
/inet addr/ or /RX packets/ or /TX packets/
/inet addr/ /RX packets/ /TX packets/
Truth table0%0/4
A or B or C
A= /inet addr/B= /RX packets/C= /TX packets/
ABCresult
1XX1
01X1
0011
0000
63  
64 # build values hash
65 0 build();
66 }
67 }
68 }
69  
70 # extract values
71 0 ▶sub extract {
Subroutine extract: not called CC=2 SCAR=17.9
Pod undocumented
72  
73 # ifconfig output line with newlines removed
74 0 my $line = shift;
75  
76 # remove spaces
77 0 $line =~ s/^\s+//;
78  
79 # store tokens split on spaces
80 0 @tmp = split (/\s/, $line);
81  
82 # check if line is RX or TX bytes
83 0 ▶ if ($line =~ /bytes/){
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /bytes/) 0 0 0
84 # slice bytes values
85 0 @tmp = @tmp[1,6];
86 }
87 }
88  
89 # build values hash
90 0 ▶sub build {
Subroutine build: not called CC=2 SCAR=17.9
Pod undocumented
91  
92 # values type count
93 0 my $i = 0;
94  
95 # loop through value types
96 0 for (@key2){
97
98 # build hash with interface name, value type, and value
99 0 $interface_values{$key1}{$_} = $tmp[$i];
100  
101 # increment values type count
102 0 $i++;
103 }
104 }
105  
106 # validate interface name
107 0 ▶sub validate {
Subroutine validate: not called CC=2 SCAR=17.9
Pod undocumented
108  
109 # interface name
110 0 my $int = shift;
111  
112 # terminate program if specified interface name is not in ifconfig output
113 0 ▶ die "specified interface $int not listed in ifconfig output!\n" if !(grep(/$int/, keys %interface_values));
Branch: 1 branch
BranchTrueFalseTotal
if not grep(/$int/, keys %interface_values) 0 0 0
114 }
115  
116 # create new Net::Int::Stats object
117 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
118  
119 # class name
120 0 my $class = shift;
121  
122 # allocate object memory
123 0 my $self = {};
124  
125 # assign object reference to class
126 0 bless($self, $class);
127  
128 # initialize values reference
129 0 $self->{VALUES} = '';
130  
131 # initialize interfaces list reference
132 0 $self->{INTERFACES} = '';
133  
134 # generate value data
135 0 data();
136  
137 # return object reference
138 0 return $self;
139 }
140  
141 # get specific ifconfig value for specific interface
142 0 ▶sub value {
Subroutine value: not called CC=1 SCAR=6.9
Pod undocumented
143  
144 # object reference
145 0 my $self = shift;
146  
147 # interface name
148 0 my $int = shift;
149  
150 # value type
151 0 my $type = shift;
152  
153 # validate if supplied interface is present
154 0 validate($int);
155  
156 # user specified value
157 0 $self->{VALUES} = $interface_values{$int}{$type};
158  
159 # return value
160 0 return $self->{VALUES};
161 }
162  
163 0 ▶sub interfaces {
Subroutine interfaces: not called CC=1 SCAR=6.9
Pod undocumented
164  
165 # object reference
166 0 my $self = shift;
167  
168 # interface list
169 0 my @int_list = keys %interface_values;
170  
171 # interface list reference
172 0 $self->{INTERFACES} = "@int_list";
173  
174 # return value
175 0 return $self->{INTERFACES};
176 }
177  
178 1 1;
179  
180 __END__
↑ Summary