added templating to google gadget xml file in monitor-server; previously it
[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() 
79 {
80         # NOTE: call create_all() to setup the database from the info model.
81         python -c "from monitor.database.info.model import *; from elixir import create_all; create_all()"
82         $MONITORPATH/init-bootman-sequence.py
83 }
84
85 function check_monitor_conf ()
86 {
87         MONITOR_CONFIG=/etc/monitor.conf
88
89         # Using plcsh add default, monitor user
90         plcsh <<EOF &>/dev/null 
91 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
92 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
93 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
94 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
95 EOF
96
97         if [ ! -f ${MONITOR_CONFIG} ] ; then
98                 cat <<EOF > ${MONITOR_CONFIG}
99 [monitorconfig]
100 # RT Web user account
101 RT_WEB_SERVER=http://${PLC_RT_HOST}/
102 RT_WEB_TOOLS_PATH=/usr/bin/
103 RT_WEB_USER=${PLC_RT_WEB_USER}
104 RT_WEB_PASSWORD=${PLC_RT_WEB_PASSWORD}
105 RT_WEB_DEBUG=0
106 RT_QUEUE=${PLC_MONITOR_RT_QUEUE}
107
108 # PLC admin account
109 API_SERVER=https://${PLC_API_HOST}:${PLC_API_PORT}/PLCAPI/
110 API_AUTH_USER=${PLC_MONITOR_EMAIL}
111 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
112
113 # SERVER PATHS
114 MONITOR_SCRIPT_ROOT=${MONITORPATH}
115 MONITOR_DATA_ROOT=/var/lib/monitor
116 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
117 MONITOR_BOOTMANAGER_LOG=/var/www/html/monitorlog
118
119 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
120 MONITOR_IP=${PLC_MONITOR_IP}
121
122 PLC_WWW_HOSTNAME=${PLC_WWW_HOST}
123 PLC_NAME=${PLC_NAME}
124
125 # used for debug mode
126 email=${PLC_MONITOR_CC_EMAIL}
127 # all messages will appear to be from this address
128 from_email=${PLC_MONITOR_EMAIL}
129 # a separate address for support messages
130 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
131 # mailing list copied on all out-going messages
132 cc_email=${PLC_MONITOR_CC_EMAIL}
133
134 # these are reserved values
135 RT_DB_HOST=${PLC_RT_HOST}
136 RT_DB_USER=
137 RT_DB_PASSWORD=
138 RT_DB_NAME=
139
140 [monitordatabase]
141 monitor_dburi=postgres://${MONITOR_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
142 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
143
144 cachetime=60
145
146 # Evaluated as true or false
147 [commandline]
148 cachecalls=True
149 embedded=False
150 zabbix_enabled=False
151
152 echo=False
153 debug=False
154 mail=True
155 bcc=False
156 run=False
157 checkopt=False
158 squeeze=True
159 policysavedb=True
160 EOF
161
162         fi
163 }
164
165 function create_httpd_conf ()
166 {
167         MONITOR_HTTP_CONF=/etc/httpd/conf.d/monitorweb.conf
168
169         if [ ! -f ${MONITOR_HTTP_CONF} ] ; then
170                 # note: we need to either start this script before httpd, or always
171                 # restart httpd, since there's no way to know beyond file's existence
172                 # whether the values have changed or not.
173                 WROTE_HTTP_CONFIG="true"
174         fi
175
176         # TODO: support HTTPS as well as port 80.  currently not specifying port
177         #               80 breaks https for other content on the myplc.
178         # TODO: make proxy port configurable.
179
180         cat <<EOF > ${MONITOR_HTTP_CONF}
181
182 # NOTE: I've tried other means of redirection, including mod_rewrite, but did
183 #       not have any success.  The means below is not idea, b/c it does not keep
184 #       non-ssl session as non-ssl.  But it works.
185
186 # NOTE: redirect path without trailing '/' to path with.  Favor SSL.
187 Redirect /monitor https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor/
188 #RedirectMatch ^/$ https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor
189
190 # NOTE: this directive strips '/monitor/' from the requested path and pastes
191 #       the remaining part to the end of the ProxyPass url below.  All TG urls
192 #       should be relative to their current position, or the absolute path
193 #       that includes /monitor/ at the beginning.  
194 # TODO: make location configurable.
195 <Location '/monitor/'>
196     #LogLevel debug
197     #Errorlog /var/log/httpd/monitorwebapp-error_log
198     #Customlog /var/log/httpd/monitorwebapp-access_log common
199
200     ProxyPass http://127.0.0.1:8082/
201     ProxyPassReverse http://127.0.0.1:8082/
202 </Location>
203
204 EOF
205 }
206
207 function start_tg_server ()
208 {
209         stop_tg_server
210         pushd ${MONITORPATH}/web/MonitorWeb/
211         mkdir -p /var/log/monitor/monitorweb/
212         cp /var/log/monitorweb.log /var/lib/monitor/monitorweb/`date +%Y-%m-%d-%H-%M`-monitorweb.log
213         ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log &
214         popd
215 }
216
217 function stop_tg_server ()
218 {
219         pid=$( cat /var/run/monitorweb.pid )
220         if [ -n "$pid" ] ; then
221                 kill $pid
222         fi
223 }
224
225 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
226     exit 0
227 fi
228
229 case "$1" in
230         start)
231                 MESSAGE=$"Bootstrap Monitoring (please wait...)"
232                 dialog "$MESSAGE"
233
234                 # DATABASE acces, creation, and data loading
235                 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
236                 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
237                 # WRITE default /etc/monitor.conf
238                 check_monitor_conf
239
240                 check_gadget_config
241
242                 if [ -n "$WROTE_PG_CONFIG" ] ; then
243                         # NOTE: restart db to enable access by users granted above.
244                         service plc restart postgresql
245                         service plc restart httpd
246                         MESSAGE=$"Bootstrap Monitoring 2 (please wait...)"
247                         dialog "$MESSAGE"
248                 fi
249
250                 check_monitor_schema_and_data
251
252                 # create /etc/httpd/conf.d/monitorweb.conf
253                 create_httpd_conf
254                 if [ -n "$WROTE_HTTP_CONFIG" ] ; then
255                         # NOTE: restart web server to enable access web cfg
256                         service plc restart httpd
257                         MESSAGE=$"Bootstrap Monitoring 3 (please wait...)"
258                         dialog "$MESSAGE"
259                 fi
260                 start_tg_server
261
262                 result "$MESSAGE"
263         ;;
264
265         restartweb)
266                 MESSAGE=$"Restarting monitor web app..."
267                 dialog "$MESSAGE"
268
269                 stop_tg_server
270                 start_tg_server
271                 
272                 result "$MESSAGE"
273         ;;
274
275         delete)
276                 MESSAGE=$"Deleting databases..."
277                 dialog "$MESSAGE"
278
279                 dropdb -U postgres $MONITOR_DB_NAME
280                 dropuser -U postgres $MONITOR_DB_USER
281
282                 result "$MESSAGE"
283         ;;
284
285         stop)
286                 MESSAGE=$"Stopping Monitor"
287                 dialog "$MESSAGE"
288
289                 stop_tg_server
290                 # todo: disable cron entry?
291
292                 result "$MESSAGE"
293         ;;
294 esac
295
296 exit $ERRORS