aremoved hard coded values to MONITOR_SCRIPT_ROOT.
[monitor.git] / monitor-server.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/planetlab/plc_config
17 local_config=/etc/planetlab/configs/site.xml
18
19 MONITORPATH=/usr/share/monitor
20
21 # Be verbose
22 set -x
23
24 # Default locations
25 PGDATA=/var/lib/pgsql/data
26 postgresql_conf=$PGDATA/postgresql.conf
27 pghba_conf=$PGDATA/pg_hba.conf
28
29 # Export so that we do not have to specify -p to psql invocations
30 export PGPORT=$PLC_DB_PORT
31
32
33 ZABBIX_DB_USER="zabbixuser"
34 ZABBIX_DB_NAME="zabbix"
35
36 function check_pg_hba ()
37 {
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
44
45                 # NOTE: restart db to enable access by users granted above.
46                 service plc restart posgresql
47         fi
48 }
49
50 function check_user_and_db()
51 {
52     CREATED=
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
57                 CREATED="true"
58     fi
59     
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
64                 CREATED="true"
65     fi
66
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
72         service plc reload
73                 CREATED="true"
74     fi
75     if [ -n "$CREATED" ] ; then
76         psql -d template1 -U postgres -c "ALTER USER $ZABBIX_DB_USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
77     fi
78 }
79
80 function if_present_load ()
81 {
82     file=$1
83     if [ -f $file ] ; then
84         psql -d $ZABBIX_DB_NAME -U $ZABBIX_DB_USER < $file
85     fi
86 }
87
88 function check_schema_and_data() 
89 {
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"
97     fi
98 }
99
100 function check_templates_and_import ()
101 {
102         # LOG IN
103         COOKIE_FILE=/tmp/cookiejar.txt
104         rm -f ${COOKIE_FILE}
105         TEMPLATES_DIR=${MONITORPATH}/zabbix/templates
106         curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
107                         --form "enter=Enter" \
108                         --form "name=Admin" \
109                         --form "password=zabbix" \
110                         "http://${PLC_MONITOR_HOST}/zabbix/index.php?login=1"
111         
112         deleted=$( grep 'deleted' $COOKIE_FILE )
113         if [ -n "$deleted" ] ; then
114                 echo "Login to the zabbix web server failed!!!"
115                 return 1
116         fi
117
118         for file in ${TEMPLATES_DIR}/*.xml ; do 
119                 # 0 - update , 1 - skip, 0 - add
120                 echo "############### IMPORTING $file" >> /var/log/monitor.log
121                 curl -s --cookie $COOKIE_FILE --cookie-jar $COOKIE_FILE \
122                         --form "config=1" \
123                         --form "import_file=@${file}" \
124                         --form "rules[host][exist]=0" \
125                         --form "rules[host][missed]=0" \
126                         --form "rules[template][exist]=0" \
127                         --form "rules[template][missed]=1" \
128                         --form "rules[item][exist]=0" \
129                         --form "rules[item][missed]=0" \
130                         --form "rules[trigger][exist]=0" \
131                         --form "rules[trigger][missed]=0" \
132                         --form "rules[graph][exist]=0" \
133                         --form "rules[graph][missed]=0" \
134                         --form "import=Import" \
135                         "http://${PLC_MONITOR_HOST}/zabbix/exp_imp.php" >> /var/log/monitor.log
136         done
137 }
138
139
140 function check_monitor_conf ()
141 {
142         MONITOR_CONFIG=/etc/monitor.conf
143
144         if [ -z "$PLC_MONITOR_IP" ] ; then
145                 PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
146         fi
147
148         # Using plcsh add default, monitor user
149         plcsh <<EOF &>/dev/null 
150 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
151 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
152 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
153 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
154 EOF
155
156         if [ ! -f ${MONITOR_CONFIG} ] ; then
157                 cat <<EOF > ${MONITOR_CONFIG}
158 [monitorconfig]
159 RT_DB_HOST=rt.planet-lab.org
160 RT_DB_USER=
161 RT_DB_PASSWORD=
162 RT_DB_NAME=
163
164 # RT Web user account
165 RT_WEB_SERVER=https://rt.planet-lab.org/
166 RT_WEB_TOOLS_PATH=
167 RT_WEB_USER=
168 RT_WEB_PASSWORD=
169 RT_WEB_DEBUG=0
170 RT_QUEUE=
171
172 # PLC admin account
173 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
174 API_AUTH_USER=${PLC_MONITOR_EMAIL}
175 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
176
177 # SERVER PATHS
178 MONITOR_SCRIPT_ROOT=${MONITORPATH}
179 MONITOR_DATA_ROOT=/var/lib/monitor
180 MONITOR_ARCHIVE_ROOT=/var/lib/monitor/archive-pdb
181
182 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
183 MONITOR_IP=${PLC_MONITOR_IP}
184
185 # used for debug mode
186 email=
187
188 # all messages will appear to be from this address
189 from_email=${PLC_MONITOR_EMAIL}
190
191 # a separate address for support messages
192 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
193
194 # mailing list copied on all out-going messages
195 cc_email=
196
197 [monitordatabase]
198 monitor_dburi=postgres://monitoruser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/monitor
199 zabbix_dburi=postgres://zabbixuser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/zabbix
200
201 cachetime=60
202
203 [commandline]
204 echo=False
205 debug=False
206 mail=True
207 bcc=False
208 run=False
209 checkopt=False
210 squeeze=True
211 policysavedb=True
212 EOF
213
214         fi
215 }
216
217 function check_zab_server ()
218 {
219         ZABBIXCFG=/etc/zabbix
220         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
221
222         if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
223                 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
224                     -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
225                     -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
226                     -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
227                     ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
228                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
229         fi
230         service zabbix_server start
231         rm -f $TMP_FILE
232
233 }
234 function check_zab_agentd ()
235 {
236         ZABBIXCFG=/etc/zabbix
237         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
238         if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
239                 HOST=`hostname`
240                 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
241                     -e "s#Hostname=.*#Hostname=$HOST#g" \
242                     ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
243                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf 
244         fi
245         service zabbix_agentd start
246         rm -f $TMP_FILE
247 }
248 function check_zab_webconfig()
249 {
250         # SETUP zabbix gui configuration
251         ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php 
252         if [ ! -f $ZABBIX_WEB_CFG ] ; then
253                 touch  $ZABBIX_WEB_CFG
254                 cat <<EOF > $ZABBIX_WEB_CFG
255 <?php
256 global \$DB;
257
258 \$DB["TYPE"]            = "POSTGRESQL";
259 \$DB["SERVER"]          = "localhost";
260 \$DB["PORT"]            = "0";
261 \$DB["DATABASE"]                = "$ZABBIX_DB_NAME";
262 \$DB["USER"]            = "$ZABBIX_DB_USER";
263 \$DB["PASSWORD"]                = "$PLC_MONITOR_DBPASSWORD";
264 \$ZBX_SERVER            = "$PLC_MONITOR_HOST";
265 \$ZBX_SERVER_PORT       = "10051";
266 \$IMAGE_FORMAT_DEFAULT  = IMAGE_FORMAT_PNG;
267 ?>
268 EOF
269                 chmod 644 $ZABBIX_WEB_CFG
270         fi
271 }
272
273 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
274     exit 0
275 fi
276
277 case "$1" in
278         start)
279                 MESSAGE=$"Bootstrap Monitoring"
280                 dialog "$MESSAGE"
281
282                 # DATABASE acces, creation, and data loading
283                 check_pg_hba
284                 check_user_and_db 
285                 check_schema_and_data
286                 check_templates_and_import
287
288                 # WRITE default /etc/monitor.conf
289                 check_monitor_conf
290
291                 # START zabbix services.  SETUP default config files.
292                 check_zab_server
293                 check_zab_agentd
294                 check_zab_webconfig
295
296
297                 result "$MESSAGE"
298         ;;
299
300         sync)
301                 MESSAGE=$"Syncing PLC db with Zabbix DB"
302                 dialog "$MESSAGE"
303
304                 # turn off zabbix server, etc. before writing to the db.
305                 service plc stop monitor 
306
307                 $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server
308                 # import any templates
309                 check_templates_and_import
310
311                 service plc start monitor 
312                 
313                 result "$MESSAGE"
314         ;;
315
316         stop)
317                 MESSAGE=$"Stopping Monitor"
318                 dialog "$MESSAGE"
319
320                 service zabbix_server stop
321                 service zabbix_agentd stop
322                 # TODO: is there anything to stop?
323                 result "$MESSAGE"
324         ;;
325 esac
326
327 exit $ERRORS