break apart init scripts for monitor, zabbix, and rt3
[monitor.git] / rt3 / monitor-rt3.init
diff --git a/rt3/monitor-rt3.init b/rt3/monitor-rt3.init
new file mode 100644 (file)
index 0000000..abd770e
--- /dev/null
@@ -0,0 +1,189 @@
+#!/bin/bash
+#
+# priority: 850
+#
+# Manage settings for the Zabbix installtion and 
+#      other monitor-related things
+#
+# Stephen Soltesz <soltesz@cs.princeton.edu>
+# Copyright (C) 2008 The Trustees of Princeton University
+#
+# $Id$
+#
+
+# Source function library and configuration
+. /etc/plc.d/functions
+. /etc/planetlab/plc_config
+local_config=/etc/planetlab/configs/site.xml
+
+MONITORPATH=/usr/share/monitor
+
+# Be verbose
+set -x
+
+# Default locations
+PGDATA=/var/lib/pgsql/data
+postgresql_conf=$PGDATA/postgresql.conf
+pghba_conf=$PGDATA/pg_hba.conf
+
+# Export so that we do not have to specify -p to psql invocations
+export PGPORT=$PLC_DB_PORT
+
+
+RT3_DB_USER="rt3user"
+RT3_DB_NAME="rt3"
+
+WROTE_PG_CONFIG=
+
+if [ -z "$PLC_MONITOR_IP" ] ; then
+       PLC_MONITOR_IP=$( gethostbyname $PLC_MONITOR_HOST )
+fi
+
+if [ "$PLC_MONITOR_ENABLED" != "1" ] ; then
+    exit 0
+fi
+
+# TODO: make values re-configurable...  this may be an issue with RT's db, though.
+function update_config ()
+{
+       pattern=$1
+       with=$2
+       file=$3
+       sed -i -e "s/$pattern/$with/g" $file
+}
+
+function check_rt_siteconfig ()
+{
+       tmp_siteconfig=$(mktemp)
+       tmp_initialdata=$(mktemp)
+       
+       # if the templates are newer than the actual config, then replace them.
+       if [ $MONITORPATH/rt3/RT_SiteConfig.pm -nt /etc/rt3/RT_SiteConfig.pm ] ;
+       then
+               # copy templates
+               cp -f $MONITORPATH/rt3/RT_SiteConfig.pm $tmp_siteconfig
+               cp -f $MONITORPATH/rt3/initialdata $tmp_initialdata
+
+               # setup RT_SiteConfig.pm
+               update_config PLC_NAME $PLC_NAME $tmp_siteconfig
+               update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_siteconfig
+
+               update_config RT_DB_NAME $RT3_DB_NAME $tmp_siteconfig
+               update_config RT_DB_USER $RT3_DB_USER $tmp_siteconfig
+               update_config RT_DB_PASSWORD $PLC_MONITOR_DBPASSWORD $tmp_siteconfig
+
+               # setup initialdata
+               update_config PLC_RT_HOSTNAME $PLC_RT_HOSTNAME $tmp_initialdata
+
+               # copy to live configuration
+               cp -f $tmp_siteconfig /etc/rt3/RT_SiteConfig.pm 
+               cp -f $tmp_initialdata /etc/rt3/initialdata 
+
+               rm -f $tmp_siteconfig
+               rm -f $tmp_initialdata
+       fi
+}
+
+function check_rt_pghba ()
+{
+       NAME=$RT3_DB_NAME
+       USER=$RT3_DB_USER
+       CONF=$PGDATA/pg_hba.conf.d/${NAME}.conf
+       PATTERN="host all postgres 127.0.0.1/32 trust"
+
+       if ! grep -q $PATTERN $CONF ; then
+               #### SETUP ACCESS from postgres user to run init for the first time.
+               echo $PATTERN >> $CONF
+       fi
+
+}
+
+function check_rt_aliases ()
+{
+
+       if ! grep -q "rt-mailgate --queue support" /etc/aliases ; 
+       then 
+        cat <<EOF >> /etc/aliases
+
+# added by RT init scripts for default queues.
+support: "|/usr/sbin/rt-mailgate --queue support --action correspond --url http://localhost/rt3/"
+monitor: "|/usr/sbin/rt-mailgate --queue monitor --action correspond --url http://localhost/rt3/"
+security: "|/usr/sbin/rt-mailgate --queue security --action correspond --url http://localhost/rt3/"
+legal: "|/usr/sbin/rt-mailgate --queue legal --action correspond --url http://localhost/rt3/"
+EOF
+               /usr/bin/newaliases
+       fi
+
+}
+
+function check_rt_init ()
+{
+       if [ ! -f /etc/rt3/setup.finished ] ; then
+               /usr/sbin/rt-setup-database --action init --dba postgres
+               touch /etc/rt3/setup.finished
+       fi
+
+       ###Last DB adjustments
+       #disable queue General, set disabled to 0 in table queues, set defaultduein to 1 for support queue
+#      /bin/su - -c "/bin/echo \"UPDATE queues SET disabled = 1 WHERE name='General';\"|/usr/bin/psql -U postgres -d rt" postgres
+#      /bin/su - -c "/bin/echo \"UPDATE queues SET defaultduein = 1 WHERE name='support';\"|/usr/bin/psql -U postgres -d rt" postgres
+#
+       #alter acl for Everyone be able to create new tickets in support list
+       #Everyone (installation id 3) on support ( installation is 3)
+#      /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
+#      /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
+#      #Everyone (installation id 3) on monitor ( installation is 2)
+#      /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
+#      /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
+       
+}
+
+case "$1" in
+       start)
+               MESSAGE=$"Bootstrap RT (please wait...)"
+               dialog "$MESSAGE"
+
+               check_pg_hba $RT3_DB_NAME $RT3_DB_USER
+               check_user_and_db $RT3_DB_NAME $RT3_DB_USER
+               check_rt_siteconfig
+               check_rt_pghba
+               if [ -n "$WROTE_PG_CONFIG" ] ; then
+                       # NOTE: restart db to enable access by users granted above.
+                       service plc restart postgresql
+                       MESSAGE=$"Bootstrap RT 2 (please wait...)"
+                       dialog "$MESSAGE"
+               fi
+               check_rt_aliases
+               check_rt_init
+
+               result "$MESSAGE"
+       ;;
+
+
+       delete)
+               MESSAGE=$"Deleting databases..."
+               dialog "$MESSAGE"
+
+               dropdb -U postgres $RT3_DB_NAME
+               dropuser -U postgres $RT3_DB_USER
+               rm -f /etc/rt3/RT_SiteConfig.pm
+               rm -f /etc/rt3/initialdata
+               PATTERN="host all postgres 127.0.0.1/32 trust"
+               sed -i -e "s/$PATTERN//g" $PGDATA/pg_hba.conf.d/${RT3_DB_USER}.conf
+
+               sed -i -e "s/.*mailgate.*//g" /etc/aliases
+
+               result "$MESSAGE"
+       ;;
+
+       stop)
+               MESSAGE=$"Stopping Monitor"
+               dialog "$MESSAGE"
+
+               # TODO: is there anything to stop?
+
+               result "$MESSAGE"
+       ;;
+esac
+
+exit $ERRORS