#! /usr/bin/python
# coding: utf-8

import datetime
import time
import subprocess

#每分钟错误频次
frequency = 5
errlog = "/usr/local/openresty/nginx/logs/error.log"

class err(object):
 
    '''
    通过分析nginx错误日志，匹配关键字并统计一分钟内的错误数量
    达到一定数量时调用代理脚本发送告警
    '''

    def __init__(self, errlog, frequency):
        self.counter = 0
        self.errlog = errlog
        self.frequency = frequency
    
    # 格式化时间    
    def strftime(self):
        mytime = datetime.datetime.now()
        return mytime.strftime("%Y/%m/%d %H:%M:")
        
    # 在日志里搜索匹配当前时间一分钟内并含有example.domain.com的日志数量，修改计数
    def errcount(self):
        mytime = self.strftime()
        with open(self.errlog, 'r') as f:
            for line in f:
                if mytime in line and 'server: example.domain.com' in line:
                    self.counter += 1
        
    def tg_sender(self):
        self.errcount()
        if self.counter >= self.frequency :
            text = "%s example.domain.com请求一分钟内超时>=%s次，当前次数为%s" % (datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), self.frequency, self.counter)
            cmd = '''/opt/tool/send_tg.sh '' '' "%s"''' % text
            p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
            p.communicate()

# 每10秒执行一次，当获取新的时间不等于上一次的时间，重置计数
def run():
    logerr = err(errlog, frequency)
    lastmytime = ""
    while True:
        mytime = logerr.strftime()
        if lastmytime != mytime:
            logerr.counter = 0
        logerr.tg_sender()
        lastmytime = mytime
        time.sleep(10)

if __name__ == "__main__":
    run()
