mirror of
https://github.com/centreon/centreon-plugins.git
synced 2025-07-27 07:34:35 +02:00
enh wsus json parsing
This commit is contained in:
parent
889e1497ef
commit
4c70be6515
@ -146,7 +146,7 @@ sub manage_selection {
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
$decoded = JSON::XS->new->utf8->decode($stdout);
|
||||
$decoded = JSON::XS->new->utf8->decode(centreon::plugins::misc::powershell_json_sanitizer(string => $stdout, output => $self->{output}));
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
|
@ -165,7 +165,7 @@ sub manage_selection {
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
$decoded = JSON::XS->new->utf8->decode($stdout);
|
||||
$decoded = JSON::XS->new->utf8->decode(centreon::plugins::misc::powershell_json_sanitizer(string => $stdout, output => $self->{output}));
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
|
@ -262,7 +262,7 @@ sub manage_selection {
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
$decoded = JSON::XS->new->utf8->decode($stdout);
|
||||
$decoded = JSON::XS->new->utf8->decode(centreon::plugins::misc::powershell_json_sanitizer(string => $stdout, output => $self->{output}));
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
|
@ -144,7 +144,7 @@ sub manage_selection {
|
||||
|
||||
my $decoded;
|
||||
eval {
|
||||
$decoded = JSON::XS->new->utf8->decode($stdout);
|
||||
$decoded = JSON::XS->new->utf8->decode(centreon::plugins::misc::powershell_json_sanitizer(string => $stdout, output => $self->{output}));
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
|
@ -22,7 +22,6 @@ package centreon::common::powershell::functions;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use centreon::plugins::misc;
|
||||
|
||||
sub escape_jsonstring {
|
||||
my (%options) = @_;
|
||||
|
@ -56,7 +56,7 @@ $ProgressPreference = "SilentlyContinue"
|
||||
Try {
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
|
||||
$wsusObject = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
|
||||
|
||||
$wsusStatus = $wsusObject.GetStatus()
|
||||
|
||||
@ -73,7 +73,8 @@ Try {
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ComputersNotContactedSinceCount" -Value $computersNotContactedSinceCount
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UnassignedComputersCount" -Value $unassignedComputersCount
|
||||
|
||||
$returnObject | ConvertTo-JSON-20
|
||||
$jsonString = $returnObject | ConvertTo-JSON-20
|
||||
Write-Host $jsonString
|
||||
} Catch {
|
||||
Write-Host $Error[0].Exception
|
||||
exit 1
|
||||
|
@ -34,7 +34,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
|
||||
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
|
||||
|
||||
$wsusServer = "' . $options{wsus_server} . '"
|
||||
$useSsl = ' . $options{secure_connection} . '
|
||||
$useSsl = ' . $options{use_ssl} . '
|
||||
$wsusPort = ' . $options{wsus_port} . '
|
||||
|
||||
Try {
|
||||
@ -49,7 +49,7 @@ $ProgressPreference = "SilentlyContinue"
|
||||
Try {
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
|
||||
$wsusObject = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
|
||||
|
||||
$wsusStatus = $wsusObject.GetStatus()
|
||||
|
||||
@ -63,7 +63,8 @@ Try {
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesWithStaleUpdateApprovalsCount" -Value $wsusStatus.UpdatesWithStaleUpdateApprovalsCount
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ExpiredUpdateCount" -Value $wsusStatus.ExpiredUpdateCount
|
||||
|
||||
$returnObject | ConvertTo-JSON-20
|
||||
$jsonString = $returnObject | ConvertTo-JSON-20
|
||||
Write-Host $jsonString
|
||||
} Catch {
|
||||
Write-Host $Error[0].Exception
|
||||
exit 1
|
||||
|
@ -39,7 +39,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
|
||||
|
||||
$ps .= '
|
||||
$wsusServer = "' . $options{wsus_server} . '"
|
||||
$useSsl = ' . $options{secure_connection} . '
|
||||
$useSsl = ' . $options{use_ssl} . '
|
||||
$wsusPort = ' . $options{wsus_port} . '
|
||||
|
||||
Try {
|
||||
@ -68,7 +68,8 @@ Try {
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationStartTime" -Value $lastSync.StartTime
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationEndTime" -Value $lastSync.EndTime
|
||||
|
||||
$returnObject | ConvertTo-JSON-20
|
||||
$jsonString = $returnObject | ConvertTo-JSON-20
|
||||
Write-Host $jsonString
|
||||
} Catch {
|
||||
Write-Host $Error[0].Exception
|
||||
exit 1
|
||||
|
@ -40,7 +40,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
|
||||
|
||||
$ps .= '
|
||||
$wsusServer = "' . $options{wsus_server} . '"
|
||||
$useSsl = ' . $options{secure_connection} . '
|
||||
$useSsl = ' . $options{use_ssl} . '
|
||||
$wsusPort = ' . $options{wsus_port} . '
|
||||
|
||||
Try {
|
||||
@ -66,7 +66,8 @@ Try {
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesNeededByComputersCount" -Value $wsusStatus.UpdatesNeededByComputersCount
|
||||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesUpToDateCount" -Value $wsusStatus.UpdatesUpToDateCount
|
||||
|
||||
$returnObject | ConvertTo-JSON-20
|
||||
$jsonString = $returnObject | ConvertTo-JSON-20
|
||||
Write-Host $jsonString
|
||||
} Catch {
|
||||
Write-Host $Error[0].Exception
|
||||
exit 1
|
||||
|
@ -327,6 +327,18 @@ sub powershell_escape {
|
||||
return $value;
|
||||
}
|
||||
|
||||
sub powershell_json_sanitizer {
|
||||
my (%options) = @_;
|
||||
|
||||
centreon::plugins::misc::mymodule_load(output => $options{output}, module => 'JSON::XS',
|
||||
error_msg => "Cannot load module 'JSON::XS'.");
|
||||
foreach my $line (split /\n/, $options{string}) {
|
||||
eval { JSON::XS->new->utf8->decode($line) };
|
||||
return $line if (!$@);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
sub minimal_version {
|
||||
my ($version_src, $version_dst) = @_;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user