Repeatedly try to AuthCheck() to avoid transient DNS errors and temporary
[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         # 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                 chmod 644 /etc/rt3/RT_SiteConfig.pm 
80                 chmod 644 /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                 WROTE_PG_CONFIG="true"
98         fi
99
100 }
101
102 function check_rt_aliases ()
103 {
104
105         if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
106         then 
107                 sed -i -e "s/^support.*postmaster//g" /etc/aliases
108                 sed -i -e "s/^security.*root//g" /etc/aliases
109         cat <<EOF >> /etc/aliases
110 # added by RT init scripts for default queues.
111 support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
112 monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
113 security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
114 legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
115 EOF
116                 /usr/bin/newaliases
117         fi
118
119 }
120
121 function check_rt_init ()
122 {
123         if [ ! -f /etc/rt3/setup.finished ] ; then
124                 /usr/sbin/rt-setup-database --action init --dba postgres
125                 for f in $MONITORPATH/rt3/rtconf.d/*.pl ; do 
126                         /usr/sbin/rt-setup-database --action insert --dba postgres --datafile $f
127                 done
128
129                 # run initial setup scripts (run only once, or for the first time)
130                 for f in $MONITORPATH/rt3/rtsetup.d/*.{pl,py,sh} ; do 
131                         $f
132                 done
133
134                 touch /etc/rt3/setup.finished
135
136         fi
137 }
138
139 check_rt_sendmail ()
140 {
141         tmp_sendmailmc=$(mktemp)
142         
143         # if the templates is newer than the processed config, then update it
144         if grep -q "Addr=127.0.0.1," /etc/mail/sendmail.mc  ; 
145         then
146                 # copy templates
147                 cp -f /etc/mail/sendmail.mc $tmp_sendmailmc
148
149                 # setup initialdata
150                 update_config "Addr=127.0.0.1," "" $tmp_sendmailmc
151
152                 # copy to live configuration
153                 cp -f $tmp_sendmailmc /etc/mail/sendmail.mc
154                 rm -f $tmp_sendmailmc
155
156                 # edit /etc/mail/access to add local IP
157                 if ! grep "$PLC_RT_IP" /etc/mail/access ; then
158                         echo "$PLC_RT_IP                RELAY" >> /etc/mail/access
159                         makemap hash /etc/mail/access.db < /etc/mail/access
160                 fi
161                 if [ !  -f /etc/smrsh/rt-mailgate ] ; then
162                         ln -s /usr/sbin/rt-mailgate /etc/smrsh/rt-mailgate
163                 fi
164                 if ! grep "$PLC_RT_HOST" /etc/mail/local-host-names ; then
165                         # edit /etc/mail/local-host-names
166                         echo "$PLC_RT_HOST" >> /etc/mail/local-host-names
167                 fi
168                 m4 /etc/mail/sendmail.mc > /etc/mail/sendmail.cf
169                 service sendmail restart
170         fi
171
172
173 }
174
175 if [ "$PLC_RT_ENABLED" != "1" ] ; then
176     exit 0
177 fi
178
179 case "$1" in
180         start)
181                 MESSAGE=$"Bootstrap RT (please wait...)"
182                 dialog "$MESSAGE"
183
184                 check_pg_hba $RT3_DB_NAME $RT3_DB_USER
185                 #check_user_and_db $RT3_DB_NAME $RT3_DB_USER
186                 check_rt_siteconfig
187                 check_rt_pghba
188                 if [ -n "$WROTE_PG_CONFIG" ] ; then
189                         # NOTE: restart db to enable access by users granted above.
190                         service plc restart postgresql
191                         MESSAGE=$"Bootstrap RT 2 (please wait...)"
192                         dialog "$MESSAGE"
193                 fi
194                 check_rt_aliases
195                 check_rt_init
196                 check_rt_sendmail
197
198                 result "$MESSAGE"
199         ;;
200
201
202         delete)
203                 MESSAGE=$"Deleting databases..."
204                 dialog "$MESSAGE"
205
206                 service plc stop httpd
207
208                 dropdb -U postgres $RT3_DB_NAME
209                 dropuser -U postgres $RT3_DB_USER
210                 rm -f /etc/rt3/RT_SiteConfig.pm
211                 rm -f /etc/rt3/initialdata
212                 PATTERN="host all postgres 127.0.0.1/32 trust"
213                 sed -i -e "s|$PATTERN||g" $PGDATA/pg_hba.conf.d/${RT3_DB_NAME}.conf
214
215                 sed -i -e "s/.*mailgate.*//g" /etc/aliases
216                 rm -f /etc/rt3/setup.finished
217
218                 sed -i -e "s/Port=smtp, Name=MTA/Port=smtp,Addr=127.0.0.1, Name=MTA/g" /etc/mail/sendmail.mc
219                 service plc start httpd
220
221                 result "$MESSAGE"
222         ;;
223
224         stop)
225                 MESSAGE=$"Stopping RT"
226                 dialog "$MESSAGE"
227
228                 # TODO: is there anything to stop?
229
230                 result "$MESSAGE"
231         ;;
232 esac
233
234 exit $ERRORS