统计nginx日志里每五分钟的访问量
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Random_lee
import time
import os
import re class StatusPV(object):
def __init__(self):
self.log_path = '/opt/apache-tomcat-7.0.69/logs/'
self.log_time = time.strftime("%Y-%m-%d")
self.log_name = 'localhost_access_log.%s.txt'%(self.log_time)
self.logfile = os.path.join(self.log_path, self.log_name) def get_filesize(self):
file_size = os.path.getsize(self.logfile)
f = open(self.logfile, 'r')
if file_size > 1000000000:
# 文件大小超过1G从中间读取
f.seek(0, 2)
self.seek= f.tell()/5
else:
self.seek=0
f.close()
def count_pv(self):
self.get_filesize()
if not os.path.exists(self.logfile):
print('error:' + self.logfile + ' not existed.')
return 0
else:
f = open(self.logfile, 'r')
f.seek(self.seek,0)
num = 0
for line in f:
data = time.strftime('%d/%b/%Y:%H:', time.localtime())
if data in line:
expr = re.compile('^(?P<RemoteIP>.*) - - (?P<datatime>.*) (?P<request>".+") (?P<status>\d{3}) (?P<web_size>\d{1,10})')
# 日志格式 10.116.201.71 - - [02/Sep/2018:09:44:13 +0800] "POST /servlet/UpdateJavaFXServlet HTTP/1.0" 200 268
try:
log_info = expr.search(line)
log_info = log_info.groupdict() # 解析日志信息
datatime = log_info["datatime"]
# 取出日志信息中的datatime
datatime = datatime.replace('[', '')
datatime = datatime.replace(']', '')
# 去掉[]
data_time = datatime.split(' ')[0]
# 取出日期时间
time_zone = datatime.split(' ')[1]
# 取出时区
if time_zone == '+0800':
# print(time_zone)
# print(data_time)
ctime = time.strptime(data_time, '%d/%b/%Y:%H:%M:%S')
# 转换为格式化时间 24/Aug/2018:15:42:08
time_stamp = time.mktime(ctime)
# 转换为时间戳
# print(time_stamp)
if time.time() - time_stamp <= 300:
# 观测的时间间隔
num += 1
# print(datatime)
else:
# print("error data_time:%s"%datatime)
pass
else:
print("log format error")
except:
pass
else:
pass
f.close()
print(num) if __name__ == '__main__':
obj_StatusPV = StatusPV()
obj_StatusPV.count_pv()
统计nginx日志里每五分钟的访问量的更多相关文章
- 统计nginx日志里流量
用awk可以,比如,我想统计nginx日志里,今天下午3点0分,这一分钟内,访问的流量(文件的大小) grep "07/Nov/2013:15:00:" *.log|awk '{ ...
- 统计nginx日志里访问次数最多的前十个IP
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -nr -k1 | head -n 10
- 用shell统计访问日志里每个ip访问次数【转】
今天,要统计一个系统的每个ip访问次数,于是我找到该系统访问日志,并写脚本实现. 访问日志情况: [root@qular ~]# cd /usr/local/nginx/logs/ [root@q ...
- 统计nginx日志
.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l .统计访问URL统计PV awk '{print $7}' access.l ...
- 统计Apache或nginx日志里访问次数最多的前十个IP
1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l 2.统计访问URL统计PV awk '{print $7}' access ...
- 通过python统计nginx日志定位php网站响应慢的问题
# 公司网站反映很慢,可能是一些页面的访问方法或者页面引起,通过程序统计nginx访问日志的页面和具体的action方法访问次数以及平均响应时间可以为程序开发的同事提供参考定位具体的代码 # 默认的n ...
- 使用Python 统计nginx日志前十ip访问量并以柱状图显示
脚本内容: import matplotlib.pyplot as plt # nginx_file = '10.6.11.91_access.log-2018-12-27' ip = {} #筛选n ...
- linux统计nginx日志中请求访问量命令
Nginx 三种分配策略:轮询.权重.ip_hash(比如你登录了一个网站,登录信息已经保存到 a 机器,但当你做后续操作时的请求会到 b 机器,那么就获取不到你原来登录的信息,此时你就需要重新登录了 ...
- 统计nginx日志单IP访问请求数排名
下面是我截取一段nginx日志 /Jan/::: +] "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:31.0) Gec ...
随机推荐
- 4.9 Parser Generators
4.9 Parser Generators This section shows how a parser generator can be used to facilitate the constr ...
- python-----复制文件夹
python复制文件夹有两种方法: 1.使用shutil模块:(要保存的路径必须事先不存在) #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time ...
- 第十五周 Leetcode 517. Super Washing Machines(HARD) 贪心
Leetcode517 很有趣的一道题 由于每一步可以任选某些数字对它们进行转移,所以实际上是在求最优解中的最复杂转移数. 那么我们考虑,到底哪一个位置要经过的流量最大呢? 枚举每个位置,考虑它左边的 ...
- Ural 1158. Censored! 有限状态自动机+DP+大整数
Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...
- 洛谷P1297 单选错位——期望
题目:https://www.luogu.org/problemnew/show/P1297 读懂题后就变得很简单啦: 对于一个问题和它的下一个问题,我们考虑: 设上一个问题有 a 个选项,下一个问题 ...
- bzoj1085 [SCOI2005]骑士精神——IDA*
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1085 搜索,IDA*,估价就是最少需要跳的步数: 代码意外地挺好写的,memcmp 用起来好 ...
- hive 内部表与外部表的区别
hive 内部表: hive> create table soyo55(name STRING,addr STRING,money STRING) row format delimited fi ...
- bzoj 2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛【树形dp】
设f[u][0/1]为u这个点不选/选,转移的时候从儿子转移,f[u][1]=sum(f[son][0])+1,f[u][0]=sum(max(f[son][0],f[e[i].to][1])) #i ...
- tomcat 参数调优
JAVA_OPTS="-Xms2g -Xmx2g -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath= ...
- Spring-Aop的两种代理方式
Spring-Aop两种代理方式: 1.JDK动态代理:用于目标类实现了接口: 2.Cglib动态代理:用于目标类没有实现接口: spring会依据目标类是否实现接口来选择使用哪种代理方式(目标类:相 ...