added templates for emailTxt
[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/planetlab/plc_config
17 local_config=/etc/planetlab/configs/site.xml
18
19 MONITORPATH=/usr/share/monitor
20
21 # Be verbose
22 set -x
23
24 # Default locations
25 PGDATA=/var/lib/pgsql/data
26 postgresql_conf=$PGDATA/postgresql.conf
27 pghba_conf=$PGDATA/pg_hba.conf
28
29 # Export so that we do not have to specify -p to psql invocations
30 export PGPORT=$PLC_DB_PORT
31
32
33 ZABBIX_DB_USER="zabbixuser"
34 ZABBIX_DB_NAME="zabbix"
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 check_pg_hba ()
46 {
47         NAME=$1
48         USER=$2
49         #### SETUP ACCESS to this user and database
50         mkdir -p $PGDATA/pg_hba.conf.d
51         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
52         if [ ! -f $CONF ] ; then
53                 echo "host $NAME $USER 127.0.0.1/32 password"   > $CONF
54                 echo "host $NAME $USER $PLC_MONITOR_IP/32 password" >> $CONF
55
56                 WROTE_PG_CONFIG="true"
57         fi
58 }
59
60 function check_user_and_db()
61 {
62     CREATED=
63         NAME=$1
64         USER=$2
65     # confirm user is present or create it
66     user_present=$( psql -U postgres -c "select * from pg_user;" -d template1 | grep $USER )
67     if [ -z $user_present ] ; then 
68         createuser --no-superuser --no-createdb --no-createrole --login --unencrypted --echo $USER -U postgres
69                 CREATED="true"
70     fi
71     
72     # confirm database is present or create it
73     db_present=$( psql -U postgres -c "select * from pg_database;" -d template1 | grep $NAME )
74     if [ -z $db_present ] ; then
75         createdb --owner=$USER $NAME -U postgres
76                 CREATED="true"
77     fi
78
79     # Create/update the unprivileged database user and password
80     if [[ -z "$PLC_MONITOR_DBPASSWORD" || "$PLC_MONITOR_DBPASSWORD" = "None" ]] ; then
81         # Zabbix doesn't like plain uuidgen passwords
82         PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
83         plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config
84         service plc reload
85                 CREATED="true"
86     fi
87     if [ -n "$CREATED" ] ; then
88         psql -d template1 -U postgres -c "ALTER USER $USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
89     fi
90 }
91
92 function if_present_load ()
93 {
94     file=$1
95     if [ -f $file ] ; then
96         psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
97     fi
98 }
99 function check_monitor_schema_and_data() 
100 {
101         # NOTE: call create_all() to setup the database from the info model.
102         python -c "from monitor.database.info.model import *; from elixir import create_all; create_all()"
103 }
104
105 function check_zabbix_schema_and_data() 
106 {
107     schema_present=$( psql -U $ZABBIX_DB_USER $ZABBIX_DB_NAME -c "\d;" < /dev/null | grep hosts )
108     if [ -z $schema_present ] ; then
109         echo "... initial import can take SEVERAL minutes. please wait ..."
110         if_present_load "/usr/local/zabbix/misc/create/schema/postgresql.sql"
111         if_present_load "/usr/local/zabbix/misc/create/data/data.sql"
112         if_present_load "/usr/local/zabbix/misc/create/data/images_pgsql.sql"
113         ## TODO: update ZABBIX Server entry, "update hosts set status=0, host='MyPLC Server' where hostid=10017"
114     fi
115 }
116
117 function check_zabbix_templates_and_import ()
118 {
119         # LOG IN
120         COOKIE_FILE=/tmp/cookiejar.txt
121         rm -f ${COOKIE_FILE}
122         TEMPLATES_DIR=${MONITORPATH}/zabbix/templates
123         curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
124                         --form "enter=Enter" \
125                         --form "name=Admin" \
126                         --form "password=zabbix" \
127                         "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1"
128         
129         deleted=$( grep 'deleted' $COOKIE_FILE )
130         if [ -n "$deleted" ] ; then
131                 echo "Login to the zabbix web server failed!!!"
132                 return 1
133         fi
134
135         for file in ${TEMPLATES_DIR}/*.xml ; do 
136                 # 0 - update , 1 - skip, 0 - add
137                 echo "############### IMPORTING $file" >> /var/log/monitor.log
138                 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
139                         --form "config=1" \
140                         --form "import_file=@${file}" \
141                         --form "rules[host][exist]=0" \
142                         --form "rules[host][missed]=0" \
143                         --form "rules[template][exist]=0" \
144                         --form "rules[template][missed]=1" \
145                         --form "rules[item][exist]=0" \
146                         --form "rules[item][missed]=0" \
147                         --form "rules[trigger][exist]=0" \
148                         --form "rules[trigger][missed]=0" \
149                         --form "rules[graph][exist]=0" \
150                         --form "rules[graph][missed]=0" \
151                         --form "import=Import" \
152                         "http://${PLC_MONITOR_HOST}/zabbix/exp_imp.php" >> /var/log/monitor.log
153         done
154 }
155
156
157 function check_monitor_conf ()
158 {
159         MONITOR_CONFIG=/etc/monitor.conf
160
161
162         # Using plcsh add default, monitor user
163         plcsh <<EOF &>/dev/null 
164 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
165 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
166 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
167 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
168 EOF
169
170         if [ ! -f ${MONITOR_CONFIG} ] ; then
171                 cat <<EOF > ${MONITOR_CONFIG}
172 [monitorconfig]
173 RT_DB_HOST=rt.planet-lab.org
174 RT_DB_USER=
175 RT_DB_PASSWORD=
176 RT_DB_NAME=
177
178 # RT Web user account
179 RT_WEB_SERVER=https://rt.planet-lab.org/
180 RT_WEB_TOOLS_PATH=
181 RT_WEB_USER=
182 RT_WEB_PASSWORD=
183 RT_WEB_DEBUG=0
184 RT_QUEUE=
185
186 # PLC admin account
187 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
188 API_AUTH_USER=${PLC_MONITOR_EMAIL}
189 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
190
191 # SERVER PATHS
192 MONITOR_SCRIPT_ROOT=${MONITORPATH}
193 MONITOR_DATA_ROOT=/var/lib/monitor
194 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
195 MONITOR_BOOTMANAGER_LOG=/var/www/html/monitorlog
196
197 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
198 MONITOR_IP=${PLC_MONITOR_IP}
199
200 PLC_WWW_HOSTNAME=${PLC_WWW_HOST}
201 PLC_NAME=${PLC_NAME}
202
203 # used for debug mode
204 email=
205
206 # all messages will appear to be from this address
207 from_email=${PLC_MONITOR_EMAIL}
208
209 # a separate address for support messages
210 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
211
212 # mailing list copied on all out-going messages
213 cc_email=
214
215 [monitordatabase]
216 monitor_dburi=postgres://${MONITOR_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
217 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
218
219 cachetime=60
220
221 # Evaluated as true or false
222 [commandline]
223 cachecalls=True
224 embedded=False
225
226 echo=False
227 debug=False
228 mail=True
229 bcc=False
230 run=False
231 checkopt=False
232 squeeze=True
233 policysavedb=True
234 EOF
235
236         fi
237 }
238
239 function create_httpd_conf ()
240 {
241         MONITOR_HTTP_CONF=/etc/httpd/conf.d/monitorweb.conf
242
243         if [ ! -f ${MONITOR_HTTP_CONF} ] ; then
244                 # note: we need to either start this script before httpd, or always
245                 # restart httpd, since there's no way to know beyond file's existence
246                 # whether the values have changed or not.
247                 WROTE_HTTP_CONFIG="true"
248         fi
249
250         # TODO: support HTTPS as well as port 80.  currently not specifying port
251         #               80 breaks https for other content on the myplc.
252         # TODO: make proxy port configurable.
253
254         cat <<EOF > ${MONITOR_HTTP_CONF}
255
256 # NOTE: I've tried other means of redirection, including mod_rewrite, but did
257 #       not have any success.  The means below is not idea, b/c it does not keep
258 #       non-ssl session as non-ssl.  But it works.
259
260 # NOTE: redirect path without trailing '/' to path with.  Favor SSL.
261 Redirect /monitor https://${PLC_MONITOR_HOST}:${PLC_WWW_SSL_PORT}/monitor/
262
263 # NOTE: this directive strips '/monitor/' from the requested path and pastes
264 #       the remaining part to the end of the ProxyPass url below.  All TG urls
265 #       should be relative to their current position, or the absolute path
266 #       that includes /monitor/ at the beginning.  
267 # TODO: make location configurable.
268 <Location '/monitor/'>
269     #LogLevel debug
270     #Errorlog /var/log/httpd/monitorwebapp-error_log
271     #Customlog /var/log/httpd/monitorwebapp-access_log common
272
273     ProxyPass http://127.0.0.1:8082/
274     ProxyPassReverse http://127.0.0.1:8082/
275 </Location>
276
277 EOF
278 }
279
280 function start_tg_server ()
281 {
282         stop_tg_server
283         pushd ${MONITORPATH}/web/MonitorWeb/
284         ./start-monitorweb.py ${MONITORPATH}/web/MonitorWeb/prod.cfg &> /var/log/monitorweb.log &
285         popd
286 }
287
288 function stop_tg_server ()
289 {
290         pid=$( cat /var/run/monitorweb.pid )
291         if [ -n "$pid" ] ; then
292                 kill $pid
293         fi
294 }
295
296 function check_zab_server ()
297 {
298         ZABBIXCFG=/etc/zabbix
299         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
300
301         if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
302                 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
303                     -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
304                     -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
305                     -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
306                     -e "s#.*ExternalScripts=.*#ExternalScripts=${MONITORPATH}/zabbix#g" \
307                     ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
308                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
309         fi
310         service zabbix_server start
311         rm -f $TMP_FILE
312
313 }
314 function check_zab_agentd ()
315 {
316         ZABBIXCFG=/etc/zabbix
317         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
318         if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
319                 HOST=`hostname`
320                 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
321                     -e "s#Hostname=.*#Hostname=$HOST#g" \
322                     ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
323                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf 
324         fi
325         service zabbix_agentd start
326         rm -f $TMP_FILE
327 }
328 function check_zab_webconfig()
329 {
330         # SETUP zabbix gui configuration
331         ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php 
332         if [ ! -f $ZABBIX_WEB_CFG ] ; then
333                 touch  $ZABBIX_WEB_CFG
334                 cat <<EOF > $ZABBIX_WEB_CFG
335 <?php
336 global \$DB;
337
338 \$DB["TYPE"]            = "POSTGRESQL";
339 \$DB["SERVER"]          = "localhost";
340 \$DB["PORT"]            = "0";
341 \$DB["DATABASE"]                = "$ZABBIX_DB_NAME";
342 \$DB["USER"]            = "$ZABBIX_DB_USER";
343 \$DB["PASSWORD"]                = "$PLC_MONITOR_DBPASSWORD";
344 \$ZBX_SERVER            = "$PLC_MONITOR_HOST";
345 \$ZBX_SERVER_PORT       = "10051";
346 \$IMAGE_FORMAT_DEFAULT  = IMAGE_FORMAT_PNG;
347 ?>
348 EOF
349                 chmod 644 $ZABBIX_WEB_CFG
350         fi
351 }
352
353 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
354     exit 0
355 fi
356
357 case "$1" in
358         start)
359                 MESSAGE=$"Bootstrap Monitoring (please wait...)"
360                 dialog "$MESSAGE"
361
362                 # DATABASE acces, creation, and data loading
363                 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
364                 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
365                 # WRITE default /etc/monitor.conf
366                 check_monitor_conf
367
368                 #check_pg_hba $ZABBIX_DB_NAME $ZABBIX_DB_USER
369                 #check_user_and_db $ZABBIX_DB_NAME $ZABBIX_DB_USER
370
371                 if [ -n "$WROTE_PG_CONFIG" ] ; then
372                         # NOTE: restart db to enable access by users granted above.
373                         service plc restart postgresql
374                         service plc restart httpd
375                         MESSAGE=$"Bootstrap Monitoring 2 (please wait...)"
376                         dialog "$MESSAGE"
377                 fi
378
379                 check_monitor_schema_and_data
380
381                 #check_zabbix_schema_and_data
382                 #check_zabbix_templates_and_import
383
384
385                 # create /etc/httpd/conf.d/monitorweb.conf
386                 create_httpd_conf
387                 if [ -n "$WROTE_HTTP_CONFIG" ] ; then
388                         # NOTE: restart web server to enable access web cfg
389                         service plc restart httpd
390                         MESSAGE=$"Bootstrap Monitoring 3 (please wait...)"
391                         dialog "$MESSAGE"
392                 fi
393                 start_tg_server
394
395                 # START zabbix services.  SETUP default config files.
396                 #check_zab_server
397                 #check_zab_agentd
398                 #check_zab_webconfig
399
400                 result "$MESSAGE"
401         ;;
402
403         restartweb)
404                 MESSAGE=$"Restarting monitor web app..."
405                 dialog "$MESSAGE"
406
407                 stop_tg_server
408                 start_tg_server
409                 
410                 result "$MESSAGE"
411         ;;
412
413         sync)
414                 MESSAGE=$"Syncing PLC db with Zabbix DB"
415                 dialog "$MESSAGE"
416
417                 # turn off zabbix server, etc. before writing to the db.
418                 service plc stop monitor 
419
420                 $MONITORPATH/zabbix/zabbixsync.py --setupids &> /var/log/monitor-server
421                 $MONITORPATH/zabbix/zabbixsync.py --setupglobal 2>&1 >> /var/log/monitor-server
422                 # import any templates
423                 check_zabbix_templates_and_import
424
425                 service plc start monitor 
426                 
427                 result "$MESSAGE"
428         ;;
429
430         delete)
431                 MESSAGE=$"Deleting databases..."
432                 dialog "$MESSAGE"
433
434                 #dropdb -U postgres $ZABBIX_DB_NAME
435                 #dropuser -U postgres $ZABBIX_DB_USER
436
437                 dropdb -U postgres $MONITOR_DB_NAME
438                 dropuser -U postgres $MONITOR_DB_USER
439
440                 result "$MESSAGE"
441         ;;
442
443         stop)
444                 MESSAGE=$"Stopping Monitor"
445                 dialog "$MESSAGE"
446
447                 stop_tg_server
448                 #service zabbix_server stop
449                 #service zabbix_agentd stop
450                 # TODO: is there anything to stop?
451                 result "$MESSAGE"
452         ;;
453 esac
454
455 exit $ERRORS