#!/bin/bash

operLogDir="/usr/local/apache/htdocs/db_backup/operLog"
alarmLogDir="/usr/local/apache/htdocs/db_backup/alarmLog"

operLogFile=$operLogDir/"operation.log__"`hostname`"__"`date -d 'yesterday' +%b.%d`"_04.02__"`date +%b.%d`"_04.02"
alarmLogFile=$alarmLogDir/"alarm.log__"`hostname`"__"`date -d 'yesterday' +%b.%d`"_04.02__"`date +%b.%d`"_04.02"

rm -rf $operLogFile
rm -rf $alarmLogFile

mysql_user=`/usr/local/mssBak/script/encryption 1 "627273706e69687c727979"`
mysql_pw=`/usr/local/mssBak/script/encryption 1 "6f716c7866697074795a4c2f434639"`


# Create log file
/usr/bin/mysql -u$mysql_user -p$mysql_pw <<_EOF_
use OMC_PUB;
SELECT sysConf.sysTypeName,sysAlarmLog.sysNo,sysAlarmLog.subSysNo,sysAlarmLevelDesc.desc_2,sysAlarmConf.alarmDesc_2,sysAlarmLog.alarmTime,sysAlarmLog.clearTime,sysAlarmLog.clearBy INTO OUTFILE "$alarmLogFile" FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM (((sysAlarmLog INNER JOIN sysConf ON sysConf.sysTypeNo=sysAlarmLog.sysTypeNo) INNER JOIN sysAlarmConf ON sysAlarmLog.compCode=sysAlarmConf.compCode AND sysAlarmLog.alarmCode=sysAlarmConf.alarmCode AND sysAlarmLog.sysTypeNo=sysAlarmConf.sysTypeNo) INNER JOIN sysAlarmLevelDesc ON sysAlarmConf.alarmLevel=sysAlarmLevelDesc.level);

SELECT * INTO OUTFILE "$operLogFile" FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM sysOperLog;
_EOF_


# bzip file
if [ -f $operLogFile ] ; then
	cd $operLogDir
	cp -f $operLogFile $operLogFile"_bak"
	bzip2 -z $operLogFile
	mv -f $operLogFile"_bak" $operLogFile
	chmod 644 $operLogFile
	chown mysql $operLogFile
	chgrp mysql $operLogFile
fi

if [ -f $alarmLogFile ] ; then
        cd $alarmLogDir
        cp -f $alarmLogFile $alarmLogFile"_bak"
        bzip2 -z $alarmLogFile
        mv -f $alarmLogFile"_bak" $alarmLogFile
        chmod 644 $alarmLogFile
        chown mysql $alarmLogFile
        chgrp mysql $alarmLogFile
fi




# Send log files
if [ ! -f /etc/syslog_server_config ] ; then
  echo "Syslog server not configured. Please run script 'config_syslog_server'"
  exit
fi

ip_addr=`cat /etc/syslog_server_config|grep "ip_addr"|awk -F":" '{print $2}'`
ping_reply=`ping -c 2 $ip_addr|grep "time="`
if [ -z "$ping_reply" ] ; then
  echo "Syslog server $ip_addr is not reachable!"
  exit
fi

file_list=`ls $operLogDir/*.bz2`
if [ ! -z "$file_list" ] ; then
  user_name=`cat /etc/syslog_server_config|grep "user_name"|awk -F":" '{print $2}'`
  password=`cat /etc/syslog_server_config|grep "password"|awk -F":" '{print $2}'`
#  local_directory=`cat /etc/syslog_server_config|grep "local_directory"|awk -F":" '{print $2}'`
#  log_type=`cat /etc/syslog_server_config|grep "log_type"|awk -F":" '{print $2}'`
  for file in $file_list; do
    if [ ! -z "`/etc/sftp_auditlog_files $ip_addr $user_name $password $operLogDir app $file|grep "100%"`" ] ; then
      rm -f $file
    fi
 done
fi


file_list=`ls $alarmLogDir/*.bz2`
if [ ! -z "$file_list" ] ; then
  user_name=`cat /etc/syslog_server_config|grep "user_name"|awk -F":" '{print $2}'`
  password=`cat /etc/syslog_server_config|grep "password"|awk -F":" '{print $2}'`
#  local_directory=`cat /etc/syslog_server_config|grep "local_directory"|awk -F":" '{print $2}'`
#  log_type=`cat /etc/syslog_server_config|grep "log_type"|awk -F":" '{print $2}'`
  for file in $file_list; do
    if [ ! -z "`/etc/sftp_auditlog_files $ip_addr $user_name $password $alarmLogDir app $file|grep "100%"`" ] ; then
      rm -f $file
    fi
 done
fi



exit 0
