您的位置:首页 > 其它

脚本统计ip的海外用户分布情况或者国内省份的分布情况

2017-11-20 20:20 465 查看
背景:日志文件会产生ip字段,现需要统计ip的分布情况


前提:必须有这么一个服务,输入指定的ip能返回相应的地理位置的信息。可以直接调用。

思想:首先提取出ip字段,一般用sed命令或者和cut结合。

例如,日志格式如下:

[INFO][2017-11-18 00:00:00] reqhandler.py 134, allocate, hid:XXXX msg:{'province': 'sn', 'ccid': -1, 'ip': 'xxx.xxx.xxx.xxx', 'isp': 'unicom', 'mid': 0, 'spaceid': '5A10BA4FCBB011E7A162000E1EFF0BA0', 'locator': 8, 'game': 10038, 'eid': 148079890, '_cmd_': 327681}
可以用以下命令来提取ip字段。

grep "allocate," /home/XX/logs/*/XX.2017-11-17 | sed "s/^.* 'ip': '//g" | sed "s/', 'isp'.*//g" >> /home/XX/ip_1117.log


然后再用脚本去统计分布情况。(ip.mmdb 为一个ip映射的文件,此脚本用于统计国内省份分布情况)

# coding: utf8
import maxminddb
import csv

def get_country():
reader = maxminddb.Reader('/home/XX/ip_log/ip.mmdb')

en2zw = {}
en2count = {}

ips = get_record_from_file('/home/XX/ip_log/test.log')
total_len = len(ips) / 1000
count = 0
for ip in ips:
count += 1
try:
result = reader.get(ip)
print result
except Exception as ex:
print ip, ex
continue
if not result:
print ip, result
continue
zw = result['subdivisions']['names'].get('zh_CN', '')
en = result['subdivisions']['names'].get('en', '')
ct = result['country']['names'].get('en', '')
print zw, en, ct
if ct != 'China':
continue

if en not in en2zw:
en2zw[en] = zw

if en not in en2count:
en2count[en] = 1
else:
en2count[en] += 1
print '统计结果'
print en2count
en2count.pop('China', None)
total = sum(en2count.values()) * 1.0
print 'total:', total
for en, count in en2count.iteritems():
print 'en:%s, zh_CN: %s, count:%s percent:%s' % (en, en2zw.get(en), count, count / total)
def get_record_from_file(filename):
u"""
从文件中读取内容并放回,不适合操作大文件
Args:
filename {str} 文档的路径
"""
ret = []
with open(filename) as f:
for record in f:
record = record.strip()
if not record:
continue
ret.append(record)
return ret

if __name__ == '__main__':
get_country()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: