5 # Manage settings for the Zabbix installtion and
6 # other monitor-related things
8 # Stephen Soltesz <soltesz@cs.princeton.edu>
9 # Copyright (C) 2008 The Trustees of Princeton University
14 # Source function library and configuration
15 . /etc/plc.d/functions
16 . /etc/planetlab/plc_config
17 local_config=/etc/planetlab/configs/site.xml
23 PGDATA=/var/lib/pgsql/data
24 postgresql_conf=$PGDATA/postgresql.conf
25 pghba_conf=$PGDATA/pg_hba.conf
27 # Export so that we do not have to specify -p to psql invocations
28 export PGPORT=$PLC_DB_PORT
33 ### import schema & data
35 ### add zabbix to pg_hba.conf
36 ## setup zabbix.conf.php
38 ZABBIX_DB_USER="zabbixuser"
39 ZABBIX_DB_NAME="zabbix"
41 function check_user_and_db()
44 # confirm user is present or create it
45 user_present=$( psql -U postgres -c "select * from pg_user;" -d template1 | grep $ZABBIX_DB_USER )
46 if [ -z $user_present ] ; then
47 createuser --no-superuser --no-createdb --no-createrole --login --unencrypted --echo $ZABBIX_DB_USER -U postgres
51 # confirm database is present or create it
52 db_present=$( psql -U postgres -c "select * from pg_database;" -d template1 | grep $ZABBIX_DB_NAME )
53 if [ -z $db_present ] ; then
54 createdb --owner=$ZABBIX_DB_USER $ZABBIX_DB_NAME -U postgres
58 # Create/update the unprivileged database user and password
59 if [ -z "$PLC_MONITOR_DBPASSWORD" ] ; then
60 # Zabbix doesn't like plain uuidgen passwords
61 PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
62 plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config
66 if [ -n "$CREATED" ] ; then
67 psql -d template1 -U postgres -c "ALTER USER $ZABBIX_DB_USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
71 function if_present_load ()
74 if [ -f $file ] ; then
75 psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
79 function check_schema_and_data()
81 schema_present=$( psql -U $ZABBIX_DB_USER $ZABBIX_DB_NAME -c "\d;" < /dev/null | grep hosts )
82 if [ -z $schema_present ] ; then
83 echo "... initial import can take SEVERAL minutes. please wait ..."
84 if_present_load "/usr/local/zabbix/misc/create/schema/postgresql.sql"
85 if_present_load "/usr/local/zabbix/misc/create/data/data.sql"
86 if_present_load "/usr/local/zabbix/misc/create/data/images_pgsql.sql"
87 ## TODO: update ZABBIX Server entry, "update hosts set status=0, host='MyPLC Server' where hostid=10017"
94 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
97 MESSAGE=$"Bootstrap Monitoring"
100 #### SETUP ACCESS to this user and database
101 mkdir -p /var/lib/pgsql/data/pg_hba.conf.d
102 ZABCONF=/var/lib/pgsql/data/pg_hba.conf.d/zabbix.conf
103 if [ ! -f $ZABCONF ] ; then
104 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER 127.0.0.1/32 password" > $ZAB
105 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER $PLC_MONITOR_IP/32 password" >> $ZAB
108 # NOTE: restart db to enable access by users granted above.
109 service plc restart posgresql
112 check_schema_and_data
114 # UPDATE /etc/zabbix/*.conf
115 ZABBIXCFG=/etc/zabbix
116 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
117 # TODO: How to know if I need to restart the services?
119 if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
120 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
121 -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
122 -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
123 -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
124 ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
125 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
127 if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
129 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
130 -e "s#Hostname=.*#Hostname=$HOST#g" \
131 ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
132 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf
134 service zabbix_server start
135 service zabbix_agentd start
137 # SETUP zabbix gui configuration
138 ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php
139 if [ ! -f $ZABBIX_WEB_CFG ] ; then
140 touch $ZABBIX_WEB_CFG
141 cat <<EOF > $ZABBIX_WEB_CFG
145 \$DB["TYPE"] = "POSTGRESQL";
146 \$DB["SERVER"] = "localhost";
148 \$DB["DATABASE"] = "$ZABBIX_DB_NAME";
149 \$DB["USER"] = "$ZABBIX_DB_USER";
150 \$DB["PASSWORD"] = "$PLC_MONITOR_DBPASSWORD";
151 \$ZBX_SERVER = "$PLC_MONITOR_HOST";
152 \$ZBX_SERVER_PORT = "10051";
153 \$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
156 chmod 644 $ZABBIX_WEB_CFG
163 MESSAGE=$"Stopping Monitor"
166 service zabbix_server stop
167 service zabbix_agentd stop
168 # TODO: is there anything to stop?