From: Chris Lundquist Date: Wed, 27 Feb 2013 23:57:36 +0000 (-0800) Subject: [perl_openvz] rework read_interface X-Git-Tag: collectd-5.5.0~251^2~2 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=da6abb216d4afab51921c5117bdb473fbb93bbff;p=collectd.git [perl_openvz] rework read_interface --- diff --git a/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm b/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm index 2e011b1f..cdb5dd77 100644 --- a/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm +++ b/bindings/perl/lib/Collectd/Plugins/OpenVZ.pm @@ -25,7 +25,7 @@ package Collectd::Plugins::OpenVZ; use strict; use warnings; -#use Collectd qw( :all ); +use Collectd qw( :all ); my $vzctl = '/usr/sbin/vzctl'; my $vzlist = '/usr/sbin/vzlist'; @@ -45,8 +45,10 @@ my @ignored_interfaces = ( "lo" ); sub interface_read($$) { my $veid = shift; my $name = shift; - my ($current_interface, $val, @lines, @parts, @counters, $i); + my ($val, @lines, @parts, @counters, $i); my @if_instances = ('if_octets', 'if_packets', 'if_errors'); + my @rx_fields = qw(if_octets if_packets if_errors drop fifo frame compressed multicast); + my @tx_fields = qw(if_octets if_packets if_errors drop fifo frame compressed); my %v = _build_report_hash($name); $v{'plugin'} = 'interface'; @@ -58,24 +60,29 @@ sub interface_read($$) { # face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed # lo: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 # venet0: 2420 27 0 0 0 0 0 0 2200 29 0 0 0 0 0 0 - foreach (@lines) { - next if (!/:/); + # + + for my $line (@lines) { + next if $line !~ /:/; + + $line =~ s/^\s+|\s+$//g; - @parts = split(/:/); - ($current_interface = $parts[0]) =~ s/^\s*(.*?)\s*$/$1/; - next if grep { $current_interface eq $_ } @ignored_interfaces; + my ($iface, %rx, %tx); - ($val = $parts[1]) =~ s/^\s*(.*?)\s*$/$1/; - @counters = split(/ +/, $val); + # Build our hash data + ($iface, @rx{@rx_fields}, @tx{@tx_fields}) = split /[: ]+/, $line; - $v{'plugin_instance'} = $current_interface; - for ($i= 0; $i <= $#if_instances; ++$i) { - $v{'type'} = $if_instances[$i]; - $v{'values'} = [ $counters[$i], $counters[$i + 8] ]; + # Skip this interface if it is in the ignored list + next if grep { $iface eq $_ } @ignored_interfaces; + + $v{'plugin_instance'} = $iface; + for my $instance (@if_instances) { + $v{'type'} = $instance; + $v{'values'} = [ $rx{$instance}, $tx{$instance} ]; plugin_dispatch_values(\%v); + } } } -} sub cpu_read($$) { my $veid = shift;