Page 1 of 2
Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:28 pm
by BanditBBS
I need to add a custom variable to about 300 services. Was hoping I'd be able to use bulk mod tool, but doesn't look like it. Any method for adding one to a list of services?
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:32 pm
by WillemDH
Bandit,
It might be your lucky day. I wrote a Bash script for a Reactor chain to add, remove or replace a custom free variable on a Nagios host. Same style as my reconfigure hostgroups script =>
http://outsideit.net/naf-nagios-reconfi ... ostgroups/ I need to clean it up a little, but I have integrated it today in my giant Powershell inventory script which now updates a whole lot of free variables every week for 600+ Windows hosts.
Give me some time and I'll deliver it to you.
Grtz
Willem
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:43 pm
by BanditBBS
Thanks Willem.....I really really wish I had time to setup reactor and use as you do!
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:47 pm
by tmcdonald
Was that a "Thanks Willem, that'll work and Nagios doesn't need to get involved" post or a "Thanks Willem, but I want Trevor to save the day instead" post?
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:49 pm
by BanditBBS
tmcdonald wrote:Was that a "Thanks Willem, that'll work and Nagios doesn't need to get involved" post or a "Thanks Willem, but I want Trevor to save the day instead" post?
I just honestly laughed out loud, very loud, when I read that Trevor!
If you have a method, please feel free to post it. Otherwise, waiting on Willem

Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:50 pm
by tmcdonald
Do you have a great way of exporting a list of host+service combos that need changing or is this something for which a checkbox + search would work best? I have a few ideas in mind, some more programmatic than others.
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 12:53 pm
by BanditBBS
Checkbox + Search would work. Sort of like how you can search for hosts to add to a service definition. Narrow down, add to the other column and then apply. The custom variable is going to be identical on all 300+
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 1:10 pm
by tmcdonald
Bah, I tried to modify the Bulk Mods directly but hit a wall with the SourceGuardian thing. I could likely give you to the SQL to do it but without spending a few hours tearing apart the CCM I can't get you a search + checkbox method.
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 1:40 pm
by WillemDH
Ok, here you go. I will post this on the Nagios Exchange once I find some time to make a blogpost about it. (I'm creating far more scripts then I have time to document them)
Code: Select all
#!/bin/bash
# Script name: naf_nagios_reconfigure_host_freevar.sh
# Version: v1.02.151028
# Author: Willem D'Haese
# Created on: 15/01/2015
# Purpose: Bash script to process host config files and dynamically update hostgroups
# Recent History:
# 21/10/15 => Creation date
# 27/10/15 => Testing, splitting up import and export config
# 28/10/15 => Finalized replace
# Copyright:
# This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public
# License along with this program. If not, see <http://www.gnu.org/licenses/>.
TEMP=`getopt -o S:u:p:H:O::N:V: --long NagiosServer:,NagiosUser:,NagiosPassword:,NagiosHost:,OldFreeVar:,NewFreeVar:,NewFreeValue: -n 'naf_reconfigure_nagios_host_freevars.sh' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true ; do
case "$1" in
-S|--NagiosServer) NagiosServer=$2 ; shift 2 ;;
-u|--NagiosUser) NagiosUser=$2 ; shift 2 ;;
-p|--NagiosPassword) NagiosPassword=$2 ; shift 2 ;;
-H|--NagiosHost) NagiosHost=$(echo $2 | tr '[:upper:]' '[:lower:]') ; shift 2 ;;
-O|--OldFreeVar) OldFreeVar=$2 ; shift 2 ;;
-N|--NewFreeVar) NewFreeVar=$2 ; shift 2 ;;
-V|--NewFreeValue) NewFreeValue=$2 ; shift 2 ;;
--) shift ; break ;;
*) echo "Argument parsing issue: $1" ; exit 1 ;;
esac
done
Verbose=1
writelog () {
if [ -z "$1" ] ; then echo "WriteLog: Log parameter #1 is zero length. Please debug..." ; exit 1
else
if [ -z "$2" ] ; then echo "WriteLog: Severity parameter #2 is zero length. Please debug..." ; exit 1
else
if [ -z "$3" ] ; then echo "WriteLog: Message parameter #3 is zero length. Please debug..." ; exit 1 ; fi
fi
fi
Now=$(date '+%Y-%m-%d %H:%M:%S,%3N')
if [ $1 = "Verbose" -a $Verbose = 1 ] ; then echo "$Now: $2: $3"
elif [ $1 = "Verbose" -a $Verbose = 0 ] ; then :
elif [ $1 = "Output" ] ; then echo "${Now}: $2: $3"
elif [ -f $1 ] ; then echo "${Now}: $2: $3" >> $1
fi
}
writelog Verbose Info "Arguments initialized successfully. OldFreeVar = $OldFreeVar , NewFreeVar = $NewFreeVar , NewFreeVarValue = $NewFreeValue"
ScriptDirectory=$(cd "$( dirname "$0" )" && pwd)
ReactorCfgFolder=/var/www/html/nagiosreactor/application/libraries/scripts/naf/nagiosxicfgs/
Logfile=/var/log/naf_actions.log
checkexistence () {
# echo "Param 1: $1, Param 2: $2"
if (echo $1 | grep "\"$2\"" > /dev/null); then
echo "Hostgroup $2 found. Continuing..."
else
Now=$(date '+%Y-%m-%d %H:%M:%S')
echo "$Now : Error: Hostgroup $2 not found."
echo "$Now : Error: Hostgroup $2 not found." >> $Logfile
exit 1
fi
}
if [ ! -z "$OldFreeVar" ] && [ ! -z "$NewFreeVar" ] && [ ! -z "$NewFreeValue" ] ; then
writelog Verbose Info "Removing $OldFreeVar"
sed -i.bak "/$OldFreeVar/d" ${ReactorCfgFolder}${NagiosHost}.cfg
writelog Verbose Info "Adding $NewFreeVar with value $NewFreeValue"
sed -i.bak "/.*register.*/ { N; s/.*register.*\n/ $NewFreeVar $NewFreeValue\n&/ }" ${ReactorCfgFolder}${NagiosHost}.cfg
writelog Output Info "Replaced $OldFreeVar to $NewFreevar in ${ReactorCfgFolder}${NagiosHost}.cfg"
rm ${ReactorCfgFolder}${NagiosHost}.cfg.bak
exit 0
elif [ ! -z "$OldFreeVar" ]; then
writelog Verbose Info "Removing $OldFreeVar ${OldFreeVar}"
sed -i.bak "/$OldFreeVar/d" ${ReactorCfgFolder}${NagiosHost}.cfg
writelog Output Info "Removed OldFreeVar ${OldFreeVar}"
rm ${ReactorCfgFolder}${NagiosHost}.cfg.bak
exit 0
elif [ ! -z "$NewFreeVar" ] && [ ! -z "$NewFreeValue" ]; then
writelog Verbose Info "Checking if free variable $NewFreeVar already exists."
if grep -q $NewFreeVar "${ReactorCfgFolder}${NagiosHost}.cfg"; then
writelog Verbose Info "Free variable already exists. Exiting."
exit 1
else
writelog Verbose Info "Adding free variable $NewFreeVar with value $NewFreeValue"
sed -i.bak "/.*register.*/ { N; s/.*register.*\n/ $NewFreeVar $NewFreeValue\n&/ }" ${ReactorCfgFolder}${NagiosHost}.cfg
rm ${ReactorCfgFolder}${NagiosHost}.cfg.bak
exit 0
fi
fi
writelog Verbose Info "Variables were not parsed succesfsfully. Please debug."
exit 1
You will need to move your host config file to ReactorCfgFolder and afterwards move it to the import folder. You don't per sé need Reactor, you could edit the script a bit to make it run on XI. Just provide NewFreeVar and NewFreeValue.
Re: Add custom variable to 300 services
Posted: Wed Oct 28, 2015 1:54 pm
by BanditBBS
Nice scripting Willem, I can actually follow along with the code no problem. Only issue is, I need to add this to services, not hosts and 300+ services spread across maybe 50 hosts, but not all the services on the hosts. That's why I was hoping Trevor could help....but alas....