#!/bin/bash

#Please see the Installing SystemEdge5 wiki page on the Critical Sites sharepoint site.
#email jason.nelson@rackspace.com with questions/suggestions.
#
#simple usage, copy sysedge_5.6_x86.tgz to remote server, user hammertime to execute script:
#ht -s ./sysedge_install.sh --script-args <COMM_STRING> --sudo-make-me-a-sandwich --expect-timeout 300 <CORE SERVER NUMBERS>

comm=$1
export comm
yum -y -q install glibc.i686
cd /home/rack
tar zxf ./sysedge_5.6_x86.tgz 
cd Linux_x86/Agent/SysMan/CA_SystemEDGE_Core/
mount -o remount,exec /tmp
./ca-setup.sh /e /home/rack/sysedge_install.out /t EULA_ACCEPTED=1 CASE_SNMP_PORT=1691 CASE_INSTALL_DOCS=0 > /dev/null 2>&1
mount -o remount,noexec /tmp

dc=`cat /root/.rackspace/datacenter`
case "$dc" in
ord) dc_ip="184.106.13.160";;
ord1) dc_ip="184.106.13.160";;
ORD1) dc_ip="184.106.13.160";;
kir) dc_ip="184.106.13.160";;
dfw) dc_ip="72.3.141.208";;
dfw1) dc_ip="72.3.141.208";;
DFW1) dc_ip="72.3.141.208";;
iad) dc_ip="173.203.179.56";;
iad2) dc_ip="173.203.179.56";;
iad3) dc_ip="173.203.179.56";;
IAD3) dc_ip="173.203.179.56";;
lon) dc_ip="83.138.176.26";;
LON3) dc_ip="83.138.176.26";;
*) echo "Unknown DC, please correct trap_community in sysedge.cf";;
esac
export dc_ip


perl -pi -e '$ip=`curl -s icanhazip.com`; chomp($ip); s/^template trap_source <address>/template trap_source <address>\ntrap_source $ip/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e 's/^template linux_freemem_include <method-list>/template linux_freemem_include <method-list>\nlinux_freemem_include buffers cached/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e '$host=`hostname | cut -d. -f1`; chomp($host); $host.="_SystemEDGE"; s/^template sysdescr <description>/template sysdescr <description>\nsysdescr $host/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e 's/^template syscontact <contact>/template syscontact <contact>\nsyscontact wse\@rackspace.com/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e '$dc=`cat /root/.rackspace/datacenter`; $dc=uc $dc; s/^template syslocation <location>/template syslocation <location>\nsyslocation $dc/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e 's/^community public read-only/community $ENV{comm} read-write/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -pi -e 's/^template trap_community <name> <address> <port> <encoding> <source>/template trap_community <name> <address> <port> <encoding> <source>\ntrap_community public $ENV{dc_ip} 162/g' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -ni -e 'print unless /devTableEntry/ and /C:/;' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
perl -ni -e 'print; print "watch process procAlive syslogd 500003 0x8 60 absolute nop 0 '\''Monitor syslog'\'' '\'''\''\nwatch process procAlive '\''/usr/sbin/sshd'\'' 500005 0x8 60 absolute nop 0 '\''Monitor sshd'\'' '\'''\''\nwatch process procAlive crond 500009 0x8 60 absolute nop 0 '\''Monitor cron status Daemon'\'' '\'''\''\nwatch process procAlive klogd 500012 0x8 60 absolute nop 0 '\''Monitor kernel log Daemon'\'' '\'''\''\n" if $_ =~ /^template watch process /' /opt/CA/SystemEDGE/config/port1691/sysedge.cf
mv /etc/sysedge.cf /etc/sysedge.cf.ORIG && ln -s /opt/CA/SystemEDGE/config/port1691/sysedge.cf /etc/sysedge.cf
service sysedge restart
