Check works on some systems- but not all.
Posted: Tue Jun 20, 2017 3:18 pm
I have a check (powershell) that checks for the last time windows updates were applied.
it works fine on some systems, but not all.
on the systems it is NOT working on, the nsclient.log file says :
when I run the command from cli on teh nagios server it reports:
I have tried it with a timeout of 5 min, with the same result. In XI, I copied the check from one of the systems that it works just fine on - then changed the host it would check it against of course.
the NSclient version I have on all the systems is: 0.4.1.105
when I run the powershell script on the windows machine, it works fine and dandy.
the script it:
There has got to be somethign I am missing
it works fine on some systems, but not all.
on the systems it is NOT working on, the nsclient.log file says :
Code: Select all
2017-06-20 13:02:26: e:c:\build\nscp\include\socket/connection.hpp:146: Failed to send data: The file handle supplied is not validCode: Select all
./check_nrpe -H g1ppdc02 -t 120 -c check_updates -a 30
CHECK_NRPE: Received 0 bytes from daemon. Check the remote server logs for error messages.
the NSclient version I have on all the systems is: 0.4.1.105
when I run the powershell script on the windows machine, it works fine and dandy.
the script it:
Code: Select all
param($warn)
#$GracePeriod = 30
#$output1 = $warn | out-file c:\scripts\grace.txt
$GracePeriod = $warn
Function Check-NewUpdates {
$OSVersion = Check-OSVersion
If ( $GracePeriod -ne $null ) { #If GracePeriod is set
$UpdateTime = Check-LastUpdate $GracePeriod
If ($UpdateTime.IsOver -eq $true) { #If is outside of GP, check for updates and return
$Updates = Check-Updates
$Output = Create-Output $Updates
}
ElseIF ($UpdateTime.IsOver -eq $false) { #If within GP, return days since check with OK status
$Output = @{}
$Output.Output = "Within grace period."
$Output.ExitCode = 0
}
} #ends if GP is set
Else { # If no grace period has been set, check and return
$UpdateTime = Check-LastUpdate 0
$Updates = Check-Updates
$Output = Create-Output $Updates
}
$days = $UpdateTime.Days
$OutputString = $Output.Output
Write-Output "$days Days since last update. $OutputString"
Exit $Output.ExitCode
}
# Function to check OS Version and return string with 7 or XP depending. Returns [string]
Function Check-OSVersion {
$version = Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
switch ($($version.CurrentVersion).split(".")[0]){
6 { [string]$Return = "7" }
5 { [string]$Return = "XP" }
} ## End of Switch to check versioning
Return $Return
} ## End of Function
# Checks for updates using winupdate api, returns hashtable with all listed updates not including hidden ones. Returns [Array](KBImportance)KB and [Int](KBImportance)Number
Function Check-Updates {
$Return = @{}
[string]$Return.CriticalKB = ""
[Int]$Return.CriticalNumber = 0
[string]$Return.ImportantKB = ""
[Int]$Return.ImportantNumber = 0
[string]$Return.ModerateKB = ""
[Int]$Return.ModerateNumber = 0
[string]$Return.LowKB = ""
[Int]$Return.LowNumber = 0
[string]$Return.UnknownKB = ""
[Int]$Return.UnknownNumber = 0
$Updates = $( New-Object -ComObject Microsoft.Update.Session ).CreateUpdateSearcher().Search("IsAssigned=1 and IsHidden=0 and IsInstalled=0").Updates
$Updates | Where {$_.MsrcSeverity -eq "Critical" } | ForEach-Object { $_.KbArticleIDs } | Sort -Unique | ForEach-Object {
$Return.CriticalNumber++
$Return.CriticalKB += "KB"+$_+"\n"
}
$Updates | Where {$_.MsrcSeverity -eq "Important" } | ForEach-Object { $_.KbArticleIDs } | Sort -Unique | ForEach-Object {
$Return.ImportantNumber++
$Return.ImportantKB += "KB"+$_+"\n"
}
$Updates | Where {$_.MsrcSeverity -eq "Moderate" } | ForEach-Object { $_.KbArticleIDs } | Sort -Unique | ForEach-Object {
$Return.ModerateNumber++
$Return.ModerateKB += "KB"+$_+"\n"
}
$Updates | Where {$_.MsrcSeverity -eq "Low" } | ForEach-Object { $_.KbArticleIDs } | Sort -Unique | ForEach-Object {
$Return.LowNumber++
$Return.LowKB += "KB"+$_+"\n"
}
$Updates | Where-Object {!$_.MsrcSeverity} | ForEach-Object { $_.KbArticleIDs } | Sort -Unique | ForEach-Object {
$Return.UnknownNumber++
$Return.UnknownKB += "KB"+$_+"\n"
}
Return $Return
} # Ends Function
# Checks if last update installed was within Grace Period, Returns [Int]Days and [Boolean]IsOver
Function Check-LastUpdate {
Param([Parameter(Mandatory=$true)][Int]$GracePeriod)
$Return = @{}
#Gets DateTime Object with last update installed
if ($(Check-OSVersion) -eq "7") {
$WMIData = Get-WmiObject -Class Win32_QuickFixEngineering
}
Else { $WMIData = $null }
If ( $WMIData -eq $null ) { ## No data for installed on, run update check, might be issue with os version too
$Return.Days = 0
$Return.IsOver = $true
}
Else { ## has data and should be processed
[DateTime]$Date = $( $WMIData | Sort InstalledOn -Descending -Unique | Select InstalledOn -First 1 ).InstalledOn
$Return.Days = $( $(Get-Date) - $Date).Days
If ( $Return.Days -gt $GracePeriod ) { #if true has been longer than grace period
$Return.IsOver = $true
}
Else { #if within Grace Period
$Return.IsOver = $false
}
}
Return $Return
}
# Creates write-ouput text for returning data to nagios, Returns [int]ExitCode and [string]Output
Function Create-Output {
Param ( [Parameter(Mandatory=$true)]$Updates )
$Return = @{}
[Int]$Return.ExitCode = 3 # Sets to unknown by default
[String]$Return.Output = "Output creation failed, something is not working!"
If ( $Updates.CriticalNumber -gt 0 ) { # If any Critical updates, writes output line and sets exit code to 2(critical)
$Return.ExitCode = 2
$Return.Output = "`n"+$Updates.CriticalNumber+" Critical Updates:"
$Return.Output += "`n"+$Updates.CriticalKB.Replace("\n","`n")
If ($Updates.ImportantNumber -gt 0) {$Return.Output += ""+$Updates.ImportantNumber+" Important Updates:`n"
$Return.Output += $Updates.ImportantKB.Replace("\n","`n")
}
If ($Updates.ModerateNumber -gt 0) {$Return.Output += ""+$Updates.ModerateNumber+" Moderate Updates:`n"
$Return.Output += $Updates.ModerateKB.Replace("\n","`n")
}
If ($Updates.LowNumber -gt 0) {$Return.Output += ""+$Updates.LowNumber+" Low Updates:`n"
$Return.Output += $Updates.LowKB.Replace("\n","`n")
}
If ($Updates.UnknownNumber -gt 0) {$Return.Output += ""+$Updates.UnknownNumber+" Unknown Updates:`n"
$Return.Output += $Updates.UnknownKB.Replace("\n","`n")
}
} #Ends Critical If
ElseIf ( $Updates.ImportantNumber -gt 0 ) { # If any Important updates, writes output line and sets exit code to 2(critical)
$Return.ExitCode = 2
$Return.Output = "`n"+$Updates.ImportantNumber+" Important Updates:"
$Return.Output += "`n"+$Updates.ImportantKB.Replace("\n","`n")
If ($Updates.ModerateNumber -gt 0) {$Return.Output += ""+$Updates.ModerateNumber+" Moderate Updates:`n"
$Return.Output += $Updates.ModerateKB.Replace("\n","`n")
}
If ($Updates.LowNumber -gt 0) {$Return.Output += ""+$Updates.LowNumber+" Low Updates:`n"
$Return.Output += $Updates.LowKB.Replace("\n","`n")
}
If ($Updates.UnknownNumber -gt 0) {$Return.Output += ""+$Updates.UnknownNumber+" Unknown Updates:`n"
$Return.Output += $Updates.UnknownKB.Replace("\n","`n")
}
} #Ends Important If
ElseIf ( $Updates.ModerateNumber -gt 0 ) { # If any Moderate updates, writes output line and sets exit code to 1(Warning)
$Return.ExitCode = 1
$Return.Output = "`n"+$Updates.ModerateNumber+" Moderate Updates:"
$Return.Output += "`n"+$Updates.ModerateKB.Replace("\n","`n")
If ($Updates.LowNumber -gt 0) {$Return.Output += ""+$Updates.LowNumber+" Low Updates:`n"
$Return.Output += $Updates.LowKB.Replace("\n","`n")
}
If ($Updates.UnknownNumber -gt 0) {$Return.Output += ""+$Updates.UnknownNumber+" Unknown Updates:`n"
$Return.Output += $Updates.UnknownKB.Replace("\n","`n")
}
} #Ends Moderate If
ElseIf ( $Updates.LowNumber -gt 0 ) { # If any Low updates, writes output line and sets exit code to 1(Warning)
$Return.ExitCode = 1
$Return.Output = "`n"+$Updates.LowNumber+" Low Updates:"
$Return.Output += "`n"+$Updates.LowKB.Replace("\n","`n")
If ($Updates.UnknownNumber -gt 0) {$Return.Output += ""+$Updates.UnknownNumber+" Unknown Updates:`n"
$Return.Output += $Updates.UnknownKB.Replace("\n","`n")
}
} #Ends Low If
ElseIf ($Updates.UnknownNumber -gt 0) { # If number of unknown severity updates are available sets exit to 1(warning)
$Return.ExitCode = 1
$Return.Output = "`n"+$Updates.UnknownNumber+" Unknown Updates:"
$Return.Output += "`n"+$Updates.UnknownKB.Replace("\n","`n")
}
ElseIf ( ($Updates.CriticalNumber -eq 0) -and ($Updates.ImportantNumber -eq 0) -and ($Updates.ModerateNumber -eq 0) -and ($Updates.LowNumber -eq 0) -and ($Updates.UnknownNumber -eq 0) ) { #If no updates, writes output and sets exit 0(OK)
$Return.ExitCode = 0
$Return.Output = "There are no updates to be done."
}
Return $Return
}
Check-NewUpdates $GracePeriod