commented out unused value
[monitor.git] / monitor.functions
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 WROTE_PG_CONFIG=
33
34 if [ -z "$PLC_MONITOR_IP" ] ; then
35         PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
36 fi
37
38 function check_pg_hba ()
39 {
40         NAME=$1
41         USER=$2
42         #### SETUP ACCESS to this user and database
43         mkdir -p $PGDATA/pg_hba.conf.d
44         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
45         if [ ! -f $CONF ] ; then
46                 echo "host $NAME $USER 127.0.0.1/32 password"   > $CONF
47                 echo "host $NAME $USER $PLC_MONITOR_IP/32 password" >> $CONF
48
49                 WROTE_PG_CONFIG="true"
50         fi
51 }
52
53 function check_user_and_db()
54 {
55     CREATED=
56         NAME=$1
57         USER=$2
58     # confirm user is present or create it
59     user_present=$( psql -U postgres -c "select * from pg_user;" -d template1 | grep $USER )
60     if [ -z $user_present ] ; then 
61         createuser --no-superuser --no-createdb --no-createrole --login --unencrypted --echo $USER -U postgres
62                 CREATED="true"
63     fi
64     
65     # confirm database is present or create it
66     db_present=$( psql -U postgres -c "select * from pg_database;" -d template1 | grep $NAME )
67     if [ -z $db_present ] ; then
68         createdb --owner=$USER $NAME -U postgres
69                 CREATED="true"
70     fi
71
72     # Create/update the unprivileged database user and password
73     if [[ -z "$PLC_MONITOR_DBPASSWORD" || "$PLC_MONITOR_DBPASSWORD" = "None" ]] ; then
74         # Zabbix doesn't like plain uuidgen passwords
75         PLC_MONITOR_DBPASSWORD=$( uuidgen | md5sum - | awk '{print $1}' )
76         plc-config --category=plc_monitor --variable=dbpassword --value="$PLC_MONITOR_DBPASSWORD" --save=$local_config $local_config
77         service plc reload
78                 CREATED="true"
79     fi
80     if [ -n "$CREATED" ] ; then
81         psql -d template1 -U postgres -c "ALTER USER $USER WITH PASSWORD '$PLC_MONITOR_DBPASSWORD';"
82     fi
83 }
84 function check_monitor_schema_and_data() 
85 {
86         # NOTE: call create_all() to setup the database from the info model.
87         python -c "from monitor.database.info.model import *; from elixir import create_all; create_all()"
88 }
89