86bbb17d21034e64affbd12b3860c304edfdbd60
[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 *)
98 help
99 exit 1
100 esac
101 done
102
103 # Print help and quit if website is not provided
104 if [ -z ${website} ]; then
105 help
106 exit 1
107 fi
108
109 # Set host_name to website if not provided
110 if [ -z ${host} ]; then
111 host=$website
112 fi
113
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."
118 exit 3
119 fi
120
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."
125 exit 2
126 fi
127 cnames=$(echo $cnames | sed 's/,/ /g')
128 fi
129
130 # Set our generic configs (these are overwritten as we go)
131 set-http-params $website $host
132
133 # Delete records on request
134 if [ $delete -eq 1 ]; then
135 delete-monitors
136 else
137 # Double check that a duplicate monitor does not exist (does nothing if it doesnt exist)
138 delete-monitors
139
140 # Basic HTTP check, include basic header
141 write-config-header >> $config_file_web
142 write-config-body >> $config_file_web
143
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
148 fi
149
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
155 done
156 fi
157
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
162 fi
163
164 # AWSTATS check, if requested
165 if [ $awstats -eq 1 ]; then
166 if [ $www -eq 1 ]; then
167 set-awstats-params www.$website
168 else
169 set-awstats-params $website
170 fi
171 write-config-body >> $config_file_web
172 fi
173
174 # Write out our footer
175 write-config-footer >> $config_file_web
176 fi
177
178 if [ $reload -eq 1 ]; then
179 # Reload nagios if required
180 $init_script reload
181 fi