#!/usr/bin/env bash # Modes (0 = off/false, 1 = true/on) ssl=0 www=0 cname=0 delete=0 awstats=0 reload=0 content=0 config_dir=/etc/nagios3/conf.d config_file_web=$config_dir/web_services.cfg init_script=/etc/init.d/nagios3 export params='' function help() { echo $0': [-s|-W|-A|-r] <[-D] -w website> [-h hostname] [-c cname1,cname2,etc...] [-C /path!string]' 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" echo -e "\t-A Adds an AWSTATS monitor for this host" echo -e "\t-C Adds a Content check for string matching" echo -e "\t-r Reloads nagios after config files are updated" } 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_ssl_cert!$1" } function set-awstats-params() { set-params 'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1" } function set-content-params() { set-params 'http-vhost' "URL Content: $1" $2 "check_http_string!$2!$3" } function write-config-header() { echo "# AUTOGENERATED CONFIG FOR HOST: $host:$website" echo "# User: $USER" echo "# Date: `date '+%D %T'`" } function write-config-footer() { echo "# END $host:$website" } 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 "}" echo } function delete-monitors() { sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host':'$website'$/,/^# END '$host':'$website'$/d' -i $config_file_web } # Process our args while getopts "rADWw:h:sc:C:" opt; do case "${opt}" in s) ssl=1 ;; h) host=${OPTARG} ;; w) website=${OPTARG} ;; W) www=1 ;; c) cnames=${OPTARG} cname=1 ;; D) delete=1 ;; A) awstats=1 ;; r) reload=1 ;; C) content_data="${OPTARG}" content=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 # Confirm we have a valid hostname (could be better, but this is good enough I think) grep -R "host_name[ ]*$host" $config_dir &>/dev/null if [ $? -ne 0 ]; then echo "Error: Could not find host \`$host' in nagios configs." exit 3 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 # Set our generic configs (these are overwritten as we go) set-http-params $website $host # Delete records on request if [ $delete -eq 1 ]; then delete-monitors else # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist) delete-monitors # Basic HTTP check, include basic header write-config-header >> $config_file_web write-config-body >> $config_file_web # www CNAME, special request :D if [ $www -eq 1 ]; then set-http-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 if [ $content -eq 1 ]; then set-content-params $website $host "$content_data" write-config-body >> $config_file_web fi # AWSTATS check, if requested if [ $awstats -eq 1 ]; then if [ $www -eq 1 ]; then set-awstats-params www.$website else set-awstats-params $website fi write-config-body >> $config_file_web fi # Write out our footer write-config-footer >> $config_file_web fi if [ $reload -eq 1 ]; then # Reload nagios if required $init_script reload fi