remove rt from monitor package.
[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_RT_IP" ] ; then
40         PLC_RT_IP=$( gethostbyname $PLC_RT_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         # TODO: need a better approach for this.
59         for f in $MONITORPATH/rt3/rtconf.d/*.pl ; do 
60                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $f
61         done
62         
63         # if the templates are newer than the actual config, then replace them.
64         if [ $MONITORPATH/rt3/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
65         then
66                 # copy templates
67                 cp -f $MONITORPATH/rt3/RT_SiteConfig.pm $tmp_siteconfig
68                 cp -f $MONITORPATH/rt3/initialdata $tmp_initialdata
69
70                 # setup RT_SiteConfig.pm
71                 update_config PLC_NAME "$PLC_NAME" $tmp_siteconfig
72                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_siteconfig
73                 update_config PLC_WWW_HOSTNAME $PLC_WWW_HOST $tmp_siteconfig
74
75                 update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
76                 update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
77                 update_config RT_DB_PASSWORD $PLC_MONITOR_DBPASSWORD $tmp_siteconfig
78
79                 # setup initialdata
80                 update_config PLC_RT_HOSTNAME $PLC_RT_HOST $tmp_initialdata
81
82                 # copy to live configuration
83                 cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
84                 cp -f $tmp_initialdata /etc/rt3/initialdata 
85                 chmod 644 /etc/rt3/RT_SiteConfig.pm 
86                 chmod 644 /etc/rt3/initialdata 
87
88                 rm -f $tmp_siteconfig
89                 rm -f $tmp_initialdata
90         fi
91 }
92
93 function check_rt_custom ()
94 {
95         rsync -qv -az $MONITORPATH/rt3/local/html /usr/share/rt3
96 }
97
98 function check_rt_pghba ()
99 {
100         NAME=$RT3_DB_NAME
101         USER=$RT3_DB_USER
102         CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
103         PATTERN="host all postgres 127.0.0.1/32 trust"
104
105         if ! grep -q "$PATTERN" $CONF ; then
106                 #### SETUP ACCESS from postgres user to run init for the first time.
107                 echo "$PATTERN" >> $CONF
108                 WROTE_PG_CONFIG="true"
109         fi
110
111 }
112
113 function check_rt_aliases ()
114 {
115
116         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
117         then 
118                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
119                 sed -i -e "s/^security.*root//g" /etc/aliases
120         cat <<EOF >> /etc/aliases
121 # added by RT init scripts for default queues.
122 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
123 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
124 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
125 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
126 EOF
127                 /usr/bin/newaliases
128         fi
129
130 }
131
132 function check_rt_init ()
133 {
134         if [ ! -f /etc/rt3/setup.finished ] ; then
135                 /usr/sbin/rt-setup-database --action init --dba postgres
136                 for f in $MONITORPATH/rt3/rtconf.d/*.pl ; do 
137                         /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
138                 done
139
140                 # run initial setup scripts (run only once, or for the first time)
141                 for f in $MONITORPATH/rt3/rtsetup.d/*.{pl,py,sh} ; do 
142                         $f
143                 done
144
145                 touch /etc/rt3/setup.finished
146
147         fi
148 }
149
150 check_rt_sendmail ()
151 {
152         tmp_sendmailmc=$(mktemp)
153         
154         # if the templates is newer than the processed config, then update it
155         if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc  ; 
156         then
157                 # copy templates
158                 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
159
160                 # setup initialdata
161                 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
162
163                 # copy to live configuration
164                 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
165                 rm -f $tmp_sendmailmc
166
167                 # edit /etc/mail/access to add local IP
168                 if ! grep "$PLC_RT_IP" /etc/mail/access ; then
169                         echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
170                         makemap hash /etc/mail/access.db < /etc/mail/access
171                 fi
172                 if [ !  -f /etc/smrsh/rt-mailgate ] ; then
173                         ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
174                 fi
175                 if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
176                         # edit /etc/mail/local-host-names
177                         echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
178                 fi
179                 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
180                 service sendmail restart
181         fi
182
183
184 }
185
186 if [ "$PLC_RT_ENABLED" != "1" ] ; then
187     exit 0
188 fi
189
190 case "$1" in
191         start)
192                 MESSAGE=$"Bootstrap RT (please wait...)"
193                 dialog "$MESSAGE"
194
195                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
196                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
197                 check_rt_siteconfig
198                 check_rt_pghba
199                 if [ -n "$WROTE_PG_CONFIG" ] ; then
200                         # NOTE: restart db to enable access by users granted above.
201                         service plc restart postgresql
202                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
203                         dialog "$MESSAGE"
204                 fi
205                 check_rt_aliases
206                 check_rt_init
207                 check_rt_sendmail
208                 check_rt_custom         # todo: restart httpd if needed.
209
210                 result "$MESSAGE"
211         ;;
212
213
214         delete)
215                 MESSAGE=$"Deleting databases..."
216                 dialog "$MESSAGE"
217
218                 service plc stop httpd
219
220                 dropdb -U postgres $RT3_DB_NAME
221                 dropuser -U postgres $RT3_DB_USER
222                 rm -f /etc/rt3/RT_SiteConfig.pm
223                 rm -f /etc/rt3/initialdata
224                 PATTERN="host all postgres 127.0.0.1/32 trust"
225                 sed -i -e "s|$PATTERN||g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
226
227                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
228                 rm -f /etc/rt3/setup.finished
229
230                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
231                 service plc start httpd
232
233                 result "$MESSAGE"
234         ;;
235
236         stop)
237                 MESSAGE=$"Stopping RT"
238                 dialog "$MESSAGE"
239
240                 # TODO: is there anything to stop?
241
242                 result "$MESSAGE"
243         ;;
244 esac
245
246 exit $ERRORS