#!/bin/bash

LOG_MGR=$1

# this is for updating /etc/rsyslog.conf (for rhel6)
#ps -ef | grep -v grep | grep -qi "/sbin/rsyslogd"
#if [ $? -eq 0 ];then
#if [ -e /etc/rsyslog.conf ];then
#echo $LOG_MGR
#sed -i -e "/x\.x\.x\.x/ s/# kern.warning/kern.warning/" -i -e "s/x.x.x.x/$LOG_MGR/" /etc/rsyslog.conf
#/etc/init.d/rsyslog restart
#fi
#fi

LOG_CONFIG=/etc/rsyslog.conf

ps -ef | grep -v grep | grep -qi "/sbin/rsyslogd"
        if [ $? -eq 0 ];then
        echo $?
                if [ -e $LOG_CONFIG ];then
                        #grep -q "authpriv.*                                              /var/log/messages" $LOG_CONFIG
                        grep -q "authpriv.*" $LOG_CONFIG | grep -q "/var/log/messages"
                                if [ $? -eq 1 ];then
                                echo $?
                                #echo "authpriv.*                                              /var/log/messages" >> $LOG_CONFIG
				echo -e "authpriv.*\t\t\t\t/var/log/messages" >> $LOG_CONFIG
                                fi
                fi
        grep -q "# Datapipe Event Management"  $LOG_CONFIG
                if [ $? -eq 1 ];then
                        echo $?
                        #echo "# Datapipe Event Management" >> $LOG_CONFIG
                        #echo "$template datapipe,"%timegenerated%,%HOSTNAME%,%syslogtag%,%msg%\n"" >> $LOG_CONFIG
                        #echo "# kern.warning;*.err;authpriv.none;*.info;mail.none;cron.none;*.emerg;local7.*            @@x.x.x.x;datapipe"  >> $LOG_CONFIG
			cat << EOF >> $LOG_CONFIG

# Datapipe Event Management
\$template datapipe,"%timegenerated%,%HOSTNAME%,%syslogtag%,%msg%\n"
# kern.warning;*.err;authpriv.none;*.info;mail.none;cron.none;*.emerg;local7.*            @@x.x.x.x;datapipe

EOF
                fi
        sed -i -e "/x\.x\.x\.x/ s/# kern.warning/kern.warning/" -i -e "s/x.x.x.x/$LOG_MGR/" $LOG_CONFIG
        /etc/init.d/rsyslog restart
        fi

# This part is for updating /etc/syslog.conf (for rhel5)

ps -ef | grep -v grep | grep -qi "syslogd -m 0"
if [ $? -eq 0 ];then
if [ -e /etc/syslog.conf ];then



if [ `grep -v '^#' /etc/syslog.conf | grep -v authpriv\.none | grep -c  \
     'authpriv'` -eq 0 ]; then 
      echo "Established the following record in /etc/syslog.conf" 
      echo "authpriv.*\t\t\t\t/var/log/secure" 
      echo -e "authpriv.*\t\t\t\t/var/log/secure" >> /etc/syslog.conf 
else 
      echo "syslog OK.  Didn't have to change syslog.conf for authpriv; the" 
echo "following record is good:" 
      grep "^authpriv" /etc/syslog.conf | grep '/var/log/secure' 
fi 
# Add record for 'auth.*', too, placing it after the authpriv record 
if [ `grep -v '^#' /etc/syslog.conf | grep -c 'auth.\*'` -eq 0 ]; then 
     ed /etc/syslog.conf  <<END_SCRIPT 
1 
/^authpriv 
a 
auth.*       /var/log/messages 
. 
w 
q 
END_SCRIPT
else 
echo "syslog OK.  Didn't have to change syslog.conf for auth.*; the" 
echo "following record is good:" 
      grep 'auth.\*' /etc/syslog.conf 
fi 
chown root:root /etc/syslog.conf 
# Original/default permissions are 0644. 
chmod 0600      /etc/syslog.conf 

cat << EOF >> /etc/syslog.conf

# Datapipe Event Management
# kern.warning;*.err;authpriv.none;*.info;mail.none;cron.none;*.emerg;local7.*            @x.x.x.x

EOF

sed -i -e "/\@x\.x\.x\.x/ s/# kern.warning/kern.warning/" -i -e "s/x.x.x.x/$LOG_MGR/" /etc/syslog.conf
/etc/init.d/syslog restart
fi
fi
