From 822427c4a20ddb0e6a68e2a8ab14844590b035b4 Mon Sep 17 00:00:00 2001 From: THEPAUT Date: Fri, 20 Apr 2018 14:05:32 +0200 Subject: [PATCH] List storages (#946) * better response time & add posibility to know drive type --- .../common/powershell/windows/liststorages.pm | 45 ++++++++----------- os/windows/local/mode/liststorages.pm | 13 +++--- 2 files changed, 23 insertions(+), 35 deletions(-) diff --git a/centreon/common/powershell/windows/liststorages.pm b/centreon/common/powershell/windows/liststorages.pm index 8be94d820..52999d07b 100644 --- a/centreon/common/powershell/windows/liststorages.pm +++ b/centreon/common/powershell/windows/liststorages.pm @@ -37,24 +37,14 @@ $ProgressPreference = "SilentlyContinue" Try { $ErrorActionPreference = "Stop" - '; + + $disks = Get-CimInstance Win32_LogicalDisk - if (defined($options{filter})) { - $ps .= ' - $disks = Get-PSDrive -PSProvider "' . $options{filter} . '" -'; - } else { - $ps .= ' - $disks = Get-PSDrive -'; - } - - $ps .= ' } Catch { Write-Host $Error[0].Exception exit 1 }Foreach ($disk in $disks) { - Write-Host "[name=" $disk.Name "][used=" $disk.Used "][free=" $disk.Free "][provider=" $disk.Provider "][path=" $disk.Root "]" + Write-Host "[name=" $disk.DeviceID "][type=" $disk.DriveType "][providername=" $disk.ProviderName "][desc=" $disk.VolumeName "][size=" $disk.Size "][freespace=" $disk.FreeSpace "]" } exit 0 @@ -66,16 +56,16 @@ exit 0 sub list { my ($self, %options) = @_; - + my %map_type = (2 => 'removable', 3 => 'local', 4 => 'network', 5 => 'floppy'); # Following output: - #[name= c ][server= SRVI-WIN-TEST ][used= 20394858038 ][free= 509408308 ][provider= Microsoft.PowerShell.Core\FileSystem ][path= C:\ ] + #[name= C: ][type= 3 ][providername= ][desc= OS ][size= 254406553600 ][freespace= 23851290624 ] #... foreach my $line (split /\n/, $options{stdout}) { - next if ($line !~ /^\[name=(.*?)\]\[used=(.*?)\]\[free=(.*?)\]\[provider=(.*?)\]\[path=(.*?)\]/); - my ($disk, $used, $free, $provider, $path) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), + next if ($line !~ /^\[name=(.*?)\]\[type=(.*?)\]\[providername=.*?\]\[desc=(.*?)\]\[size=(.*?)\]\[freespace=(.*?)\]/); + my ($disk, $type, $desc, $size, $free) = (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 => "'" . $disk . "' [used = $used, free = $free, path = $path, provider = " . $provider . ']'); + $self->{output}->output_add(long_msg => "'" . $disk . "' [size = $size, free = $free, desc = $desc, type = $map_type{$type}]"); } } @@ -84,20 +74,21 @@ sub list { sub disco_show { my ($self, %options) = @_; - + my %map_type = (2 => 'removable', 3 => 'local', 4 => 'network', 5 => 'floppy'); + # Following output: - #[name= c ][server= SRVI-WIN-TEST ][used= 20394858038 ][free= 509408308 ][provider= Microsoft.PowerShell.Core\FileSystem ][path= C:\ ] + #[name= C: ][type= 3 ][providername= ][desc= OS ][size= 254406553600 ][freespace= 23851290624 ] #... foreach my $line (split /\n/, $options{stdout}) { - next if ($line !~ /^\[name=(.*?)\]\[used=(.*?)\]\[free=(.*?)\]\[provider=(.*?)\]\[path=(.*?)\]/); - my ($disk, $used, $free, $provider, $path) = (centreon::plugins::misc::trim($1), centreon::plugins::misc::trim($2), + next if ($line !~ /^\[name=(.*?)\]\[type=(.*?)\]\[providername=.*?\]\[desc=(.*?)\]\[size=(.*?)\]\[freespace=(.*?)\]/); + my ($disk, $type, $desc, $size, $free) = (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 => $disk, - used => $used, - free => $free, - provider => $provider, - path => $path); + $self->{output}->add_disco_entry(name => $disk, + size => $size, + free => $free, + type => $map_type{$type}, + desc => $desc); } } diff --git a/os/windows/local/mode/liststorages.pm b/os/windows/local/mode/liststorages.pm index fe628cdb6..8148c6d96 100644 --- a/os/windows/local/mode/liststorages.pm +++ b/os/windows/local/mode/liststorages.pm @@ -41,8 +41,7 @@ sub new { "command:s" => { name => 'command', default => 'powershell.exe' }, "command-path:s" => { name => 'command_path' }, "command-options:s" => { name => 'command_options', default => '-InputFormat none -NoLogo -EncodedCommand' }, - "ps-exec-only" => { name => 'ps_exec_only', }, - "filter-type:s" => { name => 'filter', default => 'FileSystem'}, + "ps-exec-only" => { name => 'ps_exec_only', } }); return $self; } @@ -55,9 +54,8 @@ sub check_options { sub run { my ($self, %options) = @_; - my $ps = centreon::common::powershell::windows::liststorages::get_powershell(no_ps => $self->{option_results}->{no_ps}, - filter => $self->{option_results}->{filter}); - + my $ps = centreon::common::powershell::windows::liststorages::get_powershell(no_ps => $self->{option_results}->{no_ps}); + $self->{option_results}->{command_options} .= " " . $ps; my ($stdout) = centreon::plugins::misc::windows_execute(output => $self->{output}, @@ -81,14 +79,13 @@ sub run { sub disco_format { my ($self, %options) = @_; - $self->{output}->add_disco_format(elements => ['name', 'used', 'free', 'provider', 'path']); + $self->{output}->add_disco_format(elements => ['name', 'size', 'free', 'type', 'desc']); } sub disco_show { my ($self, %options) = @_; - my $ps = centreon::common::powershell::windows::liststorages::get_powershell(no_ps => $self->{option_results}->{no_ps}, - filter => $self->{option_results}->{filter}); + my $ps = centreon::common::powershell::windows::liststorages::get_powershell(no_ps => $self->{option_results}->{no_ps}); $self->{option_results}->{command_options} .= " " . $ps; my ($stdout) = centreon::plugins::misc::windows_execute(output => $self->{output},