clearer names for actions, and infer actions better
[monitor.git] / zabbix / monitor-zabbix.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/plc.d/monitor.functions
17 . /etc/planetlab/plc_config
18 local_config=/etc/planetlab/configs/site.xml
19
20 MONITORPATH=/usr/share/monitor
21 # Be verbose
22 set -x
23
24
25 function if_present_load ()
26 {
27     file=$1
28     if [ -f $file ] ; then
29         psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
30     fi
31 }
32
33
34 function check_zabbix_schema_and_data() 
35 {
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"
43     fi
44 }
45
46 function check_zabbix_templates_and_import ()
47 {
48         # LOG IN
49         COOKIE_FILE=/tmp/cookiejar.txt
50         rm -f ${COOKIE_FILE}
51         TEMPLATES_DIR=${MONITORPATH}/zabbix/templates
52         curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
53                         --form "enter=Enter" \
54                         --form "name=Admin" \
55                         --form "password=zabbix" \
56                         "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1"
57         
58         deleted=$( grep 'deleted' $COOKIE_FILE )
59         if [ -n "$deleted" ] ; then
60                 echo "Login to the zabbix web server failed!!!"
61                 return 1
62         fi
63
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 \
68                         --form "config=1" \
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
82         done
83 }
84
85 function check_zab_server ()
86 {
87         ZABBIXCFG=/etc/zabbix
88         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
89
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
98         fi
99         service zabbix_server start
100         rm -f $TMP_FILE
101
102 }
103 function check_zab_agentd ()
104 {
105         ZABBIXCFG=/etc/zabbix
106         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
107         if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
108                 HOST=`hostname`
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 
113         fi
114         service zabbix_agentd start
115         rm -f $TMP_FILE
116 }
117 function check_zab_webconfig()
118 {
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
124 <?php
125 global \$DB;
126
127 \$DB["TYPE"]            = "POSTGRESQL";
128 \$DB["SERVER"]          = "localhost";
129 \$DB["PORT"]            = "0";
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;
136 ?>
137 EOF
138                 chmod 644 $ZABBIX_WEB_CFG
139         fi
140 }
141
142 if [ "$PLC_ZABBIX_ENABLED" != "1" ] ; then
143     exit 0
144 fi
145
146
147 case "$1" in
148         start)
149                 MESSAGE=$"Bootstrap Zabbix (please wait...)"
150                 dialog "$MESSAGE"
151
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
155
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...)"
161                         dialog "$MESSAGE"
162                 fi
163
164                 check_zabbix_schema_and_data
165                 check_zabbix_templates_and_import
166
167                 # START zabbix services.  SETUP default config files.
168                 check_zab_server
169                 check_zab_agentd
170                 check_zab_webconfig
171
172                 result "$MESSAGE"
173         ;;
174
175         sync)
176                 MESSAGE=$"Syncing PLC db with Zabbix DB"
177                 dialog "$MESSAGE"
178
179                 # turn off zabbix server, etc. before writing to the db.
180                 service plc stop monitor-zabbix
181
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
186
187                 service plc start monitor-zabbix 
188                 
189                 result "$MESSAGE"
190         ;;
191
192         delete)
193                 MESSAGE=$"Deleting Zabbix databases..."
194                 dialog "$MESSAGE"
195
196                 dropdb -U postgres $ZABBIX_DB_NAME
197                 dropuser -U postgres $ZABBIX_DB_USER
198
199                 result "$MESSAGE"
200         ;;
201
202         stop)
203                 MESSAGE=$"Stopping Zabbix"
204                 dialog "$MESSAGE"
205
206                 service zabbix_server stop
207                 service zabbix_agentd stop
208
209                 result "$MESSAGE"
210         ;;
211 esac
212
213 exit $ERRORS