#!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Random_lee # 0 : 同步成功 # 1 : 获取到开始标识,但是运行超过5分钟未找到结束标识 # 2 : 超过30分钟没有获取到开始标识 import time import tailer import re import os import logging import subprocess logger = logging.getLogger('test.py') logger.setLevel(level=logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(funcName)s - %(process)d - %(levelname)s - %(message)s') handler = logging.FileHandler("D:log4jmontior.log") # handler = logging.FileHandler("montior.log") handler.setLevel(logging.INFO) handler.setFormatter(formatter) console = logging.StreamHandler() console.setLevel(logging.INFO) console.setFormatter(formatter) # 输出到屏幕 logger.addHandler(console) logger.addHandler(handler) class FindTag(object): def __init__(self, tag, name): self.name = name # self.log_path = '.' self.log_path = 'D:log4j' # # 待修改项 self.log_name = 'cads.log' # 待修改项 self.logfile = os.path.join(self.log_path, self.log_name) self.tag = tag self.start_time = time.time() def find(self, timer): while time.time() < self.start_time + timer: self.f = open(self.logfile, 'r') for line in tailer.tail(self.f, 2): try: tag_string = re.search(self.tag, line).group() self.tag_string = tag_string logger.info(tag_string) self.tag_time_stamp = int(re.search('d+', self.tag_string).group()) logger.info('找到了%s' % self.name) self.f.close() return True except Exception as e: pass if __name__ == '__main__': start_tag = 'START_SYN_FOC_{d+}' end_tag = 'END_SYN_FOC_{d+}' while True: zabbix_sender = 'c:zabbix\binwin32zabbix_sender.exe -c c:zabbixconfzabbix_agentd.win.conf -z 10.88.22.8 -p 10051 -k GHS.TIMER.MONTIOR -o ' obj_find_start = FindTag(tag=start_tag, name='start标识符') obj_find_end = FindTag(tag=end_tag, name='end标识符') start_time = time.ctime() # 设置查找start的超时时间 if obj_find_start.find(timer=1800): # 设置查找end的超时时间 if obj_find_end.find(timer=300): logger.info('定时器执行成功 执行耗时%s秒' % ((obj_find_end.tag_time_stamp - obj_find_start.tag_time_stamp) / 1000)) logger.info(zabbix_sender + '0') subprocess.Popen(zabbix_sender + '0', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: end_time = time.ctime() logger.error('查找结束标识超时 开始时间%s - 结束时间%s' % (start_time, end_time)) logger.info(zabbix_sender + '1') subprocess.Popen(zabbix_sender + '1', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) else: find_start_end_time = time.ctime() logger.error('查找开始标识超时 开始时间%s - 结束时间%s' % (start_time, find_start_end_time)) logger.info(zabbix_sender + '2') subprocess.Popen(zabbix_sender + '2', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)