- add install_date
[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         $MONITORPATH/init-bootman-sequence.py
48 }
49
50 function check_monitor_conf ()
51 {
52         MONITOR_CONFIG=/etc/monitor.conf
53
54         # Using plcsh add default, monitor user
55         plcsh <<EOF &>/dev/null 
56 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
57 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
58 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
59 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
60 EOF
61
62         if [ ! -f ${MONITOR_CONFIG} ] ; then
63                 cat <<EOF > ${MONITOR_CONFIG}
64 [monitorconfig]
65 # RT Web user account
66 RT_WEB_SERVER=http://${PLC_RT_HOST}/
67 RT_WEB_TOOLS_PATH=/usr/bin/
68 RT_WEB_USER=${PLC_RT_WEB_USER}
69 RT_WEB_PASSWORD=${PLC_RT_WEB_PASSWORD}
70 RT_WEB_DEBUG=0
71 RT_QUEUE=${PLC_MONITOR_RT_QUEUE}
72
73 # PLC admin account
74 API_SERVER=https://${PLC_API_HOST}:${PLC_API_PORT}/PLCAPI/
75 API_AUTH_USER=${PLC_MONITOR_EMAIL}
76 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
77
78 # SERVER PATHS
79 MONITOR_SCRIPT_ROOT=${MONITORPATH}
80 MONITOR_DATA_ROOT=/var/lib/monitor
81 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
82 MONITOR_BOOTMANAGER_LOG=/var/www/html/monitorlog
83
84 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
85 MONITOR_IP=${PLC_MONITOR_IP}
86
87 PLC_WWW_HOSTNAME=${PLC_WWW_HOST}
88 PLC_NAME=${PLC_NAME}
89
90 # used for debug mode
91 email=${PLC_MONITOR_CC_EMAIL}
92 # all messages will appear to be from this address
93 from_email=${PLC_MONITOR_EMAIL}
94 # a separate address for support messages
95 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
96 # mailing list copied on all out-going messages
97 cc_email=${PLC_MONITOR_CC_EMAIL}
98
99 # these are reserved values
100 RT_DB_HOST=${PLC_RT_HOST}
101 RT_DB_USER=
102 RT_DB_PASSWORD=
103 RT_DB_NAME=
104
105 [monitordatabase]
106 monitor_dburi=postgres://${MONITOR_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
107 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
108
109 cachetime=60
110
111 # Evaluated as true or false
112 [commandline]
113 cachecalls=True
114 embedded=False
115 zabbix_enabled=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         mkdir -p /var/log/monitor/monitorweb/
176         cp /var/log/monitorweb.log /var/lib/monitor/monitorweb/`date +%Y-%m-%d-%H-%M`-monitorweb.log
177         ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log &
178         popd
179 }
180
181 function stop_tg_server ()
182 {
183         pid=$( cat /var/run/monitorweb.pid )
184         if [ -n "$pid" ] ; then
185                 kill $pid
186         fi
187 }
188
189 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
190     exit 0
191 fi
192
193 case "$1" in
194         start)
195                 MESSAGE=$"Bootstrap Monitoring (please wait...)"
196                 dialog "$MESSAGE"
197
198                 # DATABASE acces, creation, and data loading
199                 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
200                 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
201                 # WRITE default /etc/monitor.conf
202                 check_monitor_conf
203
204                 if [ -n "$WROTE_PG_CONFIG" ] ; then
205                         # NOTE: restart db to enable access by users granted above.
206                         service plc restart postgresql
207                         service plc restart httpd
208                         MESSAGE=$"Bootstrap Monitoring 2 (please wait...)"
209                         dialog "$MESSAGE"
210                 fi
211
212                 check_monitor_schema_and_data
213
214                 # create /etc/httpd/conf.d/monitorweb.conf
215                 create_httpd_conf
216                 if [ -n "$WROTE_HTTP_CONFIG" ] ; then
217                         # NOTE: restart web server to enable access web cfg
218                         service plc restart httpd
219                         MESSAGE=$"Bootstrap Monitoring 3 (please wait...)"
220                         dialog "$MESSAGE"
221                 fi
222                 start_tg_server
223
224                 result "$MESSAGE"
225         ;;
226
227         restartweb)
228                 MESSAGE=$"Restarting monitor web app..."
229                 dialog "$MESSAGE"
230
231                 stop_tg_server
232                 start_tg_server
233                 
234                 result "$MESSAGE"
235         ;;
236
237         delete)
238                 MESSAGE=$"Deleting databases..."
239                 dialog "$MESSAGE"
240
241                 dropdb -U postgres $MONITOR_DB_NAME
242                 dropuser -U postgres $MONITOR_DB_USER
243
244                 result "$MESSAGE"
245         ;;
246
247         stop)
248                 MESSAGE=$"Stopping Monitor"
249                 dialog "$MESSAGE"
250
251                 stop_tg_server
252                 # todo: disable cron entry?
253
254                 result "$MESSAGE"
255         ;;
256 esac
257
258 exit $ERRORS