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

run script, email results

Post by JohnFLi »

I have a powershell script that returns the top 10 memory process items.

I am looking for away that when the regular memory check goes critical, Nagios will then run the powershell, and email the results of that to the people that gets notified on the alert.

I have no idea how to do this.
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 »

The typical way to do that is to wrap it so that your powershell output is the status output of the service. Besides that the notifications in NagiosXI are just commands like anything else.

What commands do your contacts use for their notifications? Depending on that you can copy/adjust the preexisting notification commands to suit your needs

Configure --> Core Configuration Manager --> Commands --> Commands

Search for "notif"
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

ok so how do I 'wrap' the memory check script into the regular memory check?
Unless it has research critical, I dont want to always be looking at the output.

basicly, I am looking for a way to get it so that IF critical, then run script and email the results.

currently for notifications, I use the generic 'notify service by email'
Everybody is somebody else’s weirdo
User avatar
Box293
Too Basu
Posts: 5126
Joined: Sun Feb 07, 2010 10:55 pm
Location: Deniliquin, Australia
Contact:

Re: run script, email results

Post by Box293 »

JohnFLi wrote:when the regular memory check goes critical,
What is the command you execute for the regular memory check? From your nagios server.
As of May 25th, 2018, all communications with Nagios Enterprises and its employees are covered under our new Privacy Policy.
jdalrymple
Skynet Drone
Posts: 2620
Joined: Wed Feb 11, 2015 1:56 pm

Re: run script, email results

Post by jdalrymple »

JohnFLi wrote:ok so how do I 'wrap' the memory check script into the regular memory check?
Unless it has research critical, I dont want to always be looking at the output.

basicly, I am looking for a way to get it so that IF critical, then run script and email the results.

currently for notifications, I use the generic 'notify service by email'
It's pretty common for your check command to *not* return output unless there is a problem. That is a matter of how you write your check command though.

A well-known example:

Code: Select all

[root@localhost libexec]# ./check_nrpe -H <winhost> -c check_drivesize -a "warning=used > 99%"
OK All 4 drive(s) are ok|'C:\ used'=64.71418GB;106.39502;96.72275;0;107.46972 'C:\ used %'=60%;98;89;0;100 'D:\ used'=122.36956GB;922.19563;838.35966;0;931.51073 'D:\ used %'=13%;98;89;0;100 'E:\ used'=0B;0;0;0;0 '\\?\Volume{80ee73c6-a6c0-11e4-bd5c-806e6f6e6963}\ used'=8.10441GB;11.61509;10.55917;0;11.73241 '\\?\Volume{80ee73c6-a6c0-11e4-bd5c-806e6f6e6963}\ used %'=69%;98;89;0;100
[root@localhost libexec]# ./check_nrpe -H <winhost> -c check_drivesize -a "warning=used > 50%"
WARNING C:\: 64.714GB/107.47GB used, RECOVERY: 8.104GB/11.732GB used|'C:\ used'=64.71418GB;53.73486;96.72275;0;107.46972 'C:\ used %'=60%;50;89;0;100 'D:\ used'=122.36956GB;465.75536;838.35966;0;931.51073 'D:\ used %'=13%;50;89;0;100 'E:\ used'=0B;0;0;0;0 '\\?\Volume{80ee73c6-a6c0-11e4-bd5c-806e6f6e6963}\ used'=8.10441GB;5.8662;10.55917;0;11.73241 '\\?\Volume{80ee73c6-a6c0-11e4-bd5c-806e6f6e6963}\ used %'=69%;50;89;0;100
Note that unless there is a problem the output is vague. If there is a problem the output is detailed. This is somewhat what you want except that you want it specifically for critical conditions. Either way this could be done upstairs in the actual check command or downstairs in your notification command using logic:

Code: Select all

#!/bin/bash

if [ $NAGIOS_SERVICESTATEID -eq "2" ]; then
   /usr/local/nagios/libexec/check_nrpe -H $NAGIOS_HOSTADDRESS -c runmypowershell | /bin/mail -s "** $NAGIOS_NOTIFICATIONTYPE Service Alert: $NAGIOS_HOSTALIAS/$NAGIOS_SERVICEDESC is $NAGIOS_SERVICESTATE **" $NAGIOS_CONTACTEMAIL
else
   /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NAGIOS_NOTIFICATIONTYPE\n\nService: $NAGIOS_SERVICEDESC\nHost: $NAGIOS_HOSTALIAS\nAddress: $NAGIOS_HOSTADDRESS\nState: $NAGIOS_SERVICESTATE\n\nDate/Time: $NAGIOS_LONGDATETIME\n\nAdditional Info:\n\n$NAGIOS_SERVICEOUTPUT" | /bin/mail -s "** $NAGIOS_NOTIFICATIONTYPE Service Alert: $NAGIOS_HOSTALIAS/$NAGIOS_SERVICEDESC is $NAGIOS_SERVICESTATE **" $NAGIOS_CONTACTEMAIL
fi
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

I think I somewhat understand.

But where you have '-c phowershell' how does that know what to run on again what machine?
Everybody is somebody else’s weirdo
User avatar
lmiltchev
Bugs find me
Posts: 13589
Joined: Mon May 23, 2011 12:15 pm

Re: run script, email results

Post by lmiltchev »

Your command must be defined on the client. You may have something similar in the nsclient.ini (under the [/settings/external scripts/scripts] section):

Code: Select all

runmypowershell = cmd /c echo scripts\<custom script>.ps1; exit $LastExitCode | powershell.exe -command -
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 »

ok, gotcha.....

and now for a little bit more spoon feeding.....

Do I just take that bash file and make it another notify-service-by-email command?
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:Do I just take that bash file and make it another notify-service-by-email command?
Yes - kind of. Things get a little bit weird, and this is why you'd do it "upstairs" typically and not "downstairs"

Notify commands are assigned to contacts. So unless you want this script to be burdensomely large (testing for the service itself that's being run in addition to the service status) you'll have to create a custom contact just for this notify command. Otherwise anytime ANY service goes critical any contact using this command will get the output of the hosts's powershell script as the E-mail body. This isn't likely to work if you have 1 contact receiving notifications for both a Cisco switch and your Windows box.

Making sense?

-- edit --

BTW, in order for this to work at all you'll have to adjust nagios.cfg:

Code: Select all

enable_environment_macros=1
User avatar
JohnFLi
Posts: 559
Joined: Mon Jun 17, 2013 3:11 pm

Re: run script, email results

Post by JohnFLi »

I was thinking that by making a new 'notify' command, I would just assign it to the specific services I want it on. That way it should still contact those that are already assigned.

I will give it a shot
Everybody is somebody else’s weirdo
Locked