#!/bin/sh

localhost=`hostname -s`
[ `uname -s` = Linux ] && LINUX=1

cd /usr/local/www/logs

rm -f *.zzz
rm -f wcmlog.*.gz 
if [ -n "$LINUX" ] ; then
	find /usr/local/www/logs -type f -name '*-analyze.*' -ctime +8 -delete
else
	find /usr/local/www/logs -type f -name '*-analyze.*' -ctime +8d -delete
fi


## IF mmeta | int |mmb
if [ "X`expr $localhost : '.*\(mmeta\)[0-9]\{1,2\}-[0-9]\{2\}'`" = "Xmmeta" -o "X`expr $localhost : '\(int\)[0-9]\{1,2\}-[0-9]\{2\}'`" = "Xint" -o "X`expr $localhost : '\(mmb\)[0-9]\{2\}'`" = "Xmmb" ]; then
    LA=`echo /db/bsconfig/configactive/*/*:8030/log-analyze | awk '{print $1}'`
    for i in `ls wcmlog*.PREV 2>&1`; do
	[ -f $i ]||continue
	if [ -n "$LINUX" ] ; then
		eval `stat --format="st_mtime=%Y" $i`
	else
		eval `stat -s $i`
	fi
	 [ $((`date +%s`-3600)) -gt $st_mtime ]&&continue
	fl=`basename $i ".PREV"`
	if [ -s ${fl}.PREV -a ! -f /usr/local/www/logs/LOG-ANALAZE.STOP ]; then
#	    nice gzip -9 -c ${fl}.PREV > ${fl}.${logdate}.gz && touch ${fl}.${logdate}.zzz
	    [ -n "$LA" -a -x "$LA" ] && nice -19 ${LA} -o ${fl}-analyze.${logdate_olap} ${fl}.PREV >/dev/null 2>&1
    	fi
    done
    exit

fi

seed=`ifconfig | awk '$1=="inet" && $2!="127.0.0.1" {print $2; exit}' | awk -F. '{print $4}'`
WT=$(($seed*29%5))
RT=$(($seed*53%30))
date8d=`date -v -8d +%Y%m%d`
logdate=`date -v -1d +%Y%m%d`
logdate_olap=`date +%Y%m%d-%H`
date4d=`date -v -4d +%Y%m%d`
date2d=`date -v -2d +%Y%m%d`
date1m=`date -v -33d +%Y%m%d`
host="dobro.yandex.ru"
suffix=".yandex.ru"
user="tejblum"
basedir="/big/logs"
STATUS="$basedir/LOGSTATUS/$localhost"
STATUS_RSYNC="LOGSTATUS/$localhost"
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"

files1="access_log blockstat_log reqs_log xmlsearch-p_log xmlreqs redir_log reqans_log imgreqans_log xmlreqans_log"
files1_2dn="error_log reqs_time encyc_log"
#files2="ushow_log"
files2=""
files3="reqwizard_log xsscatch_log profile_log"
files4="wcmlog.Q wcmlog wcmlog.U"
files5="rredir_log captcha_log clicksave_log unanswer_log"
files6="imagesform_log crc_log"


#     DEV_META
if [ "X`/Berkanavt/bin/scripts/yr +DEV_META PRINTSERVERLIST|grep -qws $localhost && { echo ok; }`" = "Xok" ]; then
    exit 0
fi


##  
if [ "$1" ];then
    COMPRESS_FILES=$1
else
    COMPRESS_FILES="$files1 $files2 $files6 $files1_2dn"
fi
for i in $COMPRESS_FILES; do
    if [ -f ${i}.PREV ]; then
	gzip -9 -c ${i}.PREV > ${i}.${logdate}.gz && touch ${i}.${logdate}.zzz
    fi
done


##
if [ "$1" ];then
    FILES=$1
else
    FILES="$files1 $files2"
fi
if [ "x$localhost" = "x`basename $host $suffix`" ]; then
	for i in $FILES $files3; do
	    if [ -f ${i}.${logdate}.gz ]; then
		CKSUM=`cksum ${i}.${logdate}.gz`
		mv ${i}.${logdate}.gz ${i}.${localhost}.${logdate}.gz && { echo ${CKSUM} > ${STATUS}.${i}.dst; rm -f ${i}.${logdate}.zzz; }
	    fi
	done
	exit
fi

thost=dobro
tnode=logs
for i in $FILES; do
	    logdate_del=${logdate}
	if [ -f ${i}.${logdate}.gz -a -f ${i}.${logdate}.zzz ]; then
	     CKSUM=`cksum ${i}.${logdate}.gz`
#	     rcp ${i}.${logdate}.gz ${user}@${host}:${basedir}/${i}.${localhost}.${logdate}.gz && { rsh -l ${user} $host "echo ${CKSUM} > ${STATUS}.${i}.dst"&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz; }
#	    rsync ${rsyncoptions} ${i}.${logdate}.gz ${thost}::${tnode}/${i}.${localhost}.${logdate}.gz && { rsh -l ${user} $host "echo ${CKSUM} > ${STATUS}.${i}.dst"&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz; }
	    rsync ${rsyncoptions} ${i}.${logdate}.gz ${thost}::${tnode}/${i}.${localhost}.${logdate}.gz && { sleep $WT;echo ${CKSUM} > ${i}.dst;rsync ${rsyncoptions} ${i}.dst ${thost}::${tnode}/${STATUS_RSYNC}.${i}.dst&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz ${i}.dst; }
	else
	     rm -f ${i}.${logdate}.gz
	fi
	sleep $RT
done
[ $1 ]&&exit
for i in $files1_2dn; do
	    logdate_del=${logdate}
	if [ -f ${i}.${logdate}.gz -a -f ${i}.${logdate}.zzz ]; then
	     CKSUM=`cksum ${i}.${logdate}.gz`
#	     rcp ${i}.${logdate}.gz ${user}@${host}:${basedir}/${i}.${localhost}.${logdate}.gz && { rsh -l ${user} $host "echo ${CKSUM} > ${STATUS}.${i}.dst"&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz; }
#	    rsync ${rsyncoptions} ${i}.${logdate}.gz ${thost}::${tnode}/${i}.${localhost}.${logdate}.gz && { rsh -l ${user} $host "echo ${CKSUM} > ${STATUS}.${i}.dst"&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz; }
	    rsync ${rsyncoptions} ${i}.${logdate}.gz ${thost}::${tnode}/${i}.${localhost}.${logdate}.gz && { sleep $WT;echo ${CKSUM} > ${i}.dst;rsync ${rsyncoptions} ${i}.dst ${thost}::${tnode}/${STATUS_RSYNC}.${i}.dst&&rm -f ${i}.${logdate_del}.gz; rm -f ${i}.${logdate}.zzz ${i}.dst; }
	else
	     rm -f ${i}.${logdate}.gz
	fi
	sleep $RT
done
#if [ -f blockdisplay.PREV ];then
#	nice gzip -c -9 blockdisplay.PREV > blockdisplay${logdate}.gz
#	rcp blockdisplay${logdate}.gz ${user}@${host}:${basedir}/blockdisplay.${localhost}.${logdate}.gz && rsh -l ${user} $host "echo ok >${STATUS}.blockdisplay.dst"
#fi
##blockdisplay store
thost=ysbackup7
tnode=block_logs
logname=blockdisplay
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"
if [ -f ${logname}.PREV ];then
    nice gzip -9 -c ${logname}.PREV >${logname}.${logdate}.gz
    sleep $RT
    rsync ${rsyncoptions} ${logname}.${logdate}.gz ${thost}::${tnode}/${logname}/${logname}.${localhost}.${logdate}.gz && { echo ok > ${logname}.dst; rsync ${rsyncoptions} ${logname}.dst ${thost}::${tnode}/LOGSTATUS/${localhost}.${logname}.dst; rm ${logname}.dst; } 
fi


for i in $files4 $files3 $files5; do
    if [ -s ${i}.PREV ]; then
	nice gzip -9 -c ${i}.PREV > ${i}.${logdate}.gz && touch ${i}.${logdate}.zzz
    fi
done

##antirobot_threat_check.log store
thost=ysbackup5
tnode=arclogs
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"
if [ -f antirobot_threat_check.log.PREV ]; then
    nice gzip -9 -c antirobot_threat_check.log.PREV >antirobot_threat_check.log.${logdate}.gz
    sleep $RT
    rsync ${rsyncoptions} antirobot_threat_check.log.${logdate}.gz ${thost}::${tnode}/antirobot/antirobot_threat_check.${localhost}.${logdate}.gz||rsync ${rsyncoptions} antirobot_threat_check.log.${logdate}.gz ${thost}::${tnode}/antirobot/antirobot_threat_check.${localhost}.${logdate}.gz
    rm -f antirobot_threat_check.log.${logdate}.gz
#
#added to cron on ysbackup5
#    rsh $thost "find /opt/backup/yandex/arclogs/antirobot -name \"antirobot_threat_check.${localhost}.*\" -type f  -ctime +33d -delete"
#
fi
###wizplace_log store
thost=ysbackup6
tnode=arclogs
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"
if [ -f wizplace_log.PREV ];then
    nice gzip -9 -c wizplace_log.PREV >wizplace_log.${logdate}.gz
    sleep $RT
    rsync ${rsyncoptions} wizplace_log.${logdate}.gz ${thost}::${tnode}/wizplace_log/wizplace_log.${localhost}.${logdate}.gz
    rm -f wizplace_log.${logdate}.gz
#
# added to crontab on ysbackup6
#    rsh $thost "find /opt/backup/yandex/arclogs/wizplace_log -name \"wizplace_log.${localhost}.*\" -type f  -ctime +33d -delete"
#
fi
###coldwater_log store
thost=ysbackup3
tnode=arclogs
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"
if [ -f coldwater_log.PREV ];then
    nice gzip -9 -c coldwater_log.PREV >coldwater_log.${logdate}.gz
    sleep $RT
    rsync ${rsyncoptions} coldwater_log.${logdate}.gz ${thost}::${tnode}/coldwater_log/coldwater_log.${localhost}.${logdate}.gz
    rm -f coldwater_log.${logdate}.gz
fi
##picsearch_log store
thost=ysbackup3
tnode=arclogs
logname=picsearch_log
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=15000 --force --delete"
if [ -f ${logname}.PREV ];then
    nice gzip -9 -c ${logname}.PREV >${logname}.${logdate}.gz
    sleep $RT
    rsync ${rsyncoptions} ${logname}.${logdate}.gz ${thost}::${tnode}/${logname}/${logname}.${localhost}.${logdate}.gz
    rm -f ${logname}.${logdate}.gz
    rsh $thost "find /opt/backup/yandex/arclogs/${logname} -name \"${logname}.${localhost}.*\" -type f  -ctime +367d -delete"
fi

    for i in eventlog.U; do
	fl=`basename $i ".PREV"`
	if [ -s ${fl}.PREV ]; then
	    /Berkanavt/bin/log-analyze -o ${fl}-analyze.${logdate} ${fl}.PREV >/dev/null 2>&1
    	fi
    done

###crc_log store
thost=ordure
tnode=crc_log
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=5000 --force --delete"
if [ -f crc_log.${logdate}.gz ];then
    sleep $RT
    rsync ${rsyncoptions} crc_log.${logdate}.gz ${thost}::${tnode}/crc_log.${localhost}.${logdate}.gz > /dev/null 2>&1
    rm -f crc_log.${logdate}.zzz
fi
##captcha_log rredir_log store
thost=ysbackup5
tnode=arclogs
LOGS="captcha_log rredir_log"
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=5000 --force --delete"
for logname in $LOGS;do 
    if [ ! -f ${logname}.${logdate}.gz ];then
	if [ -f ${logname}.PREV ];then
	    nice gzip -9 -c ${logname}.PREV >${logname}.${logdate}.gz
	fi
    fi
    if [ -f ${logname}.${logdate}.gz ];then
	rsync ${rsyncoptions} ${logname}.${logdate}.gz ${thost}::${tnode}/${logname}/${logname}.${localhost}.${logdate}.gz
    fi
done

##story_log store
thost=ysbackup6
tnode=arclogs
LOGS="story_log"
rsyncoptions="-qrLpg --inplace --timeout=60 --bwlimit=5000 --force --delete"
for logname in $LOGS;do 
    if [ ! -f ${logname}.${logdate}.gz ];then
	if [ -s ${logname}.PREV ];then
	    nice gzip -9 -c ${logname}.PREV >${logname}.${logdate}.gz
	fi
    fi
    if [ -f ${logname}.${logdate}.gz ];then
	rsync ${rsyncoptions} ${logname}.${logdate}.gz ${thost}::${tnode}/${logname}/${logname}.${localhost}.${logdate}.gz
    fi
done

[ -f reqwizard_log.${date8d}.gz ] && rm -f reqwizard_log.${date8d}.gz 
[ -f clicksave_log.${date8d}.gz ] && rm -f clicksave_log.${date8d}.gz 
[ -f unanswer_log.${date8d}.gz ] && rm -f unanswer_log.${date8d}.gz 
[ -f xsscatch_log.${date4d}.gz ] && rm -f xsscatch_log.${date4d}.gz
[ -f profile_log.${date2d}.gz ] && rm -f profile_log.${date2d}.gz
[ -f rredir_log.${date1m}.gz ] && rm -f rredir_log.${date1m}.gz
[ -f captcha_log.${date1m}.gz ] && rm -f captcha_log.${date1m}.gz
#wcmlog.PREV, wcmlog.U.PREV umerli na verhnem.
[ -f wcmlog.PREV ] && cat /dev/null > wcmlog.PREV
[ -f wcmlog.U.PREV ] && cat /dev/null > wcmlog.U.PREV
find /hol/www/logs -type f -name "dataimport*" -ctime +2 -delete
find /hol/www/logs -type f  -name "blockdisplay*" -ctime +1 -delete
find /hol/www/logs -type f  -name "crc_log" -ctime +8d -delete
find /hol/www/logs -type f -name "*.gz" -ctime +30 -delete
