enh wsus json parsing

This commit is contained in:
Colin Gagnaire 2019-03-22 17:50:58 +01:00
parent 63789d362a
commit 42fca2ebc8
10 changed files with 36 additions and 21 deletions

View File

@ -146,7 +146,7 @@ sub manage_selection {
my $decoded; my $decoded;
eval { 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 ($@) { if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");

View File

@ -165,7 +165,7 @@ sub manage_selection {
my $decoded; my $decoded;
eval { 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 ($@) { if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");

View File

@ -262,7 +262,7 @@ sub manage_selection {
my $decoded; my $decoded;
eval { 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 ($@) { if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");

View File

@ -144,7 +144,7 @@ sub manage_selection {
my $decoded; my $decoded;
eval { 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 ($@) { if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@"); $self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");

View File

@ -22,7 +22,6 @@ package centreon::common::powershell::functions;
use strict; use strict;
use warnings; use warnings;
use centreon::plugins::misc;
sub escape_jsonstring { sub escape_jsonstring {
my (%options) = @_; my (%options) = @_;
@ -77,7 +76,7 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
$jsonResult = '' $jsonResult = ''
foreach($elem in $value){ foreach($elem in $value){
#if ($elem -eq $null) {continue} #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)) $jsonResult += ($elem | ConvertTo-JSON-20 -maxDepth ($maxDepth -1))
} }
return "[" + $jsonResult + "]" return "[" + $jsonResult + "]"
@ -85,10 +84,10 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
'(System\.)?Hashtable' { # hashtable '(System\.)?Hashtable' { # hashtable
$jsonResult = '' $jsonResult = ''
foreach($key in $value.Keys){ foreach($key in $value.Keys){
if ($jsonResult.Length -gt 0) {$jsonResult +=', '} if ($jsonResult.Length -gt 0) {$jsonResult +=','}
$jsonResult += $jsonResult +=
@" @"
"{0}": {1} "{0}":{1}
"@ -f $key , ($value[$key] | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) ) "@ -f $key , ($value[$key] | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) )
} }
return "{" + $jsonResult + "}" return "{" + $jsonResult + "}"
@ -99,10 +98,10 @@ function ConvertTo-JSON-20($maxDepth = 4,$forceArray = $false) {
return "{" + return "{" +
(($value | Get-Member -MemberType *property | % { (($value | Get-Member -MemberType *property | % {
@" @"
"{0}": {1} "{0}":{1}
"@ -f $_.Name , ($value.($_.Name) | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) ) "@ -f $_.Name , ($value.($_.Name) | ConvertTo-JSON-20 -maxDepth ($maxDepth -1) )
}) -join ', ') + "}" }) -join ',') + "}"
} }
} }
} }

View File

@ -56,7 +56,7 @@ $ProgressPreference = "SilentlyContinue"
Try { Try {
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort) $wsusObject = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
$wsusStatus = $wsusObject.GetStatus() $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 "ComputersUpToDateCount" -Value $wsusStatus.ComputersUpToDateCount
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ComputersNotContactedSinceCount" -Value $computersNotContactedSinceCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ComputersNotContactedSinceCount" -Value $computersNotContactedSinceCount
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UnassignedComputersCount" -Value $unassignedComputersCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UnassignedComputersCount" -Value $unassignedComputersCount
$returnObject | ConvertTo-JSON-20 $jsonString = $returnObject | ConvertTo-JSON-20
Write-Host $jsonString
} Catch { } Catch {
Write-Host $Error[0].Exception Write-Host $Error[0].Exception
exit 1 exit 1

View File

@ -34,7 +34,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
[System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture
$wsusServer = "' . $options{wsus_server} . '" $wsusServer = "' . $options{wsus_server} . '"
$useSsl = ' . $options{secure_connection} . ' $useSsl = ' . $options{use_ssl} . '
$wsusPort = ' . $options{wsus_port} . ' $wsusPort = ' . $options{wsus_port} . '
Try { Try {
@ -49,7 +49,7 @@ $ProgressPreference = "SilentlyContinue"
Try { Try {
$ErrorActionPreference = "Stop" $ErrorActionPreference = "Stop"
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort) $wsusObject = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($wsusServer, $useSsl, $wsusPort)
$wsusStatus = $wsusObject.GetStatus() $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 "NotApprovedUpdateCount" -Value $wsusStatus.NotApprovedUpdateCount
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesWithStaleUpdateApprovalsCount" -Value $wsusStatus.UpdatesWithStaleUpdateApprovalsCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesWithStaleUpdateApprovalsCount" -Value $wsusStatus.UpdatesWithStaleUpdateApprovalsCount
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ExpiredUpdateCount" -Value $wsusStatus.ExpiredUpdateCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "ExpiredUpdateCount" -Value $wsusStatus.ExpiredUpdateCount
$returnObject | ConvertTo-JSON-20 $jsonString = $returnObject | ConvertTo-JSON-20
Write-Host $jsonString
} Catch { } Catch {
Write-Host $Error[0].Exception Write-Host $Error[0].Exception
exit 1 exit 1

View File

@ -39,7 +39,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
$ps .= ' $ps .= '
$wsusServer = "' . $options{wsus_server} . '" $wsusServer = "' . $options{wsus_server} . '"
$useSsl = ' . $options{secure_connection} . ' $useSsl = ' . $options{use_ssl} . '
$wsusPort = ' . $options{wsus_port} . ' $wsusPort = ' . $options{wsus_port} . '
Try { Try {
@ -68,7 +68,8 @@ Try {
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationStartTime" -Value $lastSync.StartTime Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationStartTime" -Value $lastSync.StartTime
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationEndTime" -Value $lastSync.EndTime Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "LastSynchronizationEndTime" -Value $lastSync.EndTime
$returnObject | ConvertTo-JSON-20 $jsonString = $returnObject | ConvertTo-JSON-20
Write-Host $jsonString
} Catch { } Catch {
Write-Host $Error[0].Exception Write-Host $Error[0].Exception
exit 1 exit 1

View File

@ -40,7 +40,7 @@ $culture = new-object "System.Globalization.CultureInfo" "en-us"
$ps .= ' $ps .= '
$wsusServer = "' . $options{wsus_server} . '" $wsusServer = "' . $options{wsus_server} . '"
$useSsl = ' . $options{secure_connection} . ' $useSsl = ' . $options{use_ssl} . '
$wsusPort = ' . $options{wsus_port} . ' $wsusPort = ' . $options{wsus_port} . '
Try { Try {
@ -66,7 +66,8 @@ Try {
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesNeededByComputersCount" -Value $wsusStatus.UpdatesNeededByComputersCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesNeededByComputersCount" -Value $wsusStatus.UpdatesNeededByComputersCount
Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesUpToDateCount" -Value $wsusStatus.UpdatesUpToDateCount Add-Member -InputObject $returnObject -MemberType NoteProperty -Name "UpdatesUpToDateCount" -Value $wsusStatus.UpdatesUpToDateCount
$returnObject | ConvertTo-JSON-20 $jsonString = $returnObject | ConvertTo-JSON-20
Write-Host $jsonString
} Catch { } Catch {
Write-Host $Error[0].Exception Write-Host $Error[0].Exception
exit 1 exit 1

View File

@ -327,6 +327,18 @@ sub powershell_escape {
return $value; 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 { sub minimal_version {
my ($version_src, $version_dst) = @_; my ($version_src, $version_dst) = @_;