totaluser.sh 3.6 KB

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