minor updates to both files to fix bugs.
[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 # Be verbose
20 set -x
21
22 # Default locations
23 PGDATA=/var/lib/pgsql/data
24 postgresql_conf=$PGDATA/postgresql.conf
25 pghba_conf=$PGDATA/pg_hba.conf
26
27 # Export so that we do not have to specify -p to psql invocations
28 export PGPORT=$PLC_DB_PORT
29
30
31 # TODO:
32 ##   setup database
33 ###    import schema & data
34 ###    tweak values
35 ###    add zabbix to pg_hba.conf
36 ##   setup zabbix.conf.php 
37 ##   
38 ZABBIX_DB_USER="zabbixuser"
39 ZABBIX_DB_NAME="zabbix"
40
41 function check_user_and_db()
42 {
43     # confirm user is present or create it
44     user_present=$( psql -U postgres -c "select * from pg_user;" -d template1 | grep $ZABBIX_DB_NAME )
45     if [ -z $user_present ] ; then 
46         createuser --no-superuser --no-createdb --no-createrole --login --unencrypted --echo $ZABBIX_DB_NAME -U postgres
47     fi
48     
49     # confirm database is present or create it
50     db_present=$( psql -U postgres -c "select * from pg_database;" -d template1 | grep $ZABBIX_DB_NAME )
51     if [ -z $db_present ] ; then
52         createdb --owner=$ZABBIX_DB_NAME $ZABBIX_DB_NAME -U postgres
53     fi
54
55     # Create/update the unprivileged database user and password
56     if [ -z "$PLC_MONITOR_DBPASSWORD" ] ; then
57         # Zabbix doesn't like plain uuidgen passwords
58         PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
59         plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config
60         psql -d template1 -U postgres -c "ALTER USER $ZABBIX_DB_NAME WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
61         service plc reload
62     fi
63 }
64
65 function if_present_load ()
66 {
67     file=$1
68     if [ -f $file ] ; then
69         psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
70     fi
71 }
72
73 function check_schema_and_data() 
74 {
75     schema_present=$( psql -U $ZABBIX_DB_USER $ZABBIX_DB_NAME -c "\d;" < /dev/null | grep hosts )
76     if [ -z $schema_present ] ; then
77         echo "... initial import can take SEVERAL minutes. please wait ..."
78         if_present_load "/usr/local/zabbix/misc/create/schema/postgresql.sql"
79         if_present_load "/usr/local/zabbix/misc/create/data/data.sql"
80         if_present_load "/usr/local/zabbix/misc/create/data/images_pgsql.sql"
81         ## TODO: update ZABBIX Server entry, "update hosts set status=0, host='MyPLC Server' where hostid=10017"
82     fi
83 }
84
85
86 case "$1" in
87     start)
88         if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
89             exit 0
90         fi
91         MESSAGE=$"Bootstrap Monitoring"
92         dialog "$MESSAGE"
93
94         check_user_and_db 
95         check_schema_and_data
96         
97         mkdir -p /var/lib/pgsql/data/pg_hba.conf.d
98         ZABCONF=/var/lib/pgsql/data/pg_hba.conf.d/zabbix.conf
99         if [ ! -f $ZABCONF ] ; then
100                 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER 127.0.0.1/32 password"   > $ZAB
101                 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER $PLC_MONITOR_IP/32 password" >> $ZAB
102         fi
103
104         # UPDATE /etc/zabbix/*.conf
105         ZABBIXCFG=/etc/zabbix
106         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
107         # TODO:  How to know if I need to restart the services?
108
109         if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
110                 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
111                     -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
112                     -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
113                     -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
114                     ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
115                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
116         fi
117         if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
118                 HOST=`hostname`
119                 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
120                     -e "s#Hostname=.*#Hostname=$HOST#g" \
121                     ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
122                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf 
123         fi
124         service zabbix_server start
125         service zabbix_agentd start
126
127         # SETUP zabbix gui configuration
128         ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php 
129         if [ ! -f $ZABBIX_WEB_CFG ] ; then
130                 touch  $ZABBIX_WEB_CFG
131                 cat <<EOF > $ZABBIX_WEB_CFG
132 <?php
133 global \$DB;
134
135 \$DB["TYPE"]            = "POSTGRESQL";
136 \$DB["SERVER"]          = "localhost";
137 \$DB["PORT"]            = "0";
138 \$DB["DATABASE"]                = "$ZABBIX_DB_NAME";
139 \$DB["USER"]            = "$ZABBIX_DB_USER";
140 \$DB["PASSWORD"]                = "$PLC_MONITOR_DBPASSWORD";
141 \$ZBX_SERVER            = "$PLC_MONITOR_HOST";
142 \$ZBX_SERVER_PORT       = "10051";
143 \$IMAGE_FORMAT_DEFAULT  = IMAGE_FORMAT_PNG;
144 ?>
145 EOF
146                 chmod 644 $ZABBIX_WEB_CFG
147         fi
148
149         result "$MESSAGE"
150         ;;
151
152     stop)
153         MESSAGE=$"Stopping Monitor"
154         dialog "$MESSAGE"
155
156         service zabbix_server stop
157         service zabbix_agentd stop
158         # TODO: is there anything to stop?
159         result "$MESSAGE"
160         ;;
161 esac
162
163 exit $ERRORS