Fixed quotings
[nagios-scripts.git] / nagios-website-add.sh
1 #!/usr/bin/env bash
2
3 # Modes (0 = off/false, 1 = true/on)
4 ssl=0
5 www=0
6 cname=0
7 delete=0
8 awstats=0
9 reload=0
10 content=0
11
12 config_dir=/etc/nagios3/conf.d
13 config_file_web=$config_dir/web_services.cfg
14 init_script=/etc/init.d/nagios3
15
16 export params=''
17
18 function help() {
19 echo $0': [-s|-W|-A|-r] <[-D] -w website> [-h hostname] [-c cname1,cname2,etc...] [-C /path!string]'
20 echo -e "\t-s Add SSL monitoring"
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"
23 echo -e "\t-h Provide a hostname, useful for VHOSTs"
24 echo -e "\t-c A comma seperated list of cnames to add monitors for"
25 echo -e "\t-A Adds an AWSTATS monitor for this host"
26 echo -e "\t-C Adds a Content check for string matching"
27 echo -e "\t-r Reloads nagios after config files are updated"
28 }
29
30 function set-params() {
31 # Array is: use, description, host_name, check_command
32 params=("$1" "$2" "$3" "$4")
33 }
34
35 function set-http-params() {
36 set-params 'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
37 }
38
39 function set-https-params() {
40 set-params 'http-vhost' "SSL Cert: $1" $2 "check_ssl_cert!$1"
41 }
42
43 function set-awstats-params() {
44 set-params 'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1"
45 }
46
47 function set-content-params() {
48 set-params 'http-vhost' "URL Content: $1" $2 "check_http_string!$2!$3"
49 }
50
51 function write-config-header() {
52 echo "# AUTOGENERATED CONFIG FOR HOST: $host:$website"
53 echo "# User: $USER"
54 echo "# Date: `date '+%D %T'`"
55 }
56
57 function write-config-footer() {
58 echo "# END $host:$website"
59 }
60
61 function 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 "}"
68 echo
69 }
70
71 function delete-monitors() {
72 sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host':'$website'$/,/^# END '$host':'$website'$/d' -i $config_file_web
73 }
74
75 # Process our args
76 while getopts "rADWw:h:sc:C:" opt; do
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 ;;
94 D)
95 delete=1
96 ;;
97 A)
98 awstats=1
99 ;;
100 r)
101 reload=1
102 ;;
103 C)
104 content_data="${OPTARG}"
105 content=1
106 ;;
107 *)
108 help
109 exit 1
110 esac
111 done
112
113 # Print help and quit if website is not provided
114 if [ -z ${website} ]; then
115 help
116 exit 1
117 fi
118
119 # Set host_name to website if not provided
120 if [ -z ${host} ]; then
121 host=$website
122 fi
123
124 # Confirm we have a valid hostname (could be better, but this is good enough I think)
125 grep -R "host_name[ ]*$host" $config_dir &>/dev/null
126 if [ $? -ne 0 ]; then
127 echo "Error: Could not find host \`$host' in nagios configs."
128 exit 3
129 fi
130
131 # Prep CNAMEs (if provided) for a for loop by replacing the delimiter
132 if [ $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')
138 fi
139
140 # Set our generic configs (these are overwritten as we go)
141 set-http-params $website $host
142
143 # Delete records on request
144 if [ $delete -eq 1 ]; then
145 delete-monitors
146 else
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
152 write-config-body >> $config_file_web
153
154 # www CNAME, special request :D
155 if [ $www -eq 1 ]; then
156 set-http-params www.$website $host
157 write-config-body >> $config_file_web
158 fi
159
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
173
174 if [ $content -eq 1 ]; then
175 set-content-params $website $host "$content_data"
176 write-config-body >> $config_file_web
177 fi
178
179 # AWSTATS check, if requested
180 if [ $awstats -eq 1 ]; then
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
189 # Write out our footer
190 write-config-footer >> $config_file_web
191 fi
192
193 if [ $reload -eq 1 ]; then
194 # Reload nagios if required
195 $init_script reload
196 fi