Page 1 of 1

How to write to Nagios log file from eventhandler script

Posted: Wed Dec 14, 2016 5:39 pm
by OzGobo
Hi All,

I'm playing with event handlers, but I'd like to write back to the Nagios log file from the scripts. I'm comfortable with echo >> but I'm unsure of the actual format. Is this specified somewhere? I think its just: [TIMESTAMP] [MESSAGE]

I'm also not sure how I generate the timestamp, but this seems to do the trick assuming that the timestamp nagios uses is epoch: date +":%s"

I'm running on CentOS 7

Cheers,

Scott

Re: How to write to Nagios log file from eventhandler script

Posted: Thu Dec 15, 2016 1:26 pm
by mcapra
You might want to use a dedicated log file for the eventhandler's outputs if possible. Writing directly to the nagios.log file with 3rd party components can interfere with some things that Nagios XI does on the back-end (mostly with reporting).

Re: How to write to Nagios log file from eventhandler script

Posted: Thu Dec 15, 2016 10:51 pm
by OzGobo
mcapra wrote:You might want to use a dedicated log file for the eventhandler's outputs if possible. Writing directly to the nagios.log file with 3rd party components can interfere with some things that Nagios XI does on the back-end (mostly with reporting).
Thanks for your reply mcapra. I wanted to use the Nagios log so that its available in the event log in the web interface mostly for people that aren't me (I'm the only one here with any linux skills). I'm not savvy enough in the webspace to change the web interface to pick that up. We don't currently have any plans to upgrade to Nagios XI as we are a not for profit and budget is always an issue. Is there a way to add another log file to the interface with configuration rather than changing the web interface code? On your comment I'll have a look at the reports in the core deployment I have and see if there's any issues.

I did actually figure this out in the end. For anyone else that may be interested, first see mcapra's response - what I'm suggesting may well be a bad idea!!!

After a bit of testing and playing around I found that this command worked nicely:

echo [`date +"%s"`] rest of the message you wanted to enter >>/path/to/nagios.log

Leaving out the [] on the time stamp will have some funky results. I passed in the path to the script using the $LOGFILE$ Nagios variable.

Cheers,

Scott.

Re: How to write to Nagios log file from eventhandler script

Posted: Fri Dec 16, 2016 11:17 am
by rkennedy
Thanks for sharing. One way around this, would be to log to a file that is publically available in the HTTP directory. On my system, this happens to be /usr/local/nagios/var/share/ and I created a file called log.txt

From here, modify side.php and add the link -

Code: Select all

                <ul class="navsectionlinks">
                        <li><a href="main.php" target="<?php echo $link_target;?>">Home</a></li>
                        <li><a href="https://go.nagios.com/nagioscore/docs" target="_blank">Documentation</a></li>
                </ul>
Add this below the Documentation link -

Code: Select all

                        <li><a href="http://192.168.3.115/nagios/log.txt" target="_blank">Event Log</a></li>
Final result being this -

Code: Select all

                <ul class="navsectionlinks">
                        <li><a href="main.php" target="<?php echo $link_target;?>">Home</a></li>
                        <li><a href="https://go.nagios.com/nagioscore/docs" target="_blank">Documentation</a></li>
                        <li><a href="http://192.168.3.115/nagios/log.txt" target="_blank">Event Log</a></li>
                </ul>
(modify 192.168.3.115 to your IP/FQDN)

Re: How to write to Nagios log file from eventhandler script

Posted: Mon Dec 19, 2016 9:29 pm
by OzGobo
rkennedy wrote: One way around this, would be to log to a file that is publically available in the HTTP directory
That's awesome rkennedy! Thanks for sharing. :)

Re: How to write to Nagios log file from eventhandler script

Posted: Tue Dec 20, 2016 11:42 am
by dwhitfield
@OzGobo, are we ready to lock this up?

Re: How to write to Nagios log file from eventhandler script

Posted: Wed Dec 21, 2016 4:58 pm
by OzGobo
Hi there,

Yep, lock it up. Thanks so much for the help.

Cheers,

Scott.