Back to the main page

addtonagios script

I wrote this one to add a system to Nagios, read its man page here
Sure may need lots of adjustments for another environment, this is just to get an idea ...
# 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



# cat determine_parent.sh
#!/bin/bash

###############################################
#
# this script is not supposed to run by itself,
# it'll be called by addtonagios tool
#
# see 'man addtonagios'
#
################################################

# check if there is one argument
if [ $# != 1 ]; then
        echo "Argument (hostname) is needed to find its parent" ; exit 2
fi

# the arguemnt is hostname
device=$1

network=`host -t A ${device} |awk '{print $4}' | awk -F. '{print $1"."$2"."$3}'`
device_ip=`host ${device} | awk '{print $4}'`

# default is unknown gateway
dg=""

# vlan 321
if [ ${network} = 10.68.54 ]; then
        dg=dg-vlan321
        return "${dg}"
elif [ ${network} = 10.68.55 ]; then
        dg=dg-vlan321
        return "${dg}"

# vlan 322
elif [ ${network} = 10.68.48 ]; then
        dg=dg-vlan322
        return "${dg}"
elif [ ${network} = 10.68.49 ]; then
        dg=dg-vlan322
        return "${dg}"
elif [ ${network} = 10.68.50 ]; then
        dg=dg-vlan322
        return "${dg}"
elif [ ${network} = 10.68.51 ]; then
        dg=dg-vlan322
        return "${dg}"

# vlan 351
elif [ ${network} = 10.211.8 ]; then
        dg=dg-vlan351
        return "${dg}"
elif [ ${network} = 10.211.9 ]; then
        dg=dg-vlan351
        return "${dg}"
elif [ ${network} = 10.211.10 ]; then
        dg=dg-vlan351
        return "${dg}"
elif [ ${network} = 10.211.11 ]; then
        dg=dg-vlan351
        return "${dg}"

# vlan 352
elif [ ${network} = 10.211.17 ]; then
        dg=dg-vlan352
        return "${dg}"

# vlan 350
elif [ ${network} = 10.211.0 ]; then
        dg=dg-vlan350
        return "${dg}"
elif [ ${network} = 10.211.1 ]; then
        dg=dg-vlan350
        return "${dg}"
elif [ ${network} = 10.211.2 ]; then
        dg=dg-vlan350
        return "${dg}"
elif [ ${network} = 10.211.3 ]; then
        dg=dg-vlan350
        return "${dg}"

# vlan 315
elif [ ${network} = 10.147.38 ]; then
        dg=dg-vlan315
        return "${dg}"

# vlan 311
elif [ ${network} = 10.147.84 ]; then
        dg=dg-vlan311
        return "${dg}"
elif [ ${network} = 10.147.85 ]; then
        dg=dg-vlan311
        return "${dg}"
elif [ ${network} = 10.147.86 ]; then
        dg=dg-vlan311
        return "${dg}"
elif [ ${network} = 10.147.87 ]; then
        dg=dg-vlan311
        return "${dg}"

# vlan 312
elif [ ${network} = 10.147.80 ]; then
        dg=dg-vlan312
        return "${dg}"
elif [ ${network} = 10.147.81 ]; then
        dg=dg-vlan312
        return "${dg}"
elif [ ${network} = 10.147.82 ]; then
        dg=dg-vlan312
        return "${dg}"
elif [ ${network} = 10.147.83 ]; then
        dg=dg-vlan312
        return "${dg}"

# vlan521
elif [ ${network} = 10.80.120 ]; then
        dg=dg-vlan521
        return "${dg}"
elif [ ${network} = 10.80.121 ]; then
        dg=dg-vlan521
        return "${dg}"

# vlan 523
elif [ ${network} = 10.80.122 ]; then
        dg=dg-vlan523
        return "${dg}"
elif [ ${network} = 10.80.123 ]; then
        dg=dg-vlan523
        return "${dg}"

# vlan1154 and vlan2154
elif [ ${network} = 10.80.154 ]; then
last_octet=`echo ${device_ip} | awk -F. '{print $4}'`
   if [ ${last_octet} -le 127 ]; then
        dg=dg-vlan1154
        return "${dg}"
   else
        dg=dg-vlan2154
        return "${dg}"
   fi

# vlan 450
elif [ ${network} = 10.80.150 ]; then
        dg=dg-vlan450
        return "${dg}"

# vlan 451
elif [ ${network} = 10.80.151 ]; then
        dg=dg-vlan451
        return "${dg}"

# vlan336
elif [ ${network} = 10.129.36 ]; then
        dg=dg-vlan336
        return "${dg}"

# vlan368
elif [ ${network} = 10.129.68 ]; then
        dg=dg-vlan368
        return "${dg}"
elif [ ${network} = 10.129.69 ]; then
        dg=dg-vlan368
        return "${dg}"
elif [ ${network} = 10.129.70 ]; then
        dg=dg-vlan368
        return "${dg}"
elif [ ${network} = 10.129.71 ]; then
        dg=dg-vlan368
        return "${dg}"

# Dublin, Ireland
elif [ ${network} = 10.169.82 ]; then
        dg=dg-dublin
        return "${dg}"
elif [ ${network} = 10.169.83 ]; then
        dg=dg-dublin
        return "${dg}"

# no parent found
else
        echo "Cannot find parent of ${device}, you have to configure parent manually"
        return "${dg}"
fi

#echo ; echo Parent is ${dg} ; echo

exit 0


Back to the main page