86bbb17d21034e64affbd12b3860c304edfdbd60
3 # Modes (0 = off/false, 1 = true/on)
11 config_dir
=/etc
/nagios
3/conf.d
12 config_file_web
=$config_dir/web_services.cfg
13 init_script
=/etc
/init.d
/nagios3
18 echo $0': [-s|-W|-A|-r] <[-D] -w website> [-h hostname] [-c cname1,cname2,etc...]'
19 echo -e "\t-s Add SSL monitoring"
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"
22 echo -e "\t-h Provide a hostname, useful for VHOSTs"
23 echo -e "\t-c A comma seperated list of cnames to add monitors for"
24 echo -e "\t-A Adds an AWSTATS monitor for this host"
25 echo -e "\t-r Reloads nagios after config files are updated"
28 function set-params
() {
29 # Array is: use, description, host_name, check_command
30 params
=("$1" "$2" "$3" "$4")
33 function set-http-params
() {
34 set-params
'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
37 function set-https-params
() {
38 set-params
'http-vhost' "SSL Cert: $1" $2 "check_http_vhost2!$2!$1"
41 function set-awstats-params
() {
42 set-params
'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1"
45 function write-config-header
() {
46 echo "# AUTOGENERATED CONFIG FOR HOST: ${params[2]}"
48 echo "# Date: `date '+%D %T'`"
51 function write-config-footer
() {
52 echo "# END ${params[2]}"
55 function 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]}"
65 function delete-monitors
() {
66 sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host'$/,/^# END '$host'$/d' -i $config_file_web
70 while getopts "rADWw:h:sc:" opt
; do
103 # Print help and quit if website is not provided
104 if [ -z ${website} ]; then
109 # Set host_name to website if not provided
110 if [ -z ${host} ]; then
114 # Confirm we have a valid hostname (could be better, but this is good enough I think)
115 grep -R "host_name[ ]*$host" $config_dir &>/dev
/null
116 if [ $?
-ne 0 ]; then
117 echo "Error: Could not find host \`$host' in nagios configs."
121 # Prep CNAMEs (if provided) for a for loop by replacing the delimiter
122 if [ $cname -eq 1 ]; then
123 if [ -z ${cnames} ]; then
124 echo "Error: CNAMEs garbled."
127 cnames
=$
(echo $cnames |
sed 's/,/ /g')
130 # Set our generic configs (these are overwritten as we go)
131 set-http-params
$website $host
133 # Delete records on request
134 if [ $delete -eq 1 ]; then
137 # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist)
140 # Basic HTTP check, include basic header
141 write-config-header
>> $config_file_web
142 write-config-body
>> $config_file_web
144 # www CNAME, special request :D
145 if [ $www -eq 1 ]; then
146 set-https-params www.
$website $host
147 write-config-body
>> $config_file_web
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
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
164 # AWSTATS check, if requested
165 if [ $awstats -eq 1 ]; then
166 if [ $www -eq 1 ]; then
167 set-awstats-params www.
$website
169 set-awstats-params
$website
171 write-config-body
>> $config_file_web
174 # Write out our footer
175 write-config-footer
>> $config_file_web
178 if [ $reload -eq 1 ]; then
179 # Reload nagios if required