enhance windows local powershell checks
This commit is contained in:
parent
0c114307b7
commit
38bd5f3144
|
@ -22,7 +22,7 @@ package centreon::common::powershell::windows::liststorages;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use centreon::plugins::misc;
|
use centreon::common::powershell::functions;
|
||||||
|
|
||||||
sub get_powershell {
|
sub get_powershell {
|
||||||
my (%options) = @_;
|
my (%options) = @_;
|
||||||
|
@ -30,18 +30,38 @@ sub get_powershell {
|
||||||
my $ps = '
|
my $ps = '
|
||||||
$culture = new-object "System.Globalization.CultureInfo" "en-us"
|
$culture = new-object "System.Globalization.CultureInfo" "en-us"
|
||||||
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
|
[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"
|
$ProgressPreference = "SilentlyContinue"
|
||||||
|
|
||||||
Try {
|
Try {
|
||||||
$ErrorActionPreference = "Stop"
|
$ErrorActionPreference = "Stop"
|
||||||
|
|
||||||
$disks = Get-CimInstance Win32_LogicalDisk
|
$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 {
|
} Catch {
|
||||||
Write-Host $Error[0].Exception
|
Write-Host $Error[0].Exception
|
||||||
exit 1
|
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
|
exit 0
|
||||||
|
@ -52,51 +72,6 @@ exit 0
|
||||||
|
|
||||||
1;
|
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__
|
__END__
|
||||||
|
|
||||||
=head1 DESCRIPTION
|
=head1 DESCRIPTION
|
||||||
|
|
|
@ -22,6 +22,7 @@ package centreon::common::powershell::windows::pendingreboot;
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use centreon::common::powershell::functions;
|
||||||
|
|
||||||
sub get_powershell {
|
sub get_powershell {
|
||||||
my (%options) = @_;
|
my (%options) = @_;
|
||||||
|
@ -29,6 +30,12 @@ sub get_powershell {
|
||||||
my $ps = '
|
my $ps = '
|
||||||
$culture = new-object "System.Globalization.CultureInfo" "en-us"
|
$culture = new-object "System.Globalization.CultureInfo" "en-us"
|
||||||
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
|
[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"
|
$ProgressPreference = "SilentlyContinue"
|
||||||
|
|
||||||
Try {
|
Try {
|
||||||
|
@ -107,13 +114,18 @@ Try {
|
||||||
$SCCM = $null
|
$SCCM = $null
|
||||||
}
|
}
|
||||||
|
|
||||||
Write-Host ("[CBServicing={0}]" -f $CBSRebootPend ) -NoNewline
|
$item = @{
|
||||||
Write-Host ("[WindowsUpdate={0}]" -f $WUAURebootReq ) -NoNewline
|
CBServicing = $CBSRebootPend;
|
||||||
Write-Host ("[CCMClientSDK={0}]" -f $SCCM ) -NoNewline
|
WindowsUpdate = $WUAURebootReq;
|
||||||
Write-Host ("[PendComputerRename={0}]" -f $CompPendRen ) -NoNewline
|
CCMClientSDK = $SCCM;
|
||||||
Write-Host ("[PendFileRename={0}]" -f $PendFileRename ) -NoNewline
|
PendComputerRename = $CompPendRen;
|
||||||
Write-Host ("[PendFileRenVal={0}]" -f $RegValuePFRO ) -NoNewline
|
PendFileRename = $PendFileRename;
|
||||||
Write-Host ("[RebootPending={0}]" -f ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename) )
|
PendFileRenVal = $RegValuePFRO;
|
||||||
|
RebootPending = ($CompPendRen -or $CBSRebootPend -or $WUAURebootReq -or $SCCM -or $PendFileRename)
|
||||||
|
}
|
||||||
|
|
||||||
|
$jsonString = $item | ConvertTo-JSON-20
|
||||||
|
Write-Host $jsonString
|
||||||
} Catch {
|
} Catch {
|
||||||
Write-Host $Error[0].Exception
|
Write-Host $Error[0].Exception
|
||||||
exit 1
|
exit 1
|
||||||
|
|
|
@ -24,8 +24,9 @@ use base qw(centreon::plugins::mode);
|
||||||
|
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
use centreon::plugins::misc;
|
|
||||||
use centreon::common::powershell::windows::liststorages;
|
use centreon::common::powershell::windows::liststorages;
|
||||||
|
use centreon::plugins::misc;
|
||||||
|
use JSON::XS;
|
||||||
|
|
||||||
sub new {
|
sub new {
|
||||||
my ($class, %options) = @_;
|
my ($class, %options) = @_;
|
||||||
|
@ -50,7 +51,17 @@ sub check_options {
|
||||||
$self->SUPER::init(%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) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
if (!defined($self->{option_results}->{no_ps})) {
|
if (!defined($self->{option_results}->{no_ps})) {
|
||||||
|
@ -67,9 +78,9 @@ sub run {
|
||||||
$self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps);
|
$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},
|
output => $self->{output},
|
||||||
timeout => $self->{option_results}->{timeout},
|
options => $self->{option_results},
|
||||||
command => $self->{option_results}->{command},
|
command => $self->{option_results}->{command},
|
||||||
command_path => $self->{option_results}->{command_path},
|
command_path => $self->{option_results}->{command_path},
|
||||||
command_options => $self->{option_results}->{command_options}
|
command_options => $self->{option_results}->{command_options}
|
||||||
|
@ -79,14 +90,44 @@ sub run {
|
||||||
severity => 'OK',
|
severity => 'OK',
|
||||||
short_msg => $stdout
|
short_msg => $stdout
|
||||||
);
|
);
|
||||||
} else {
|
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
|
||||||
$self->{output}->output_add(
|
$self->{output}->exit();
|
||||||
severity => 'OK',
|
|
||||||
short_msg => 'List disk:'
|
|
||||||
);
|
|
||||||
centreon::common::powershell::windows::liststorages::list($self, stdout => $stdout);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
}
|
}
|
||||||
|
@ -100,19 +141,16 @@ sub disco_format {
|
||||||
sub disco_show {
|
sub disco_show {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
|
||||||
if (!defined($self->{option_results}->{no_ps})) {
|
my $result = $self->manage_selection();
|
||||||
my $ps = centreon::common::powershell::windows::liststorages::get_powershell();
|
foreach (@$result) {
|
||||||
$self->{option_results}->{command_options} .= " " . centreon::plugins::misc::powershell_encoded($ps);
|
$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;
|
1;
|
||||||
|
|
|
@ -27,6 +27,7 @@ use warnings;
|
||||||
use centreon::plugins::misc;
|
use centreon::plugins::misc;
|
||||||
use centreon::common::powershell::windows::pendingreboot;
|
use centreon::common::powershell::windows::pendingreboot;
|
||||||
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
|
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
|
||||||
|
use JSON::XS;
|
||||||
|
|
||||||
sub custom_status_output {
|
sub custom_status_output {
|
||||||
my ($self, %options) = @_;
|
my ($self, %options) = @_;
|
||||||
|
@ -62,14 +63,16 @@ sub set_counters {
|
||||||
];
|
];
|
||||||
$self->{maps_counters}->{pendingreboot} = [
|
$self->{maps_counters}->{pendingreboot} = [
|
||||||
{ label => 'status', , threshold => 0, set => {
|
{ label => 'status', , threshold => 0, set => {
|
||||||
key_values => [ { name => 'CBServicing' }, { name => 'RebootPending' }, { name => 'WindowsUpdate' },
|
key_values => [
|
||||||
{ name => 'CCMClientSDK' }, { name => 'PendComputerRename' }, { name => 'PendFileRename' } ],
|
{ name => 'CBServicing' }, { name => 'RebootPending' }, { name => 'WindowsUpdate' },
|
||||||
|
{ name => 'CCMClientSDK' }, { name => 'PendComputerRename' }, { name => 'PendFileRename' }
|
||||||
|
],
|
||||||
closure_custom_calc => $self->can('custom_status_calc'),
|
closure_custom_calc => $self->can('custom_status_calc'),
|
||||||
closure_custom_output => $self->can('custom_status_output'),
|
closure_custom_output => $self->can('custom_status_output'),
|
||||||
closure_custom_perfdata => sub { return 0; },
|
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}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
|
||||||
$self->{output}->exit();
|
$self->{output}->exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[CBServicing=False][WindowsUpdate=False][CCMClientSDK=][PendComputerRename=False][PendFileRename=False][PendFileRenVal=][RebootPending=False]
|
my $decoded;
|
||||||
$self->{pendingreboot} = {};
|
eval {
|
||||||
while ($stdout =~ /\[(.*?)=\s*(.*?)\s*\]/mg) {
|
$decoded = JSON::XS->new->utf8->decode($stdout);
|
||||||
$self->{pendingreboot}->{$1} = $2;
|
};
|
||||||
|
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;
|
1;
|
||||||
|
|
Loading…
Reference in New Issue