hope I got the merge wright...
[monitor.git] / monitor-server.init
1 #!/bin/bash
2 #
3 # priority: 850
4 #
5 # Manage settings for the Zabbix installtion and 
6 #       other monitor-related things
7 #
8 # Stephen Soltesz <soltesz@cs.princeton.edu>
9 # Copyright (C) 2008 The Trustees of Princeton University
10 #
11 # $Id$
12 #
13
14 # Source function library and configuration
15 . /etc/plc.d/functions
16 . /etc/plc.d/monitor.functions
17 . /etc/planetlab/plc_config
18 local_config=/etc/planetlab/configs/site.xml
19
20 MONITORPATH=/usr/share/monitor
21 WEB_ROOT_PATH=web/MonitorWeb/monitorweb
22 WEB_XML_PATH=static/xml
23
24 # Be verbose
25 set -x
26
27 # Default locations
28 PGDATA=/var/lib/pgsql/data
29 postgresql_conf=$PGDATA/postgresql.conf
30 pghba_conf=$PGDATA/pg_hba.conf
31
32 # Export so that we do not have to specify -p to psql invocations
33 export PGPORT=$PLC_DB_PORT
34
35
36 MONITOR_DB_USER="monitoruser"
37 MONITOR_DB_NAME="monitor"
38
39 WROTE_PG_CONFIG=
40
41 if [ -z "$PLC_MONITOR_IP" ] ; then
42         PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
43 fi
44
45 function update_config ()
46 {
47         pattern=$1
48         with=$2
49         file=$3
50         sed -i -e "s/$pattern/$with/g" $file
51 }
52 function apply_template ()
53 {
54         TEMPLATE=$1
55         DESTFILE=$2
56
57         tmp_file=$(mktemp)
58         cp $TEMPLATE $tmp_file
59
60         update_config PLC_NAME "$PLC_NAME" $tmp_file
61         update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_file
62         update_config MONITOR_HOSTNAME $PLC_MONITOR_HOST $tmp_file
63
64         cp $tmp_file $DESTFILE
65         rm -f $tmp_file
66 }
67
68 function check_gadget_config ()
69 {
70         for input_file in $MONITORPATH/$WEB_ROOT_PATH/$WEB_XML_PATH/*.in ; do 
71                 output_file=$MONITORPATH/$WEB_ROOT_PATH/$WEB_XML_PATH/`basename $input_file | sed -e 's/.in\$//'`
72                 if [ $input_file -nt $output_file ] ; then 
73                         apply_template $input_file $output_file
74                 fi
75         done
76 }
77
78 function check_monitor_schema_and_data_init() 
79 {
80         # from monitor.functions
81         check_monitor_schema_and_data
82
83         $MONITORPATH/config.d/init-bootman-sequence.py
84 }
85
86 function check_monitor_conf ()
87 {
88         MONITOR_CONFIG=/etc/monitor.conf
89
90         # Using plcsh add default, monitor user
91         plcsh <<EOF &>/dev/null 
92 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
93 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
94 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
95 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
96 EOF
97
98         if [ ! -f ${MONITOR_CONFIG} ] ; then
99                 cat <<EOF > ${MONITOR_CONFIG}
100 [monitorconfig]
101 # RT Web user account
102 RT_WEB_SERVER=http://${PLC_RT_HOST}/
103 RT_WEB_TOOLS_PATH=/usr/bin/
104 RT_WEB_USER=${PLC_RT_WEB_USER}
105 RT_WEB_PASSWORD=${PLC_RT_WEB_PASSWORD}
106 RT_WEB_DEBUG=0
107 RT_QUEUE=${PLC_MONITOR_RT_QUEUE}
108
109 # PLC admin account
110 API_SERVER=https://${PLC_API_HOST}:${PLC_API_PORT}/PLCAPI/
111 API_AUTH_USER=${PLC_MONITOR_EMAIL}
112 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
113
114 # SERVER PATHS
115 MONITOR_SCRIPT_ROOT=${MONITORPATH}
116 MONITOR_DATA_ROOT=/var/lib/monitor
117 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
118 MONITOR_BOOTMANAGER_LOG=/var/www/html/monitorlog
119
120 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
121 MONITOR_IP=${PLC_MONITOR_IP}
122
123 PLC_WWW_HOSTNAME=${PLC_WWW_HOST}
124 PLC_NAME=${PLC_NAME}
125
126 # used for debug mode
127 email=${PLC_MONITOR_CC_EMAIL}
128 # all messages will appear to be from this address
129 from_email=${PLC_MONITOR_EMAIL}
130 # a separate address for support messages
131 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
132 # mailing list copied on all out-going messages
133 cc_email=${PLC_MONITOR_CC_EMAIL}
134
135 # these are reserved values
136 RT_DB_HOST=${PLC_RT_HOST}
137 RT_DB_USER=
138 RT_DB_PASSWORD=
139 RT_DB_NAME=
140
141 [monitordatabase]
142 monitor_dburi=postgres://${MONITOR_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
143 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
144
145 cachetime=60
146
147 # Evaluated as true or false
148 [commandline]
149 cachecalls=True
150 embedded=False
151 zabbix_enabled=False
152
153 echo=False
154 debug=False
155 mail=True
156 bcc=False
157 run=False
158 checkopt=False
159 squeeze=True
160 policysavedb=True
161 EOF
162
163         fi
164 }
165
166 function create_httpd_conf ()
167 {
168         MONITOR_HTTP_CONF=/etc/httpd/conf.d/monitorweb.conf
169
170         if [ ! -f ${MONITOR_HTTP_CONF} ] ; then
171                 # note: we need to either start this script before httpd, or always
172                 # restart httpd, since there's no way to know beyond file's existence
173                 # whether the values have changed or not.
174                 WROTE_HTTP_CONFIG="true"
175         fi
176
177         # TODO: support HTTPS as well as port 80.  currently not specifying port
178         #               80 breaks https for other content on the myplc.
179         # TODO: make proxy port configurable.
180
181         cat <<EOF > ${MONITOR_HTTP_CONF}
182
183 # NOTE: I've tried other means of redirection, including mod_rewrite, but did
184 #       not have any success.  The means below is not idea, b/c it does not keep
185 #       non-ssl session as non-ssl.  But it works.
186
187 # NOTE: redirect path without trailing '/' to path with.  Favor SSL.
188 Redirect /monitor https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor/
189 #RedirectMatch ^/$ https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor
190
191 # NOTE: this directive strips '/monitor/' from the requested path and pastes
192 #       the remaining part to the end of the ProxyPass url below.  All TG urls
193 #       should be relative to their current position, or the absolute path
194 #       that includes /monitor/ at the beginning.  
195 # TODO: make location configurable.
196 <Location '/monitor/'>
197     #LogLevel debug
198     #Errorlog /var/log/httpd/monitorwebapp-error_log
199     #Customlog /var/log/httpd/monitorwebapp-access_log common
200
201     ProxyPass http://127.0.0.1:8082/
202     ProxyPassReverse http://127.0.0.1:8082/
203 </Location>
204
205 EOF
206 }
207
208 function start_tg_server ()
209 {
210         stop_tg_server
211         pushd ${MONITORPATH}/web/MonitorWeb/
212         mkdir -p /var/log/monitor/monitorweb/
213         cp /var/log/monitorweb.log /var/lib/monitor/monitorweb/`date +%Y-%m-%d-%H-%M`-monitorweb.log
214         ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log &
215         popd
216 }
217
218 function stop_tg_server ()
219 {
220         pid=$( cat /var/run/monitorweb.pid )
221         if [ -n "$pid" ] ; then
222                 kill $pid
223         fi
224 }
225
226 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
227     exit 0
228 fi
229
230 case "$1" in
231         start)
232                 MESSAGE=$"Bootstrap Monitoring (please wait...)"
233                 dialog "$MESSAGE"
234
235                 # DATABASE acces, creation, and data loading
236                 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
237                 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
238                 # WRITE default /etc/monitor.conf
239                 check_monitor_conf
240
241                 check_gadget_config
242
243                 if [ -n "$WROTE_PG_CONFIG" ] ; then
244                         # NOTE: restart db to enable access by users granted above.
245                         service plc restart postgresql
246                         service plc restart httpd
247                         MESSAGE=$"Bootstrap Monitoring 2 (please wait...)"
248                         dialog "$MESSAGE"
249                 fi
250
251                 check_monitor_schema_and_data_init
252
253                 # create /etc/httpd/conf.d/monitorweb.conf
254                 create_httpd_conf
255                 if [ -n "$WROTE_HTTP_CONFIG" ] ; then
256                         # NOTE: restart web server to enable access web cfg
257                         service plc restart httpd
258                         MESSAGE=$"Bootstrap Monitoring 3 (please wait...)"
259                         dialog "$MESSAGE"
260                 fi
261                 start_tg_server
262
263                 result "$MESSAGE"
264         ;;
265
266         restartweb)
267                 MESSAGE=$"Restarting monitor web app..."
268                 dialog "$MESSAGE"
269
270                 stop_tg_server
271                 start_tg_server
272                 
273                 result "$MESSAGE"
274         ;;
275
276         delete)
277                 MESSAGE=$"Deleting databases..."
278                 dialog "$MESSAGE"
279
280                 dropdb -U postgres $MONITOR_DB_NAME
281                 dropuser -U postgres $MONITOR_DB_USER
282
283                 result "$MESSAGE"
284         ;;
285
286         stop)
287                 MESSAGE=$"Stopping Monitor"
288                 dialog "$MESSAGE"
289
290                 stop_tg_server
291                 # todo: disable cron entry?
292
293                 result "$MESSAGE"
294         ;;
295 esac
296
297 exit $ERRORS