/ Nagios ^

Nagios: Adding a Config Changelog

Last updated 02 Aug 2021

(According to the Nagios Core 4.x Version History, as of 4.4.6 the configuration will be verified before reloading when using systemd.)

A useful feature I added to my Nagios Core installation is a changelog to keep track of changes to configuration files which is also available through the web interface. I use the following restart.sh script which first checks the config is valid - if there are any errors it will quit but if it's successful (or only warnings), it will prompt for a summary of the changes and then restart nagios and nrpe (you'll need to check the paths are correct for your installation):

#!/bin/bash
# 2019-01-09 https://roddie.digital
# Check Nagios configuration is valid before restarting it
# and append summary to config changelog

/usr/sbin/nagios -v /etc/nagios/nagios.cfg

warnings=$(/usr/sbin/nagios -v /etc/nagios/nagios.cfg | grep -o "Warnings.*")
warnings=${warnings#*:}
warnings=$(echo $warnings | xargs)

errors=$(/usr/sbin/nagios -v /etc/nagios/nagios.cfg | grep -o "Errors.*")
errors=${errors#*:}
errors=$(echo $errors | xargs)

if [ "$errors" = "0" ]
then
    #echo "No errors so let's check for warnings..."
    if [ "$warnings" = "0" ]
    then
        #echo "No warnings so let's restart it..."
        read -p "Add summary of change: " updcom
        upddts=$(date "+%Y-%m-%d %T:")
        updlog="$upddts $updcom"
        echo $updlog >> change.log && tac change.log > /usr/share/nagios/html/changelog.txt
        systemctl restart nagios && systemctl restart nrpe
        systemctl status nagios
    else
        #echo "Some warnings, so check the output and make a decision"
        echo "Warnings found, either add summary to continue or Control-C to quit"
        read -p "Add summary of change: " updcom
        upddts=$(date "+%Y-%m-%d %T:")
        updlog="$upddts $updcom"
        echo $updlog >> change.log && tac change.log > /usr/share/nagios/html/changelog.txt
        systemctl restart nagios && systemctl restart nrpe
        systemctl status nagios
    fi
else
    #echo "Some errors; quitting..."
    exit
fi

In case I want to record any changes outwith the Nagios config files (eg the PHP files instead) then I use this addchange.sh script to add a change without checking or restarting Nagios:

#!/bin/bash

changelog='/etc/nagios/change.log'

read -p "Add summary of change: " updcom
        upddts=$(date "+%Y-%m-%d %T:")
        updlog="$upddts $updcom"
        echo $updlog >> $changelog && tac $changelog > /usr/share/nagios/html/changelog.txt

For tidiness, I use the following cron job to add a blank line between months:

1 0 1 * * echo '' >> /etc/nagios/change.log

Then in /usr/share/nagios/html/side.php you can add the following line so that the Changelog appears in the side bar and can be displayed in the main Nagios web frame:

<li><a href="/nagios/changelog.txt" target="<?php echo $link_target;?>">Changelog</a></li>

You can obviously change date format for your own preference but this is how the script currently writes it:

2021-04-12 09:29:46: Some text about the change goes here

If you've installed Nagios Core from your distro's repositories (rather than from source) and are making changes to side.php, make sure you have a backup of it before performing any upgrades or your changes will be lost.


Return to roddie.digital / top