Powershell & NRDP
Posted: Wed Nov 23, 2016 9:11 am
Hi guys,
I've been working on a Powershell script to monitor a SAP job that is being started by a task scheduler (Starting an important job this way is not my prefered way of doing things, I'm just the monitoring guy in this case ^^). So I wrote the below script which seems to be working just fine, however I stupidly made the mistake of adding the Nagios exits in the script as well. That would only work if Nagios were to one to start the script which is something we absolutely do not want.
So my question: Is there anyone with experience with pushing the exit codes from a PowerShell script to Nagios (with NRDP?)? So Nagios will not touch this script at all.
I've been working on a Powershell script to monitor a SAP job that is being started by a task scheduler (Starting an important job this way is not my prefered way of doing things, I'm just the monitoring guy in this case ^^). So I wrote the below script which seems to be working just fine, however I stupidly made the mistake of adding the Nagios exits in the script as well. That would only work if Nagios were to one to start the script which is something we absolutely do not want.
So my question: Is there anyone with experience with pushing the exit codes from a PowerShell script to Nagios (with NRDP?)? So Nagios will not touch this script at all.
Code: Select all
#Execute commands and capture output
$File1 = "C:\temp\sap\connection.txt"
$File2 = "J:\usr\sap\inbound\processing\proces.txt"
$File3 = "C:\temp\sap\dgl.txt"
$DirectoryInfo = Get-ChildItem -force J:\usr\sap\inbound\ | Measure-Object
$DirectoryInfo.count
if($DirectoryInfo.count -eq 0)
{
if(Test-Path $File1)
{
if(Get-ChildItem -Path C:\temp\sap\ -File connection.txt | where {$_.Lastwritetime -lt (Get-Date).AddHours(-1)})
{
$ErrorMessage = "No Inbound DGL files have been received in the past hour"
Write-Host $ErrorMessage
Exit $ReturnStateCritical
}
}
else
{
New-Item C:\temp\sap\connection.txt -type file
}
}
else
{
Remove-Item C:\temp\sap\connection.txt -Recurse
Move-Item J:\usr\sap\inbound\*.* J:\usr\sap\inbound\processing\
Copy-Item J:\usr\sap\inbound\processing\*.* J:\usr\sap\inbound\processing\proces.txt
if(Test-Path $File2)
{
"C:\Program Files (x86)\SAP\FrontEnd\SAPgui\nwrfcsdk\bin\startrfc.exe" -3 -u **** -p **** -c 500 -l D -h **** -s 00 -E PATHNAME=J:\usr\sap\inbound\processing\proces.txt -E PORT=DGL_CAL_WM -F EDI_DATA_INCOMING > "C:\temp\sap\dgl.txt"
if(Test-Path $File3)
{
if($File3 contains "")
{
$OKMessage = "OK: DGL job last run status contains no errors"
Write-Host $OKMessage
$RemoveOld = (Get-Date).AddDays(-7)
Get-ChildItem -Path "C:\temp\sap" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $RemoveOld } | Remove-Item -Force
exit $ReturnStateOK
}
else
{
$ErrorMessage = "Critical: an error has been found processing the DGL job. Please check SAP"
Write-Host $ErrorMessage
Rename-Item "C:\temp\sap\dgl.txt" -NewName "DGL error $(Get-Date -f dd-MM-yyyy_HH_mm).txt"
$RemoveOld = (Get-Date).AddDays(-7)
Get-ChildItem -Path "C:\temp\sap" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $RemoveOld } | Remove-Item -Force
exit $ReturnStateCritical
}
}
else
{
$UnknownMessage = "DGL Inbound status is unknown. Please check if the process is working as expected."
Write-Host $UnknownMessage
$RemoveOld = (Get-Date).AddDays(-7)
Get-ChildItem -Path "C:\temp\sap" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $RemoveOld } | Remove-Item -Force
exit $ReturnStateUnknown
}
}
else
{
$ErrorMessage = "ERROR: Data was copied from J:\usr\sap\inbound\ to processing but proces.txt was not found!"
Write-Host $ErrorMessage
$RemoveOld = (Get-Date).AddDays(-7)
Get-ChildItem -Path "C:\temp\sap" -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.CreationTime -lt $RemoveOld } | Remove-Item -Force
exit $ReturnStateCritical
}
}