3 # Modes (0 = off/false, 1 = true/on)
12 config_dir
=/etc
/nagios
3/conf.d
13 config_file_web
=$config_dir/web_services.cfg
14 init_script
=/etc
/init.d
/nagios3
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"
30 function set-params
() {
31 # Array is: use, description, host_name, check_command
32 params
=("$1" "$2" "$3" "$4")
35 function set-http-params
() {
36 set-params
'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
39 function set-https-params
() {
40 set-params
'http-vhost' "SSL Cert: $1" $2 "check_ssl_cert!$1"
43 function set-awstats-params
() {
44 set-params
'check-termite' "AWSTATS: $1" "termite.fsf.org" "check_termite!$1"
47 function set-content-params
() {
48 set-params
'http-vhost' "URL Content: $1" $2 "check_http_string!$2!$3"
51 function write-config-header
() {
52 echo "# AUTOGENERATED CONFIG FOR HOST: $host:$website"
54 echo "# Date: `date '+%D %T'`"
57 function write-config-footer
() {
58 echo "# END $host:$website"
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]}"
71 function delete-monitors
() {
72 sed '/^# AUTOGENERATED CONFIG FOR HOST: '$host':'$website'$/,/^# END '$host':'$website'$/d' -i $config_file_web
76 while getopts "rADWw:h:sc:C:" opt
; do
104 content_data
="${OPTARG}"
113 # Print help and quit if website is not provided
114 if [ -z ${website} ]; then
119 # Set host_name to website if not provided
120 if [ -z ${host} ]; then
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."
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."
137 cnames
=$
(echo $cnames |
sed 's/,/ /g')
140 # Set our generic configs (these are overwritten as we go)
141 set-http-params
$website $host
143 # Delete records on request
144 if [ $delete -eq 1 ]; then
147 # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist)
150 # Basic HTTP check, include basic header
151 write-config-header
>> $config_file_web
152 write-config-body
>> $config_file_web
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
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
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
174 if [ $content -eq 1 ]; then
175 set-content-params
$website $host "$content_data"
176 write-config-body
>> $config_file_web
179 # AWSTATS check, if requested
180 if [ $awstats -eq 1 ]; then
181 if [ $www -eq 1 ]; then
182 set-awstats-params www.
$website
184 set-awstats-params
$website
186 write-config-body
>> $config_file_web
189 # Write out our footer
190 write-config-footer
>> $config_file_web
193 if [ $reload -eq 1 ]; then
194 # Reload nagios if required