From 4347be5e75e2c3d7df8eed3aee26d8c383bafb22 Mon Sep 17 00:00:00 2001 From: Quentin Garnier Date: Fri, 6 Jun 2014 16:46:35 +0200 Subject: [PATCH] Refs #5077 Add mode ssh from a mgmt card --- .../sun/mgmt_cards/mode/showenvironment.pm | 76 ++++++++++++++++--- .../hardware/server/sun/mgmt_cards/plugin.pm | 2 +- 2 files changed, 66 insertions(+), 12 deletions(-) diff --git a/centreon-plugins/hardware/server/sun/mgmt_cards/mode/showenvironment.pm b/centreon-plugins/hardware/server/sun/mgmt_cards/mode/showenvironment.pm index 403b358a3..40e1bc6a1 100644 --- a/centreon-plugins/hardware/server/sun/mgmt_cards/mode/showenvironment.pm +++ b/centreon-plugins/hardware/server/sun/mgmt_cards/mode/showenvironment.pm @@ -39,7 +39,6 @@ use base qw(centreon::plugins::mode); use strict; use warnings; -use hardware::server::sun::mgmt_cards::lib::telnet; sub new { my ($class, %options) = @_; @@ -54,6 +53,8 @@ sub new { "username:s" => { name => 'username' }, "password:s" => { name => 'password' }, "timeout:s" => { name => 'timeout', default => 30 }, + "command-plink:s" => { name => 'command_plink', default => 'plink' }, + "ssh" => { name => 'ssh' }, }); return $self; } @@ -74,24 +75,69 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Need to specify a password."); $self->{output}->option_exit(); } + + if (!defined($self->{option_results}->{ssh})) { + require hardware::server::sun::mgmt_cards::lib::telnet; + } +} + +sub ssh_command { + my ($self, %options) = @_; + + my $cmd_in = $self->{option_results}->{username} . '\n' . $self->{option_results}->{password} . '\nshowenvironment\nlogout\n'; + my $cmd = "echo -e '$cmd_in' | " . $self->{option_results}->{command_plink} . " -batch " . $self->{option_results}->{hostname} . " 2>&1"; + my ($lerror, $stdout, $exit_code) = centreon::plugins::misc::backtick( + command => $cmd, + timeout => $self->{option_results}->{timeout}, + wait_exit => 1 + ); + $stdout =~ s/\r//g; + if ($lerror <= -1000) { + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => $stdout); + $self->{output}->display(); + $self->{output}->exit(); + } + if ($exit_code != 0) { + $stdout =~ s/\n/ - /g; + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Command error: $stdout"); + $self->{output}->display(); + $self->{output}->exit(); + } + + if ($stdout !~ /Environmental Status/mi) { + $self->{output}->output_add(long_msg => $stdout); + $self->{output}->output_add(severity => 'UNKNOWN', + short_msg => "Command 'showenvironment' problems (see additional info)."); + $self->{output}->display(); + $self->{output}->exit(); + } + + return $stdout; } sub run { my ($self, %options) = @_; - - my $telnet_handle = hardware::server::sun::mgmt_cards::lib::telnet::connect( - username => $self->{option_results}->{username}, - password => $self->{option_results}->{password}, - hostname => $self->{option_results}->{hostname}, - port => $self->{option_results}->{port}, - timeout => $self->{option_results}->{timeout}, - output => $self->{output}); - my @lines = $telnet_handle->cmd("showenvironment"); + my $output; + + if (defined($self->{option_results}->{ssh})) { + $output = $self->ssh_command(); + } else { + my $telnet_handle = hardware::server::sun::mgmt_cards::lib::telnet::connect( + username => $self->{option_results}->{username}, + password => $self->{option_results}->{password}, + hostname => $self->{option_results}->{hostname}, + port => $self->{option_results}->{port}, + timeout => $self->{option_results}->{timeout}, + output => $self->{output}); + my @lines = $telnet_handle->cmd("showenvironment"); + $output = join("", @lines); + } $self->{output}->output_add(severity => 'OK', short_msg => "No problems detected."); - my ($output) = join("", @lines); $output =~ s/\r//g; my $long_msg = $output; $long_msg =~ s/\|/~/mg; @@ -254,6 +300,14 @@ telnet password. Timeout in seconds for the command (Default: 30). +=item B<--command-plink> + +Plink command (default: plink). Use to set a path. + +=item B<--ssh> + +Use ssh (with plink) instead of telnet. + =back =cut diff --git a/centreon-plugins/hardware/server/sun/mgmt_cards/plugin.pm b/centreon-plugins/hardware/server/sun/mgmt_cards/plugin.pm index 9153af23d..cb0f83924 100644 --- a/centreon-plugins/hardware/server/sun/mgmt_cards/plugin.pm +++ b/centreon-plugins/hardware/server/sun/mgmt_cards/plugin.pm @@ -71,7 +71,7 @@ Check a variety of Sun Hardware through management cards: - mode 'showfaults': ALOM4v (in T1xxx, T2xxx) (in ssh with 'plink' command) ; - mode 'showstatus': XSCF (Mxxxx - M3000, M4000, M5000,...) (in ssh with 'plink' command) ; - mode 'showboards': ScApp (SFxxxx - sf6900, sf6800, sf3800,...) (in telnet with Net::Telnet) ; -- mode 'showenvironment': ALOM (v240, v440, v245,...) (in telnet with Net::Telnet) ; +- mode 'showenvironment': ALOM (v240, v440, v245,...) (in telnet with Net::Telnet or in ssh with 'plink' command) ; - mode 'environment-v8xx': RSC cards (v890, v880) (in telnet with Net::Telnet) ; - mode 'environment-v4xx': RSC cards (v480, v490) (in telnet with Net::Telnet) ; - mode 'environment-sf2xx': RSC cards (sf280) (in telnet with Net::Telnet).