Page 1 of 1

Script doesn't run when Nagios is Daemonized

Posted: Wed Jul 18, 2012 3:03 pm
by MLaing
Currently running: Nagios 3.4.1 on Centos 6.2

I have a special script that I wrote to send me email notifications in the event that our primary Internet connection fails. This script is a simple BASH script as follows:

Code: Select all

#!/bin/bash

/sbin/route add -host `/usr/bin/nslookup site2.mydomain.com | /bin/awk 'NF > 1' | /bin/awk '{ field = $NF}; END{print field }'` gw 192.168.1.2

/usr/bin/ssh [email protected] "sendmail -t < /root/email_wic_nagios"

/sbin/route del -host `/usr/bin/nslookup site2.mydomain.com | /bin/awk 'NF > 1' | /bin/awk '{ field = $NF}; END{print field }'` gw 192.168.1.2]
I know this is ugly. I'm just trying to get it to work for now. This flips over to a secondary cheap Internet connection we have, connects to a remote server (It uses a keyfile to authenticate), and uses that to send an email.

In the command.cfg, I added the following command:

Code: Select all

define command{
        command_name    notify-internet-down
        command_line    /usr/bin/sudo /usr/local/nagios/libexec/internet_down.sh
}
I'm using sudo because the command changes routes (Which requires root). I've added the proper entries to the /etc/sudoers file. I can run this command as the user "nagios" and it works successfully.

Next I've got a special user and I've assigned their host_notification_commands and service_notification_commands to the "notify-internet-down" command.



Now when I'm testing and I take the "Internet" host down, I don't get a notification. However, if I start nagios as the nagios user without the -d (To daemonize it), the notification script works when I bring the "Internet" host down. I've tried to turn on debugging, but I'm not seeing any information on what is wrong. Any help is appreciated. Thanks!

Re: Script doesn't run when Nagios is Daemonized

Posted: Wed Jul 18, 2012 4:42 pm
by agriffin
When you start Nagios daemonized, are you using a service file? In other words, are you running either 'service nagios start' or '/etc/init.d/nagios start'? If you are, it's possible that other options are getting specified that you aren't aware of. So when you run nagios manually it works because some configuration is different.

Re: Script doesn't run when Nagios is Daemonized

Posted: Wed Jul 18, 2012 4:48 pm
by MLaing
I've been starting/stopping it with /etc/init.d/nagios . The service command doesn't recognize the service nagios (I compiled the binaries).


When I start it as not daemonized I start it by running /usr/local/nagios/bin/nagios /usr/local/nagios/etc/nagios.cfg . I can remove the -d from the init.d startup file and try it that way if you wish.

Re: Script doesn't run when Nagios is Daemonized

Posted: Wed Jul 18, 2012 4:54 pm
by MLaing
So I edited the /etc/init.d/nagios file and found the line "$NagiosBin -d $NagiosCfgFile". I removed the -d and then started this from the command line (I ran /etc/init.d/nagios start as the user root). Anyway, I clicked into the "Internet" host that I've setup and clicked "Send custom host notification" and it worked. I then added the -d back to the init.d file and started nagios. The "Send custom host notification" no longer sends me an email.


Also, I shouldn't say the script doesn't run. I just don't get an email anymore. I figure something is going wrong in the process of running the script.

Re: Script doesn't run when Nagios is Daemonized

Posted: Thu Jul 19, 2012 9:45 am
by MLaing
The problem has something to do with it running sudo. I removed all the commands that needed to run as sudo and it runs fine. I then had it try to run some of the commands with sudo inside the script and those commands didn't execute (The others did though). I've redone things so that I don't need to run sudo and the script runs fine now. It would be nice to be able to use sudo though.

Re: Script doesn't run when Nagios is Daemonized

Posted: Thu Jul 19, 2012 9:54 am
by agriffin
You probably had settings in your sudoers file that require a tty before access is granted. When Nagios is daemonized it detaches from its terminal so things would stop working.

Re: Script doesn't run when Nagios is Daemonized

Posted: Thu Jul 19, 2012 3:03 pm
by MLaing
So you were right agriffin.


Apparently Centos doesn't use the defaults for the sudoers and this was in the config:

Defaults requiretty


I commented it out and it started working. Thanks!

Re: Script doesn't run when Nagios is Daemonized

Posted: Mon Jul 23, 2012 9:26 am
by agriffin
Glad I could help!