Page 1 of 2

How to script a host acknowledgement

Posted: Thu Jun 16, 2016 1:32 pm
by linuser
We have nagios 3.5.1 running on a RHEL 6 server. We have a process we go by to add new customers that we monitor. I created a script that does all of the backend work..ie, create the host file, add the host to the file in the hostgroups folder, verify and restart nagios. Our next step is acknowledging the sensor because at the time of setup, the customers are not yet online. We do this through the web UI. I would like to incorporate this piece as well into my script and do the work from the backend. ie the command line. Here is what I know about acknowledgements.

1) After a nagios restart a host check must be performed
2) The check must get a soft-fail return

All of this I have seen in the logs. And I *think* its at this point that the sensor turns pink and lets you acknowledge it. Can anyone tell me how I can incorporate whatever steps I need into my bash script?

Final note here not all of the sensors need acknowledging. There are a few that we setup that are already online. So i'm thinking here some type of if/then to handle that type of condition. I think a simple sleep 180 command would take care of the time it takes to turn pink I just need to know what to do next.

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 1:36 pm
by linuser

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 2:40 pm
by tmcdonald
Those commands should still work, yes. You can execute them after the config has been added and the nagios process restarted to pick up the changes. You might consider setting the acknowledgement as "Sticky" so it will stay until the host comes up.

Also, since this is not a Nagios XI question I will be moving this to the Nagios Core section of the forum. In the future please make sure to post in the appropriate areas.

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 2:55 pm
by linuser
For some reason I am having problems. I am starting with a simple script like the one in the link. I compared the log entry from an acknowledgement from the UI and using that format for the script.

Code: Select all

ACKNOWLEDGE_HOST_PROBLEM;hostname;2;1;0;Nagios Admin;down for mx
Next I put that into the script:

Code: Select all

now=`date +%s`
commandfile='/etc/nagios/objects/commands.cfg'

/usr/bin/printf "[%lu] ACKNOWLEDGE_HOST_PROBLEM;hostname;2;1;0;Nagios Admin;down for mx\n" $now > $commandfile
The script runs with no problems and it puts the entry into the commands.cfg file. However, it does not make an entry into the nagios logfile and thus does not acknowledged the host problem. What could be the problem?

I dont have a nagios.cmd file the only "commands" file I have is the commands.cfg. I wonder if I should be pointing it somewhere else or am I missing a step?

Also, what does the [%lu] do?

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:03 pm
by tmcdonald
You should not be writing anything into commands.cfg in this manner. In fact that probably means your system is going to break next time you try to restart nagios because you now have no commands defined, just whatever you saved into it with the printf command. If you have backups of commands.cfg now is the time to restore them.

The file to write to is /usr/local/nagios/var/rw/nagios.cmd and you definitely should have one in your system, just maybe not at that exact location.

The [%lu] is just a placeholder that will be filled with the value of the $now variable, which holds the current timestamp.

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:09 pm
by linuser
I see that now and have restored a backup. I have no nagios.cmd file on this system.

This is all I have:

Code: Select all

[root@labnagios]# find / -type f -name '*.cmd'
/var/www/html/ossec-hids-2.8.3/src/win32/gen_win.cmd
/var/www/html/ossec-hids-2.8.3/active-response/win/route-null.cmd
/var/www/html/ossec-hids-2.8.3/active-response/win/netsh.cmd
/var/www/html/ossec-hids-2.8.3/active-response/win/restart-ossec.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/include/config/auto.conf.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.elfconfig.h.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.mk_elfconfig.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.empty.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.file2alias.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.sumversion.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.mod-extract.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.modpost.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/mod/.modpost.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/kconfig/.conf.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/kconfig/.conf.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/kconfig/.kxgettext.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/kconfig/.zconf.tab.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/basic/.fixdep.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/basic/.hash.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/basic/.docproc.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/.unifdef.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/genksyms/.lex.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/genksyms/.genksyms.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/genksyms/.parse.o.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/genksyms/.genksyms.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/selinux/mdp/.mdp.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/selinux/genheaders/.genheaders.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/.kallsyms.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/.pnmtologo.cmd
/usr/src/kernels/2.6.32-573.12.1.el6.x86_64/scripts/.conmakehash.cmd
I have also checked out internal nagios server and external nagios server and neither on of these have the nagios.cmd file either.

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:20 pm
by linuser
Ah! Its a named pipe file. So:

Code: Select all

[root@labnagios cmd]# find / -type p -name nagios.cmd
/var/spool/nagios/cmd/nagios.cmd
So there it is. But as luck would have it. making that my commandfile variable still did not write a log entry and acknowledge the problem. Any other suggestions?

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:27 pm
by linuser
ok got it. Apparently printf does not like writing to a named pipe. CHanged my script command to use echo and it now works!

Code: Select all

echo -e "[%lu] ACKNOWLEDGE_HOST_PROBLEM;hostname;2;1;0;Nagios Admin;down for mx\n" $now > $commandfile

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:34 pm
by linuser
So my next step is to force a host alert as soon as possible so I can use the command to acknowledge the sensor. What command do I need to run and in what format does it need to be in to perform a ping check and trigger the host alert in the logs?

Re: How to script a host acknowledgement

Posted: Thu Jun 16, 2016 3:50 pm
by linuser
ok so I do a similar process to force a check and about 1 minute later the host alert shows up at which point I can ack the sensor. I think I am good now. Thanks for the help.