From df2f11141ada67dcdb4d7eaae2bcf71a67d0e090 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Fri, 6 Aug 2021 11:18:53 +0200 Subject: [PATCH] enh(fujitsu/eternus/dx): psu mode - check drive power supplies (#3025) --- .../exchange/local/mode/listdatabases.pm | 49 ++++++++++-- .../powershell/exchange/listdatabases.pm | 54 ++++--------- .../fujitsu/eternus/dx/ssh/mode/psu.pm | 78 +++++++++++-------- 3 files changed, 101 insertions(+), 80 deletions(-) diff --git a/centreon-plugins/apps/microsoft/exchange/local/mode/listdatabases.pm b/centreon-plugins/apps/microsoft/exchange/local/mode/listdatabases.pm index 6c9cf4d13..7bd4614dd 100644 --- a/centreon-plugins/apps/microsoft/exchange/local/mode/listdatabases.pm +++ b/centreon-plugins/apps/microsoft/exchange/local/mode/listdatabases.pm @@ -26,6 +26,7 @@ use strict; use warnings; use centreon::plugins::misc; use centreon::common::powershell::exchange::listdatabases; +use JSON::XS; sub new { my ($class, %options) = @_; @@ -42,6 +43,7 @@ sub new { 'command-path:s' => { name => 'command_path' }, 'command-options:s' => { name => 'command_options', default => '-InputFormat none -NoLogo -EncodedCommand' }, 'ps-exec-only' => { name => 'ps_exec_only' }, + 'ps-display' => { name => 'ps_display' }, 'ps-database-filter:s' => { name => 'ps_database_filter' } }); @@ -53,6 +55,20 @@ sub check_options { $self->SUPER::init(%options); } +sub manage_selection { + my ($self, %options) = @_; + + my $decoded; + eval { + $decoded = JSON::XS->new->decode($options{stdout}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); + $self->{output}->option_exit(); + } + return $decoded; +} + sub run { my ($self, %options) = @_; @@ -75,9 +91,9 @@ sub run { $self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps); } - my ($stdout) = centreon::plugins::misc::windows_execute( + my ($stdout) = centreon::plugins::misc::execute( output => $self->{output}, - timeout => $self->{option_results}->{timeout}, + options => $self->{option_results}, command => $self->{option_results}->{command}, command_path => $self->{option_results}->{command_path}, command_options => $self->{option_results}->{command_options} @@ -92,7 +108,18 @@ sub run { severity => 'OK', short_msg => 'List databases:' ); - centreon::common::powershell::exchange::listdatabases::list($self, stdout => $stdout); + + my $decoded = $self->manage_selection(stdout => $stdout); + foreach my $db (@$decoded) { + $self->{output}->output_add( + long_msg => sprintf( + "[name: %s][server: %s][mounted: %s]", + $db->{name}, + $db->{server}, + $db->{mounted} =~ /True|1/i ? 'true' : 'false' + ) + ); + } } $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); @@ -117,14 +144,24 @@ sub disco_show { ); $self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps); } - my ($stdout) = centreon::plugins::misc::windows_execute( + + my ($stdout) = centreon::plugins::misc::execute( output => $self->{output}, - timeout => $self->{option_results}->{timeout}, + options => $self->{option_results}, command => $self->{option_results}->{command}, command_path => $self->{option_results}->{command_path}, command_options => $self->{option_results}->{command_options} ); - centreon::common::powershell::exchange::listdatabases::disco_show($self, stdout => $stdout); + + my $decoded = $self->manage_selection(stdout => $stdout); + + foreach my $db (@$decoded) { + $self->{output}->add_disco_entry( + name => $db->{name}, + server => $db->{server}, + mounted => $db->{mounted} =~ /True|1/i ? 'true' : 'false' + ); + } } 1; diff --git a/centreon-plugins/centreon/common/powershell/exchange/listdatabases.pm b/centreon-plugins/centreon/common/powershell/exchange/listdatabases.pm index 3a427920d..c22a9a4ee 100644 --- a/centreon-plugins/centreon/common/powershell/exchange/listdatabases.pm +++ b/centreon-plugins/centreon/common/powershell/exchange/listdatabases.pm @@ -24,11 +24,14 @@ use strict; use warnings; use centreon::plugins::misc; use centreon::common::powershell::exchange::powershell; +use centreon::common::powershell::functions; sub get_powershell { my (%options) = @_; my $ps = centreon::common::powershell::exchange::powershell::powershell_init(%options); + $ps .= centreon::common::powershell::functions::escape_jsonstring(%options); + $ps .= centreon::common::powershell::functions::convert_to_json(%options); $ps .= ' # Check to make sure all databases are mounted @@ -51,57 +54,28 @@ try { Write-Host $Error[0].Exception exit 1 } + +$items = New-Object System.Collections.Generic.List[Hashtable]; Foreach ($DB in $MountedDB) { - Write-Host "[name=" $DB.Name "][server=" $DB.Server "][mounted=" $DB.Mounted "][size=" $DB.DatabaseSize "][asize=" $DB.AvailableNewMailboxSpace "]" -NoNewline - + $item = @{} + $item.name = $DB.Name + $item.server = $DB.Server.Name + $item.mounted = $DB.Mounted + $item.size = $DB.DatabaseSize.ToBytes().ToString() + $item.asize = $DB.AvailableNewMailboxSpace.ToBytes().ToString() + $items.Add($item) '; $ps .= ' } +$jsonString = $items | ConvertTo-JSON-20 -forceArray $true +Write-Host $jsonString exit 0 '; return $ps; } -sub list { - my ($self, %options) = @_; - - # Following output: - #[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][mounted= True ][size= 136.1 MB (142,671,872 bytes) ][asize= 124.4 MB (130,482,176 bytes) ][mapi= Success ][mailflow= Success ][latency= 50,00 ] - #... - - foreach my $line (split /\n/, $options{stdout}) { - next if ($line !~ /^\[name=(.*?)\]\[server=(.*?)\]\[mounted=(.*?)\]\[size=(.*?)\]\[asize=(.*?)\]/); - my ($database, $server, $mounted, $size, $asize) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), - centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5)); - - $self->{output}->output_add(long_msg => "'" . $database . "' [server = $server, mounted = " . $mounted . ']'); - } -} - -sub disco_show { - my ($self, %options) = @_; - - # Following output: - #[name= Mailbox Database 0975194476 ][server= SRVI-WIN-TEST ][mounted= True ][size= 136.1 MB (142,671,872 bytes) ][asize= 124.4 MB (130,482,176 bytes) ][mapi= Success ][mailflow= Success ][latency= 50,00 ] - #... - - foreach my $line (split /\n/, $options{stdout}) { - next if ($line !~ /^\[name=(.*?)\]\[server=(.*?)\]\[mounted=(.*?)\]\[size=(.*?)\]\[asize=(.*?)\]/); - my ($database, $server, $mounted, $size, $asize) = ( - centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), - centreon::plugins::misc::trim($3), centreon::plugins::misc::trim($4), centreon::plugins::misc::trim($5) - ); - - $self->{output}->add_disco_entry( - name => $database, - server => $server, - mounted => $mounted - ); - } -} - 1; __END__ diff --git a/centreon-plugins/storage/fujitsu/eternus/dx/ssh/mode/psu.pm b/centreon-plugins/storage/fujitsu/eternus/dx/ssh/mode/psu.pm index 10c4472ef..2d975a0fd 100644 --- a/centreon-plugins/storage/fujitsu/eternus/dx/ssh/mode/psu.pm +++ b/centreon-plugins/storage/fujitsu/eternus/dx/ssh/mode/psu.pm @@ -35,8 +35,8 @@ my $thresholds = { ['Undefined', 'WARNING'], ['Normal (Inside unused parts)', 'WARNING'], ['Error', 'CRITICAL'], - ['Unknown', 'UNKNOWN'], - ], + ['Unknown', 'UNKNOWN'] + ] }; sub new { @@ -44,20 +44,20 @@ sub new { my $self = $class->SUPER::new(package => __PACKAGE__, %options); bless $self, $class; - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "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 }, - "command:s" => { name => 'command', default => 'show' }, - "command-path:s" => { name => 'command_path' }, - "command-options:s" => { name => 'command_options', default => 'enclosure-status -type all' }, - "filter:s@" => { name => 'filter' }, - "threshold-overload:s@" => { name => 'threshold_overload' }, - "no-component:s" => { name => 'no_component' }, - }); + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + '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 }, + 'command:s' => { name => 'command', default => 'show' }, + 'command-path:s' => { name => 'command_path' }, + 'command-options:s' => { name => 'command_options', default => 'enclosure-status -type all' }, + 'filter:s@' => { name => 'filter' }, + 'threshold-overload:s@' => { name => 'threshold_overload' }, + 'no-component:s' => { name => 'no_component' } + }); + $self->{no_components} = undef; return $self; } @@ -76,7 +76,7 @@ sub check_options { my @values = split (/,/, $val); push @{$self->{filter}}, { filter => $values[0], instance => $values[1] }; } - + $self->{overload_th} = {}; foreach my $val (@{$self->{option_results}->{threshold_overload}}) { if ($val !~ /^(.*?),(.*?),(.*)$/) { @@ -104,12 +104,14 @@ sub check_options { sub run { my ($self, %options) = @_; - my $stdout = centreon::plugins::misc::execute(output => $self->{output}, - options => $self->{option_results}, - ssh_pipe => 1, - 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}, + ssh_pipe => 1, + command => $self->{option_results}->{command}, + command_path => $self->{option_results}->{command_path}, + command_options => $self->{option_results}->{command_options} + ); #Controller Enclosure #0 Information #... @@ -149,35 +151,43 @@ sub run { # PSU#1 [Normal / 0xE001] my $total_components = 0; - while ($stdout =~ /^(Controller|Frontend)\s+Enclosure\s+(#\d+\s+|)Status(.*?)(\n\n|\Z)/msg) { + while ($stdout =~ /^(Controller|Frontend|Drive)\s+Enclosure\s+(#\d+\s+|)Status(.*?)(\n\n|\Z)/msg) { my ($type, $num, $content) = ($1, $2, $3); - + my $prefix = 'fe'; - if ($type =~ /^C/) { + if ($type =~ /controller/i) { $prefix = 'ce' . centreon::plugins::misc::trim($num); + } elsif ($type =~ /drive/i) { + $prefix = 'drive' . centreon::plugins::misc::trim($num); } while ($content =~ /PSU#(\d+)\s+\[\s*(\S+)/msig) { my ($psu_number, $psu_status) = ($1, $2); my $psu_name = $prefix . '_' . $psu_number; - + next if ($self->check_filter(section => 'psu', instance => $psu_name)); $total_components++; $self->{output}->output_add(long_msg => sprintf("Power Supply '%s' status is '%s'", $psu_name, $psu_status)); my $exit = $self->get_severity(section => 'psu', value => $psu_status); if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Power Supply '%s' status is '%s'.", $psu_name, $psu_status)); + $self->{output}->output_add( + severity => $exit, + short_msg => sprintf("Power Supply '%s' status is '%s'.", $psu_name, $psu_status) + ); } } } - $self->{output}->output_add(severity => 'OK', - short_msg => sprintf("All %d power supplies are ok.", $total_components)); - + $self->{output}->output_add( + severity => 'OK', + short_msg => sprintf("All %d power supplies are ok.", $total_components) + ); + if (defined($self->{option_results}->{no_component}) && $total_components == 0) { - $self->{output}->output_add(severity => $self->{no_components}, - short_msg => 'No components are checked.'); + $self->{output}->output_add( + severity => $self->{no_components}, + short_msg => 'No components are checked.' + ); } $self->{output}->display();