commit fe3828cffd638dddfb554500d99c6ca8296ee4cc Author: Jack-Benny Persson Date: Tue Dec 31 23:57:29 2013 +0100 First commit diff --git a/dummy_checks/check_domain b/dummy_checks/check_domain new file mode 100644 index 0000000..e69de29 diff --git a/dummy_checks/check_ftp b/dummy_checks/check_ftp new file mode 100644 index 0000000..e69de29 diff --git a/dummy_checks/check_http b/dummy_checks/check_http new file mode 100644 index 0000000..e69de29 diff --git a/dummy_checks/check_ssh b/dummy_checks/check_ssh new file mode 100644 index 0000000..e69de29 diff --git a/nagios-bootstrap.sh b/nagios-bootstrap.sh new file mode 100755 index 0000000..e0bce4e --- /dev/null +++ b/nagios-bootstrap.sh @@ -0,0 +1,92 @@ +#!/bin/bash + +# Binaries +Nmap="/usr/bin/nmap" +Sed="/bin/sed" +Awk="/usr/bin/awk" +Printf="/usr/bin/printf" +Ls="/bin/ls" +Grep="/bin/grep" +Cut="/usr/bin/cut" + +# Variables +Version="0.1" +Plugindir="dummy_checks" + +# Sanity check +for Bin in $Nmap $Sed $Awk $Printf; do + if [ ! -x $Bin ]; then + echo "Can't execute $Bin" + fi +done + +print_usage() +{ + $Printf "Usage: `basename $0` -H -o -h (help)\n" +} + +print_help() +{ + $Printf "-h This help screen\n" + $Printf "-H Host to scan and bootstrap for Nagios\n" + $Printf "-o Output file to save the configuration file in\n" +} + +# Parse options and arguments +while getopts H:ho: Opts; do + case "$Opts" in + h) print_help + exit 1 + ;; + o) Outfile="$OPTARG" + exec 1> $Outfile + ;; + H) Host="$OPTARG" + ;; + *) print_usage + exit 1 + ;; + esac +done + +### Main ### + +# Scan the host and save all the output in a variable +Output=`$Nmap $Host` + +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"." '{ printf $1 }'` + +Services=`nmap labrat.nixnet.jke | sed -n '7,$p' | head -n -2 \ + | awk '{ print $3 }'` + +Index=0 +for i in ${Services[@]}; do + TempServ=`$Ls ${Plugindir}/ | $Grep $i` + if [ $? -eq 0 ]; then + CheckCommand[$Index]=$TempServ + ((Index++)) + fi +done + +$Printf "define host {\n" +$Printf "\thost_name\t${Hostname}\n" +$Printf "\talias\t\t${Alias}\n" +$Printf "\taddress\t\t${IP}\n" +$Printf "\tuse\t\tgeneric-host\n" +$Printf "\t}\n\n" + +CheckIndex=0 +for i in ${CheckCommand[@]}; do + Desc=`echo ${CheckCommand[$CheckIndex]} | $Cut -c7-50` + $Printf "define service {\n" + $Printf "\tuse\t\t\tgeneric-service\n" + $Printf "\thost_name\t\t${Hostname}\n" + $Printf "\tservice_description\t${Desc}\n" + $Printf "\tcheck_command\t\t${CheckCommand[$CheckIndex]}\n" + $Printf "}\n\n" + ((CheckIndex++)) +done + +exit 0 diff --git a/sample.cfg b/sample.cfg new file mode 100644 index 0000000..d51392a --- /dev/null +++ b/sample.cfg @@ -0,0 +1,15 @@ +define host { + host_name labrat.example.com + alias labrat + address 192.168.0.27 + use generic-host + } + +define service { + + use generic-service + host_name labrat.example.com + service_description HTTP + check_command check_http + } + diff --git a/sampleoutput.txt b/sampleoutput.txt new file mode 100644 index 0000000..fbaad38 --- /dev/null +++ b/sampleoutput.txt @@ -0,0 +1,12 @@ + +Starting Nmap 6.00 ( http://nmap.org ) at 2013-12-31 19:47 CET +Nmap scan report for labrat.nixnet.jke (192.168.0.27) +Host is up (0.026s latency). +Not shown: 996 closed ports +PORT STATE SERVICE +22/tcp open ssh +80/tcp open http +111/tcp open rpcbind +514/tcp open shell + +Nmap done: 1 IP address (1 host up) scanned in 1.76 seconds diff --git a/testconf.cfg b/testconf.cfg new file mode 100644 index 0000000..1881be4 --- /dev/null +++ b/testconf.cfg @@ -0,0 +1,21 @@ +define host { + host_name labrat.nixnet.jke + alias labrat + ddress 192.168.0.27 + use generic-host + } + +define service { + use generic-service + host_name labrat.nixnet.jke + service_description ssh + check_command check_ssh +} + +define service { + use generic-service + host_name labrat.nixnet.jke + service_description http + check_command check_http +} +