run script, email results

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

wouldn't something like this is what I would use the 'event handler' for?
Everybody is somebody else’s weirdo
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: run script, email results

Post by jdalrymple »

JohnFLi wrote:I would just assign it to the specific services I want it on.
http://nagios.sourceforge.net/docs/nagi ... ml#service

You can't assign a service_notification_commands to a service, hence my mention of adding logic to your notify command to identify the service. Your absolute best bet is to do it all in the plugin. Anything else will be hacky unless you have some contacts that only monitor Windows Memory.
JohnFLi wrote:wouldn't something like this is what I would use the 'event handler' for?
You could go that route...

Event handlers aren't intended to provide notifications. Nagios provides "notifications" to provide people information about a service, such as the top 10 processes using memory or something like that. The intention of an "event handler" is to handle an event. It is modular, you can do what you want.

To achieve your goal using an event handler, you could use your bash script in the context of a notification then just disable notifications. Be aware of when event handlers are executed before making your decision:
When Are Event Handlers Executed?

Event handlers are executed when a service or host:

Is in a SOFT problem state
Initially goes into a HARD problem state
Initially recovers from a SOFT or HARD problem state
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

lol "such as the top 10 processes using memory" that is exactly what I am trying to do, BUT only want it to email that info if the memory starts getting close to max'd

I made my powershell script also write the output to a file (mainly to verify that the event handler is running it) and it is.


Your absolute best bet is to do it all in the plugin
ahhhhhh...make powershell send the email itself? Thought about that yesterday.

Couldn't get powershell to put out (story of my life :roll: ) lol
Everybody is somebody else’s weirdo
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: run script, email results

Post by jdalrymple »

JohnFLi wrote:I made my powershell script also write the output to a file (mainly to verify that the event handler is running it) and it is.
...
JohnFLi wrote:ahhhhhh...make powershell send the email itself? Thought about that yesterday.
no:

Code: Select all

if ($MemUsed -gt $Critical) { Write-Output $top10MemUsers }
elseif ($MemUsed -gt $Warning) { Write-Output "Things are bad, but not critical" }
else { Write-Output "Things look great" }
Nagios will be happy to handle the email component for you, that's what notifications are for. You just have to stuff into it what you want to be notified with.
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

And where would I stuff that into?



I was working on having powershell send the email. Running the script on the windos box, works just fine.
But from the cli on the nagios box it gives me this (and for some reason, the email doesn't go, even though the file is still created):

Code: Select all

 -from "Nagios <nagio
[email protected]>" -subject "Top 10 Memory users  (testing)" -body "Read Attachemnt" -Attachments "c:\scripts\top20memoryprocess" -smtpserver smtpserver
    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], FileNotFou
   ndException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerS
   hell.Commands.SendMailMessage
[root@G1VPNAG03 libexec]#
Everybody is somebody else’s weirdo
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: run script, email results

Post by jdalrymple »

JohnFLi wrote:And where would I stuff that into?
Into the plugin itself. I'm assuming at this point that you actually have a plugin checking the memory and your Powershell script is actually something completely dispatched from that plugin? What plugin are you using to check your Windows Memory?
JohnFli wrote:I was working on having powershell send the email. Running the script on the windos box, works just fine.
But from the cli on the nagios box it gives me this (and for some reason, the email doesn't go, even though the file is still created):

Code: Select all

 -from "Nagios <nagio
[email protected]>" -subject "Top 10 Memory users  (testing)" -body "Read Attachemnt" -Attachments "c:\scripts\top20memoryprocess" -smtpserver smtpserver
    + CategoryInfo          : NotSpecified: (:) [Send-MailMessage], FileNotFou
   ndException
    + FullyQualifiedErrorId : System.IO.FileNotFoundException,Microsoft.PowerS
   hell.Commands.SendMailMessage
[root@G1VPNAG03 libexec]#
You cannot interpret powershell scripts on Linux. It just doesn't work. The script needs to be run on the Windows box.
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: run script, email results

Post by tgriep »

You cannot run the powershell script on the Nagios server, you will have to tell the windows system to run the powershell script and an event handler can do that for you.
Here are a couple of links that talk about event handlers. The second link would closely pertain to what you are trying to do.
https://assets.nagios.com/downloads/nag ... ios-XI.pdf
https://assets.nagios.com/downloads/nag ... h_NRPE.pdf
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

i am using the generic mem check:

Code: Select all

$USER1$/check_nt -H $HOSTADDRESS$ -s "$ARG1$" -p 12489 -v $ARG2$   ( where arg2 is MEM)
You cannot interpret powershell scripts on Linux. It just doesn't work. The script needs to be run on the Windows box.
But the powershell IS on the windows box.
The powershell does create a text file and powershell is also suppose to send it via email but for some reason the nrpe command that tells the powershell to run, is also trying to get ahold of that text file.
Everybody is somebody else’s weirdo
User avatar
tgriep
Madmin
Posts: 9190
Joined: Thu Oct 30, 2014 9:02 am

Re: run script, email results

Post by tgriep »

Can you post how you are running the nrpe command from the Nagios system?
Can you upload the nsclient.ini file from your windows system to this post so we can review it?
Be sure to check out our Knowledgebase for helpful articles and solutions!
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

The command (Using drive space to fire it off as its easy to change settings to make it happen)

Code: Select all

define service {
	host_name			G1VPDOCAVE01
	service_description		Drive C: Disk Usage
	use				xiwizard_windowsserver_nsclient_service
	check_command			check_xi_service_nsclient!!USEDDISKSPACE!-l C -w 90 -c 95!!!!!
	max_check_attempts		6
	check_interval			1
	retry_interval			1
	check_period			xi_timeperiod_24x7
	event_handler			check_top20_memory
	event_handler_enabled		1
	notification_interval		5
	notification_period		Work hours
	notification_options		w,c,r,
	contacts			Me
	_xiwizard			windowsserver
	register			1
	}	

the command:

Code: Select all

define command {
       command_name                  		check_top20_memory
       command_line                  		$USER1$/check_nrpe -H $HOSTADDRESS$ -c check_top20memory
}
NSCLinet.ini

Code: Select all

# If you want to fill this file with all avalible options run the following command:
#   nscp settings --generate --add-defaults --load-all
# If you want to activate a module and bring in all its options use:
#   nscp settings --activate-module <MODULE NAME> --add-defaults
# For details run: nscp settings --help


; Undocumented section
[/modules]

; CheckDisk - CheckDisk can check various file and disk related things. The current version has commands to check Size of hard drives and directories.
CheckDisk = 1

; Event log Checker. - Check for errors and warnings in the event log. This is only supported through NRPE so if you plan to use only NSClient this wont help you at all.
CheckEventLog = 1

; Check External Scripts - A simple wrapper to run external scripts and batch files.
CheckExternalScripts = 1

; Helper function - Various helper function to extend other checks. This is also only supported through NRPE.
CheckHelpers = 1

; Check NSCP - Checkes the state of the agent
CheckNSCP = 1

; CheckSystem - Various system related checks, such as CPU load, process state, service state memory usage and PDH counters.
CheckSystem = 1

; CheckWMI - CheckWMI can check various file and disk related things. The current version has commands to check Size of hard drives and directories.
CheckWMI = 1

; NRPE server - A simple server that listens for incoming NRPE connection and handles them.
NRPEServer = 1

; NSClient server - A simple server that listens for incoming NSClient (check_nt) connection and handles them. Although NRPE is the preferred method NSClient is fully supported and can be used for simplicity or for compatibility.
NSClientServer = 1

NSCAClient = 1
NSClientServer = 1

[/settings/NRPE/server]

; COMMAND ARGUMENT PROCESSING - This option determines whether or not the we will allow clients to specify arguments to commands that are executed.
allow arguments = true

; COMMAND ALLOW NASTY META CHARS - This option determines whether or not the we will allow clients to specify nasty (as in |`&><'"\[]{}) characters in arguments.
allow nasty characters = true

; Undocumented section
[/settings/default]

; ALLOWED HOSTS - A comaseparated list of allowed hosts. You can use netmasks (/ syntax) or * to create ranges.
allowed hosts = g1vpnag01,g1vpnag03,10.100.3.133


; A list of aliases available. An alias is an internal command that has been "wrapped" (to add arguments). Be careful so you don't create loops (ie check_loop=check_a, check_a=check_loop)
[/settings/external scripts/alias]

; alias_cpu - Alias for alias_cpu. To configure this item add a section called: /settings/external scripts/alias/alias_cpu
alias_cpu = checkCPU warn=80 crit=90 time=5m time=1m time=30s

; alias_cpu_ex - Alias for alias_cpu_ex. To configure this item add a section called: /settings/external scripts/alias/alias_cpu_ex
alias_cpu_ex = checkCPU warn=$ARG1$ crit=$ARG2$ time=5m time=1m time=30s

; alias_disk - Alias for alias_disk. To configure this item add a section called: /settings/external scripts/alias/alias_disk
alias_disk = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED

; alias_disk_loose - Alias for alias_disk_loose. To configure this item add a section called: /settings/external scripts/alias/alias_disk_loose
alias_disk_loose = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll FilterType=FIXED ignore-unreadable

; alias_event_log - Alias for alias_event_log. To configure this item add a section called: /settings/external scripts/alias/alias_event_log
alias_event_log = CheckEventLog file=application file=system MaxWarn=1 MaxCrit=1 "filter=generated gt -2d AND severity NOT IN ('success', 'informational') AND source != 'SideBySide'" truncate=800 unique descriptions "syntax=%severity%: %source%: %message% (%count%)"

; alias_file_age - Alias for alias_file_age. To configure this item add a section called: /settings/external scripts/alias/alias_file_age
alias_file_age = checkFile2 filter=out "file=$ARG1$" filter-written=>1d MaxWarn=1 MaxCrit=1 "syntax=%filename% %write%"

; alias_file_size - Alias for alias_file_size. To configure this item add a section called: /settings/external scripts/alias/alias_file_size
alias_file_size = CheckFiles "filter=size > $ARG2$" "path=$ARG1$" MaxWarn=1 MaxCrit=1 "syntax=%filename% %size%" max-dir-depth=10

; alias_mem - Alias for alias_mem. To configure this item add a section called: /settings/external scripts/alias/alias_mem
alias_mem = checkMem MaxWarn=80% MaxCrit=90% ShowAll=long type=physical type=virtual type=paged type=page

; alias_process - Alias for alias_process. To configure this item add a section called: /settings/external scripts/alias/alias_process
alias_process = checkProcState "$ARG1$=started"

; alias_process_count - Alias for alias_process_count. To configure this item add a section called: /settings/external scripts/alias/alias_process_count
alias_process_count = checkProcState MaxWarnCount=$ARG2$ MaxCritCount=$ARG3$ "$ARG1$=started"

; alias_process_hung - Alias for alias_process_hung. To configure this item add a section called: /settings/external scripts/alias/alias_process_hung
alias_process_hung = checkProcState MaxWarnCount=1 MaxCritCount=1 "$ARG1$=hung"

; alias_process_stopped - Alias for alias_process_stopped. To configure this item add a section called: /settings/external scripts/alias/alias_process_stopped
alias_process_stopped = checkProcState "$ARG1$=stopped"

; alias_sched_all - Alias for alias_sched_all. To configure this item add a section called: /settings/external scripts/alias/alias_sched_all
alias_sched_all = CheckTaskSched "filter=exit_code ne 0" "syntax=%title%: %exit_code%" warn=>0

; alias_sched_long - Alias for alias_sched_long. To configure this item add a section called: /settings/external scripts/alias/alias_sched_long
alias_sched_long = CheckTaskSched "filter=status = 'running' AND most_recent_run_time < -$ARG1$" "syntax=%title% (%most_recent_run_time%)" warn=>0

; alias_sched_task - Alias for alias_sched_task. To configure this item add a section called: /settings/external scripts/alias/alias_sched_task
alias_sched_task = CheckTaskSched "filter=title eq '$ARG1$' AND exit_code ne 0" "syntax=%title% (%most_recent_run_time%)" warn=>0

; alias_service - Alias for alias_service. To configure this item add a section called: /settings/external scripts/alias/alias_service
alias_service = checkServiceState CheckAll

; alias_service_ex - Alias for alias_service_ex. To configure this item add a section called: /settings/external scripts/alias/alias_service_ex
alias_service_ex = checkServiceState CheckAll "exclude=Net Driver HPZ12" "exclude=Pml Driver HPZ12" exclude=stisvc

; alias_up - Alias for alias_up. To configure this item add a section called: /settings/external scripts/alias/alias_up
alias_up = checkUpTime MinWarn=1d MinWarn=1h

; alias_updates - Alias for alias_updates. To configure this item add a section called: /settings/external scripts/alias/alias_updates
alias_updates = check_updates -warning 0 -critical 0

; alias_volumes - Alias for alias_volumes. To configure this item add a section called: /settings/external scripts/alias/alias_volumes
alias_volumes = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll=volumes FilterType=FIXED

; alias_volumes_loose - Alias for alias_volumes_loose. To configure this item add a section called: /settings/external scripts/alias/alias_volumes_loose
alias_volumes_loose = CheckDriveSize MinWarn=10% MinCrit=5% CheckAll=volumes FilterType=FIXED ignore-unreadable 

; default - Alias for default. To configure this item add a section called: /settings/external scripts/alias/default
default = 
[/settings/external scripts/scripts]
windows_updates=cmd /c echo C:\scripts\check_win_updates.ps1; exit $LastExitCode | powershell.exe -command -
check_top20memory=cmd /c echo C:\scripts\top20memoryprocess.ps1; exit $LastExitCode | powershell.exe -command -

When using the CLI for it (including the result):

Code: Select all

[root@G1VPNAG03 libexec]# ./check_nrpe -H g1vpdocave01 -c check_top20memory


Send-MailMessage : Mailbox unavailable. The server response was: 5.7.1 Client d
oes not have permissions to send as this sender
At C:\scripts\top20memoryprocess.ps1:3 char:17
+ send-mailmessage <<<<  -to "John <[email protected]>" -from "Nagios <nagio
[email protected]>" -subject "Top 20 Memory users  (testing)" -body "$top20memoryUs
ers" -Attachments "c:\scripts\top20memoryprocess.txt" -smtpserver exchange.fire
mountain.local
    + CategoryInfo          : InvalidOperation: (System.Net.Mail.SmtpClient:Sm
   tpClient) [Send-MailMessage], SmtpException
    + FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.Send
   MailMessage

@{ProcessName=w3wp; Id=4560; WS=422141952} @{ProcessName=ControlTimerService; Id=1304; WS=341671936} @{ProcessName=MediaService; Id=1160; WS=284434432} @{ProcessName=ReportService; Id=2560; WS=266752000} @{ProcessName=powershell_ise; Id=4340; WS=128917504} @{ProcessName=powershell_ise; Id=5288; WS=123617280} @{ProcessName=svchost; Id=840; WS=88158208} @{P
the powershell

Code: Select all

$top20memoryUsers = Get-Process | Sort WS -Descending | Select ProcessName, Id, WS | select -first 20
$top20memoryUsers | out-file c:\scripts\top20memoryprocess.txt

send-mailmessage -to "John <[email protected]>" -from "Nagios <[email protected]>" -subject "Top 20 Memory users  (testing)" -body "$top20memoryUsers" -Attachments "c:\scripts\top20memoryprocess.txt" -smtpserver exchange.mydomain.local
Write-Host $top20memoryUsers
Everybody is somebody else’s weirdo
Locked