cleaned and improved monitor-rt3.init
[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/plc.d/monitor.functions
17 . /etc/planetlab/plc_config
18 local_config=/etc/planetlab/configs/site.xml
19
20 MONITORPATH=/usr/share/monitor
21
22 # Be verbose
23 set -x
24
25 # Default locations
26 PGDATA=/var/lib/pgsql/data
27 postgresql_conf=$PGDATA/postgresql.conf
28 pghba_conf=$PGDATA/pg_hba.conf
29
30 # Export so that we do not have to specify -p to psql invocations
31 export PGPORT=$PLC_DB_PORT
32
33
34 RT3_DB_USER="rt3user"
35 RT3_DB_NAME="rt3"
36
37 WROTE_PG_CONFIG=
38
39 if [ -z "$PLC_MONITOR_IP" ] ; then
40         PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
41 fi
42
43
44 # TODO: make values re-configurable...  this may be an issue with RT's db, though.
45 function update_config ()
46 {
47         pattern=$1
48         with=$2
49         file=$3
50         sed -i -e "s/$pattern/$with/g" $file
51 }
52
53 function check_rt_siteconfig ()
54 {
55         tmp_siteconfig=$(mktemp)
56         tmp_initialdata=$(mktemp)
57         
58         # if the templates are newer than the actual config, then replace them.
59         if [ $MONITORPATH/rt3/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
60         then
61                 # copy templates
62                 cp -f $MONITORPATH/rt3/RT_SiteConfig.pm $tmp_siteconfig
63                 cp -f $MONITORPATH/rt3/initialdata $tmp_initialdata
64
65                 # setup RT_SiteConfig.pm
66                 update_config PLC_NAME "$PLC_NAME" $tmp_siteconfig
67                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_siteconfig
68
69                 update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
70                 update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
71                 update_config RT_DB_PASSWORD $PLC_MONITOR_DBPASSWORD $tmp_siteconfig
72
73                 # setup initialdata
74                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_initialdata
75
76                 # copy to live configuration
77                 cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
78                 cp -f $tmp_initialdata /etc/rt3/initialdata 
79
80                 rm -f $tmp_siteconfig
81                 rm -f $tmp_initialdata
82         fi
83 }
84
85 function check_rt_pghba ()
86 {
87         NAME=$RT3_DB_NAME
88         USER=$RT3_DB_USER
89         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
90         PATTERN="host all postgres 127.0.0.1/32 trust"
91
92         if ! grep -q "$PATTERN" $CONF ; then
93                 #### SETUP ACCESS from postgres user to run init for the first time.
94                 echo "$PATTERN" >> $CONF
95         fi
96
97 }
98
99 function check_rt_aliases ()
100 {
101
102         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
103         then 
104                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
105                 sed -i -e "s/^security.*postmaster//g" /etc/aliases
106         cat <<EOF >> /etc/aliases
107 # added by RT init scripts for default queues.
108 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
109 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
110 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
111 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
112 EOF
113                 /usr/bin/newaliases
114         fi
115
116 }
117
118 function check_rt_init ()
119 {
120         if [ ! -f /etc/rt3/setup.finished ] ; then
121                 /usr/sbin/rt-setup-database --action init --dba postgres
122                 touch /etc/rt3/setup.finished
123
124         ###Last DB adjustments
125         #set defaultduein to 1 for support queue
126         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
127 UPDATE queues SET defaultduein = 1 WHERE name='support'
128 EOF
129
130         #alter acl for Everyone be able to create new tickets in support list
131         #Everyone (groups.id == 3) on support (queues.id == 1 (based on order in initialdata))
132         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
133 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',1,0,0);
134 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',1,0,0);
135 EOF
136         #Everyone (groups.id == 3) on monitor ( queues.id == 2 (based on order in initaldata))
137         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
138 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'CreateTicket','RT::Queue',2,0,0);
139 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ReplyToTicket','RT::Queue',2,0,0);
140 EOF
141         #setup custom field values
142         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
143 INSERT INTO objectcustomfields (customfield, objectid, sortorder, creator, lastupdatedby) VALUES (1,1,1,12,12);
144 INSERT INTO customfields (name, "type", maxvalues, repeated, pattern, lookuptype, description, sortorder, creator, lastupdatedby,disabled) VALUES ('Problem Category', 'Select',1,0,'(?#Mandatory).','RT::Queue-RT::Ticket','How severe this problem is',0,12,12,0);
145 EOF
146         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
147 INSERT INTO customfieldvalues (customfield, name, sortorder, creator, lastupdatedby) VALUES (1,'Critical',0,12,12);
148 INSERT INTO customfieldvalues (customfield, name, sortorder, creator, lastupdatedby) VALUES (1,'Major',1,12,12);
149 INSERT INTO customfieldvalues (customfield, name, sortorder, creator, lastupdatedby) VALUES (1,'Minor',2,12,12);
150 INSERT INTO customfieldvalues (customfield, name, sortorder, creator, lastupdatedby) VALUES (1,'Trivial',3,12,12);
151 EOF
152         # give permissions to users to set this field.
153         cat <<EOF | /usr/bin/psql -U postgres -d $RT3_DB_NAME
154 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'ModifyCustomField','RT::CustomField',1,0,0);
155 INSERT INTO acl (principaltype,principalid,rightname,objecttype,objectid,delegatedby,delegatedfrom) VALUES ('Group',3,'SeeCustomField','RT::CustomField',1,0,0);
156 EOF
157         fi
158
159 }
160
161 if [ "$PLC_RT_ENABLED" != "1" ] ; then
162     exit 0
163 fi
164
165 case "$1" in
166         start)
167                 MESSAGE=$"Bootstrap RT (please wait...)"
168                 dialog "$MESSAGE"
169
170                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
171                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
172                 check_rt_siteconfig
173                 check_rt_pghba
174                 if [ -n "$WROTE_PG_CONFIG" ] ; then
175                         # NOTE: restart db to enable access by users granted above.
176                         service plc restart postgresql
177                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
178                         dialog "$MESSAGE"
179                 fi
180                 check_rt_aliases
181                 check_rt_init
182
183                 result "$MESSAGE"
184         ;;
185
186
187         delete)
188                 MESSAGE=$"Deleting databases..."
189                 dialog "$MESSAGE"
190
191                 service plc stop httpd
192
193                 dropdb -U postgres $RT3_DB_NAME
194                 dropuser -U postgres $RT3_DB_USER
195                 rm -f /etc/rt3/RT_SiteConfig.pm
196                 rm -f /etc/rt3/initialdata
197                 PATTERN="host all postgres 127.0.0.1/32 trust"
198                 sed -i -e "s|$PATTERN||g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
199
200                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
201                 rm -f /etc/rt3/setup.finished
202
203                 result "$MESSAGE"
204         ;;
205
206         stop)
207                 MESSAGE=$"Stopping RT"
208                 dialog "$MESSAGE"
209
210                 # TODO: is there anything to stop?
211
212                 result "$MESSAGE"
213         ;;
214 esac
215
216 exit $ERRORS