add default /etc/monitor.conf to monitor-server.init
[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-server
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_monitor_conf ()
101 {
102         MONITOR_CONFIG=/etc/monitor.conf
103
104         if [ -z "$PLC_MONITOR_IP" ] ; then
105                 PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
106         fi
107
108         # Using plcsh add default, monitor user
109         plcsh <<EOF &>/dev/null 
110 AddPerson({'first_name' : 'Monitor', 'last_name' : 'Server', 'password' : '${PLC_MONITOR_DBPASSWORD}', 'email' : '${PLC_MONITOR_EMAIL}'})
111 AddRoleToPerson('admin', '${PLC_MONITOR_EMAIL}')
112 AddPersonToSite('${PLC_MONITOR_EMAIL}', '${PLC_SLICE_PREFIX}')
113 UpdatePerson('${PLC_MONITOR_EMAIL}', { 'enabled' : True, 'password' : '${PLC_MONITOR_DBPASSWORD}' })
114 EOF
115
116         if [ ! -f ${MONITOR_CONFIG} ] ; then
117                 cat <<EOF > ${MONITOR_CONFIG}
118 [monitorconfig]
119 RT_DB_HOST=rt.planet-lab.org
120 RT_DB_USER=
121 RT_DB_PASSWORD=
122 RT_DB_NAME=
123
124 # RT Web user account
125 RT_WEB_SERVER=https://rt.planet-lab.org/
126 RT_WEB_TOOLS_PATH=
127 RT_WEB_USER=
128 RT_WEB_PASSWORD=
129 RT_WEB_DEBUG=0
130 RT_QUEUE=
131
132 # PLC admin account
133 API_SERVER=https://${PLC_BOOT_HOST}/PLCAPI/
134 API_AUTH_USER=${PLC_MONITOR_EMAIL}
135 API_AUTH_PASSWORD=${PLC_MONITOR_DBPASSWORD}
136
137 # SERVER PATHS
138 MONITOR_SCRIPT_ROOT=/usr/share/monitor-server
139 MONITOR_DATA_ROOT=/var/lib/monitor-server
140 MONITOR_ARCHIVE_ROOT=/var/lib/monitor-server/archive-pdb
141
142 MONITOR_HOSTNAME=${PLC_MONITOR_HOST}
143 MONITOR_IP=${PLC_MONITOR_IP}
144
145 # used for debug mode
146 email=
147
148 # all messages will appear to be from this address
149 from_email=${PLC_MONITOR_EMAIL}
150
151 # a separate address for support messages
152 support_email=${PLC_MAIL_SUPPORT_ADDRESS}
153
154 # mailing list copied on all out-going messages
155 cc_email=
156
157 [monitordatabase]
158 monitor_dburi=postgres://monitoruser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/monitor
159 zabbix_dburi=postgres://zabbixuser:${PLC_MONITOR_DBPASSWORD}@localhost:5432/zabbix
160
161 cachetime=60
162
163 [commandline]
164 echo=False
165 debug=False
166 mail=True
167 bcc=False
168 run=False
169 checkopt=False
170 squeeze=True
171 policysavedb=True
172 EOF
173
174         fi
175 }
176
177 function check_zab_server ()
178 {
179         ZABBIXCFG=/etc/zabbix
180         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
181
182         if [ -f ${ZABBIXCFG}/zabbix_server.conf ] ; then
183                 sed -e "s/#DBHost=.*/DBHost=$PLC_MONITOR_HOST/g" \
184                     -e "s#DBName=.*#DBName=$ZABBIX_DB_NAME#g" \
185                     -e "s#DBUser=.*#DBUser=$ZABBIX_DB_USER#g" \
186                     -e "s#DBPassword=.*#DBPassword=$PLC_MONITOR_DBPASSWORD#g" \
187                     ${ZABBIXCFG}/zabbix_server.conf > $TMP_FILE
188                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_server.conf
189         fi
190         service zabbix_server start
191         rm -f $TMP_FILE
192
193 }
194 function check_zab_agentd ()
195 {
196         ZABBIXCFG=/etc/zabbix
197         TMP_FILE=`mktemp /tmp/zbxtmpXXXXXX`
198         if [ -f ${ZABBIXCFG}/zabbix_agentd.conf ] ; then
199                 HOST=`hostname`
200                 sed -e "s#Server=.*#Server=$PLC_MONITOR_HOST#g" \
201                     -e "s#Hostname=.*#Hostname=$HOST#g" \
202                     ${ZABBIXCFG}/zabbix_agentd.conf > $TMP_FILE
203                 cat $TMP_FILE > ${ZABBIXCFG}/zabbix_agentd.conf 
204         fi
205         service zabbix_agentd start
206         rm -f $TMP_FILE
207 }
208 function check_zab_webconfig()
209 {
210         # SETUP zabbix gui configuration
211         ZABBIX_WEB_CFG=/var/www/html/zabbix/conf/zabbix.conf.php 
212         if [ ! -f $ZABBIX_WEB_CFG ] ; then
213                 touch  $ZABBIX_WEB_CFG
214                 cat <<EOF > $ZABBIX_WEB_CFG
215 <?php
216 global \$DB;
217
218 \$DB["TYPE"]            = "POSTGRESQL";
219 \$DB["SERVER"]          = "localhost";
220 \$DB["PORT"]            = "0";
221 \$DB["DATABASE"]                = "$ZABBIX_DB_NAME";
222 \$DB["USER"]            = "$ZABBIX_DB_USER";
223 \$DB["PASSWORD"]                = "$PLC_MONITOR_DBPASSWORD";
224 \$ZBX_SERVER            = "$PLC_MONITOR_HOST";
225 \$ZBX_SERVER_PORT       = "10051";
226 \$IMAGE_FORMAT_DEFAULT  = IMAGE_FORMAT_PNG;
227 ?>
228 EOF
229                 chmod 644 $ZABBIX_WEB_CFG
230         fi
231 }
232
233 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
234     exit 0
235 fi
236
237 case "$1" in
238         start)
239                 MESSAGE=$"Bootstrap Monitoring"
240                 dialog "$MESSAGE"
241
242
243                 # DATABASE acces, creation, and data loading
244                 check_pg_hba
245                 check_user_and_db 
246                 check_schema_and_data
247
248                 # WRITE default /etc/monitor.conf
249                 check_monitor_conf
250
251                 # START zabbix services.  SETUP default config files.
252                 check_zab_server
253                 check_zab_agentd
254                 check_zab_webconfig
255
256
257                 result "$MESSAGE"
258         ;;
259
260         sync)
261                 MESSAGE=$"Syncing PLC db with Zabbix DB"
262                 dialog "$MESSAGE"
263
264                 $MONITORPATH/zabbix/zabbixsync.py --setupglobal &> /var/log/monitor-server
265                 
266                 result "$MESSAGE"
267         ;;
268
269         stop)
270                 MESSAGE=$"Stopping Monitor"
271                 dialog "$MESSAGE"
272
273                 service zabbix_server stop
274                 service zabbix_agentd stop
275                 # TODO: is there anything to stop?
276                 result "$MESSAGE"
277         ;;
278 esac
279
280 exit $ERRORS