Added copyright head, replaced hardcoded binaries in the code, added extra sanity checks etc
This commit is contained in:
parent
c8a518704c
commit
ceae25a8b6
@ -1,5 +1,28 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
################################################################################
|
||||||
|
# #
|
||||||
|
# Copyright (C) 2014 Jack-Benny Persson <jack-benny@cyberinfo.se> #
|
||||||
|
# #
|
||||||
|
# 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
|
# Binaries
|
||||||
Nmap="/usr/bin/nmap"
|
Nmap="/usr/bin/nmap"
|
||||||
Sed="/bin/sed"
|
Sed="/bin/sed"
|
||||||
@ -8,6 +31,8 @@ Printf="/usr/bin/printf"
|
|||||||
Ls="/bin/ls"
|
Ls="/bin/ls"
|
||||||
Grep="/bin/grep"
|
Grep="/bin/grep"
|
||||||
Cut="/usr/bin/cut"
|
Cut="/usr/bin/cut"
|
||||||
|
Tr="/usr/bin/tr"
|
||||||
|
Head="/usr/bin/head"
|
||||||
|
|
||||||
# Variables
|
# Variables
|
||||||
Version="0.1"
|
Version="0.1"
|
||||||
@ -22,7 +47,7 @@ done
|
|||||||
|
|
||||||
print_usage()
|
print_usage()
|
||||||
{
|
{
|
||||||
$Printf "Usage: `basename $0` -H <host> -o <output> -h (help)\n"
|
$Printf "Usage: `basename $0` -H <host> -o <output-file> -h (help)\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
print_help()
|
print_help()
|
||||||
@ -36,31 +61,47 @@ print_help()
|
|||||||
while getopts H:ho: Opts; do
|
while getopts H:ho: Opts; do
|
||||||
case "$Opts" in
|
case "$Opts" in
|
||||||
h) print_help
|
h) print_help
|
||||||
exit 1
|
exit 0
|
||||||
;;
|
;;
|
||||||
o) Outfile="$OPTARG"
|
o) Outfile="$OPTARG"
|
||||||
exec 1> $Outfile
|
exec 1> $Outfile
|
||||||
;;
|
;;
|
||||||
H) Host="$OPTARG"
|
H) Host="$OPTARG"
|
||||||
;;
|
;;
|
||||||
*) print_usage
|
*) print_usage >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Check if have at least two args (one option + one argument)
|
||||||
|
if [ $# -lt 2 ]; then
|
||||||
|
print_usage >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
### Main ###
|
### Main ###
|
||||||
|
|
||||||
# Scan the host and save all the output in a variable
|
# 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 }'`
|
# Check if the host is up before contiuing
|
||||||
Hostname=`echo "$Output" | $Sed -n '3p' | awk '{ print $5 }'`
|
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 }'`
|
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
|
Index=0
|
||||||
for i in ${Services[@]}; do
|
for i in ${Services[@]}; do
|
||||||
TempServ=`$Ls ${Plugindir}/ | $Grep $i`
|
TempServ=`$Ls ${Plugindir}/ | $Grep $i`
|
||||||
@ -70,6 +111,7 @@ for i in ${Services[@]}; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# Print the host definition
|
||||||
$Printf "define host {\n"
|
$Printf "define host {\n"
|
||||||
$Printf "\thost_name\t${Hostname}\n"
|
$Printf "\thost_name\t${Hostname}\n"
|
||||||
$Printf "\talias\t\t${Alias}\n"
|
$Printf "\talias\t\t${Alias}\n"
|
||||||
@ -77,6 +119,7 @@ $Printf "\taddress\t\t${IP}\n"
|
|||||||
$Printf "\tuse\t\tgeneric-host\n"
|
$Printf "\tuse\t\tgeneric-host\n"
|
||||||
$Printf "\t}\n\n"
|
$Printf "\t}\n\n"
|
||||||
|
|
||||||
|
# Loop through the services and print a service definition for each one
|
||||||
CheckIndex=0
|
CheckIndex=0
|
||||||
for i in ${CheckCommand[@]}; do
|
for i in ${CheckCommand[@]}; do
|
||||||
Desc=`echo ${CheckCommand[$CheckIndex]} | $Cut -c7-50`
|
Desc=`echo ${CheckCommand[$CheckIndex]} | $Cut -c7-50`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user