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
19 MONITORPATH=/usr/share/monitor
25 PGDATA=/var/lib/pgsql/data
26 postgresql_conf=$PGDATA/postgresql.conf
27 pghba_conf=$PGDATA/pg_hba.conf
29 # Export so that we do not have to specify -p to psql invocations
30 export PGPORT=$PLC_DB_PORT
33 ZABBIX_DB_USER="zabbixuser"
34 ZABBIX_DB_NAME="zabbix"
36 MONITOR_DB_USER="monitoruser"
37 MONITOR_DB_NAME="monitor"
41 if [ -z "$PLC_MONITOR_IP" ] ; then
42 PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
45 function check_pg_hba ()
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
56 WROTE_PG_CONFIG="true"
60 function check_user_and_db()
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
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
79 # Create/update the unprivileged database user and password
80 if [ -z "$PLC_MONITOR_DBPASSWORD" ] ; 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
87 if [ -n "$CREATED" ] ; then
88 psql -d template1 -U postgres -c "ALTER USER $USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
92 function if_present_load ()
95 if [ -f $file ] ; then
96 psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
99 function check_monitor_schema_and_data()
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()"
105 function check_zabbix_schema_and_data()
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"
117 function check_zabbix_templates_and_import ()
120 COOKIE_FILE=/tmp/cookiejar.txt
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"
129 deleted=$( grep 'deleted' $COOKIE_FILE )
130 if [ -n "$deleted" ] ; then
131 echo "Login to the zabbix web server failed!!!"
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 \
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
157 function check_monitor_conf ()
159 MONITOR_CONFIG=/etc/monitor.conf
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}' })
170 if [ ! -f ${MONITOR_CONFIG} ] ; then
171 cat <<EOF > ${MONITOR_CONFIG}
173 RT_DB_HOST=rt.planet-lab.org
178 # RT Web user account
179 RT_WEB_SERVER=https://rt.planet-lab.org/
187 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
188 API_AUTH_USER=${PLC_MONITOR_EMAIL}
189 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
192 MONITOR_SCRIPT_ROOT=${MONITORPATH}
193 MONITOR_DATA_ROOT=/var/lib/monitor
194 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
196 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
197 MONITOR_IP=${PLC_MONITOR_IP}
199 # used for debug mode
202 # all messages will appear to be from this address
203 from_email=${PLC_MONITOR_EMAIL}
205 # a separate address for support messages
206 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
208 # mailing list copied on all out-going messages
212 monitor_dburi=postgres://${MONITOR_DB_NAME}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${MONITOR_DB_NAME}
213 zabbix_dburi=postgres://${ZABBIX_DB_USER}:${PLC_MONITOR_DBPASSWORD}@localhost:5432/${ZABBIX_DB_NAME}
217 # Evaluated as true or false
234 function check_zab_server ()
236 ZABBIXCFG=/etc/zabbix
237 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
239 if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
240 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
241 -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
242 -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
243 -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
244 -e "s/.*ExternalScripts=.*/ExternalScripts=${MONITORPATH}\/zabbix/g" \
245 ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
246 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
248 service zabbix_server start
252 function check_zab_agentd ()
254 ZABBIXCFG=/etc/zabbix
255 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
256 if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
258 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
259 -e "s#Hostname=.*#Hostname=$HOST#g" \
260 ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
261 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf
263 service zabbix_agentd start
266 function check_zab_webconfig()
268 # SETUP zabbix gui configuration
269 ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php
270 if [ ! -f $ZABBIX_WEB_CFG ] ; then
271 touch $ZABBIX_WEB_CFG
272 cat <<EOF > $ZABBIX_WEB_CFG
276 \$DB["TYPE"] = "POSTGRESQL";
277 \$DB["SERVER"] = "localhost";
279 \$DB["DATABASE"] = "$ZABBIX_DB_NAME";
280 \$DB["USER"] = "$ZABBIX_DB_USER";
281 \$DB["PASSWORD"] = "$PLC_MONITOR_DBPASSWORD";
282 \$ZBX_SERVER = "$PLC_MONITOR_HOST";
283 \$ZBX_SERVER_PORT = "10051";
284 \$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
287 chmod 644 $ZABBIX_WEB_CFG
291 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
297 MESSAGE=$"Bootstrap Monitoring"
300 # DATABASE acces, creation, and data loading
301 check_pg_hba $MONITOR_DB_NAME $MONITOR_DB_USER
302 check_user_and_db $MONITOR_DB_NAME $MONITOR_DB_USER
303 check_monitor_schema_and_data
305 check_pg_hba $ZABBIX_DB_NAME $ZABBIX_DB_USER
306 check_user_and_db $ZABBIX_DB_NAME $ZABBIX_DB_USER
308 if [ -n "$WROTE_PG_CONFIG" ] ; then
309 # NOTE: restart db to enable access by users granted above.
310 service plc restart postgresql
313 check_zabbix_schema_and_data
314 check_zabbix_templates_and_import
316 # WRITE default /etc/monitor.conf
319 # START zabbix services. SETUP default config files.
328 MESSAGE=$"Syncing PLC db with Zabbix DB"
331 # turn off zabbix server, etc. before writing to the db.
332 service plc stop monitor
334 $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server
335 # import any templates
336 check_zabbix_templates_and_import
338 service plc start monitor
344 MESSAGE=$"Stopping Monitor"
347 service zabbix_server stop
348 service zabbix_agentd stop
349 # TODO: is there anything to stop?