Skip to content

Instantly share code, notes, and snippets.

@orymate
Created July 28, 2011 23:43
Show Gist options
  • Select an option

  • Save orymate/1112836 to your computer and use it in GitHub Desktop.

Select an option

Save orymate/1112836 to your computer and use it in GitHub Desktop.
#!/bin/bash
ETC=/etc/nagios3/conf.d/
HGROUPS=/etc/nagios3/conf.d/hostgroups_nagios2.cfg
NMAPOPTS=''
# $1: hostname
# $2: group name
nagios-togroup () {
echo "/hostgroup_name[ \t][ \t]*$2/"
echo "/members/ s/$/,$1/"
echo "# member of $2" >&2
}
nagios-host-groups () {
awk '$1 == "hostgroup_name" {printf "%s/", $2}' $HGROUPS
}
if [ ! -d $ETC -o ! -w $ETC -o ! -w $HGROUPS ]
then
echo "You should be able to write $ETC and $HGROUPS"
exit 1
fi
if [ $# -eq 0 -o "$1" = -h -o "$1" = --help ]
then
echo Usage: $0 hosts... >&2
exit 1
fi
while [ ! -z "$1" ]
do
HOST="$1"
shift
if ! ping -q -c1 "$HOST"
then
read -p "Try adding unpingable host? [y/N] "
[ "$REPLY" = y ] || continue
NMAPOPTS="$NMAPOPTS -PN"
fi
IP="$(host $HOST 2>/dev/null | awk '/has address/ { print $4;end=1} END {if (!end)print "'"$HOST"'";}' 2>/dev/null)"
if grep -R "$IP" $ETC
then
read -p "Are you sure adding $HOST as a new host? [y/N] "
[ "$REPLY" = y ] || continue
fi
NAME=$(sed 's/[^a-zA-Z0-9].*$//' <<<"$HOST")
read -e -i "$NAME" -p "New nagios name of $HOST: " NAME
while [ -f "/etc/nagios3/conf.d/${NAME}.cfg" ]
do
echo "$ETC/${NAME}.cfg exists."
read -e -i "$NAME" -p "New nagios name of $HOST: " NAME
done
tmp=$(mktemp)
script=$(mktemp)
cfg=$(mktemp)
cat >$cfg <<EOF
# Generated with $0 by ${USER}@$(hostname -f), $(date)
define host{
use generic-host ; Name of host template to use
host_name ${HOST}
alias ${NAME}
address ${IP}
}
EOF
echo Scanning open ports of $NAME...
nmap $NMAPOPTS "$HOST" | tee /dev/stderr | sed -e '1,/^PORT/ d' -e '/^$/,$ d' -e '/MAC/ d' >$tmp
for i in $(seq $(wc -l <$tmp))
do
LINE=$(sed -n -e "$i p" $tmp)
echo "$LINE"
case $(awk '{print $3}' <<<"$LINE") in
http)
read -p "Add $NAME to http-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" http-servers >>$script 2>>$cfg
;;
https)
read -p "Add $NAME to https-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" https-servers >>$script 2>>$cfg
;;
ssh)
read -p "Add $NAME to ssh-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" ssh-servers >>$script 2>>$cfg
;;
imap)
read -p "Add $NAME to imap-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" imap-servers >>$script 2>>$cfg
;;
smtp)
read -p "Add $NAME to smtp-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" smtp-servers >>$script 2>>$cfg
;;
ldap)
read -p "Add $NAME to ldap-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" ldap-servers >>$script 2>>$cfg
;;
domain)
read -p "Add $NAME to dns-servers group? [Y/n] "
[ "$REPLY" = n ] && continue
nagios-togroup "$HOST" dns-servers >>$script 2>>$cfg
;;
*)
echo No default behavior for this service.
echo "Select a group, '#' for keeping a comment about this service,"
read -p "or NOP to ignore it [$(nagios-host-groups)#/NOP]"
[ "$REPLY" = "#" ] && sed -n -e 's/^/# TODO /' -e "$i p" $tmp >>$cfg && continue
[ -z "$REPLY" -o "$(tr NOP nop <<<"$REPLY")" = nop ] || nagios-togroup "$HOST" "$REPLY" >>$script 2>>$cfg
;;
esac
done
rm $tmp
read -p "Write changes? [Y/n]"
[ "$REPLY" = n ] && echo Note: Temporary files have been kept at $script and $cfg && echo You can remove them with && echo " rm $script $cfg" && continue
mv $cfg $ETC/$NAME.cfg
chmod 644 $ETC/$NAME.cfg
echo wq >>$script
ed $HGROUPS <$script && rm $script
done
echo You can reload the configuration with
echo " " /etc/init.d/nagios* reload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment