Fixed mixup between SSL and http checks
[nagios-scripts.git] / nagios-website-add.sh
CommitLineData
b25f4ffe
LMM
1#!/usr/bin/env bash
2
90b2bdc9 3# Modes (0 = off/false, 1 = true/on)
b25f4ffe
LMM
4ssl=0
5www=0
6cname=0
90b2bdc9 7delete=0
13fe7b7b 8awstats=0
ce89a666 9reload=0
90b2bdc9 10
b25f4ffe
LMM
11config_dir=/etc/nagios3/conf.d
12config_file_web=$config_dir/web_services.cfg
ce89a666 13init_script=/etc/init.d/nagios3
b25f4ffe
LMM
14
15export params=''
16
17function help() {
ce89a666 18 echo $0': [-s|-W|-A|-r] <[-D] -w website> [-h hostname] [-c cname1,cname2,etc...]'
b25f4ffe 19 echo -e "\t-s Add SSL monitoring"
ce89a666
LMM
20 echo -e "\t-W Also add a monitor for the WWW record"
21 echo -e "\t-D Delete the monitor(s) for the hostname/website provided"
b25f4ffe 22 echo -e "\t-h Provide a hostname, useful for VHOSTs"
ce89a666 23 echo -e "\t-c A comma seperated list of cnames to add monitors for"
13fe7b7b 24 echo -e "\t-A Adds an AWSTATS monitor for this host"
ce89a666 25 echo -e "\t-r Reloads nagios after config files are updated"
b25f4ffe
LMM
26}
27
28function set-params() {
29 # Array is: use, description, host_name, check_command
30 params=("$1" "$2" "$3" "$4")
31}
32
33function set-http-params() {
34 set-params 'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
35}
36
37function set-https-params() {
4b92d3e2 38 set-params 'http-vhost' "SSL Cert: $1" $2 "check_ssl_cert!$1"
b25f4ffe
LMM
39}
40
13fe7b7b
LMM
41function set-awstats-params() {
42 set-params 'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1"
43}
44
b25f4ffe 45function write-config-header() {
33a1368a 46 echo "# AUTOGENERATED CONFIG FOR HOST: ${params[2]}"
b25f4ffe
LMM
47 echo "# User: $USER"
48 echo "# Date: `date '+%D %T'`"
49}
50
51function write-config-footer() {
90b2bdc9 52 echo "# END ${params[2]}"
b25f4ffe
LMM
53}
54
55function write-config-body() {
56 echo "define service {"
57 echo -e "\tuse\t\t\t${params[0]}"
58 echo -e "\tdescription\t\t${params[1]}"
59 echo -e "\thost_name\t\t${params[2]}"
60 echo -e "\tcheck_command\t\t${params[3]}"
61 echo "}"
ce89a666 62 echo
b25f4ffe
LMM
63}
64
90b2bdc9
LMM
65function delete-monitors() {
66 sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host'$/,/^# END '$host'$/d' -i $config_file_web
67}
b25f4ffe
LMM
68
69# Process our args
ce89a666 70while getopts "rADWw:h:sc:" opt; do
b25f4ffe
LMM
71 case "${opt}" in
72 s)
73 ssl=1
74 ;;
75 h)
76 host=${OPTARG}
77 ;;
78 w)
79 website=${OPTARG}
80 ;;
81 W)
82 www=1
83 ;;
84 c)
85 cnames=${OPTARG}
86 cname=1
87 ;;
90b2bdc9
LMM
88 D)
89 delete=1
90 ;;
13fe7b7b
LMM
91 A)
92 awstats=1
93 ;;
ce89a666
LMM
94 r)
95 reload=1
b3b73367 96 ;;
b25f4ffe
LMM
97 *)
98 help
99 exit 1
100 esac
101done
102
103# Print help and quit if website is not provided
104if [ -z ${website} ]; then
105 help
106 exit 1
107fi
108
109# Set host_name to website if not provided
110if [ -z ${host} ]; then
111 host=$website
112fi
113
ce89a666
LMM
114# Confirm we have a valid hostname (could be better, but this is good enough I think)
115grep -R "host_name[ ]*$host" $config_dir &>/dev/null
116if [ $? -ne 0 ]; then
117 echo "Error: Could not find host \`$host' in nagios configs."
118 exit 3
119fi
120
b25f4ffe
LMM
121# Prep CNAMEs (if provided) for a for loop by replacing the delimiter
122if [ $cname -eq 1 ]; then
123 if [ -z ${cnames} ]; then
124 echo "Error: CNAMEs garbled."
125 exit 2
126 fi
127 cnames=$(echo $cnames | sed 's/,/ /g')
128fi
b25f4ffe 129
90b2bdc9 130# Set our generic configs (these are overwritten as we go)
b25f4ffe 131set-http-params $website $host
b25f4ffe 132
90b2bdc9
LMM
133# Delete records on request
134if [ $delete -eq 1 ]; then
135 delete-monitors
136else
137 # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist)
138 delete-monitors
139
140 # Basic HTTP check, include basic header
141 write-config-header >> $config_file_web
4cfcdb06 142 write-config-body >> $config_file_web
c0558cd0 143
90b2bdc9
LMM
144 # www CNAME, special request :D
145 if [ $www -eq 1 ]; then
4b92d3e2 146 set-http-params www.$website $host
4cfcdb06 147 write-config-body >> $config_file_web
90b2bdc9 148 fi
b25f4ffe 149
90b2bdc9
LMM
150 # Additional CNAMEs if requested
151 if [ $cname -eq 1 ]; then
152 for name in $cnames; do
153 set-http-params $name.$website $host
154 write-config-body >> $config_file_web
155 done
156 fi
157
158 # SSL cert check, if requeted
159 if [ $ssl -eq 1 ]; then
160 set-https-params $website $host
161 write-config-body >> $config_file_web
162 fi
13fe7b7b
LMM
163
164 # AWSTATS check, if requested
ba0f458c 165 if [ $awstats -eq 1 ]; then
13fe7b7b
LMM
166 if [ $www -eq 1 ]; then
167 set-awstats-params www.$website
168 else
169 set-awstats-params $website
170 fi
171 write-config-body >> $config_file_web
172 fi
173
90b2bdc9
LMM
174 # Write out our footer
175 write-config-footer >> $config_file_web
ce89a666
LMM
176fi
177
178if [ $reload -eq 1 ]; then
179 # Reload nagios if required
180 $init_script reload
90b2bdc9 181fi