Cleaned up some whitespace
[nagios-scripts.git] / nagios-website-add.sh
1 #!/usr/bin/env bash
2
3 ssl=0
4 www=0
5 cname=0
6 config_dir=/etc/nagios3/conf.d
7 config_file_web=$config_dir/web_services.cfg
8
9 export params=''
10
11 function help() {
12 echo $0': [-s|-W|-D] <-w website> [-h hostname] [-c cname1,cname2,etc...]'
13 echo -e "\t-s Add SSL monitoring"
14 echo -e "\t-W Also add a monitor for the WWW record."
15 echo -e "\t-D Delete the monitor(s) for the hostname/website provided."
16 echo -e "\t-h Provide a hostname, useful for VHOSTs"
17 echo -e "\t-c A comma seperated list of cnames to add monitors for."
18 }
19
20 function set-params() {
21 # Array is: use, description, host_name, check_command
22 params=("$1" "$2" "$3" "$4")
23 }
24
25 function set-http-params() {
26 set-params 'http-vhost2' "URL: $1" $2 "check_http_vhost2!$2!$1"
27 }
28
29 function set-https-params() {
30 set-params 'http-vhost' "SSL Cert: $1" $2 "check_http_vhost2!$2!$1"
31 }
32
33 function write-config-header() {
34 echo "# AUTOGENERATED CONFIG FOR ${params[1]}"
35 echo "# User: $USER"
36 echo "# Date: `date '+%D %T'`"
37 }
38
39 function write-config-footer() {
40 echo "# END ${params[1]}"
41 }
42
43 function write-config-body() {
44 echo "define service {"
45 echo -e "\tuse\t\t\t${params[0]}"
46 echo -e "\tdescription\t\t${params[1]}"
47 echo -e "\thost_name\t\t${params[2]}"
48 echo -e "\tcheck_command\t\t${params[3]}"
49 echo "}"
50 }
51
52 #define service {
53 # use http-vhost2
54 # description www.defectivebydesign.org
55 # host_name www0.defectivebydesign.org
56 # check_command check_http_vhost2!www0.defectivebydesign.org!www.defectivebydesign.org
57 #}
58
59 # Service: SSL CERT
60 #define service{
61 # use http-vhost
62 # host_name www0.defectivebydesign.org
63 # description www.defectivebydesign.org SSL cert
64 # check_command check_ssl_cert!www.defectivebydesign.org
65 #}
66
67 # Process our args
68 while getopts "Ww:h:sc:" opt; do
69 case "${opt}" in
70 s)
71 ssl=1
72 ;;
73 h)
74 host=${OPTARG}
75 ;;
76 w)
77 website=${OPTARG}
78 ;;
79 W)
80 www=1
81 ;;
82 c)
83 cnames=${OPTARG}
84 cname=1
85 ;;
86 *)
87 help
88 exit 1
89 esac
90 done
91
92 # Print help and quit if website is not provided
93 if [ -z ${website} ]; then
94 help
95 exit 1
96 fi
97
98 # Set host_name to website if not provided
99 if [ -z ${host} ]; then
100 host=$website
101 fi
102
103 # Prep CNAMEs (if provided) for a for loop by replacing the delimiter
104 if [ $cname -eq 1 ]; then
105 if [ -z ${cnames} ]; then
106 echo "Error: CNAMEs garbled."
107 exit 2
108 fi
109 cnames=$(echo $cnames | sed 's/,/ /g')
110 fi
111
112
113 # Basic HTTP check, include basic header
114 set-http-params $website $host
115 write-config-header
116 write-config-body
117
118 # Additional CNAMEs if requested
119 if [ $cname -eq 1 ]; then
120 for name in $cnames; do
121 set-http-params $name.$website $host
122 write-config-body
123 done
124 fi
125
126 # SSL cert check, if requeted
127 if [ $ssl -eq 1 ]; then
128 set-https-params $website $host
129 write-config-body
130 fi
131
132 # Write out our footer
133 write-config-footer