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