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/plc.d/monitor.functions
17 . /etc/planetlab/plc_config
18 local_config=/etc/planetlab/configs/site.xml
20 MONITORPATH=/usr/share/monitor
25 function if_present_load ()
28 if [ -f $file ] ; then
29 psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
34 function check_zabbix_schema_and_data()
36 schema_present=$( psql -U $ZABBIX_DB_USER $ZABBIX_DB_NAME -c "\d;" < /dev/null | grep hosts )
37 if [ -z $schema_present ] ; then
38 echo "... initial import can take SEVERAL minutes. please wait ..."
39 if_present_load "/usr/local/zabbix/misc/create/schema/postgresql.sql"
40 if_present_load "/usr/local/zabbix/misc/create/data/data.sql"
41 if_present_load "/usr/local/zabbix/misc/create/data/images_pgsql.sql"
42 ## TODO: update ZABBIX Server entry, "update hosts set status=0, host='MyPLC Server' where hostid=10017"
46 function check_zabbix_templates_and_import ()
49 COOKIE_FILE=/tmp/cookiejar.txt
51 TEMPLATES_DIR=${MONITORPATH}/zabbix/templates
52 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
53 --form "enter=Enter" \
55 --form "password=zabbix" \
56 "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1"
58 deleted=$( grep 'deleted' $COOKIE_FILE )
59 if [ -n "$deleted" ] ; then
60 echo "Login to the zabbix web server failed!!!"
64 for file in ${TEMPLATES_DIR}/*.xml ; do
65 # 0 - update , 1 - skip, 0 - add
66 echo "############### IMPORTING $file" >> /var/log/monitor.log
67 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
69 --form "import_file=@${file}" \
70 --form "rules[host][exist]=0" \
71 --form "rules[host][missed]=0" \
72 --form "rules[template][exist]=0" \
73 --form "rules[template][missed]=1" \
74 --form "rules[item][exist]=0" \
75 --form "rules[item][missed]=0" \
76 --form "rules[trigger][exist]=0" \
77 --form "rules[trigger][missed]=0" \
78 --form "rules[graph][exist]=0" \
79 --form "rules[graph][missed]=0" \
80 --form "import=Import" \
81 "http://${PLC_MONITOR_HOST}/zabbix/exp_imp.php" >> /var/log/monitor.log
85 function check_zab_server ()
88 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
90 if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
91 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
92 -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
93 -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
94 -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
95 -e "s#.*ExternalScripts=.*#ExternalScripts=${MONITORPATH}/zabbix#g" \
96 ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
97 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
99 service zabbix_server start
103 function check_zab_agentd ()
105 ZABBIXCFG=/etc/zabbix
106 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
107 if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
109 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
110 -e "s#Hostname=.*#Hostname=$HOST#g" \
111 ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
112 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf
114 service zabbix_agentd start
117 function check_zab_webconfig()
119 # SETUP zabbix gui configuration
120 ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php
121 if [ ! -f $ZABBIX_WEB_CFG ] ; then
122 touch $ZABBIX_WEB_CFG
123 cat <<EOF > $ZABBIX_WEB_CFG
127 \$DB["TYPE"] = "POSTGRESQL";
128 \$DB["SERVER"] = "localhost";
130 \$DB["DATABASE"] = "$ZABBIX_DB_NAME";
131 \$DB["USER"] = "$ZABBIX_DB_USER";
132 \$DB["PASSWORD"] = "$PLC_MONITOR_DBPASSWORD";
133 \$ZBX_SERVER = "$PLC_MONITOR_HOST";
134 \$ZBX_SERVER_PORT = "10051";
135 \$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
138 chmod 644 $ZABBIX_WEB_CFG
142 if [ "$PLC_ZABBIX_ENABLED" != "1" ] ; then
149 MESSAGE=$"Bootstrap Zabbix (please wait...)"
152 # DATABASE acces, creation, and data loading
153 check_pg_hba $ZABBIX_DB_NAME $ZABBIX_DB_USER
154 check_user_and_db $ZABBIX_DB_NAME $ZABBIX_DB_USER
156 if [ -n "$WROTE_PG_CONFIG" ] ; then
157 # NOTE: restart db to enable access by users granted above.
158 service plc restart postgresql
159 service plc restart httpd
160 MESSAGE=$"Bootstrap Zabbix 2 (please wait...)"
164 check_zabbix_schema_and_data
165 check_zabbix_templates_and_import
167 # START zabbix services. SETUP default config files.
176 MESSAGE=$"Syncing PLC db with Zabbix DB"
179 # turn off zabbix server, etc. before writing to the db.
180 service plc stop monitor-zabbix
182 $MONITORPATH/zabbix/zabbixsync.py --setupids &> /var/log/monitor-server
183 $MONITORPATH/zabbix/zabbixsync.py --setupglobal 2>&1 >> /var/log/monitor-server
184 # import any templates
185 check_zabbix_templates_and_import
187 service plc start monitor-zabbix
193 MESSAGE=$"Deleting Zabbix databases..."
196 dropdb -U postgres $ZABBIX_DB_NAME
197 dropuser -U postgres $ZABBIX_DB_USER
203 MESSAGE=$"Stopping Zabbix"
206 service zabbix_server stop
207 service zabbix_agentd stop