zabbix监控windows日志脚本
脚本用于监控windows服务器上日志,查看日志文件的末尾N行,如果N行中包含某字段,则输出0,否则输出1,然后再zabbix的配置文件空定义kye,进行监控。文本文件的换行符是"\n"
编辑脚本log.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import sys
import re
def last_lines(filename, lines = 1):
lines = int(lines)
block_size = 1024
block = ''
nl_count = 0
start = 0
fsock = file(filename, 'rU')
try:
# seek to end
fsock.seek(0, 2)
# get seek position
curpos = fsock.tell()
while(curpos > 0): #while not EOF
# seek ahead block_size+the length of last read block
curpos -= (block_size + len(block));
if curpos < 0:
curpos = 0
fsock.seek(curpos)
# read to end
block = fsock.read()
nl_count = block.count('\n')
# if read enough(more)
if nl_count >= lines:
break
# get the exact start position
for n in range(nl_count - lines + 1):
start = block.find('\n', start) + 1
finally:
fsock.close()
return block
list = last_lines(sys.argv, sys.argv)
if re.search("aaa", list): #查看是否包含“aaa”
print 0
else:
print 1
运行方法如下:python log.py log.txt 10 #最后10行
页:
[1]