Python脚本实时处理log文件的方法
作者:Tony_Deng 时间:2021-02-23 06:40:32
这个Python脚本是用来对实时文件的内容监控,比如 Error 或者 time out 字段都可以进行自定义;算是我的第一个真正的Python脚本,自己感觉还是比较臃肿,不过打算放到blog上记录一下(还是初学者,大神们勿喷哈),真心希望博友们能够再指点一下(现在记录每次的文件大小值是输出到了一个文件中,并且里面还嵌套了有shell命令,这些我认为都是可以优化掉的吧,只是现在我还不知道怎么做);告警是基于zabbix,自定义的模板是120s执行一次
#!/usr/local/bin/python3.5
###Destription: 实时读取log信息
###Author: Danny Deng
###Datetime: 2016-11-17
import re,time,subprocess,os,linecache
#####定义log文件
file_name = "/usr/local/nginx/logs/error.log"
file_number = "/usr/local/zabbix_agent/number.txt"
j = int(0)
seek = int(0)
##判断过程:文件是否存在---判断存储日志大小的文件是否存在---判断number size 与 filesize的大小
###定义函数按行读取文件内容
def readline():
####if判断 seek是否大于0,大于则赋值,否则初始为0
while True:
######定义文件,根据seek值进行每行读取,每次tell赋值给seek
with open(file_name,'r') as f:
global seek
#seek = seek
f.seek(seek)
data = f.readline()
if data:
seek = f.tell()
yield data
else:
######Python变量转换为shell变量
global file_number
os.environ['seek'] = str(seek)
os.environ['file_number'] = str(file_number)
os.system('echo $seek > $file_number')
os.system('chown zabbix.zabbix $file_number')
return
def func_for():
j = int(0)
for i in readline():
f_find = re.findall(r"check time out", i,flags=re.IGNORECASE)
if "check time out" in f_find:
j += 1
#####没有输出0,有值输出出现error匹配到的次数值
try:
print(j)
except NameError:
print(int("0"))
###判断日志文件是否存在
if os.path.isfile(file_name):
###判断存储文件内容大小的文件是否存在
if os.path.isfile(file_number):
####存在则读取文件size大小,赋值给seek_number
seek_number = int(linecache.getline(file_number, 1))
####然后继续判断存储的文件大小与现在文件大小(确定文件是否是重新生成的)
if os.path.getsize(file_name) >= seek_number and seek_number > 0:
seek = seek_number
func_for()
###若为新文件则,seek 赋值为0
else:
#open(arg1, "a+").write("0")
#seek = int(linecache.getline(file_number, 1))
seek = int(0)
func_for()
####file_number 不存在则新建,并赋值seek变量为0
else:
#open(file_number, "a+").write("0")
#seek = int(linecache.getline(file_number, 1))
os.environ['file_number'] = str(file_number)
os.system('echo 0 > $file_number')
os.system('chown zabbix.zabbix $file_number')
func_for()
else:
print("Error")
quit()
以上所述是小编给大家介绍的Python脚本实时处理log文件脚本网站的支持!
来源:http://www.cnblogs.com/tony-d/archive/2016/11/21/6084119.html
标签:python,log,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python入门教程之基本算术运算符
2023-05-24 11:48:21
Python 关于反射和类的特殊成员方法
2021-10-16 19:30:13
layer ui 导入文件之前传入数据的实例
2023-08-15 11:39:17
OpenCV形状检测的示例详解
2023-11-11 06:17:14
![](https://img.aspxhome.com/file/2023/9/97489_0s.png)
python中adb有什么功能
2022-06-22 13:25:15
Selenium python时间控件输入问题解决方案
2023-09-05 19:35:25
![](https://img.aspxhome.com/file/2023/8/79608_0s.png)
python用列表生成式写嵌套循环的方法
2022-02-14 05:45:03
Python日志模块logging简介
2022-07-14 23:55:22
![](https://img.aspxhome.com/file/2023/9/94069_0s.png)
Python3.4学习笔记之类型判断,异常处理,终止程序操作小结
2022-06-03 10:45:54
Python 12306抢火车票脚本
2023-09-12 13:36:11
Matplotlib绘制条形图的方法你知道吗
2022-12-05 15:23:59
![](https://img.aspxhome.com/file/2023/5/83685_0s.png)
擦亮自己的眼睛去看SQL Server之历史渊源
2011-08-29 15:40:53
python 批量修改 labelImg 生成的xml文件的方法
2022-09-03 12:04:23
使用python3调用wxpy模块监控linux日志并定时发送消息给群组或好友
2022-05-12 03:35:29
基于python使用Pillow做动态图在图中生成二维码以及图像处理
2022-12-29 16:33:03
![](https://img.aspxhome.com/file/2023/5/97235_0s.png)
SQL 比较一个集合是否在另一个集合里存在的方法分享
2011-12-01 10:19:26
python数据结构之面向对象
2021-04-09 08:02:06
![](https://img.aspxhome.com/file/2023/0/94120_0s.png)
关于python 读取csv最快的Datatable的用法,你都学会了吗
2022-02-22 20:38:32
![](https://img.aspxhome.com/file/2023/5/101585_0s.jpg)
Pycharm cannot set up a python SDK问题的原因及解决方法
2022-12-21 15:05:34
![](https://img.aspxhome.com/file/2023/0/100760_0s.jpg)
段正淳的css笔记(4)css代码的简写
2007-11-01 22:03:00