# cat /etc/nagios/scripts/addtonagios
#!/bin/bash
# -------------------------------------------------
# Zarko, 2017
# version 1
# For more info, see the man page (man addtonagios)
# -------------------------------------------------
# Some colorful stuff
# -------------------
# ANSI escape sequence (echo -e = enables it)
# -- Bold text: \033[1m ... \033[0m
# \033 is escape
# [1 turns ON bold atribute, [0 turns it OFF, m terminates each escape sequence
# -- Colors
# \E[ begins escape sequence
# COLOR FOREGROUND BACKGROUND
# black 30 40
# red 31 41
# green 32 42
# yellow 33 43
# blue 34 44
# magenta 35 45
# cyan 36 46
# white 37 47
# Restore terminal settings to normal: echo -ne "\E[0m"
# Restore terminal settings to normal: tput sgr0
# let's log what the script does, let's log a lot!
progname=`/bin/basename $0`
loggerinfo="logger -t "${progname}" Info:"
loggerwarning="logger -t "${progname}" Warning:"
loggerproblem="logger -t "${progname}" Problem:"
${loggerinfo} ======== Start at `date +'%Y-%m-%dT%H-%M-%S_%Z'` ===============
# define some messages for a user to see on the screen
message_info() {
echo ; echo "Info: $*" ; echo
${loggerinfo} "$*"
}
message_warning() {
echo ; echo "Warning: $*" ; echo
${loggerwarning} "$*"
}
message_problem() {
echo ; echo -e "\E[37;41m Problem: \E[0m $*" ; echo
${loggerproblem} "$*"
${loggerinfo} ======== Finish at `date +'%Y-%m-%dT%H-%M-%S_%Z'` ===============
exit 1
}
# check if a user is root
[[ `id | awk -F\) '{print $1}' | awk -F\( '{print $2}'` = root ]] || \
message_problem "You are not root."
# usage
usage() {
echo ; echo -e " \
Usage:
\033[1m\E[31;47m "${progname}" -h <FQDN > -r < s|i > \E[0m\033[0m
Arguments:
\033[1mFQDN\033[0m
host's FQDN
\033[1ms|i\033[0m
run the program non interactively (s) or interactively (i)
\033[1m\E[33;40ms\E[0m\033[0m assumes you know all required information (if not, read man page)
\033[1m\E[33;40mi\E[0m\033[0m askes some questions (still suggested to read man page)
"
exit 1
}
# find list of system type
type_list=`cat /etc/nagios/objects/host-check-templates.cfg | grep name | awk '{print $2}'`
# find list of hostgroups
hostgroup_list=`cat /etc/nagios/hostgroups/hostgroups.cfg | grep hostgroup_name | grep -v ^# | awk '{print $2}'`
# find list of nt_templates
nt_template_list=`grep name /etc/nagios/objects/notification-templates.cfg | awk '{print $2}'`
# THE FUNCTION TO RUN NON INTERACTIVELY
run_non_interactively() {
echo ${type} ${host_group} ${nt_template} ${host_alias}
# check if type is valid
[[ -z "${type}" ]] && type=hc_generic
( [[ "${type_list}" =~ (^|[[:space:]])"${type}"($|[[:space:]]) ]] ) || \
message_problem "\E[35;40m"${type}"\E[0m is not valid system type, read man page and run script again."
# check if hostgroup is not blank
[[ -z "${host_group}" ]] && \
message_problem "\E[35;40m"${host_group}"\E[0m is not valid Nagios hostgroup, read man page and run script again."
# check if hostgroup is valid
( [[ "${hostgroup_list}" =~ (^|[[:space:]])"${host_group}"($|[[:space:]]) ]] ) || \
message_problem "\E[35;40m"${host_group}"\E[0m is not valid Nagios hostgroup, read man page and run script again."
# check if nt template is valid
[[ -z "${nt_template}" ]] && nt_template=nt_no-email
[[ "${nt_template_list}" =~ (^|[[:space:]])"${nt_template}"($|[[:space:]]) ]] || \
message_problem "\E[35;40m"${nt_template}"\E[0m is not valid notification template, read man page and run script again."
# determine default gateway or parent by calling external script
. /etc/nagios/scripts/determine_parent.sh "${host_name}"
${loggerinfo} "Creating file /etc/nagios/hosts/"${host_name}".cfg "
echo "
#################
# host definition
#################
define host{
use "${type}","${nt_template}"
host_name "${host_name}"
address `host -t A ${host_name} | awk '{print $4}'`
alias "${host_alias}"
parents "${dg}"
hostgroups "${host_group}"
}
" | tee /etc/nagios/hosts/"${host_name}".cfg
${loggerinfo} "Setting nagios as owner for /etc/nagios/hosts/"${host_name}".cfg"
chown nagios:nagios /etc/nagios/hosts/"${host_name}".cfg
}
# THE FUNCTION TO RUN INTERACTIVELY
run_interactively() {
# Let's start asking questions
echo ; echo -e " \
\E[33;40m
I'll have to ask you some questions now, press ENTER when ready. \E[0m" ; echo
read x_ready
echo; echo -e " \
Please select one of presented \E[31;40m system types \E[0m (press ENTER for \E[35;40m hc_generic\E[0m ):
${type_list}
" ; echo
read type
[[ -z "${type}" ]] && type=hc_generic
( [[ "${type_list}" =~ (^|[[:space:]])"${type}"($|[[:space:]]) ]] ) || \
message_problem "\E[35;40m"${type}"\E[0m is not valid system type, run script again."
# I need hostgroup, if any
echo; echo -e " \
Please select ONLY ONE of presented \E[31;40m Nagios hostgroups \E[0m (a hostgroup is MUST):
${hostgroup_list}
" ; echo
read host_group
[[ -z "${host_group}" ]] && \
message_problem "\E[35;40m"${host_group}"\E[0m is not valid Nagios hostgroup, run script again."
( [[ "${hostgroup_list}" =~ (^|[[:space:]])"${host_group}"($|[[:space:]]) ]] ) || \
message_problem "\E[35;40m"${host_group}"\E[0m is not valid Nagios hostgroup, run script again."
# I need notification template, default is no email sent out
echo; echo -e " \
Please select ONLY ONE of presented \E[31;40m notification template \E[0m (press ENTER for \E[35;40m nt_no-email\E[0m):
${nt_template_list}
" ; echo
read nt_template
[[ -z "${nt_template}" ]] && nt_template=nt_no-email
[[ "${nt_template_list}" =~ (^|[[:space:]])"${nt_template}"($|[[:space:]]) ]] || \
message_problem "\E[35;40m"${nt_template}"\E[0m is not valid notification template, run script again."
# I need an alias/cname/description
echo; echo -e " \
Please input \E[31;40m alias/description \E[0m (press ENTER for none):" ; echo
read host_alias
# determine default gateway or parent by calling external script
. /etc/nagios/scripts/determine_parent.sh "${host_name}"
${loggerinfo} "Creating file /etc/nagios/hosts/"${host_name}".cfg "
echo "
#################
# host definition
#################
define host{
use "${type}","${nt_template}"
host_name "${host_name}"
address `host -t A ${host_name} | awk '{print $4}'`
alias "${host_alias}"
parents "${dg}"
hostgroups "${host_group}"
}
" | tee /etc/nagios/hosts/"${host_name}".cfg
${loggerinfo} "Setting nagios as owner for /etc/nagios/hosts/"${host_name}".cfg"
chown nagios:nagios /etc/nagios/hosts/"${host_name}".cfg
}
# -------
# MAIN
#---------
while [ "$1" != "" ]; do
case $1 in
-h ) shift; host_name=$1 ;;
-r ) shift; run_as=$1 ;;
-t ) shift; type=$1 ;;
-H ) shift; host_group=$1 ;;
-n ) shift; nt_template=$1 ;;
-a ) shift; host_alias=$1 ;;
* ) usage ;;
esac
shift
done
# check if there is a hostname
if [ -z "${host_name}" ]; then
echo ; echo "Looks like there is no hostname" ; usage
fi
# did user enter FQDN?
domain_name=`echo ${host_name} | awk -F\. '{print $(NF-1)"."$NF}'`
if [ oracle.com != ${domain_name} ]; then
echo ; echo "Looks like you didn't input FQDN, I expect to see oracle.com at the end." ; usage
fi
# Is host in DNS?
host "${host_name}" > /dev/null
if [ $? -ne 0 ]; then
message_problem "${host_name} is not in DNS"
fi
# Does host already exist in Nagios?
(ls /etc/nagios/hosts/ | grep -w "${host_name}".cfg) > /dev/null
if [ $? -eq 0 ]; then
message_problem "${host_name} already exists in Nagios"
fi
(grep -r -x "${host_name}" /etc/nagios/hosts/) > /dev/null
if [ $? -eq 0 ]; then
message_problem "${host_name} already exists in Nagios"
fi
# check if user knows how to select run type
if [ "${run_as}" = "s" ]; then
${loggerinfo} "Running NON INTERACTIVELY"
run_non_interactively
elif [ "${run_as}" = "i" ]; then
${loggerinfo} "Running INTERACTIVELY"
run_interactively
else
usage
fi
# Nagios service check and exit if it fails
# so git work is not performed
${loggerinfo} "Checking Nagios service configuration"
/sbin/nagios -v /etc/nagios/nagios.cfg > /dev/null
if [ $? -ne 0 ]; then
message_problem "Nagios configuration has error, it has to be corrected manually!"
fi
${loggerinfo} "Nagios configuration is OK, reloading Nagios service"
/bin/systemctl reload nagios || message_problem "Can't reload Nagios service"
# commit the change
cd /etc/nagios/hosts
${loggerinfo} "Adding /etc/nagios/hosts/${host_name}.cfg to staging"
/usr/bin/git add /etc/nagios/hosts/"${host_name}".cfg || message_warning "Cannot add /etc/nagios/hosts/${host_name}.cfg to staging"
${loggerinfo} "Commiting /etc/nagios/hosts/${host_name}.cfg to local repo"
/usr/bin/git commit -m "The script addtonagios is adding /etc/nagios/hosts/${host_name}.cfg" /etc/nagios/hosts/"${host_name}".cfg || \
message_warning "Cannot commit /etc/nagios/hosts/${host_name}.cfg to local repo"
${loggerinfo} "Pushing /etc/nagios/hosts/${host_name}.cfg to the remote"
/usr/bin/git push || message_warning "Cannot push /etc/nagios/hosts/${host_name}.cfg to the remote repo"
${loggerinfo} ======== Finish at `date +'%Y-%m-%dT%H-%M-%S_%Z'` ===============
echo -ne "\E[0m"
exit 0
|