zabbix监控windows日志脚本
脚本用于监控windows服务器上日志,查看日志文件的末尾N行,如果N行中包含某字段,则输出0,否则输出1,然后再zabbix的配置文件空定义kye,进行监控。
文本文件的换行符是"\n"
编辑脚本log.py
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[start:] list = last_lines(sys.argv[1], sys.argv[2]) if re.search("aaa", list): #查看是否包含“aaa” print 0 else: print 1
运行方法如下:python log.py log.txt 10 #最后10行
本文出自 “阿三哥” 博客,谢绝转载!