#!/bin/bash
#########################################################
# #
# Collect Server Messages File V2.10 #
# #
# Range: 1 to 3 Days #
# Location: /var/log/messages #
# by Hata 2010/10/04 #
#########################################################
### Check c_hosts File ######################################################################################
if [ ! -f c_hosts ]; then
clear
printf "\n\n\n\t\t\tc_hosts is no exist !\n\n\n "
exit 1
fi
##### Define Environment #####################################################################################
work_area="/nishome/hata/check_messages/"
hostname=`hostname`
grep_date=`date --date=-1days|awk '{print $2,$3}'`
grep_date_2=`date --date=-1days|awk '{print $2"-"$3}'`
grep_1days=`date --date=-1days|awk '{print $3}'`
grep_2days=`date --date=-2days|awk '{print $3}'`
grep_3days=`date --date=-3days|awk '{print $3}'`
grep_1days_month=`date --date=-1days|awk '{print $2}'`
grep_2days_month=`date --date=-2days|awk '{print $2}'`
grep_3days_month=`date --date=-3days|awk '{print $2}'`
#printf " $grep_1days_month $grep_2days_month $grep_3days_month $grep_1days $grep_2days $grep_3days\n"
act_hosts=`cat c_hosts |grep -v "#" |grep linux | awk '{print $1}'`
act_aix_hosts=`cat c_hosts |grep -v "#" |grep aix |awk '{print $1}'`
date8=`date +%Y%m%d`
clear
##### If last_day Directory is exist, Stop this program.
if [ -d last_day ]; then
rm -rf last_day
fi
##### If GZip file is exist, Stop this program.
#if [ -f $grep_date_2.tar.gz ]; then
# clear
# printf "\n\n\t $File \e[1;33;5m $grep_date_2.tar.gz \e[0m Already Exist.\n\n\n"
# exit 1
#fi
mkdir last_day
cd last_day
### Function of Collect Message File #########################################################################
function collect_message(){
if [ $grep_2days_month != $grep_1days_month ]; then
for i in $act_hosts
do
grep_date1="$grep_1days_month $grep_1days"
grep_date2="$grep_2days_month $grep_2days"
ssh $i cat /var/log/messages |grep "$grep_date1" |grep -v sysusage $$ ssh $i cat /var/log/messages |grep "$grep_date2" |grep -v sysusage >> $i.mesg
if [ -s $i.mesg ]; then
printf "Host : $i is Done.\n " >> $work_area/collect.log 2>&1
else
printf "Host : $i have no data.\n " >> $work_area/collect.log 2>&1
fi
done
else
for i in $act_hosts
do
grep_date1="$grep_1days_month [$grep_3days-$grep_1days]"
printf " $i $grep_date1 $grep_date2\n"
ssh $i cat /var/log/messages |grep "$grep_date1" |grep -v sysusage >> $i.mesg
if [ -s $i.mesg ]; then
printf "Host : $i is Done.\n " >> $work_area/collect.log 2>&1
else
printf "Host : $i have no data.\n " >> $work_area/collect.log 2>&1
fi
done
fi
if [ $grep_3days_month != $grep_1days_month ]; then
grep_date1="$grep_1days_month $grep_1days"
elif [ $grep_2days_month != $grep_1days_month ]; then
grep_date1="$grep_1days_month $grep_1days"
grep_date2="$grep_2days_month [$grep_3days-$grep_2days]"
ssh $i cat /var/log/messages |grep "$grep_date1" |grep -v sysusage $$ ssh $i cat /var/log/messages |grep "$grep_date2" |grep -v sysusage >> $i.mesg
# ssh $i cat /var/log/messages |grep "$grep_date" |grep -v sysusage >> $i.mesg
if [ -s $i.mesg ]; then
printf "Host : $i is Done.\n " >> $work_area/collect.log 2>&1
else
printf "Host : $i have no data.\n " >> $work_area/collect.log 2>&1
fi
else
grep_date1="$grep_1days_month [$grep_2days-$grep_1days]"
grep_date2="$grep_2days_month $grep_3days"
ssh $i cat /var/log/messages |grep "$grep_date1" |grep -v sysusage $$ ssh $i cat /var/log/messages |grep "$grep_date2" |grep -v sysusage >> $i.mesg
if [ -s $i.mesg ]; then
printf "Host : $i is Done.\n " >> $work_area/collect.log 2>&1
else
printf "Host : $i have no data.\n " >> $work_area/collect.log 2>&1
fi
fi
}
### Function of Packing Message File to TAR.GZ File ##########################################################
function packing_data(){
cd ..
tar zcvf $grep_date_2.tar.gz last_day > /dev/null
rm -rf $work_area/last_day
chown hata $grep_date_2.tar.gz
clear
printf "\n\n\t Job is Done.\n\n"
filesize=`ls -la |grep $grep_date_2 |awk '{print$5}'`
printf " File $grep_date_2.tar.gz size is $filesize\n\n."
}
### Function of AIX
function aix_collect(){
for i in $act_aix_hosts
do ssh $i errpt -a > AIX.$i.mesg
printf "Host : $i is Done.\n " >> $work_area/collect.log 1>&2
done
}
### Function for Check /usr/local/sysusage/etc/sysusage.cfg
function chk_sys(){
for i in $act_hosts
do ssh $i test -f /usr/local/sysusage/etc/sysusage.cfg
done
}
### Main Function ##############################################################################################
printf " \n\n\n\n\n"
printf "\t\t#############################################################################\n"
printf "\t\t# #\n"
printf "\t\t# Please Input How Many Days You Want Collect.( 1 to 3 days) #\n"
printf "\t\t# #\n"
printf "\t\t#############################################################################\n"
printf " \n"
printf "\t\t aix_collect : Renew AIXINFO\n"
printf "\t\t sys : Check Mail TO On sysusage.cfg\n\n\n"
read -p " Input Your Choice : " choice
case $choice in
"1")
printf "\n\n\n\t\t\t Collect Date $grep_1days Data\n"
grep_date=`date +%b" "%d --date=-1days`
collect_message 1
packing_data
exit 0
;;
"2")
printf "\n\n\n\t\t\t Collect Date $grep_2days_month-$grep_2days to $grep_1days_month-$grep_1days Data\n"
# grep_date="Sep [$grep_2days-$grep_1days]"
grep_date_2="$grep_2days_month-$grep_2days-$grep_1days_month-$grep_1days"
collect_message 2
packing_data
exit 0
;;
"3")
printf "\n\n\n\t\t\t Collect Date $grep_3days_month-$grep_3days to $grep_1days_month-$grep_1days Data\n"
# grep_date="Sep [$grep_3days-$grep_1days]"
grep_date_2="$grep_3days_month-$grep_3days-$grep_1days_month-$grep_1days"
collect_message 3
packing_data
exit 0
;;
"q")
clear
exit 0
;;
"*")
echo "Usage $0 { 1|2|3 }"
;;
esac
#printf "\n\n\n\t\t Collect $grep_date Message Data .......\n\n"
No comments:
Post a Comment