abd770e0e82c8151a4ae0b43399fe632dbd4df21
[monitor.git] / rt3 / monitor-rt3.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 RT3_DB_USER="rt3user"
34 RT3_DB_NAME="rt3"
35
36 WROTE_PG_CONFIG=
37
38 if [ -z "$PLC_MONITOR_IP" ] ; then
39         PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
40 fi
41
42 if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
43     exit 0
44 fi
45
46 # TODO: make values re-configurable...  this may be an issue with RT's db, though.
47 function update_config ()
48 {
49         pattern=$1
50         with=$2
51         file=$3
52         sed -i -e "s/$pattern/$with/g" $file
53 }
54
55 function check_rt_siteconfig ()
56 {
57         tmp_siteconfig=$(mktemp)
58         tmp_initialdata=$(mktemp)
59         
60         # if the templates are newer than the actual config, then replace them.
61         if [ $MONITORPATH/rt3/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
62         then
63                 # copy templates
64                 cp -f $MONITORPATH/rt3/RT_SiteConfig.pm $tmp_siteconfig
65                 cp -f $MONITORPATH/rt3/initialdata $tmp_initialdata
66
67                 # setup RT_SiteConfig.pm
68                 update_config PLC_NAME $PLC_NAME $tmp_siteconfig
69                 update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_siteconfig
70
71                 update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
72                 update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
73                 update_config RT_DB_PASSWORD $PLC_MONITOR_DBPASSWORD $tmp_siteconfig
74
75                 # setup initialdata
76                 update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_initialdata
77
78                 # copy to live configuration
79                 cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
80                 cp -f $tmp_initialdata /etc/rt3/initialdata 
81
82                 rm -f $tmp_siteconfig
83                 rm -f $tmp_initialdata
84         fi
85 }
86
87 function check_rt_pghba ()
88 {
89         NAME=$RT3_DB_NAME
90         USER=$RT3_DB_USER
91         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
92         PATTERN="host all postgres 127.0.0.1/32 trust"
93
94         if ! grep -q $PATTERN $CONF ; then
95                 #### SETUP ACCESS from postgres user to run init for the first time.
96                 echo $PATTERN >> $CONF
97         fi
98
99 }
100
101 function check_rt_aliases ()
102 {
103
104         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
105         then 
106         cat <<EOF >> /etc/aliases
107
108 # added by RT init scripts for default queues.
109 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
110 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
111 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
112 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
113 EOF
114                 /usr/bin/newaliases
115         fi
116
117 }
118
119 function check_rt_init ()
120 {
121         if [ ! -f /etc/rt3/setup.finished ] ; then
122                 /usr/sbin/rt-setup-database --action init --dba postgres
123                 touch /etc/rt3/setup.finished
124         fi
125
126         ###Last DB adjustments
127         #disable queue General, set disabled to 0 in table queues, set defaultduein to 1 for support queue
128 #       /bin/su - -c "/bin/echo \"UPDATE queues SET disabled = 1 WHERE name='General';\"|/usr/bin/psql -U postgres -d rt" postgres
129 #       /bin/su - -c "/bin/echo \"UPDATE queues SET defaultduein = 1 WHERE name='support';\"|/usr/bin/psql -U postgres -d rt" postgres
130 #
131         #alter acl for Everyone be able to create new tickets in support list
132         #Everyone (installation id 3) on support ( installation is 3)
133 #       /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',3,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
134 #       /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',3,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
135 #       #Everyone (installation id 3) on monitor ( installation is 2)
136 #       /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',2,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
137 #       /bin/su - -c "/bin/echo \"INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',2,0,0);\"|/usr/bin/psql -U postgres -d rt" postgres
138         
139 }
140
141 case "$1" in
142         start)
143                 MESSAGE=$"Bootstrap RT (please wait...)"
144                 dialog "$MESSAGE"
145
146                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
147                 check_user_and_db $RT3_DB_NAME $RT3_DB_USER
148                 check_rt_siteconfig
149                 check_rt_pghba
150                 if [ -n "$WROTE_PG_CONFIG" ] ; then
151                         # NOTE: restart db to enable access by users granted above.
152                         service plc restart postgresql
153                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
154                         dialog "$MESSAGE"
155                 fi
156                 check_rt_aliases
157                 check_rt_init
158
159                 result "$MESSAGE"
160         ;;
161
162
163         delete)
164                 MESSAGE=$"Deleting databases..."
165                 dialog "$MESSAGE"
166
167                 dropdb -U postgres $RT3_DB_NAME
168                 dropuser -U postgres $RT3_DB_USER
169                 rm -f /etc/rt3/RT_SiteConfig.pm
170                 rm -f /etc/rt3/initialdata
171                 PATTERN="host all postgres 127.0.0.1/32 trust"
172                 sed -i -e "s/$PATTERN//g" $PGDATA/pg_hba.conf.d/${RT3_DB_USER}.conf
173
174                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
175
176                 result "$MESSAGE"
177         ;;
178
179         stop)
180                 MESSAGE=$"Stopping Monitor"
181                 dialog "$MESSAGE"
182
183                 # TODO: is there anything to stop?
184
185                 result "$MESSAGE"
186         ;;
187 esac
188
189 exit $ERRORS