• 增量统计日志行数(只统计上一秒)

dns_qps.py

#!/usr/bin/env python
#_*_coding:utf-8_*_ import datetime
import re
import os log_files = './dns_logs' #填写要分析的源日志
seek_files = './seek_log.tmp' #生成的临时文件,只用于保存位置seek信息,建议放/tmp/下
last_second = datetime.datetime.now() - datetime.timedelta(seconds=1) #取上一秒的时间,因为当前秒可能日志取不全
current_time = last_second.strftime('%d-%b-%Y %T') #时间格式根据日志格式做修改
#current_time = '17-Jun-2016 14:17:20' if not os.path.exists(seek_files):
with open(seek_files, 'w+') as s:
s.write(str(0)) def write_file_end(log_files, seek_files):
with open(log_files, 'r') as f: #先找到log的最后字节位置
f.seek(0, 2) #seek(0)移动到文件起始、(0,2)移动到结尾
end_seek = f.tell()
with open(seek_files, 'w+') as s:
s.write(str(end_seek)) #把log的最后字节位置记录到seek文件 def get_count(log_files, begin):
count = 0
dns_pattern = re.compile(current_time+r'\.(\w+)(\s+)'+"queries: info: client") #这个正则要根据你的日志格式来,可以放到前边,声明一个变量,我这图省事了
with open(log_files, 'r') as f: #打开log,并移位到seek中记录的地方,从那开始读取行数并做累加,读完以后在更新seek文件到最新点
f.seek(begin)
for line in f.xreadlines():
if dns_pattern.match(line):
count += 1
print(count) if __name__ == '__main__':
try:
with open(seek_files, 'r') as t:
if len(t.read()) == 0: #seek文件为空,强制begin=0,不为空,begin就等于seek
begin = 0
else:
t.seek(0)
begin = int(t.read())
with open(log_files, 'r') as f: #拿到end,end值为log文件的最后位置
f.seek(0, 2)
end = f.tell()
if end < begin: #因为日志定期会切分,切分后log的end将为0,此时begin强制为0,不然输出的count将为0
begin = 0
get_count(log_files, begin) #得到上一秒的总行数
write_file_end(log_files, seek_files) #把日志最后的位置保存给seek文件,用于下一秒的获取
except Exception, e:
print(0)

注意事项

把这个脚本放在zabbix之类的监控里,一秒执行一次,就可以算出每秒(其实是上一秒)增量部分的行数了,然后zabbix拿着这个度数绘图

# 运行脚本
/usr/bin/python2.6 dns_qps.py

下边更进一步,拿着这个增量,基于源ip,我们再做一个排序,把排序结果记录到一个log里

  • 增量统计日志(tcpdump出来的数据)并排序,记录日志

dns_request_sort.py

#!/usr/bin/env python
#_*_coding:utf-8_*_ import datetime
import re
import os
import logging
import sys master_dir = "/Data/logs/dns_qps/tcpdump_53" #把tcpdump的数据放这个目录里,原生的tcpdump数据
today = datetime.datetime.now().strftime('%Y%m%d')
log_files = os.path.join(master_dir, today+'.log'),
log_files = log_files[0]
if not os.path.exists(log_files):
sys.exit('Can not find logfile') # for tcpdump
seek_files = '/tmp/seek_log_4_tcpdump_53.tmp' #同理存放seek信息的
last_second = datetime.datetime.now() - datetime.timedelta(seconds=1) #取上一秒的时间,因为当前秒可能日志取不全
current_time = last_second.strftime('%T')
#current_time = '16:54:17'
warning_num = 4 #设定阈值
warning_log = "/Data/logs/dns_qps/dns_warning_sort.log" #生成的排序ip的日志 def Mylogger(msg, logfile, level='info'):
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s [%(levelname)s] %(message)s',
filename=logfile,
filemode='a+')
if level == 'critical':
logging.critical(msg)
elif level == 'error':
logging.error(msg)
elif level == 'warn':
logging.warn(msg)
elif level == 'info':
logging.info(msg)
else:
logging.debug(msg) if not os.path.exists(seek_files):
with open(seek_files, 'w+') as s:
s.write(str(0)) def write_file_end(log_files, seek_files):
with open(log_files, 'r') as f: #先找到log的最后字节位置
f.seek(0, 2)
end_seek = f.tell()
with open(seek_files, 'w+') as s:
s.write(str(end_seek)) #把log的最后字节位置记录到seek文件 def get_count(log_files, begin):
dns_pattern = re.compile(current_time+r'\.\d+ IP (\d+\.\d+\.\d+\.\d+)\.\d+[\s\S]*')
ip_list = []
with open(log_files, 'r') as f: #打开log,并移位到seek中记录的地方,从那开始读取行数并做累加,读完以后在更新seek文件到最新点
f.seek(begin)
for line in f.xreadlines():
if dns_pattern.match(line):
ip_list.append(dns_pattern.match(line).groups()[0]) #groups()[0]是匹配的ip字段
for item in set(ip_list): #最后得到一个大列表,里边放着一秒内的多个ip,可能有重叠,set()去重并循环它,每个item就是一个ip
if ip_list.count(item) >= warning_num: #列表中出现的ip的数量超过阈值就记录日志
Mylogger("%s %s 此IP: %s 访问达到 %d 次" % (today, current_time, item, ip_list.count(item)),
warning_log, level='warn') if __name__ == '__main__':
try:
with open(seek_files, 'r') as t:
if len(t.read()) == 0:
begin = 0
else:
t.seek(0)
begin = int(t.read())
with open(log_files, 'r') as f:
f.seek(0, 2)
end = f.tell()
if end < begin: #因为日志定期会切分,切分后log的end将为0,此时begin强制为0,不然输出的count将为0
begin = 0
get_count(log_files, begin)
write_file_end(log_files, seek_files)
except Exception, e:
pass

最后附上跑tcpdump的那个脚本

本来想用python的pypcap来抓取数据包,然后用dpkt来分析的,但测了一下,抓出来的东西都是乱码,就放弃了,直接用shell调用tcpdump脚本了

while_53.sh

#!/usr/bin/env bash

master_dir="/Data/logs/dns_qps/tcpdump_53"
device="em1"
mkdir -p ${master_dir}
chown -R zabbix.zabbix ${master_dir} sudo /bin/kill -9 `ps -ef|grep tcpdump|grep -v grep|awk '{print$2}'` 2>/dev/null
today=`date +%Y%m%d`
sudo /usr/sbin/tcpdump -i ${device} -nn udp port 53|grep "> 10.*\.53:" >> ${master_dir}/${today}.log &

把2个脚本放到crontab跑起来

00 00 * * * cd /Data/shell && sh while_53.sh  #这个每天重启一次tcpdump用于按天分割日志
* * * * * cd /Data/shell && /usr/bin/python dns_request_sort.py #没那么敏感的要求,一分钟跑一次足矣
# 看下生成的日志
2016-08-26 16:20:01,568 [WARNING] 20160826 16:20:00 此IP: 10.1.0.110 访问达到 70 次
2016-08-26 16:21:01,616 [WARNING] 20160826 16:21:00 此IP: 10.1.0.110 访问达到 67 次
2016-08-26 16:22:01,665 [WARNING] 20160826 16:22:00 此IP: 10.1.0.110 访问达到 68 次
2016-08-26 16:23:01,714 [WARNING] 20160826 16:23:00 此IP: 10.1.0.110 访问达到 65 次
2016-08-26 16:24:01,766 [WARNING] 20160826 16:24:00 此IP: 10.1.20.253 访问达到 100 次
2016-08-26 16:24:01,766 [WARNING] 20160826 16:24:00 此IP: 10.1.0.110 访问达到 72 次
2016-08-26 16:25:01,815 [WARNING] 20160826 16:25:00 此IP: 10.1.0.110 访问达到 59 次

【原创】基于日志增量,统计qps,并基于ip排序的更多相关文章

  1. MySQL5.6主从复制搭建基于日志(binlog)

    什么是MySQL主从复制 简单来说,就是保证主SQL(Master)和从SQL(Slave)的数据是一致性的,向Master插入数据后,Slave会自动从Master把修改的数据同步过来(有一定的延迟 ...

  2. (转)MySQL 主从复制搭建,基于日志(binlog

    原文:http://blog.jobbole.com/110934/ 什么是MySQL主从复制 简单来说,就是保证主SQL(Master)和从SQL(Slave)的数据是一致性的,向Master插入数 ...

  3. Mysql5.7基于日志转为基于事务主从复制

    将基于日志的复制变更为基于事务的复制 mysql版本要高于5.7.6 gtid_mode要设为off 处理步骤 详细步骤 1.查看主从mysql版本是否高于5.7.6 show variables l ...

  4. 基于日志数据分析以防御CC攻击的想法

    1. What - 什么是CC攻击 CC攻击,即针对应用层HTTP协议的DDos攻击,攻击者在短时间内向目标服务器发送大量的HTTP请求,使得服务器会非常繁忙,资源消耗会增加:同时,如果请求中包含基于 ...

  5. Mysql5.7基于日志主从复制

    主从同步概念 主从同步是异步复制 Mysql两种复制类型: 基于二进制日志 使用GTID完成基于事务的复制 基于日志三种方式: Mysql5.7需要注意的问题: 老版本方法创建mysql用户 #mys ...

  6. 基于短语的统计机器翻(PBMT) 开源工具 :Moses

    如何运行Moses 1. Moses的历史 Moses是Pharaoh的升级版本,增加了许多功能.它是一个基于短语的统计机器翻译系统,整个系统用C++语言写成,从训练到解码完全开放源代码,可以运行在L ...

  7. R学习:《机器学习与数据科学基于R的统计学习方法》中文PDF+代码

    当前,机器学习和数据科学都是很重要和热门的相关学科,需要深入地研究学习才能精通. <机器学习与数据科学基于R的统计学习方法>试图指导读者掌握如何完成涉及机器学习的数据科学项目.为数据科学家 ...

  8. 5-5配置Mysql复制 基于日志点的复制

    配置MySQL复制 基于日志点的复制配置步骤 设置简单密码(可以选择不需要) set GLOBAL validate_password_length=6; set global validate_pa ...

  9. 基于机器学习的web异常检测——基于HMM的状态序列建模,将原始数据转化为状态机表示,然后求解概率判断异常与否

    基于机器学习的web异常检测 from: https://jaq.alibaba.com/community/art/show?articleid=746 Web防火墙是信息安全的第一道防线.随着网络 ...

随机推荐

  1. java06

    阅读并运行示例PassArray.java,观察并分析程序输出的结果 小结:引用传递.如果方法中有代码则更改了数组元素的值,因为引用时传递的是地址. 阅读程序WhatDoesThisDo.java, ...

  2. CocoaPods pod install

    加参数可以提升更新的速度 方法1: pod install --verbose --no-repo-update pod update --verbose --no-repo-update 方法2: ...

  3. opecv获取图像轮廓

    获取轮廓 #import <opencv2/opencv.hpp> #import <opencv2/imgcodecs/ios.h> #import <opencv2/ ...

  4. 【大型网站技术实践】初级篇:搭建MySQL主从复制经典架构

    一.业务发展驱动数据发展 随着网站业务的不断发展,用户量的不断增加,数据量成倍地增长,数据库的访问量也呈线性地增长.特别是在用户访问高峰期间,并发访问量突然增大,数据库的负载压力也会增大,如果架构方案 ...

  5. 史上最牛js

    js的功能有多强大,能做到多极致?当然前提是能用,不要搞到需要超级计算器才能运行,那不算. 今天一朋友给我介绍了这个:http://bellard.org/jslinux/ 倒腾了半天后,我只能这么感 ...

  6. SQL Azure (17) SQL Azure V12 - 跨数据中心标准地域复制(Standard Geo-Replication)

    <Windows Azure Platform 系列文章目录> 熟悉Microsoft Azure平台的读者都了解,Azure SQL Database提供不同等级的,跨数据中心的异地冗余 ...

  7. Android开发学习之路-Android N新特性-多窗口模式

    我们都知道,在最新的Android N系统中,加入了一个新的功能,就是多窗口模式.多窗口模式允许我们在屏幕上显示两个窗口,每个窗口显示的内容不同,也就是说,我们可以一遍看电视剧,一边聊微信. 这里我们 ...

  8. Ado net Source 用法

    Ado net Source 是用于获取数据源的,使用的connection manager是 ado net connection. Ado Net Source 的Data Access Mode ...

  9. 让低版本的 Android 项目显示出 Material 风格的点击效果

    每天都被不同的需求纠缠的生活是幸福而又不幸的,这不我们家亲爱的设计师们又让我们在低版本的 Android 平台上实现一下类似于 Material Design 的点击效果. 虽然大家都知道 Mater ...

  10. OpenCascade Draw Test Harness

    OpenCascade Draw Test Harness eryar@163.com Abstract. Draw is a command interpreter based on Tcl/Tk ...