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