(plugin) os::linux::local - mode traffic add option --guess-speed (#3941)

This commit is contained in:
qgarnier 2022-09-29 15:28:15 +02:00 committed by GitHub
parent 660c36b2ac
commit ab788c2404
1 changed files with 51 additions and 9 deletions

View File

@ -96,6 +96,12 @@ sub custom_traffic_calc {
return 0;
}
sub prefix_interface_output {
my ($self, %options) = @_;
return "Interface '" . $options{instance_value}->{display} . "' ";
}
sub set_counters {
my ($self, %options) = @_;
@ -141,18 +147,13 @@ sub new {
'exclude-interface:s' => { name => 'exclude_interface' },
'units:s' => { name => 'units', default => 'b/s' },
'speed:s' => { name => 'speed' },
'guess-speed' => { name => 'guess_speed' },
'no-loopback' => { name => 'no_loopback' }
});
return $self;
}
sub prefix_interface_output {
my ($self, %options) = @_;
return "Interface '" . $options{instance_value}->{display} . "' ";
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
@ -172,6 +173,23 @@ sub check_options {
}
}
sub guess_speed {
my ($self, %options) = @_;
if ($self->{iwconfig_output} =~ /^$options{name}\s+IEEE.*?Bit\s+Rate=([0-9\.]+)\s+Mb/ms) {
return $1 * 1000000;
}
my ($output) = $options{custom}->execute_command(
command => 'ethtool ' . $options{name},
no_quit => 1
);
if ($output =~ /Speed:\s+(.*)Mb/) {
return $1 * 1000000;
}
return undef;
}
sub do_selection {
my ($self, %options) = @_;
@ -181,6 +199,13 @@ sub do_selection {
command_options => '-s addr 2>&1'
);
if (defined($self->{option_results}->{guess_speed})) {
($self->{iwconfig_output}) = $options{custom}->execute_command(
command => 'iwconfig',
no_quit => 1
);
}
# ifconfig
my $interface_pattern = '^(\S+)(.*?)(\n\n|\n$)';
if ($stdout =~ /^\d+:\s+\S+:\s+</ms) {
@ -208,8 +233,21 @@ sub do_selection {
$self->{interface}->{$interface_name} = {
display => $interface_name,
status => $states,
speed_in => defined($self->{option_results}->{speed}) ? $self->{option_results}->{speed} : '',
speed_out => defined($self->{option_results}->{speed}) ? $self->{option_results}->{speed} : '',
speed_in => '',
speed_out => ''
};
if (defined($self->{option_results}->{guess_speed})) {
my $speed = $self->guess_speed(custom => $options{custom}, name => $interface_name);
if (defined($speed)) {
$self->{interface}->{$interface_name}->{speed_in} = $speed;
$self->{interface}->{$interface_name}->{speed_out} = $speed;
}
}
if (defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '') {
$self->{interface}->{$interface_name}->{speed_in} = $self->{option_results}->{speed};
$self->{interface}->{$interface_name}->{speed_out} = $self->{option_results}->{speed};
};
# ip addr patterns
@ -243,7 +281,7 @@ __END__
=head1 MODE
Check Traffic
Check traffic
Command used: /sbin/ip -s addr 2>&1
@ -301,6 +339,10 @@ Filter interfaces type (regexp can be used).
Set interface speed (in Mb).
=item B<--guess-speed>
Try to guess speed with commands ethtool and iwconfig.
=item B<--no-loopback>
Don't display loopback interfaces.