| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Net::Int::Stats; |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $VERSION = '2.1'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
27433
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
40
|
|
|
6
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
733
|
|
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
############ Global Declarations ############## |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
# hash of hashes |
|
11
|
|
|
|
|
|
|
# key1 - interface, key2 - type ex: rx_packets, values ex: 'packets:12345' |
|
12
|
|
|
|
|
|
|
my %interface_values; |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# tmp array to store string tokens |
|
15
|
|
|
|
|
|
|
my @tmp; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# interface name |
|
18
|
|
|
|
|
|
|
my $key1; |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
# value types |
|
21
|
|
|
|
|
|
|
my @key2; |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
########## End Global Declarations ########### |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# generate ifconfig values |
|
26
|
|
|
|
|
|
|
sub data { |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
# store ifconfig output |
|
29
|
1
|
|
|
1
|
0
|
7637
|
my @ifconfig_out = `/sbin/ifconfig`; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# loop through each line of ifconfig output |
|
32
|
1
|
|
|
|
|
29
|
foreach (@ifconfig_out){ |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# skip if blank line |
|
35
|
18
|
100
|
|
|
|
74
|
next if /^$/; |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
# get interface name if not white space |
|
38
|
16
|
100
|
|
|
|
52
|
if (!/^\s/){ |
|
39
|
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
# extract values |
|
41
|
2
|
|
|
|
|
18
|
extract($_); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
# store first token of interface name |
|
44
|
2
|
|
|
|
|
7
|
$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
|
16
|
100
|
100
|
|
|
126
|
if (/RX/ || /TX/ || /collisions/ || /inet addr/){ |
|
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
# key2 values |
|
52
|
10
|
100
|
|
|
|
37
|
@key2 = qw(inet_addr) if (/inet addr/); |
|
53
|
10
|
100
|
|
|
|
25
|
@key2 = qw(rx_packets rx_errors rx_dropped rx_overruns rx_frame) if (/RX packets/); |
|
54
|
10
|
100
|
|
|
|
28
|
@key2 = qw(tx_packets tx_errors tx_dropped tx_overruns tx_carrier) if (/TX packets/); |
|
55
|
10
|
100
|
|
|
|
23
|
@key2 = qw(rx_bytes tx_bytes) if (/RX bytes/); |
|
56
|
10
|
100
|
|
|
|
23
|
@key2 = qw(collisions txqueuelen) if (/collisions/); |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
# extract values |
|
59
|
10
|
|
|
|
|
33
|
extract($_); |
|
60
|
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
# shift first token of 'inet' or 'RX' or 'TX' |
|
62
|
10
|
100
|
100
|
|
|
71
|
shift(@tmp) if (/inet addr/ || /RX packets/ || /TX packets/); |
|
|
|
|
100
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# build values hash |
|
65
|
10
|
|
|
|
|
19
|
build(); |
|
66
|
|
|
|
|
|
|
} |
|
67
|
|
|
|
|
|
|
} |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# extract values |
|
71
|
|
|
|
|
|
|
sub extract { |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# ifconfig output line with newlines removed |
|
74
|
12
|
|
|
12
|
0
|
18
|
my $line = shift; |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# remove spaces |
|
77
|
12
|
|
|
|
|
47
|
$line =~ s/^\s+//; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# store tokens split on spaces |
|
80
|
12
|
|
|
|
|
90
|
@tmp = split (/\s/, $line); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# check if line is RX or TX bytes |
|
83
|
12
|
100
|
|
|
|
48
|
if ($line =~ /bytes/){ |
|
84
|
|
|
|
|
|
|
# slice bytes values |
|
85
|
2
|
|
|
|
|
8
|
@tmp = @tmp[1,6]; |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# build values hash |
|
90
|
|
|
|
|
|
|
sub build { |
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
# values type count |
|
93
|
10
|
|
|
10
|
0
|
11
|
my $i = 0; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# loop through value types |
|
96
|
10
|
|
|
|
|
12
|
for (@key2){ |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
# build hash with interface name, value type, and value |
|
99
|
30
|
|
|
|
|
87
|
$interface_values{$key1}{$_} = $tmp[$i]; |
|
100
|
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
# increment values type count |
|
102
|
30
|
|
|
|
|
47
|
$i++; |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
} |
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# validate interface name |
|
107
|
|
|
|
|
|
|
sub validate { |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
# interface name |
|
110
|
0
|
|
|
0
|
0
|
0
|
my $int = shift; |
|
111
|
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
# terminate program if specified interface name is not in ifconfig output |
|
113
|
0
|
0
|
|
|
|
0
|
die "specified interface $int not listed in ifconfig output!\n" if !(grep(/$int/, keys %interface_values)); |
|
114
|
|
|
|
|
|
|
} |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# create new Net::Int::Stats object |
|
117
|
|
|
|
|
|
|
sub new { |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# class name |
|
120
|
1
|
|
|
1
|
0
|
1340
|
my $class = shift; |
|
121
|
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# allocate object memory |
|
123
|
1
|
|
|
|
|
3
|
my $self = {}; |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# assign object reference to class |
|
126
|
1
|
|
|
|
|
3
|
bless($self, $class); |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# initialize values reference |
|
129
|
1
|
|
|
|
|
9
|
$self->{VALUES} = ''; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# initialize interfaces list reference |
|
132
|
1
|
|
|
|
|
3
|
$self->{INTERFACES} = ''; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# generate value data |
|
135
|
1
|
|
|
|
|
5
|
data(); |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
# return object reference |
|
138
|
1
|
|
|
|
|
18
|
return $self; |
|
139
|
|
|
|
|
|
|
} |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# get specific ifconfig value for specific interface |
|
142
|
|
|
|
|
|
|
sub value { |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# object reference |
|
145
|
0
|
|
|
0
|
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
|
|
|
|
|
|
|
sub interfaces { |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
# object reference |
|
166
|
0
|
|
|
0
|
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; |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
__END__ |