How to write to Nagios log file from eventhandler script

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
OzGobo
Posts: 4
Joined: Mon Jun 27, 2016 10:25 pm

How to write to Nagios log file from eventhandler script

Post 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
User avatar
mcapra
Posts: 3739
Joined: Thu May 05, 2016 3:54 pm

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

Post 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).
Former Nagios employee
https://www.mcapra.com/
OzGobo
Posts: 4
Joined: Mon Jun 27, 2016 10:25 pm

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

Post 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.
rkennedy
Posts: 6579
Joined: Mon Oct 05, 2015 11:45 am

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

Post 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)
Former Nagios Employee
OzGobo
Posts: 4
Joined: Mon Jun 27, 2016 10:25 pm

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

Post 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. :)
dwhitfield
Former Nagios Staff
Posts: 4583
Joined: Wed Sep 21, 2016 10:29 am
Location: NoLo, Minneapolis, MN
Contact:

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

Post by dwhitfield »

@OzGobo, are we ready to lock this up?
OzGobo
Posts: 4
Joined: Mon Jun 27, 2016 10:25 pm

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

Post by OzGobo »

Hi there,

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

Cheers,

Scott.
Locked