PowerShell script

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
jkinning
Posts: 747
Joined: Wed Oct 09, 2013 2:54 pm

PowerShell script

Post 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
Last edited by jkinning on Fri Apr 07, 2017 12:58 pm, edited 1 time in total.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: PowerShell script

Post 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
Former Nagios employee
https://www.mcapra.com/
jkinning
Posts: 747
Joined: Wed Oct 09, 2013 2:54 pm

Re: PowerShell script

Post 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.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: PowerShell script

Post 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")
}
Former Nagios employee
https://www.mcapra.com/
jkinning
Posts: 747
Joined: Wed Oct 09, 2013 2:54 pm

Re: PowerShell script

Post 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
SteveBeauchemin
Posts: 524
Joined: Mon Oct 14, 2013 7:19 pm

Re: PowerShell script

Post by SteveBeauchemin »

I am sure this is not an issue, but gotta ask...

As the last line of your powershell script do you

Code: Select all

exit $exitCode
Just checking...

Steve B
XI 5.7.3 / Core 4.4.6 / NagVis 1.9.8 / LiveStatus 1.5.0p11 / RRDCached 1.7.0 / Redis 3.2.8 /
SNMPTT / Gearman 0.33-7 / Mod_Gearman 3.0.7 / NLS 2.0.8 / NNA 2.3.1 /
NSClient 0.5.0 / NRPE Solaris 3.2.1 Linux 3.2.1 HPUX 3.2.1
jkinning
Posts: 747
Joined: Wed Oct 09, 2013 2:54 pm

Re: PowerShell script

Post 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.
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

Re: PowerShell script

Post 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 -
Former Nagios employee
https://www.mcapra.com/
jkinning
Posts: 747
Joined: Wed Oct 09, 2013 2:54 pm

Re: PowerShell script

Post by jkinning »

Gotcha! Thanks for the clarification.

I'll give it a go and let you know.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: PowerShell script

Post 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
Former Nagios employee
Creator:
Human Design Website
Get Your Human Design Chart
Locked