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-server
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 function check_pg_hba ()
38 #### SETUP ACCESS to this user and database
39 mkdir -p $PGDATA/pg_hba.conf.d
40 ZABCONF=$PGDATA/pg_hba.conf.d/zabbix.conf
41 if [ ! -f $ZABCONF ] ; then
42 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER 127.0.0.1/32 password" > $ZABCONF
43 echo "host $ZABBIX_DB_NAME $ZABBIX_DB_USER $PLC_MONITOR_IP/32 password" >> $ZABCONF
45 # NOTE: restart db to enable access by users granted above.
46 service plc restart posgresql
50 function check_user_and_db()
53 # confirm user is present or create it
54 user_present=$( psql -U postgres -c "select * from pg_user;" -d template1 | grep $ZABBIX_DB_USER )
55 if [ -z $user_present ] ; then
56 createuser --no-superuser --no-createdb --no-createrole --login --unencrypted --echo $ZABBIX_DB_USER -U postgres
60 # confirm database is present or create it
61 db_present=$( psql -U postgres -c "select * from pg_database;" -d template1 | grep $ZABBIX_DB_NAME )
62 if [ -z $db_present ] ; then
63 createdb --owner=$ZABBIX_DB_USER $ZABBIX_DB_NAME -U postgres
67 # Create/update the unprivileged database user and password
68 if [ -z "$PLC_MONITOR_DBPASSWORD" ] ; then
69 # Zabbix doesn't like plain uuidgen passwords
70 PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
71 plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config
75 if [ -n "$CREATED" ] ; then
76 psql -d template1 -U postgres -c "ALTER USER $ZABBIX_DB_USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
80 function if_present_load ()
83 if [ -f $file ] ; then
84 psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
88 function check_schema_and_data()
90 schema_present=$( psql -U $ZABBIX_DB_USER $ZABBIX_DB_NAME -c "\d;" < /dev/null | grep hosts )
91 if [ -z $schema_present ] ; then
92 echo "... initial import can take SEVERAL minutes. please wait ..."
93 if_present_load "/usr/local/zabbix/misc/create/schema/postgresql.sql"
94 if_present_load "/usr/local/zabbix/misc/create/data/data.sql"
95 if_present_load "/usr/local/zabbix/misc/create/data/images_pgsql.sql"
96 ## TODO: update ZABBIX Server entry, "update hosts set status=0, host='MyPLC Server' where hostid=10017"
100 function check_templates_and_import ()
103 COOKIE_FILE=/tmp/cookiejar.txt
104 TEMPLATES_DIR=/usr/share/monitor/zabbix/templates
105 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
106 --form "enter=Enter" \
107 --form "name=Admin" \
108 --form "password=zabbix" \
109 "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1"
111 deleted=$( grep 'deleted' $COOKIE_FILE )
112 if [ -n "$deleted" ] ; then
113 echo "Login to the zabbix web server failed!!!"
117 for file in ${TEMPLATES_DIR}/*.xml ; do
118 # 0 - update , 1 - skip, 0 - add
119 echo "############### IMPORTING $file" >> /var/log/monitor.log
120 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
122 --form "import_file=@${file}" \
123 --form "rules[host][exist]=0" \
124 --form "rules[host][missed]=0" \
125 --form "rules[template][exist]=0" \
126 --form "rules[template][missed]=1" \
127 --form "rules[item][exist]=0" \
128 --form "rules[item][missed]=0" \
129 --form "rules[trigger][exist]=0" \
130 --form "rules[trigger][missed]=0" \
131 --form "rules[graph][exist]=0" \
132 --form "rules[graph][missed]=0" \
133 --form "import=Import" \
134 "http://${PLC_MONITOR_HOST}/zabbix/exp_imp.php" >> /var/log/monitor.log
139 function check_monitor_conf ()
141 MONITOR_CONFIG=/etc/monitor.conf
143 if [ -z "$PLC_MONITOR_IP" ] ; then
144 PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
147 # Using plcsh add default, monitor user
148 plcsh <<EOF &>/dev/null
149 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
150 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
151 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
152 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
155 if [ ! -f ${MONITOR_CONFIG} ] ; then
156 cat <<EOF > ${MONITOR_CONFIG}
158 RT_DB_HOST=rt.planet-lab.org
163 # RT Web user account
164 RT_WEB_SERVER=https://rt.planet-lab.org/
172 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
173 API_AUTH_USER=${PLC_MONITOR_EMAIL}
174 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
177 MONITOR_SCRIPT_ROOT=/usr/share/monitor-server
178 MONITOR_DATA_ROOT=/var/lib/monitor-server
179 MONITOR_ARCHIVE_ROOT=/var/lib/monitor-server/archive-pdb
181 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
182 MONITOR_IP=${PLC_MONITOR_IP}
184 # used for debug mode
187 # all messages will appear to be from this address
188 from_email=${PLC_MONITOR_EMAIL}
190 # a separate address for support messages
191 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
193 # mailing list copied on all out-going messages
197 monitor_dburi=postgres://monitoruser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/monitor
198 zabbix_dburi=postgres://zabbixuser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/zabbix
216 function check_zab_server ()
218 ZABBIXCFG=/etc/zabbix
219 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
221 if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
222 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
223 -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
224 -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
225 -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
226 ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
227 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
229 service zabbix_server start
233 function check_zab_agentd ()
235 ZABBIXCFG=/etc/zabbix
236 TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
237 if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
239 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
240 -e "s#Hostname=.*#Hostname=$HOST#g" \
241 ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
242 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf
244 service zabbix_agentd start
247 function check_zab_webconfig()
249 # SETUP zabbix gui configuration
250 ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php
251 if [ ! -f $ZABBIX_WEB_CFG ] ; then
252 touch $ZABBIX_WEB_CFG
253 cat <<EOF > $ZABBIX_WEB_CFG
257 \$DB["TYPE"] = "POSTGRESQL";
258 \$DB["SERVER"] = "localhost";
260 \$DB["DATABASE"] = "$ZABBIX_DB_NAME";
261 \$DB["USER"] = "$ZABBIX_DB_USER";
262 \$DB["PASSWORD"] = "$PLC_MONITOR_DBPASSWORD";
263 \$ZBX_SERVER = "$PLC_MONITOR_HOST";
264 \$ZBX_SERVER_PORT = "10051";
265 \$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
268 chmod 644 $ZABBIX_WEB_CFG
272 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
278 MESSAGE=$"Bootstrap Monitoring"
281 # DATABASE acces, creation, and data loading
284 check_schema_and_data
285 check_templates_and_import
287 # WRITE default /etc/monitor.conf
290 # START zabbix services. SETUP default config files.
300 MESSAGE=$"Syncing PLC db with Zabbix DB"
303 # turn off zabbix server, etc. before writing to the db.
304 service plc stop monitor
306 $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server
307 # import any templates
308 check_templates_and_import
310 service plc start monitor
316 MESSAGE=$"Stopping Monitor"
319 service zabbix_server stop
320 service zabbix_agentd stop
321 # TODO: is there anything to stop?