improved post server setup code in Monitor.spec
[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
22 # Be verbose
23 set -x
24
25 # Default locations
26 PGDATA=/var/lib/pgsql/data
27 postgresql_conf=$PGDATA/postgresql.conf
28 pghba_conf=$PGDATA/pg_hba.conf
29
30 # Export so that we do not have to specify -p to psql invocations
31 export PGPORT=$PLC_DB_PORT
32
33
34 MONITOR_DB_USER="monitoruser"
35 MONITOR_DB_NAME="monitor"
36
37 WROTE_PG_CONFIG=
38
39 if [ -z "$PLC_MONITOR_IP" ] ; then
40         PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
41 fi
42
43 function check_monitor_schema_and_data() 
44 {
45         # NOTE: call create_all() to setup the database from the info model.
46         python -c "from monitor.database.info.model import *; from elixir import create_all; create_all()"
47 }
48
49 function check_monitor_conf ()
50 {
51         MONITOR_CONFIG=/etc/monitor.conf
52
53         # Using plcsh add default, monitor user
54         plcsh <<EOF &>/dev/null 
55 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
56 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
57 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
58 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
59 EOF
60
61         if [ ! -f ${MONITOR_CONFIG} ] ; then
62                 cat <<EOF > ${MONITOR_CONFIG}
63 [monitorconfig]
64 RT_DB_HOST=rt.planet-lab.org
65 RT_DB_USER=
66 RT_DB_PASSWORD=
67 RT_DB_NAME=
68
69 # RT Web user account
70 RT_WEB_SERVER=https://rt.planet-lab.org/
71 RT_WEB_TOOLS_PATH=
72 RT_WEB_USER=
73 RT_WEB_PASSWORD=
74 RT_WEB_DEBUG=0
75 RT_QUEUE=
76
77 # PLC admin account
78 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
79 API_AUTH_USER=${PLC_MONITOR_EMAIL}
80 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
81
82 # SERVER PATHS
83 MONITOR_SCRIPT_ROOT=${MONITORPATH}
84 MONITOR_DATA_ROOT=/var/lib/monitor
85 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
86 MONITOR_BOOTMANAGER_LOG=/var/www/html/monitorlog
87
88 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
89 MONITOR_IP=${PLC_MONITOR_IP}
90
91 PLC_WWW_HOSTNAME=${PLC_WWW_HOST}
92 PLC_NAME=${PLC_NAME}
93
94 # used for debug mode
95 email=
96
97 # all messages will appear to be from this address
98 from_email=${PLC_MONITOR_EMAIL}
99
100 # a separate address for support messages
101 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
102
103 # mailing list copied on all out-going messages
104 cc_email=
105
106 [monitordatabase]
107 monitor_dburi=postgres://${MONITOR_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
108 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
109
110 cachetime=60
111
112 # Evaluated as true or false
113 [commandline]
114 cachecalls=True
115 embedded=False
116
117 echo=False
118 debug=False
119 mail=True
120 bcc=False
121 run=False
122 checkopt=False
123 squeeze=True
124 policysavedb=True
125 EOF
126
127         fi
128 }
129
130 function create_httpd_conf ()
131 {
132         MONITOR_HTTP_CONF=/etc/httpd/conf.d/monitorweb.conf
133
134         if [ ! -f ${MONITOR_HTTP_CONF} ] ; then
135                 # note: we need to either start this script before httpd, or always
136                 # restart httpd, since there's no way to know beyond file's existence
137                 # whether the values have changed or not.
138                 WROTE_HTTP_CONFIG="true"
139         fi
140
141         # TODO: support HTTPS as well as port 80.  currently not specifying port
142         #               80 breaks https for other content on the myplc.
143         # TODO: make proxy port configurable.
144
145         cat <<EOF > ${MONITOR_HTTP_CONF}
146
147 # NOTE: I've tried other means of redirection, including mod_rewrite, but did
148 #       not have any success.  The means below is not idea, b/c it does not keep
149 #       non-ssl session as non-ssl.  But it works.
150
151 # NOTE: redirect path without trailing '/' to path with.  Favor SSL.
152 Redirect /monitor https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor/
153
154 # NOTE: this directive strips '/monitor/' from the requested path and pastes
155 #       the remaining part to the end of the ProxyPass url below.  All TG urls
156 #       should be relative to their current position, or the absolute path
157 #       that includes /monitor/ at the beginning.  
158 # TODO: make location configurable.
159 <Location '/monitor/'>
160     #LogLevel debug
161     #Errorlog /var/log/httpd/monitorwebapp-error_log
162     #Customlog /var/log/httpd/monitorwebapp-access_log common
163
164     ProxyPass http://127.0.0.1:8082/
165     ProxyPassReverse http://127.0.0.1:8082/
166 </Location>
167
168 EOF
169 }
170
171 function start_tg_server ()
172 {
173         stop_tg_server
174         pushd ${MONITORPATH}/web/MonitorWeb/
175         ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log &
176         popd
177 }
178
179 function stop_tg_server ()
180 {
181         pid=$( cat /var/run/monitorweb.pid )
182         if [ -n "$pid" ] ; then
183                 kill $pid
184         fi
185 }
186
187 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
188     exit 0
189 fi
190
191 case "$1" in
192         start)
193                 MESSAGE=$"Bootstrap Monitoring (please wait...)"
194                 dialog "$MESSAGE"
195
196                 # DATABASE acces, creation, and data loading
197                 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
198                 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
199                 # WRITE default /etc/monitor.conf
200                 check_monitor_conf
201
202                 if [ -n "$WROTE_PG_CONFIG" ] ; then
203                         # NOTE: restart db to enable access by users granted above.
204                         service plc restart postgresql
205                         service plc restart httpd
206                         MESSAGE=$"Bootstrap Monitoring 2 (please wait...)"
207                         dialog "$MESSAGE"
208                 fi
209
210                 check_monitor_schema_and_data
211
212                 # create /etc/httpd/conf.d/monitorweb.conf
213                 create_httpd_conf
214                 if [ -n "$WROTE_HTTP_CONFIG" ] ; then
215                         # NOTE: restart web server to enable access web cfg
216                         service plc restart httpd
217                         MESSAGE=$"Bootstrap Monitoring 3 (please wait...)"
218                         dialog "$MESSAGE"
219                 fi
220                 start_tg_server
221
222                 result "$MESSAGE"
223         ;;
224
225         restartweb)
226                 MESSAGE=$"Restarting monitor web app..."
227                 dialog "$MESSAGE"
228
229                 stop_tg_server
230                 start_tg_server
231                 
232                 result "$MESSAGE"
233         ;;
234
235         delete)
236                 MESSAGE=$"Deleting databases..."
237                 dialog "$MESSAGE"
238
239                 dropdb -U postgres $MONITOR_DB_NAME
240                 dropuser -U postgres $MONITOR_DB_USER
241
242                 result "$MESSAGE"
243         ;;
244
245         stop)
246                 MESSAGE=$"Stopping Monitor"
247                 dialog "$MESSAGE"
248
249                 stop_tg_server
250                 # todo: disable cron entry?
251
252                 result "$MESSAGE"
253         ;;
254 esac
255
256 exit $ERRORS