| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # system::freebsd::netstat Brik | ||
| 5 | # | ||
| 6 | package Metabrik::System::Freebsd::Netstat; | ||
| 7 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use base qw(Metabrik::Shell::Command); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 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 | my $self = shift; | ||
| 36 | my ($interface) = @_; | ||
| 37 | |||
| 38 | $interface ||= $self->interface; | ||
| 39 | $self->brik_help_run_undef_arg('stats', $interface) or return; | ||
| 40 | my $ref = $self->brik_help_run_invalid_arg('stats', $interface, 'ARRAY', 'SCALAR') | ||
| 41 | or return; | ||
| 42 | |||
| 43 | my $info = { }; | ||
| 44 | |||
| 45 | if ($ref eq 'ARRAY') { | ||
| 46 | my @list = (); | ||
| 47 | for my $this (@$interface) { | ||
| 48 | $info = $self->stats($this) or next; | ||
| 49 | push @list, $info; | ||
| 50 | } | ||
| 51 | |||
| 52 | 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 <Link#1> <mac> 47110755 0 0 17188577531 72244809 0 94220997385 0", | ||
| 61 | # 2: "re0 - <ip>/24 <ip> 44645072 - - 16112030427 71929477 - 92914300016 -", | ||
| 62 | |||
| 63 | my $cmd = 'netstat -i -b -n -I '.$interface; | ||
| 64 | |||
| 65 | my $lines = $self->capture($cmd) or return; | ||
| 66 | |||
| 67 | for my $line (@$lines) { | ||
| 68 | $line =~ s{^\s*}{}; | ||
| 69 | $line =~ s{\s*$}{}; | ||
| 70 | |||
| 71 | # Only the line with valid MTU depicts a physical interface. We only keep that one. | ||
| 72 | if ($line =~ m{^\S+\s+\d+}) { | ||
| 73 | my @t = split(/\s+/, $line); | ||
| 74 | |||
| 75 | if ($interface eq $t[0]) { | ||
| 76 | my $offset = 0; | ||
| 77 | $info->{interface} = $t[$offset++]; | ||
| 78 | $info->{mtu} = $t[$offset++]; | ||
| 79 | $info->{network} = $t[$offset++]; | ||
| 80 | $info->{mac_address} = $t[$offset++]; | ||
| 81 | $info->{total_input_packets} = $t[$offset++]; | ||
| 82 | $info->{total_input_errors} = $t[$offset++]; | ||
| 83 | $info->{total_input_drop} = $t[$offset++]; | ||
| 84 | $info->{total_input_bytes} = $t[$offset++]; | ||
| 85 | $info->{total_output_packets} = $t[$offset++]; | ||
| 86 | $info->{total_output_errors} = $t[$offset++]; | ||
| 87 | $info->{total_output_bytes} = $t[$offset++]; | ||
| 88 | $info->{coll} = $t[$offset++]; | ||
| 89 | |||
| 90 | last; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | return $info; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub compute_diff { | ||
| 100 | my $self = shift; | ||
| 101 | my ($first, $second) = @_; | ||
| 102 | |||
| 103 | $self->brik_help_run_undef_arg('compute_diff', $first) or return; | ||
| 104 | $self->brik_help_run_undef_arg('compute_diff', $second) or return; | ||
| 105 | $self->brik_help_run_invalid_arg('compute_diff', $first, 'HASH') or return; | ||
| 106 | $self->brik_help_run_invalid_arg('compute_diff', $second, 'HASH') or return; | ||
| 107 | |||
| 108 | my $diff = {}; | ||
| 109 | if (exists($first->{total_input_bytes}) && exists($second->{total_input_bytes})) { | ||
| 110 | $diff->{input_bytes} = $second->{total_input_bytes} - $first->{total_input_bytes}; | ||
| 111 | } | ||
| 112 | if (exists($first->{total_output_bytes}) && exists($second->{total_output_bytes})) { | ||
| 113 | $diff->{output_bytes} = $second->{total_output_bytes} - $first->{total_output_bytes}; | ||
| 114 | } | ||
| 115 | if (exists($first->{total_input_packets}) && exists($second->{total_input_packets})) { | ||
| 116 | $diff->{input_packets} = $second->{total_input_packets} - $first->{total_input_packets}; | ||
| 117 | } | ||
| 118 | if (exists($first->{total_output_packets}) && exists($second->{total_output_packets})) { | ||
| 119 | $diff->{output_packets} = $second->{total_output_packets} - $first->{total_output_packets}; | ||
| 120 | } | ||
| 121 | if (exists($first->{total_input_errors}) && exists($second->{total_input_errors})) { | ||
| 122 | $diff->{input_errors} = $second->{total_input_errors} - $first->{total_input_errors}; | ||
| 123 | } | ||
| 124 | if (exists($first->{total_output_errors}) && exists($second->{total_output_errors})) { | ||
| 125 | $diff->{output_errors} = $second->{total_output_errors} - $first->{total_output_errors}; | ||
| 126 | } | ||
| 127 | |||
| 128 | return $diff; | ||
| 129 | } | ||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | __END__ | ||