enhance windows local powershell checks

This commit is contained in:
garnier-quentin 2020-03-02 16:32:37 +01:00
parent 0c114307b7
commit 38bd5f3144
4 changed files with 126 additions and 86 deletions

View File

@ -22,7 +22,7 @@ package centreon::common::powershell::windows::liststorages;
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::common::powershell::functions;
sub get_powershell {
my (%options) = @_;
@ -30,18 +30,38 @@ sub get_powershell {
my $ps = '
$culture = new-object "System.Globalization.CultureInfo" "en-us"
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
';
$ps .= centreon::common::powershell::functions::escape_jsonstring(%options);
$ps .= centreon::common::powershell::functions::convert_to_json(%options);
$ps .= '
$ProgressPreference = "SilentlyContinue"
Try {
$ErrorActionPreference = "Stop"
$disks = Get-CimInstance Win32_LogicalDisk
$items = New-Object System.Collections.Generic.List[Hashtable];
Foreach ($disk in $disks) {
$item = @{
name = $disk.DeviceID;
type = $disk.DriveType;
providername = $disk.ProviderName;
desc = $disk.VolumeName;
size = $disk.Size.toString();
freespace = $disk.FreeSpace.toString()
}
$items.Add($item)
}
$jsonString = $items | ConvertTo-JSON-20 -forceArray 1
Write-Host $jsonString
} Catch {
Write-Host $Error[0].Exception
exit 1
}Foreach ($disk in $disks) {
Write-Host "[name=" $disk.DeviceID "][type=" $disk.DriveType "][providername=" $disk.ProviderName "][desc=" $disk.VolumeName "][size=" $disk.Size "][freespace=" $disk.FreeSpace "]"
}
exit 0
@ -52,51 +72,6 @@ exit 0
1;
sub list {
my ($self, %options) = @_;
my %map_type = (2 => 'removable', 3 => 'local', 4 => 'network', 5 => 'floppy');
# Following output:
#[name= C: ][type= 3 ][providername= ][desc= OS ][size= 254406553600 ][freespace= 23851290624 ]
#...
foreach my $line (split /\n/, $options{stdout}) {
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 . "' [size = $size, free = $free, desc = $desc, type = $map_type{$type}]");
}
}
1;
sub disco_show {
my ($self, %options) = @_;
my %map_type = (2 => 'removable', 3 => 'local', 4 => 'network', 5 => 'floppy');
# Following output:
#[name= C: ][type= 3 ][providername= ][desc= OS ][size= 254406553600 ][freespace= 23851290624 ]
#...
foreach my $line (split /\n/, $options{stdout}) {
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,
size => $size,
free => $free,
type => $map_type{$type},
desc => $desc
);
}
}
1;
__END__
=head1 DESCRIPTION

View File

@ -22,6 +22,7 @@ package centreon::common::powershell::windows::pendingreboot;
use strict;
use warnings;
use centreon::common::powershell::functions;
sub get_powershell {
my (%options) = @_;
@ -29,6 +30,12 @@ sub get_powershell {
my $ps = '
$culture = new-object "System.Globalization.CultureInfo" "en-us"
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
';
$ps .= centreon::common::powershell::functions::escape_jsonstring(%options);
$ps .= centreon::common::powershell::functions::convert_to_json(%options);
$ps .= '
$ProgressPreference = "SilentlyContinue"
Try {
@ -107,13 +114,18 @@ Try {
$SCCM = $null
}
Write-Host ("[CBServicing={0}]" -f $CBSRebootPend ) -NoNewline
Write-Host ("[WindowsUpdate={0}]" -f $WUAURebootReq ) -NoNewline
Write-Host ("[CCMClientSDK={0}]" -f $SCCM ) -NoNewline
Write-Host ("[PendComputerRename={0}]" -f $CompPendRen ) -NoNewline
Write-Host ("[PendFileRename={0}]" -f $PendFileRename ) -NoNewline
Write-Host ("[PendFileRenVal={0}]" -f $RegValuePFRO ) -NoNewline
Write-Host ("[RebootPending={0}]" -f ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename) )
$item = @{
CBServicing = $CBSRebootPend;
WindowsUpdate = $WUAURebootReq;
CCMClientSDK = $SCCM;
PendComputerRename = $CompPendRen;
PendFileRename = $PendFileRename;
PendFileRenVal = $RegValuePFRO;
RebootPending = ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename)
}
$jsonString = $item | ConvertTo-JSON-20
Write-Host $jsonString
} Catch {
Write-Host $Error[0].Exception
exit 1

View File

@ -24,8 +24,9 @@ use base qw(centreon::plugins::mode);
use strict;
use warnings;
use centreon::plugins::misc;
use centreon::common::powershell::windows::liststorages;
use centreon::plugins::misc;
use JSON::XS;
sub new {
my ($class, %options) = @_;
@ -50,7 +51,17 @@ sub check_options {
$self->SUPER::init(%options);
}
sub run {
my $map_type = {
0 => 'unknown',
1 => 'noRootDirectory',
2 => 'removableDisk',
3 => 'localDisk',
4 => 'networkDrive',
5 => 'compactDisc',
6 => 'ramDisk'
};
sub manage_selection {
my ($self, %options) = @_;
if (!defined($self->{option_results}->{no_ps})) {
@ -67,9 +78,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}
@ -79,14 +90,44 @@ sub run {
severity => 'OK',
short_msg => $stdout
);
} else {
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List disk:'
);
centreon::common::powershell::windows::liststorages::list($self, stdout => $stdout);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($stdout);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
return $decoded;
}
sub run {
my ($self, %options) = @_;
my $result = $self->manage_selection();
foreach (@$result) {
$self->{output}->output_add(
long_msg => sprintf(
"'%s' [size: %s][free: %s][desc: %s][type: %s]",
$_->{name},
$_->{size},
$_->{freespace},
$_->{desc},
$map_type->{ $_->{type} }
)
);
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List disks:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
@ -100,19 +141,16 @@ sub disco_format {
sub disco_show {
my ($self, %options) = @_;
if (!defined($self->{option_results}->{no_ps})) {
my $ps = centreon::common::powershell::windows::liststorages::get_powershell();
$self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps);
my $result = $self->manage_selection();
foreach (@$result) {
$self->{output}->add_disco_entry(
name => $_->{name},
size => $_->{size},
free => $_->{freespace},
type => $map_type->{ $_->{type} },
desc => $_->{desc}
);
}
my ($stdout) = centreon::plugins::misc::windows_execute(
output => $self->{output},
timeout => $self->{option_results}->{timeout},
command => $self->{option_results}->{command},
command_path => $self->{option_results}->{command_path},
command_options => $self->{option_results}->{command_options}
);
centreon::common::powershell::windows::liststorages::disco_show($self, stdout => $stdout);
}
1;

View File

@ -27,6 +27,7 @@ use warnings;
use centreon::plugins::misc;
use centreon::common::powershell::windows::pendingreboot;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
use JSON::XS;
sub custom_status_output {
my ($self, %options) = @_;
@ -62,14 +63,16 @@ sub set_counters {
];
$self->{maps_counters}->{pendingreboot} = [
{ label => 'status', , threshold => 0, set => {
key_values => [ { name => 'CBServicing' }, { name => 'RebootPending' }, { name => 'WindowsUpdate' },
{ name => 'CCMClientSDK' }, { name => 'PendComputerRename' }, { name => 'PendFileRename' } ],
key_values => [
{ name => 'CBServicing' }, { name => 'RebootPending' }, { name => 'WindowsUpdate' },
{ name => 'CCMClientSDK' }, { name => 'PendComputerRename' }, { name => 'PendFileRename' }
],
closure_custom_calc => $self->can('custom_status_calc'),
closure_custom_output => $self->can('custom_status_output'),
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold,
closure_custom_threshold_check => \&catalog_status_threshold
}
},
}
];
}
@ -132,12 +135,24 @@ sub manage_selection {
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
#[CBServicing=False][WindowsUpdate=False][CCMClientSDK=][PendComputerRename=False][PendFileRename=False][PendFileRenVal=][RebootPending=False]
$self->{pendingreboot} = {};
while ($stdout =~ /\[(.*?)=\s*(.*?)\s*\]/mg) {
$self->{pendingreboot}->{$1} = $2;
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($stdout);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
$self->{output}->option_exit();
}
#{ CBServicing: false, WindowsUpdate: false, CCMClientSDK: null, PendComputerRename: false, PendFileRename: false, PendFileRenVal: null, RebootPending: false }
foreach (keys %$decoded) {
$decoded->{$_} = '-' if (!defined($decoded->{$_}));
$decoded->{$_} = 'true' if ($decoded->{$_} =~ /true|1/i);
$decoded->{$_} = 'false' if ($decoded->{$_} =~ /false|0/i);
}
$self->{pendingreboot} = $decoded;
}
1;