Nagios perform command on server down

This support forum board is for support questions relating to Nagios XI, our flagship commercial network monitoring solution.
MDL
Posts: 12
Joined: Wed May 16, 2012 5:03 am

Nagios perform command on server down

Post by MDL »

Hi All,

I have a question about a Nagios XI feature.
I have 3 servers: Nagios, server A and server B. Nagios is monitoring both server A and B. I want Nagios to perform an action on server B when server A goes down. Server A and B are both accessible by the Nagios machine.
So basically, Nagios should start a Windows batch script on server B when server A goes down.

Can anyone tell me how to configure this?
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Nagios perform command on server down

Post by scottwilkerson »

If these are windows machines running NSClient++ you could do something like the following:

in the nsc.ini you need to make sure you are using NRPE and have the following uncommented

Code: Select all

NRPEListener.dll
CheckExternalScripts.dll
Add your custom vbs under this section

Code: Select all

[External Scripts]
a_is_down=c:\path\to\script.vbs
On the XI server, go to Configure -> Core Config Manager -> Commands -> Add New

Command* = run_vbs_on_b
Command line* = $USER1$/check_nrpe -H <SERVER_B_IP> -c a_is_down
Command type = misc command

Save
Then, go to Configure -> Core Config Manager -> Host -> A -> Modify -> Check Settings Tab
Set
Event handler = run_vbs_on_b
Save
Apply configuration
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
MDL
Posts: 12
Joined: Wed May 16, 2012 5:03 am

Re: Nagios perform command on server down

Post by MDL »

Hello Scott,

Thank you for your reply. However, I am facing some problems.
I did what you said to do. In the nsc.ini located in de NSclient++ directory I removed the ';' comment tag behind NRPEListener.dll and CheckExternalScripts.dll.
Then, under [External Script] I added the line a_is_down=C:\Script\Script.bat (I am using a .bat here not a .vbs).
After that is all set, I went to Nagios XI -> Core config -> Commands -> Add New.
I named the command run_script_on_b_when_a_down
command line = $USER1$/check_nrpe -H <SERVER_B_IP> -c a_is_down
command type = misc command
Saved.

Then under the Check Settings Tab in the modify menu for server A, I set the event handler on run_script_on_b_when_a_down
Then I pressed save and applied the configuration. So far so good.

Then to test, I took server A down by shutting the server off. Nagios told me A is down. However, the script isn't running on server B... Nothing happens at all. What am I doing wrong here? Maybe because the script is a batch file instead of visualbasic? Or should I restart the NSclient on server B because I edited the nsc.ini?

Thank you in advance for your response.
User avatar
lmiltchev
Former Nagios Staff
Posts: 13587
Joined: Mon May 23, 2011 12:15 pm

Re: Nagios perform command on server down

Post by lmiltchev »

You definitely have to restart the nsclient++ service if you made any changes in the NCS.in file, so that the changes can take effect. You can restart it from a DOS prompt:

Code: Select all

net stop nsclientpp
net start nsclientpp
Hope this helps.
Be sure to check out our Knowledgebase for helpful articles and solutions!
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Nagios perform command on server down

Post by scottwilkerson »

Also, make sure on the Check Settings Tab you have "Event handler enabled"=on

I forgot to mention this the first time...
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
MDL
Posts: 12
Joined: Wed May 16, 2012 5:03 am

Re: Nagios perform command on server down

Post by MDL »

Hello,

I have 10 IIS Windows servers on which the action should be performed. Could you tell me if the following is possible?
On the New Command page, configure the following command: $USER1$/check_nrpe -H <SERVER_B_IP> <SERVER_C_IP> <SERVER_D_IP> <SERVER_E_IP> ... -c a_is_down

Thanks in advance.
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Nagios perform command on server down

Post by scottwilkerson »

This is not possible, however what is possible would be to write a shell script that contained the seperate commands and add it as the command

Something like this

Code: Select all

#! /bin/sh
/usr/local/nagios/libexec/check_nrpe -H <SERVER_B_IP> -c a_is_down
/usr/local/nagios/libexec/check_nrpe -H <SERVER_C_IP> -c a_is_down
/usr/local/nagios/libexec/check_nrpe -H <SERVER_D_IP> -c a_is_down
/usr/local/nagios/libexec/check_nrpe -H <SERVER_E_IP> -c a_is_down
/usr/local/nagios/libexec/check_nrpe -H <SERVER_F_IP> -c a_is_down
/usr/local/nagios/libexec/check_nrpe -H <SERVER_G_IP> -c a_is_down
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
MDL
Posts: 12
Joined: Wed May 16, 2012 5:03 am

Re: Nagios perform command on server down

Post by MDL »

Thanks again for your response.
I still can't get the command to work. I did everything Scottwilkerson described. Then I turned on the option "Event handler enabled"=on on server A in Nagios. After that, I stopped and started the nsclient on the destination machine (server B).
But still, when I shutdown server A, and Nagios tells me that it is down, there is still nothing happening on server B.
The only 2 lines in the batch script which should be run on server B are the following:

Code: Select all

REN C:\Windows\System32\Drivers\etc\hosts hosts_original
REN C:\Windows\System32\Drivers\etc\hosts_backup hosts
The script works fine when I manually start it but Nagios still doesn't perform anything when I take server A down.

What am I doing wrong?
scottwilkerson
DevOps Engineer
Posts: 19396
Joined: Tue Nov 15, 2011 3:11 pm
Location: Nagios Enterprises
Contact:

Re: Nagios perform command on server down

Post by scottwilkerson »

Can you verify that if you run the following from the command line on your Nagios server it runs on the client

Code: Select all

/usr/local/nagios/libexec/check_nrpe -H <SERVER_B_IP> -c a_is_down
I am thinking that it could possible be a permissions problem. After executing the command above look at the nsclient.log on SERVER_B to see if you see any errors
Former Nagios employee
Creator:
ahumandesign.com
enneagrams.com
MDL
Posts: 12
Joined: Wed May 16, 2012 5:03 am

Re: Nagios perform command on server down

Post by MDL »

When I run the command i get the following:
UNKNOWN: No handler for that command

Hope you can help me out.
Locked