Page 1 of 2
PowerShell script
Posted: Wed Apr 05, 2017 2:22 pm
by jkinning
I have a powershell script that I am trying to get Nagios to use for monitoring a csv log file. When I try to run it I am not getting what I believe I should but if I run the script locally it works. I am obviously missing something but not sure what I need to change or add to my nsclient.ini file.
It takes two arguments – a filename and the number of minutes to look back in the csv log.
The response codes are the normal Nagios client responses:
• 0 = OK
• 2 = Critical
• 3 = Unknown, File Not Found
We need an alert any time it responds with 2 or if there are two consecutive runs with a response of 3.
Both are attached.
Code: Select all
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv 5'
No output available from command (cmd /c echo scripts/powershell/csv_monitor.ps1 $ARG1$; exit $LastExitCode |'powershell.exe'=0 '/noprofile'=0 '-command'=0 '-).'=0
./check_nrpe -H <host> -c csv_monitor -a D:\Websites\ClicPublic\App_Data\logs\logfile.csv 5
File Not Found
./check_nrpe -H <host> -c csv_monitor -a D:\Websites\ClicPublic\App_Data\logs\logfile.csv
File Not Found
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv'
No output available from command (cmd /c echo scripts/powershell/csv_monitor.ps1 $ARG1$; exit $LastExitCode |'powershell.exe'=0 '/noprofile'=0 '-command'=0 '-).'=0
Re: PowerShell script
Posted: Wed Apr 05, 2017 4:22 pm
by mcapra
Back-slashes are not a friend of Linux. You'll probably need to escape them in your command definition. See if this works from the CLI:
Code: Select all
./check_nrpe -H <host> -c csv_monitor -a D:\\Websites\\ClicPublic\\App_Data\\logs\\logfile.csv
Re: PowerShell script
Posted: Thu Apr 06, 2017 8:57 am
by jkinning
Ok, I think things are getting closer.
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\\ClicPublic\\App_Data\\logs\\logfile.csv 20'
Getting coords for zip code: 41018 Finished getting all agents in range by zip.
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\\ClicPublic\\App_Data\\logs\\logfile.csv 1'
No output available from command (cmd /c echo scripts/powershell/csv_monitor.ps1 $ARG1$; exit $LastExitCode |'powershell.exe'=0 '/noprofile'=0 '-command'=0 '-).'=0
The first line is what we are wanting to be CRITICAL with that output. So I get the output but Nagios doesn't see it as a critical message, normally I see CRITICAL:
The other line is an OK but I can't figure out how to just have it say OK and not give me all the other output.
Re: PowerShell script
Posted: Thu Apr 06, 2017 2:09 pm
by mcapra
Looking over the Powershell, it seems like there may be a case where the file is found to exist but there are no errors found in it which would result in no calls to
Write-Host, which is why NRPE says "No output available".
You might try something like this:
Code: Select all
If ($errors.Count -ge 1) {
#if there are any records in the response array, return Critical
$exitCode = $CRITICAL
Write-Host($errors)
}
else {
Write-Host("No errors found")
}
Re: PowerShell script
Posted: Fri Apr 07, 2017 8:20 am
by jkinning
That did help but I am not sure why Nagios isn't picking up the status codes. When I run it from Nagios I am not seeing the OK: or CRITICAL: for Nagios to know to send out notifications.
Code: Select all
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv 20'
Getting all agents in range by zip code: zipCode=41018, maximumAgents=10 Finished getting all agents in range by zip.
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv 5'
No errors found
I am thinking I should see something more like this
Code: Select all
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv 20'
CRITICAL:Getting all agents in range by zip code: zipCode=41018, maximumAgents=10 Finished getting all agents in range by zip.
./check_nrpe -H <host> -c csv_monitor -a 'D:\Websites\ClicPublic\App_Data\logs\logfile.csv 5'
OK:No errors found
Re: PowerShell script
Posted: Fri Apr 07, 2017 10:57 am
by SteveBeauchemin
I am sure this is not an issue, but gotta ask...
As the last line of your powershell script do you
Just checking...
Steve B
Re: PowerShell script
Posted: Fri Apr 07, 2017 11:41 am
by jkinning
Yes, and the script is attached to the first post.
Code: Select all
#close with response code
exit $exitCode
Not understanding why this isn't working.
Re: PowerShell script
Posted: Fri Apr 07, 2017 1:33 pm
by mcapra
OK/CRITICAL/et. al won't get appended to the status output unless you explicitly do that in the script's output (via stdout or the Windows equivalent) itself. The exit code will still be preserved though, and that's ultimately what Nagios XI cares about in terms of alerting. The status output is simply for the user's benefit.
Code: Select all
[root@xi-stable tmp]# /usr/local/nagios/libexec/check_nrpe -H 192.168.67.99 -c test -a 0
This is an ok.
[root@xi-stable tmp]# echo $?
0
[root@xi-stable tmp]# /usr/local/nagios/libexec/check_nrpe -H 192.168.67.99 -c test -a 1
This is a warning.
[root@xi-stable tmp]# echo $?
1
[root@xi-stable tmp]# /usr/local/nagios/libexec/check_nrpe -H 192.168.67.99 -c test -a 2
This is a critical.
[root@xi-stable tmp]# echo $?
2
My sample script:
Code: Select all
if ($args[0] -eq 0) {
Write-Host "This is an ok."
exit 0
}
if ($args[0] -eq 1) {
Write-Host "This is a warning."
exit 1
}
if ($args[0] -eq 2) {
Write-Host "This is a critical."
exit 2
}
And my NSClient++ entry for it:
Code: Select all
test=cmd /c echo scripts\check_test.ps1 $ARG1$; exit $LastExitCode | powershell.exe -noprofile -nologo -command -
Re: PowerShell script
Posted: Fri Apr 07, 2017 2:09 pm
by jkinning
Gotcha! Thanks for the clarification.
I'll give it a go and let you know.
Re: PowerShell script
Posted: Fri Apr 07, 2017 2:12 pm
by scottwilkerson
jkinning wrote:Gotcha! Thanks for the clarification.
I'll give it a go and let you know.
Thanks looking forward to hearing how it goes