nginx用户统计

1 概念

PV:页面访问量,即PageView,用户每次对网站的访问均被记录,用户对同一页面的多次访问,访问量累计。

UV:独立访问用户数:即UniqueVisitor,访问网站的一台电脑客户端为一个访客。00:00-24:00内相同的客户端只被计算一次。

 

2 统计uv测试

'''
create  2018/7/4
version 1.0
auth    jabbok
info    get the uv_number from nginx log and insert into mysql(monitor.uv)
'''

import pymysql
import subprocess
import time

def insert(mdate,num):

    conn = pymysql.connect("127.0.0.1","user","passeord","database")
    cursor = conn.cursor()
    sql = """
    insert into uv (id,vistors) VALUES (%d,%d)
    """ %(mdate,num)
    try:
        cursor.execute(sql)
        conn.commit()
        return 0
    except:
        conn.rollback()
        return 1

if __name__ == "__main__":
    mdate = int(time.strftime('%Y%m%d',time.localtime(time.time())))
    cmd = "cat /var/log/nginx/access.log-%d | awk '{print $1}' | sort -r | uniq -c | wc -l > /home/scripts/uv.info" %(mdate)
    print(cmd)
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
    time.sleep(3)
    with open('uv.info') as obj:
        num = int(obj.readlines()[0])
        insert(mdate,num)

  

[root@iZbp1ezumclop9223l31okZ scripts]# crontab -l
10 10 * * * cd /home/scripts;/usr/bin/python uv_monitor.py 1>a1 2>b1

  

MariaDB [monitor]> select * from uv;
+----------+---------+
| id       | vistors |
+----------+---------+
| 20180704 |    xx|
| 20180705 |    xx|
+----------+---------+

  

 

posted @ 2018-07-05 10:21  jabbok  阅读(684)  评论(0编辑  收藏  举报