line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# system::freebsd::netstat Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::System::Freebsd::Netstat; |
7
|
1
|
|
|
1
|
|
745
|
use strict; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
28
|
|
8
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
41
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
5
|
use base qw(Metabrik::Shell::Command); |
|
1
|
|
|
|
|
13
|
|
|
1
|
|
|
|
|
821
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
attributes => { |
19
|
|
|
|
|
|
|
interface => [ qw(name) ], |
20
|
|
|
|
|
|
|
}, |
21
|
|
|
|
|
|
|
attributes_default => { |
22
|
|
|
|
|
|
|
interface => 're0', |
23
|
|
|
|
|
|
|
}, |
24
|
|
|
|
|
|
|
commands => { |
25
|
|
|
|
|
|
|
stats => [ qw(interface|interface_list|OPTIONAL) ], |
26
|
|
|
|
|
|
|
compute_diff => [ qw(first_stats second_stats) ], |
27
|
|
|
|
|
|
|
}, |
28
|
|
|
|
|
|
|
require_binaries => { |
29
|
|
|
|
|
|
|
netstat => [ ], |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
}; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub stats { |
35
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
36
|
0
|
|
|
|
|
|
my ($interface) = @_; |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
0
|
|
|
|
$interface ||= $self->interface; |
39
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('stats', $interface) or return; |
40
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('stats', $interface, 'ARRAY', 'SCALAR') |
41
|
|
|
|
|
|
|
or return; |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
my $info = { }; |
44
|
|
|
|
|
|
|
|
45
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
46
|
0
|
|
|
|
|
|
my @list = (); |
47
|
0
|
|
|
|
|
|
for my $this (@$interface) { |
48
|
0
|
0
|
|
|
|
|
$info = $self->stats($this) or next; |
49
|
0
|
|
|
|
|
|
push @list, $info; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return \@list; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
else { |
55
|
|
|
|
|
|
|
# |
56
|
|
|
|
|
|
|
# FreeBSD 10.2-RELEASE |
57
|
|
|
|
|
|
|
# |
58
|
|
|
|
|
|
|
# netstat -i -b -n -I re0 |
59
|
|
|
|
|
|
|
# 0: "Name Mtu Network Address Ipkts Ierrs Idrop Ibytes Opkts Oerrs Obytes Coll", |
60
|
|
|
|
|
|
|
# 1: "re0 1500 47110755 0 0 17188577531 72244809 0 94220997385 0", |
61
|
|
|
|
|
|
|
# 2: "re0 - /24 44645072 - - 16112030427 71929477 - 92914300016 -", |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
|
my $cmd = 'netstat -i -b -n -I '.$interface; |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
my $lines = $self->capture($cmd) or return; |
66
|
|
|
|
|
|
|
|
67
|
0
|
|
|
|
|
|
for my $line (@$lines) { |
68
|
0
|
|
|
|
|
|
$line =~ s{^\s*}{}; |
69
|
0
|
|
|
|
|
|
$line =~ s{\s*$}{}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Only the line with valid MTU depicts a physical interface. We only keep that one. |
72
|
0
|
0
|
|
|
|
|
if ($line =~ m{^\S+\s+\d+}) { |
73
|
0
|
|
|
|
|
|
my @t = split(/\s+/, $line); |
74
|
|
|
|
|
|
|
|
75
|
0
|
0
|
|
|
|
|
if ($interface eq $t[0]) { |
76
|
0
|
|
|
|
|
|
my $offset = 0; |
77
|
0
|
|
|
|
|
|
$info->{interface} = $t[$offset++]; |
78
|
0
|
|
|
|
|
|
$info->{mtu} = $t[$offset++]; |
79
|
0
|
|
|
|
|
|
$info->{network} = $t[$offset++]; |
80
|
0
|
|
|
|
|
|
$info->{mac_address} = $t[$offset++]; |
81
|
0
|
|
|
|
|
|
$info->{total_input_packets} = $t[$offset++]; |
82
|
0
|
|
|
|
|
|
$info->{total_input_errors} = $t[$offset++]; |
83
|
0
|
|
|
|
|
|
$info->{total_input_drop} = $t[$offset++]; |
84
|
0
|
|
|
|
|
|
$info->{total_input_bytes} = $t[$offset++]; |
85
|
0
|
|
|
|
|
|
$info->{total_output_packets} = $t[$offset++]; |
86
|
0
|
|
|
|
|
|
$info->{total_output_errors} = $t[$offset++]; |
87
|
0
|
|
|
|
|
|
$info->{total_output_bytes} = $t[$offset++]; |
88
|
0
|
|
|
|
|
|
$info->{coll} = $t[$offset++]; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
last; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
return $info; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub compute_diff { |
100
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
101
|
0
|
|
|
|
|
|
my ($first, $second) = @_; |
102
|
|
|
|
|
|
|
|
103
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('compute_diff', $first) or return; |
104
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('compute_diff', $second) or return; |
105
|
0
|
0
|
|
|
|
|
$self->brik_help_run_invalid_arg('compute_diff', $first, 'HASH') or return; |
106
|
0
|
0
|
|
|
|
|
$self->brik_help_run_invalid_arg('compute_diff', $second, 'HASH') or return; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
my $diff = {}; |
109
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_input_bytes}) && exists($second->{total_input_bytes})) { |
110
|
0
|
|
|
|
|
|
$diff->{input_bytes} = $second->{total_input_bytes} - $first->{total_input_bytes}; |
111
|
|
|
|
|
|
|
} |
112
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_output_bytes}) && exists($second->{total_output_bytes})) { |
113
|
0
|
|
|
|
|
|
$diff->{output_bytes} = $second->{total_output_bytes} - $first->{total_output_bytes}; |
114
|
|
|
|
|
|
|
} |
115
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_input_packets}) && exists($second->{total_input_packets})) { |
116
|
0
|
|
|
|
|
|
$diff->{input_packets} = $second->{total_input_packets} - $first->{total_input_packets}; |
117
|
|
|
|
|
|
|
} |
118
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_output_packets}) && exists($second->{total_output_packets})) { |
119
|
0
|
|
|
|
|
|
$diff->{output_packets} = $second->{total_output_packets} - $first->{total_output_packets}; |
120
|
|
|
|
|
|
|
} |
121
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_input_errors}) && exists($second->{total_input_errors})) { |
122
|
0
|
|
|
|
|
|
$diff->{input_errors} = $second->{total_input_errors} - $first->{total_input_errors}; |
123
|
|
|
|
|
|
|
} |
124
|
0
|
0
|
0
|
|
|
|
if (exists($first->{total_output_errors}) && exists($second->{total_output_errors})) { |
125
|
0
|
|
|
|
|
|
$diff->{output_errors} = $second->{total_output_errors} - $first->{total_output_errors}; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
|
128
|
0
|
|
|
|
|
|
return $diff; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
1; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
__END__ |