Added reload, fixed bugs
[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
11 config_dir=/etc/nagios3/conf.d
12 config_file_web=$config_dir/web_services.cfg
13 init_script=/etc/init.d/nagios3
14
15 export params=''
16
17 function help() {
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"
26 }
27
28 function set-params() {
29 # Array is: use, description, host_name, check_command
30 params=("$1" "$2" "$3" "$4")
31 }
32
33 function set-http-params() {
34 set-params 'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
35 }
36
37 function set-https-params() {
38 set-params 'http-vhost' "SSL Cert: $1" $2 "check_http_vhost2!$2!$1"
39 }
40
41 function set-awstats-params() {
42 set-params 'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1"
43 }
44
45 function write-config-header() {
46 echo "# AUTOGENERATED CONFIG FOR HOST: ${params[2]}"
47 echo "# User: $USER"
48 echo "# Date: `date '+%D %T'`"
49 }
50
51 function write-config-footer() {
52 echo "# END ${params[2]}"
53 }
54
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]}"
61 echo "}"
62 echo
63 }
64
65 function delete-monitors() {
66 sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host'$/,/^# END '$host'$/d' -i $config_file_web
67 }
68
69 # Process our args
70 while getopts "rADWw:h:sc:" opt; do
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 ;;
88 D)
89 delete=1
90 ;;
91 A)
92 awstats=1
93 ;;
94 r)
95 reload=1
96 *)
97 help
98 exit 1
99 esac
100 done
101
102 # Print help and quit if website is not provided
103 if [ -z ${website} ]; then
104 help
105 exit 1
106 fi
107
108 # Set host_name to website if not provided
109 if [ -z ${host} ]; then
110 host=$website
111 fi
112
113 # Confirm we have a valid hostname (could be better, but this is good enough I think)
114 grep -R "host_name[ ]*$host" $config_dir &>/dev/null
115 if [ $? -ne 0 ]; then
116 echo "Error: Could not find host \`$host' in nagios configs."
117 exit 3
118 fi
119
120 # Prep CNAMEs (if provided) for a for loop by replacing the delimiter
121 if [ $cname -eq 1 ]; then
122 if [ -z ${cnames} ]; then
123 echo "Error: CNAMEs garbled."
124 exit 2
125 fi
126 cnames=$(echo $cnames | sed 's/,/ /g')
127 fi
128
129 # Set our generic configs (these are overwritten as we go)
130 set-http-params $website $host
131
132 # Delete records on request
133 if [ $delete -eq 1 ]; then
134 delete-monitors
135 else
136 # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist)
137 delete-monitors
138
139 # Basic HTTP check, include basic header
140 write-config-header >> $config_file_web
141 write-config-body >> $config_file_web
142
143 # www CNAME, special request :D
144 if [ $www -eq 1 ]; then
145 set-https-params www.$website $host
146 write-config-body >> $config_file_web
147 fi
148
149 # Additional CNAMEs if requested
150 if [ $cname -eq 1 ]; then
151 for name in $cnames; do
152 set-http-params $name.$website $host
153 write-config-body >> $config_file_web
154 done
155 fi
156
157 # SSL cert check, if requeted
158 if [ $ssl -eq 1 ]; then
159 set-https-params $website $host
160 write-config-body >> $config_file_web
161 fi
162
163 # AWSTATS check, if requested
164 if [ $awstats -eq 1 ]; then
165 if [ $www -eq 1 ]; then
166 set-awstats-params www.$website
167 else
168 set-awstats-params $website
169 fi
170 write-config-body >> $config_file_web
171 fi
172
173 # Write out our footer
174 write-config-footer >> $config_file_web
175 fi
176
177 if [ $reload -eq 1 ]; then
178 # Reload nagios if required
179 $init_script reload
180 fi