From ed93627649048c75874450d3317d07061cee578d Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Tue, 19 Mar 2019 16:22:44 +0100 Subject: [PATCH] use iproute commands by default (Fix #916) --- os/linux/local/mode/listinterfaces.pm | 88 ++++++++++++++++----------- os/linux/local/mode/packeterrors.pm | 14 +++-- os/linux/local/mode/traffic.pm | 29 +++++---- 3 files changed, 78 insertions(+), 53 deletions(-) diff --git a/os/linux/local/mode/listinterfaces.pm b/os/linux/local/mode/listinterfaces.pm index b0834715a..70b80e998 100644 --- a/os/linux/local/mode/listinterfaces.pm +++ b/os/linux/local/mode/listinterfaces.pm @@ -32,23 +32,23 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "remote" => { name => 'remote' }, - "ssh-option:s@" => { name => 'ssh_option' }, - "ssh-path:s" => { name => 'ssh_path' }, - "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "sudo" => { name => 'sudo' }, - "command:s" => { name => 'command', default => 'ifconfig' }, - "command-path:s" => { name => 'command_path', default => '/sbin' }, - "command-options:s" => { name => 'command_options', default => '-a 2>&1' }, - "filter-name:s" => { name => 'filter_name', }, - "filter-state:s" => { name => 'filter_state', }, - "no-loopback" => { name => 'no_loopback', }, - "skip-novalues" => { name => 'skip_novalues', }, - }); + $options{options}->add_options(arguments => { + "hostname:s" => { name => 'hostname' }, + "remote" => { name => 'remote' }, + "ssh-option:s@" => { name => 'ssh_option' }, + "ssh-path:s" => { name => 'ssh_path' }, + "ssh-command:s" => { name => 'ssh_command', default => 'ssh' }, + "timeout:s" => { name => 'timeout', default => 30 }, + "sudo" => { name => 'sudo' }, + "command:s" => { name => 'command', default => 'ip' }, + "command-path:s" => { name => 'command_path', default => '/sbin' }, + "command-options:s" => { name => 'command_options', default => '-s addr 2>&1' }, + "filter-name:s" => { name => 'filter_name' }, + "filter-state:s" => { name => 'filter_state' }, + "no-loopback" => { name => 'no_loopback' }, + "skip-novalues" => { name => 'skip_novalues' }, + }); + $self->{result} = {}; return $self; } @@ -61,17 +61,36 @@ sub check_options { sub manage_selection { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); - while ($stdout =~ /^(\S+)(.*?)(\n\n|\n$)/msg) { + my $stdout = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + sudo => $self->{option_results}->{sudo}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); + + my $mapping = { + ifconfig => { + get_interface => '^(\S+)(.*?)(\n\n|\n$)', + test => 'RX bytes:\S+.*?TX bytes:\S+', + }, + iproute => { + get_interface => '^\d+:\s+(\S+)(.*?)(?=\n\d|\Z$)', + test => 'RX:\s+bytes.*?\d+', + }, + }; + + my $type = 'ifconfig'; + if ($stdout =~ /^\d+:\s+\S+:\s+{$type}->{get_interface}/msg) { my ($interface_name, $values) = ($1, $2); $interface_name =~ s/:$//; my $states = ''; - $states .= 'R' if ($values =~ /RUNNING/ms); + $states .= 'R' if ($values =~ /RUNNING|LOWER_UP/ms); $states .= 'U' if ($values =~ /UP/ms); if (defined($self->{option_results}->{no_loopback}) && $values =~ /LOOPBACK/ms) { @@ -89,12 +108,12 @@ sub manage_selection { next; } - $values =~ /RX bytes:(\S+).*?TX bytes:(\S+)/msi; - if (defined($self->{option_results}->{skip_novalues}) && !defined($1)) { + if (defined($self->{option_results}->{skip_novalues}) && $values =~ /$mapping->{$type}->{test}/msi) { $self->{output}->output_add(long_msg => "Skipping interface '" . $interface_name . "': no values"); next; } - $self->{result}->{$interface_name} = {state => $states}; + + $self->{result}->{$interface_name} = { state => $states }; } } @@ -123,9 +142,10 @@ sub disco_show { $self->manage_selection(); foreach my $name (sort(keys %{$self->{result}})) { - $self->{output}->add_disco_entry(name => $name, - state => $self->{result}->{$name}->{state} - ); + $self->{output}->add_disco_entry( + name => $name, + state => $self->{result}->{$name}->{state} + ); } } @@ -169,7 +189,7 @@ Use 'sudo' to execute the command. =item B<--command> -Command to get information (Default: 'ifconfig'). +Command to get information (Default: 'ip'). Can be changed if you have output in a file. =item B<--command-path> @@ -178,7 +198,7 @@ Command path (Default: '/sbin'). =item B<--command-options> -Command options (Default: '-a 2>&1'). +Command options (Default: '-s addr 2>&1'). =item B<--filter-name> @@ -199,4 +219,4 @@ Filter interface without in/out byte values. =back -=cut \ No newline at end of file +=cut diff --git a/os/linux/local/mode/packeterrors.pm b/os/linux/local/mode/packeterrors.pm index 9a2dbffa8..af5d1c856 100644 --- a/os/linux/local/mode/packeterrors.pm +++ b/os/linux/local/mode/packeterrors.pm @@ -184,12 +184,14 @@ sub check_options { sub do_selection { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); + my $stdout = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + sudo => $self->{option_results}->{sudo}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); my $mapping = { ifconfig => { diff --git a/os/linux/local/mode/traffic.pm b/os/linux/local/mode/traffic.pm index 87e3a2ee0..4a4b236fe 100644 --- a/os/linux/local/mode/traffic.pm +++ b/os/linux/local/mode/traffic.pm @@ -164,13 +164,13 @@ sub new { "filter-state:s" => { name => 'filter_state', }, "units:s" => { name => 'units', default => 'b/s' }, "name:s" => { name => 'name' }, - "regexp" => { name => 'use_regexp' }, - "regexp-isensitive" => { name => 'use_regexpi' }, - "speed:s" => { name => 'speed' }, - "no-loopback" => { name => 'no_loopback', }, - "unknown-status:s" => { name => 'unknown_status', default => '' }, - "warning-status:s" => { name => 'warning_status', default => '' }, - "critical-status:s" => { name => 'critical_status', default => '%{status} ne "RU"' }, + "regexp" => { name => 'use_regexp' }, + "regexp-isensitive" => { name => 'use_regexpi' }, + "speed:s" => { name => 'speed' }, + "no-loopback" => { name => 'no_loopback', }, + "unknown-status:s" => { name => 'unknown_status', default => '' }, + "warning-status:s" => { name => 'warning_status', default => '' }, + "critical-status:s" => { name => 'critical_status', default => '%{status} ne "RU"' }, }); return $self; @@ -211,12 +211,15 @@ sub do_selection { my ($self, %options) = @_; $self->{interface} = {}; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - sudo => $self->{option_results}->{sudo}, - command => $self->{option_results}->{command}, - command_path => $self->{option_results}->{command_path}, - command_options => $self->{option_results}->{command_options}); + my $stdout = centreon::plugins::misc::execute( + output => $self->{output}, + options => $self->{option_results}, + sudo => $self->{option_results}->{sudo}, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); + # ifconfig my $interface_pattern = '^(\S+)(.*?)(\n\n|\n$)'; if ($stdout =~ /^\d+:\s+\S+:\s+