diff --git a/nagios-bootstrap.sh b/nagios-bootstrap.sh index 3715f21..7cc79fd 100755 --- a/nagios-bootstrap.sh +++ b/nagios-bootstrap.sh @@ -1,5 +1,28 @@ #!/bin/bash +################################################################################ +# # +# Copyright (C) 2014 Jack-Benny Persson # +# # +# 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 2 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, write to the Free Software # +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # +# # +################################################################################ + +# nagios-bootstrap +# Version 0.1 + # Binaries Nmap="/usr/bin/nmap" Sed="/bin/sed" @@ -8,6 +31,8 @@ Printf="/usr/bin/printf" Ls="/bin/ls" Grep="/bin/grep" Cut="/usr/bin/cut" +Tr="/usr/bin/tr" +Head="/usr/bin/head" # Variables Version="0.1" @@ -22,7 +47,7 @@ done print_usage() { - $Printf "Usage: `basename $0` -H -o -h (help)\n" + $Printf "Usage: `basename $0` -H -o -h (help)\n" } print_help() @@ -36,31 +61,47 @@ print_help() while getopts H:ho: Opts; do case "$Opts" in h) print_help - exit 1 + exit 0 ;; o) Outfile="$OPTARG" exec 1> $Outfile ;; H) Host="$OPTARG" ;; - *) print_usage + *) print_usage >&2 exit 1 ;; esac done +# Check if have at least two args (one option + one argument) +if [ $# -lt 2 ]; then + print_usage >&2 + exit 1 +fi + ### Main ### # Scan the host and save all the output in a variable -Output=`$Nmap $Host` +Output=`$Nmap $Host 2> /dev/null` -IP=`echo "$Output" | $Sed -n '3p' | tr -d '()' | awk '{ print $6 }'` -Hostname=`echo "$Output" | $Sed -n '3p' | awk '{ print $5 }'` +# Check if the host is up before contiuing +echo $Output | $Grep "Host is up" &> /dev/null +if [ $? -ne 0 ]; then + echo "Host appears to be down or host not found" >&2 + exit 1 +fi + +# Set all the host and service variables +IP=`echo "$Output" | $Sed -n '3p' | $Tr -d '()' | $Awk '{ print $6 }'` +Hostname=`echo "$Output" | $Sed -n '3p' | $Awk '{ print $5 }'` Alias=`echo $Hostname | $Awk -F"." '{ print $1 }'` +Services=`$Nmap $Host | $Sed -n '7,$p' | $Head -n -2 \ + | $Awk '{ print $3 }'` -Services=`nmap $Host | sed -n '7,$p' | head -n -2 \ - | awk '{ print $3 }'` +# Loop through the services that were found open and check if we have +# a matching Nagios check for it Index=0 for i in ${Services[@]}; do TempServ=`$Ls ${Plugindir}/ | $Grep $i` @@ -70,6 +111,7 @@ for i in ${Services[@]}; do fi done +# Print the host definition $Printf "define host {\n" $Printf "\thost_name\t${Hostname}\n" $Printf "\talias\t\t${Alias}\n" @@ -77,6 +119,7 @@ $Printf "\taddress\t\t${IP}\n" $Printf "\tuse\t\tgeneric-host\n" $Printf "\t}\n\n" +# Loop through the services and print a service definition for each one CheckIndex=0 for i in ${CheckCommand[@]}; do Desc=`echo ${CheckCommand[$CheckIndex]} | $Cut -c7-50`