#!/bin/sh ## 统计每个月每个网站的:总注册用户数/当月新注册用户数/登陆用户数/重复登陆用户数 ## 仅统计了网站的流量,数据来源为我们自己的统计程序 total.php ## alfa@condenast 2016-09-07 ## 定义各个目录 LOGPATH=/mnt/nfsroot/log/sdc TMPPATH=/tmp/usertotal ## 默认统计上一个月的 if [ $# = 1 ] ; then MONTH=$1 else MONTH=`date +%Y-%m -d -1month` fi ## mis 中各个字段的对应 domainlist="vogue gq self adstyle cntraveler" MISFIELD[0]='"8":{"month-01":"alluser"},"7":{"month-01":"newuser"},"2":{"month-01":"reloginuser"},"1":{"month-01":"loginuser"}' MISFIELD[1]='"10":{"month-01":"alluser"},"9":{"month-01":"newuser"},"4":{"month-01":"reloginuser"},"3":{"month-01":"loginuser"}' MISFIELD[2]='"12":{"month-01":"alluser"},"11":{"month-01":"newuser"},"6":{"month-01":"reloginuser"},"5":{"month-01":"loginuser"}' MISFIELD[3]='"16":{"month-01":"alluser"},"15":{"month-01":"newuser"},"14":{"month-01":"reloginuser"},"13":{"month-01":"loginuser"}' MISFIELD[4]='"20":{"month-01":"alluser"},"19":{"month-01":"newuser"},"18":{"month-01":"reloginuser"},"17":{"month-01":"loginuser"}' ## 定义统计区间(用于数据库中数据统计) REGDATEBEGIN=`date -d "$MONTH-01 " +%s` REGDATEEND=`date -d "$MONTH-01 1month" +%s` MYSQLBIN="/usr/bin/mysql -h 172.16.1.19 -P 3309 -u passport -p9wQRn8x!mf -D passport -e " ## 建立临时目录 检索出所有登陆日志 mkdir -p $TMPPATH zcat $LOGPATH/*userlog$MONTH* |awk -F "\t" '{if($5>0){print $5 ":" $7 ":" $8}}'|tr '[A-Z]' '[a-z]'|sed -e "s/http:\/\/.*\(vogue\|gq\|self\|adstyle\|cntraveler\)\.com\.cn\/.*/\1/"|awk -F ":" '{print $3"\t"$1"\t"$2}' > $TMPPATH/loginuser$MONTH.txt i=0 ## 按域名开始统计 for domain in $domainlist #vogue gq self adstyle cntraveler do ## 计算登陆时间 ==>> 按用户ID+时间排序 && 删除两次访问低于30分钟的记录 && 统计出ID && 统计ID总数 && 超过2次的全部标记为重复登陆过 grep $domain $TMPPATH/loginuser$MONTH.txt |sort|awk 'BEGIN{userid=0;lasttime=0;domain="";}{if(userid!=$2){print $1 "\t" userid "\t" lasttime;domain=$1;userid=$2;lasttime=$3;}else if($3-lasttime > 300 ){print $1 "\t" userid "\t" lasttime;lasttime=$3}}END{print domain "\t" userid "\t" lasttime;}' |awk '{print $2}' |uniq -c|awk '{if($1 == 1)print 1;else print 2}'|sort > $TMPPATH/$domain-$MONTH.txt ## 计算登陆ID 和 重复登陆ID loginuser=`wc -l $TMPPATH/$domain-$MONTH.txt|awk '{print $1}'` reloginuser=`grep 2 $TMPPATH/$domain-$MONTH.txt|wc -l|awk '{print $1}'` ## 从 passport 数据库中读取当月注册用户数及总注册数 #### 数据库中网站对应翻译 if [ "$domain" == "adstyle" ] ;then passportdomain="ad" elif [ "$domain" == "cntraveler" ] ;then passportdomain="cnt" else passportdomain="$domain" fi newuser=`$MYSQLBIN "select count(*) from passport_members where fromsite=\"$passportdomain\" and regdate >=$REGDATEBEGIN and regdate < $REGDATEEND and is_display = 1" |sed -n 2p` alluser=`$MYSQLBIN "select count(*) from passport_members where fromsite=\"$passportdomain\" and regdate < $REGDATEEND and is_display = 1" |sed -n 2p` ## 将数据提交给Mis echo $domain ":" $loginuser "/" $reloginuser "/" $newuser "/" $alluser data=`echo ${MISFIELD[$i]} |sed -e "s/month/$MONTH/g"|sed -e "s/alluser/$alluser/"|sed -e "s/newuser/$newuser/"|sed -e "s/reloginuser/$reloginuser/"|sed -e "s/loginuser/$loginuser/"|sed -e "s/\"/%22/g"` echo $data wget "http://mis.adstyle.cn/analysis/api.php?user/push&data={$data}" -O /dev/null let i++ done ### 删除临时目录 怕目录变量出错,在这里直接写上目录名称 #rm -rf /tmp/usertotal