python 日志监控与分析
原创
©著作权归作者所有:来自51CTO博客作者SongpingWang的原创作品,请联系作者获取转载授权,否则将追究法律责任
文章目录
一、linecache模块
我之所以写这个,主要是我需要分析日志文件,但是我不想用到那个实时的日志文件分析,而是定时分析日志文件,故需要反复的使用一个文件进行读取操作,优先考虑这种方式把文件对象当成缓存,下次就不需要从头开始读取了。相比open()那种方法要快N倍,它是你读取文件的效率之源。
linecache模块的作用是将文件内容读取到内存中,进行缓存,而不是每次都要从硬盘中读取,这样效率提高很多,又省去了对硬盘IO控制器的频繁操作。
模块linecache(行-缓存),这对于读取内容非常多的文件,效果甚好,而且它还可以读取指定的行内容。
linecache模块函数讲解
# -*- coding: utf-8 -*-
#python 27
import linecache
'''
>>> help(linecache)
Help on module linecache:
FUNCTIONS
checkcache = extended_linecache_checkcache(filename=None, orig_checkcache=<function checkcache>)
Extend linecache.checkcache to preserve the <pyshell#...> entries
Rather than repeating the linecache code, patch it to save the
<pyshell#...> entries, call the original linecache.checkcache()
(skipping them), and then restore the saved entries.
orig_checkcache is bound at definition time to the original
method, allowing it to be patched.
clearcache()
Clear the cache entirely.
getline(filename, lineno, module_globals=None)
DATA
__all__ = ['getline', 'clearcache', 'checkcache']
'''
#从名为filename的文件中得到第lineno行。这个函数从不会抛出一个异常–产生错误时它将返回”(换行符将包含在找到的行里)。
#linecache.getline(filename,lineno)
#查找特定行(第一行)的数据,返回一个字符串
filename='1.txt'
re= linecache.getline(filename,1)
print re.strip()#因为本身带有换行符
#得到所有行数据,返回一个list
re= linecache.getlines(filename)
print re
#获取多少行的数据
re= linecache.getlines(filename)[0:4]
print re
#更新文件名为filename的缓存。如果filename文件更新了,使用这个函数可以更新linecache.getlines(filename)返回的列表
linecache.updatecache(filename)
#清理缓存
linecache.clearcache()
1 )、 linecache.getline(filename, lineno[, module_globals]) ,这个方法从filename也就是文件中读取内容,得到第 lineno行,注意没有去掉换行符,它将包含在行内。
2 )、 linecache.clearcache() ,清除现有的文件缓存。
3 )、 linecache.checkcache([filename]) ,参数是文件名,作用是检查缓存内容的有效性,可能硬盘内容发生了变化,更新了,如果不提供参数,将检查缓存中所有的项。
# coding=utf-8
import os
import linecache
def get_content(path):
'''读取缓存中文件内容,以字符串形式返回'''
if os.path.exists(path):
content = ''
cache_data = linecache.getlines(path)
for line in range(len(cache_data)):
content += cache_data[line]
return content
else:
print('the path [{}] is not exist!'.format(path))
def main():
path = 'c:\\test.txt'
content = get_content(path)
print(content)
if __name__ == '__main__':
main()
二、实时监控获取日志文件的增量内容进行分析
import time
def run(log_path):
count = 0
position = 0
with open(log_path, mode='r', encoding='utf8') as f1:
while True:
line = f1.readline().strip()
if line:
count += 1
analysis_log(line) # 自定义分析函数
print("count %s line %s" % (count, line))
cur_position = f1.tell() # 记录上次读取文件的位置
if cur_position == position:
time.sleep(0.1)
continue
else:
position = cur_position
time.sleep(0.1)
if __name__ == "__main__":
log_path = "./nohup.log"
run(log_path)
三、Linux服务器CPU、内存、磁盘空间、负载情况查看
# -*- coding:utf-8 -*- -
import os, time
last_worktime=0
last_idletime=0
def get_cpu():
global last_worktime, last_idletime
f=open("/proc/stat","r")
line=""
while not "cpu " in line: line=f.readline()
f.close()
spl=line.split(" ")
worktime=int(spl[2])+int(spl[3])+int(spl[4])
idletime=int(spl[5])
dworktime=(worktime-last_worktime)
didletime=(idletime-last_idletime)
rate=float(dworktime)/(didletime+dworktime)
last_worktime=worktime
last_idletime=idletime
if(last_worktime==0): return 0
return rate
def get_mem_usage_percent():
try:
f = open('/proc/meminfo', 'r')
for line in f:
if line.startswith('MemTotal:'):
mem_total = int(line.split()[1])
elif line.startswith('MemFree:'):
mem_free = int(line.split()[1])
elif line.startswith('Buffers:'):
mem_buffer = int(line.split()[1])
elif line.startswith('Cached:'):
mem_cache = int(line.split()[1])
elif line.startswith('SwapTotal:'):
vmem_total = int(line.split()[1])
elif line.startswith('SwapFree:'):
vmem_free = int(line.split()[1])
else:
continue
f.close()
except:
return None
physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total)
virtual_percent = 0
if vmem_total > 0:
virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total)
return physical_percent, virtual_percent
def usage_percent(use, total):
try:
ret = (float(use) / total) * 100
except ZeroDivisionError:
raise Exception("ERROR - zero division error")
return ret
statvfs = os.statvfs('/')
total_disk_space = statvfs.f_frsize * statvfs.f_blocks
free_disk_space = statvfs.f_frsize * statvfs.f_bfree
disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space
disk_usage = int(disk_usage)
disk_tip = "硬盘空间使用率(最大100%):"+str(disk_usage)+"%"
print(disk_tip)
mem_usage = get_mem_usage_percent()
mem_usage = int(mem_usage[0])
mem_tip = "物理内存使用率(最大100%):"+str(mem_usage)+"%"
print(mem_tip)
cpu_usage = int(get_cpu()*100)
cpu_tip = "CPU使用率(最大100%):"+str(cpu_usage)+"%"
print(cpu_tip)
load_average = os.getloadavg()
load_tip = "系统负载(三个数值中有一个超过3就是高):"+str(load_average)
print(load_tip)
其它:
pyinotify监控文件内容变化: https://blog.51cto.com/guoshiwei/2124306 watchdog