标签:web line 自己 客户 open 控制台 toolbar css mon
需求:在阿里云-CDN管理控制台的监控页面里,有对PV、UV和热点资源的统计。于是自己也写了脚本来获取相关数据。
分析:
PV:指网站的访问请求数。包含同一来源IP的多次请求。
UV:值网站的独立访客数。同一来源IP的多次请求只计算一次。
来看一条Nginx的access日志信息:
# head -1 access.log 192.165.158.238 - - 2017-03-06T20:47:04+08:00 "GET http://download.helloworld.com/ HTTP/1.1" 200 851 425 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" "-" 0.000 -
以空格为分隔符的第一列,代表客户端的来源IP;第六列代表了用户请求的资源。
Linux Shell实现:
# awk 'END{print "PV is:",NR}' access.log PV is: 1881955 # awk '{s[$1]+=1} END{for(i in s){sum+=1}} END{print "UV is:",sum}' access.log UV is: 64953 # awk '{s[$6]+=1} END{for(i in s){print s[i],i}}' access.log | sort -rn | head -10 # 只打印出访问次数最多的10条记录 92838 http://download.helloworld.com/hello/hello 88873 http://download.helloworld.com/world/hi/ 57711 http://appy.helloworld.com/world/js/jquery-1.10.1.min.js 46980 http://download.helloworld.com/favicon.ico 38759 http://appy.helloworld.com/world/css/style.css?t=00001 38684 http://appy.helloworld.com/world/css/base.css 35404 http://appy.helloworld.com/favicon.ico 34907 http://download.helloworld.com/world/js/jquery-1.10.1.min.js 34882 http://appy.helloworld.com/world/img/hi.jpg 34445 http://download.helloworld.com/world/css/base.css
Python实现:
# cat count.py from __future__ import print_function from collections import Counter ips = [] #定义存储客户端来源IP的列表 hot_resources = Counter() #用计数器来统计资源的访问情况 with open('access.log', 'r') as fin: for line in fin: ip = line.split()[0] if ip: ips.append(ip) resource = line.split()[5] if resource: hot_resources[resource] += 1 print("PV is: {0:d}".format(len(ips))) print("UV is: {0:d}".format(len(set(ips)))) for key, val in hot_resources.most_common(10): #计数器提供了most_common,可以输出最大的10条记录 print(val, key) # python count.py PV is: 1881955 UV is: 64953 92838 http://download.helloworld.com/hello/hello 88873 http://download.helloworld.com/world/hi/ 57711 http://appy.helloworld.com/world/js/jquery-1.10.1.min.js 46980 http://download.helloworld.com/favicon.ico 38759 http://appy.helloworld.com/world/css/style.css?t=00001 38684 http://appy.helloworld.com/world/css/base.css 35404 http://appy.helloworld.com/favicon.ico 34907 http://download.helloworld.com/world/js/jquery-1.10.1.min.js 34882 http://appy.helloworld.com/world/img/hi.jpg 34445 http://download.helloworld.com/world/css/base.css
PS:
现阶段正在自学Python,但是线上业务并没有用到。只好将Shell实现的功能,用Python再实现一遍,以做练习。
标签:web line 自己 客户 open 控制台 toolbar css mon
原文地址:http://blog.51cto.com/13568014/2059163