#!/usr/bin/env bash ssl=0 www=0 cname=0 config_dir=/etc/nagios3/conf.d config_file_web=$config_dir/web_services.cfg export params='' function help() { echo $0': [-s|-W|-D] <-w website> [-h hostname] [-c cname1,cname2,etc...]' echo -e "\t-s Add SSL monitoring" echo -e "\t-W Also add a monitor for the WWW record." echo -e "\t-D Delete the monitor(s) for the hostname/website provided." echo -e "\t-h Provide a hostname, useful for VHOSTs" echo -e "\t-c A comma seperated list of cnames to add monitors for." } function set-params() { # Array is: use, description, host_name, check_command params=("$1" "$2" "$3" "$4") } function set-http-params() { set-params 'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1" } function set-https-params() { set-params 'http-vhost' "SSL Cert: $1" $2 "check_http_vhost2!$2!$1" } function write-config-header() { echo "# AUTOGENERATED CONFIG FOR ${params[1]}" echo "# User: $USER" echo "# Date: `date '+%D %T'`" } function write-config-footer() { echo "# END ${params[1]}" } function write-config-body() { echo "define service {" echo -e "\tuse\t\t\t${params[0]}" echo -e "\tdescription\t\t${params[1]}" echo -e "\thost_name\t\t${params[2]}" echo -e "\tcheck_command\t\t${params[3]}" echo "}" } #define service { # use http-vhost2 # description www.defectivebydesign.org # host_name www0.defectivebydesign.org # check_command check_http_vhost2!www0.defectivebydesign.org!www.defectivebydesign.org #} # Service: SSL CERT #define service{ # use http-vhost # host_name www0.defectivebydesign.org # description www.defectivebydesign.org SSL cert # check_command check_ssl_cert!www.defectivebydesign.org #} # Process our args while getopts "Ww:h:sc:" opt; do case "${opt}" in s) ssl=1 ;; h) host=${OPTARG} ;; w) website=${OPTARG} ;; W) www=1 ;; c) cnames=${OPTARG} cname=1 ;; *) help exit 1 esac done # Print help and quit if website is not provided if [ -z ${website} ]; then help exit 1 fi # Set host_name to website if not provided if [ -z ${host} ]; then host=$website fi # Prep CNAMEs (if provided) for a for loop by replacing the delimiter if [ $cname -eq 1 ]; then if [ -z ${cnames} ]; then echo "Error: CNAMEs garbled." exit 2 fi cnames=$(echo $cnames | sed 's/,/ /g') fi # Basic HTTP check, include basic header set-http-params $website $host write-config-header >> $config_file_web write-config-body >> $config_file_web # www CNAME, special request :D if [ $www -eq 1 ]; then set-https-params www.$website $host write-config-body >> $config_file_web fi # Additional CNAMEs if requested if [ $cname -eq 1 ]; then for name in $cnames; do set-http-params $name.$website $host write-config-body >> $config_file_web done fi # SSL cert check, if requeted if [ $ssl -eq 1 ]; then set-https-params $website $host write-config-body >> $config_file_web fi # Write out our footer write-config-footer >> $config_file_web