#!/usr/bin/python# -*-coding:-utf-8-*-import timeimport re#定义时间格式date_now = time.strftime('%Y-%m-%d
#!/usr/bin/python# -*-coding:-utf-8-*-import timeimport re#定义时间格式date_now = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time()))date1 = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - 60 * 5))date2 = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - 60 * 4))date3 = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - 60 * 3))date4 = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - 60 * 2))date5 = time.strftime('%Y-%m-%d %H:%M', time.localtime(time.time() - 60 * 1))date_strings = [date1, date2, date3, date4, date5]#计数变量num = 0#写日志def log_out(logfile, content): with open(logfile, 'a') as f: f.write(content)#统计方法def test(file, date, keyword): number = 0 with open(file, "r") as filename: for line in filename: match = re.search(date, line) if match is None: continue match = re.search(keyword, line) if match is None: continue number += 1 return number#开始执行for date_keyword in date_strings: num = num + test("/data1/logs/interface/kafka_produce_41.log", date_keyword, "Kafka异常") num = num + test("/data1/logs/interface/kafka_produce_42.log", date_keyword, "Kafka异常")#判断结果if num > 50: log_out("/tmp/test.txt", date_now + " fail:kafka-produce响应异常/n")else: log_out("/tmp/test.txt", date_now + " success/n")