How to shutdown a host from Nagios Xi

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
Johnjones
Posts: 16
Joined: Wed May 04, 2016 7:24 pm

How to shutdown a host from Nagios Xi

Post by Johnjones »

I have a UPS it sends SNMP TRAPS to nagios . If the UPS battery status is critical.I want to initiate a graceful shutdown of the host connected to the UPS from Nagios XI. Can anyone please help me with this?
Last edited by Johnjones on Tue May 10, 2016 3:25 pm, edited 1 time in total.
User avatar
hsmith
Agent Smith
Posts: 3539
Joined: Thu Jul 30, 2015 11:09 am
Location: 127.0.0.1
Contact:

Re: How to shutdown a host from Nagios Xi

Post by hsmith »

Event Handlers are going to be your best friend in making this work. Read up, and let us know what you need help with.
Former Nagios Employee.
me.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: How to shutdown a host from Nagios Xi

Post by rkennedy »

You will need a remote agent installed such as NRPE, or NSClient++ installed on the machine you're looking to shut down. It will then need a command defined, that references a bash or windows script to shut down the system.

Then, you'll want to use event handlers which will trigger every time a state changes (OK->CRITICAL,CRITICAL->WARNING). Have this event handler script detect if it's critical, and if it is, then send the command over NRPE to shut it down.

This post has quite a bit of useful information - https://support.nagios.com/forum/viewto ... 999#bottom
Former Nagios Employee
Johnjones
Posts: 16
Joined: Wed May 04, 2016 7:24 pm

Re: How to shutdown a host from Nagios Xi

Post by Johnjones »

rkennedy wrote:You will need a remote agent installed such as NRPE, or NSClient++ installed on the machine you're looking to shut down. It will then need a command defined, that references a bash or windows script to shut down the system.

Then, you'll want to use event handlers which will trigger every time a state changes (OK->CRITICAL,CRITICAL->WARNING). Have this event handler script detect if it's critical, and if it is, then send the command over NRPE to shut it down.

This post has quite a bit of useful information - https://support.nagios.com/forum/viewto ... 999#bottom

Thank you very much it really helped. Can you tell if what I am doing is right. I added a shutdown script to the client server and then added a shutdown command to nrpe.cfg referring to that script. This is the shutdown script host_shutdown.

Code: Select all

#host_shutdown
!#/bin/bash
logger  "shutting down the server due to Low UPS battery at `date`" 
sudo shutdown -h now 
Then I added this event handling script to the nagios server.

Code: Select all

#!/bin/bash

SERVICESTATE=$1 # UPS state
HOSTADDRESS=$2 # host address which is connected to the UPS

if [ "$SERVICESTATE" == "CRITICAL" ] ; then
      /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c host_shutdown
    fi
exit 0
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: How to shutdown a host from Nagios Xi

Post by rkennedy »

It looks right, from the Nagios machine can you run /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c host_shutdown (replace $HOSTADDRESS$ with the IP of the machine that you setup the machine that you're looking to restart.)

Does that work for you?

One thing I didn't see mentioned, did you define the command for the event handler, and assign it to the UPS service?
Former Nagios Employee
Johnjones
Posts: 16
Joined: Wed May 04, 2016 7:24 pm

Re: How to shutdown a host from Nagios Xi

Post by Johnjones »

rkennedy wrote:It looks right, from the Nagios machine can you run /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS -c host_shutdown (replace $HOSTADDRESS$ with the IP of the machine that you setup the machine that you're looking to restart.)

Does that work for you?
i tried running it manually but the output is this

Code: Select all

[root@localhost ~]# /usr/local/nagios/libexec/check_nrpe -H xxx.xx.x.xxx -c host_shutdown
NRPE: Unable to read output
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: How to shutdown a host from Nagios Xi

Post by rkennedy »

This may not be relevant as there isn't any echo in your bash, nor a 'exit 0' -- did the server actually restart? What happens if you modify the shutdown bash script to this?

Code: Select all

#host_shutdown
!#/bin/bash
logger  "shutting down the server due to Low UPS battery at `date`"
echo "shutting down"
sudo shutdown -h now
exit 0 
Former Nagios Employee
Johnjones
Posts: 16
Joined: Wed May 04, 2016 7:24 pm

Re: How to shutdown a host from Nagios Xi

Post by Johnjones »

I tried the script you posted but, same output. It didn't work and the server didn't shutdown.

I'm running NRPE using xinetd so I added the host_shutdown command to the nrpe.cfg. how to restart nrpe to take this in to effect?
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

Re: How to shutdown a host from Nagios Xi

Post by rkennedy »

Ah - try 'service xinetd restart' (since it's running under xinetd). Take a look at your /var/log/messages for an error message on the client side when you attempt to restart from the server using the check_nrpe command.
Former Nagios Employee
Johnjones
Posts: 16
Joined: Wed May 04, 2016 7:24 pm

Re: How to shutdown a host from Nagios Xi

Post by Johnjones »

I gave nagios sudo access to the client server and changed the file permissions for host_shutdown script to nagios:nagios in client server and it worked. I was able to shutdown the server. :)
Locked