enh wsus json parsing
This commit is contained in:
parent
63789d362a
commit
42fca2ebc8
|
@ -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) = @_;
|
||||
|
@ -77,7 +76,7 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
|
|||
$jsonResult = ''
|
||||
foreach($elem in $value){
|
||||
#if ($elem -eq $null) {continue}
|
||||
if ($jsonResult.Length -gt 0) {$jsonResult +=', '}
|
||||
if ($jsonResult.Length -gt 0) {$jsonResult +=','}
|
||||
$jsonResult += ($elem | ConvertTo-JSON-20 -maxDepth ($maxDepth -1))
|
||||
}
|
||||
return "[" + $jsonResult + "]"
|
||||
|
@ -85,10 +84,10 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
|
|||
'(System\.)?Hashtable' { # hashtable
|
||||
$jsonResult = ''
|
||||
foreach($key in $value.Keys){
|
||||
if ($jsonResult.Length -gt 0) {$jsonResult +=', '}
|
||||
if ($jsonResult.Length -gt 0) {$jsonResult +=','}
|
||||
$jsonResult +=
|
||||
@"
|
||||
"{0}": {1}
|
||||
"{0}":{1}
|
||||
"@ -f $key , ($value[$key] | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) )
|
||||
}
|
||||
return "{" + $jsonResult + "}"
|
||||
|
@ -99,10 +98,10 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
|
|||
return "{" +
|
||||
(($value | Get-Member -MemberType *property | % {
|
||||
@"
|
||||
"{0}": {1}
|
||||
"{0}":{1}
|
||||
"@ -f $_.Name , ($value.($_.Name) | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) )
|
||||
|
||||
}) -join ', ') + "}"
|
||||
}) -join ',') + "}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
@ -72,8 +72,9 @@ Try {
|
|||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ComputersUpToDateCount" -Value $wsusStatus.ComputersUpToDateCount
|
||||
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()
|
||||
|
||||
|
@ -62,8 +62,9 @@ Try {
|
|||
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "NotApprovedUpdateCount" -Value $wsusStatus.NotApprovedUpdateCount
|
||||
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…
Reference in New Issue