zoukankan      html  css  js  c++  java
  • 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|
    +----------+---------+
    

      

  • 相关阅读:
    在Linux下删除文件及文件夹(rm)
    修改Linux文件权限
    文件分页显示(ls -al |more)
    linux的文件权限
    Linux中的重启(reboot)
    linux关机前同步数据(sync)
    hdu4990 Reading comprehension 矩阵快速幂
    hdu4965 Fast Matrix Calculation 矩阵快速幂
    hdu4847 Wow! Such Doge! KMP
    hdu4705 Y 树形DP
  • 原文地址:https://www.cnblogs.com/jabbok/p/9266903.html
Copyright © 2011-2022 走看看