ngx_err_monitor.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #! /usr/bin/python
  2. # coding: utf-8
  3. import datetime
  4. import time
  5. import subprocess
  6. #每分钟错误频次
  7. frequency = 5
  8. errlog = "/usr/local/openresty/nginx/logs/error.log"
  9. class err(object):
  10. '''
  11. 通过分析nginx错误日志,匹配关键字并统计一分钟内的错误数量
  12. 达到一定数量时调用代理脚本发送告警
  13. '''
  14. def __init__(self, errlog, frequency):
  15. self.counter = 0
  16. self.errlog = errlog
  17. self.frequency = frequency
  18. # 格式化时间
  19. def strftime(self):
  20. mytime = datetime.datetime.now()
  21. return mytime.strftime("%Y/%m/%d %H:%M:")
  22. # 在日志里搜索匹配当前时间一分钟内并含有example.domain.com的日志数量,修改计数
  23. def errcount(self):
  24. mytime = self.strftime()
  25. with open(self.errlog, 'r') as f:
  26. for line in f:
  27. if mytime in line and 'server: example.domain.com' in line:
  28. self.counter += 1
  29. def tg_sender(self):
  30. self.errcount()
  31. if self.counter >= self.frequency :
  32. text = "%s example.domain.com请求一分钟内超时>=%s次,当前次数为%s" % (datetime.datetime.now().strftime("%Y/%m/%d %H:%M:%S"), self.frequency, self.counter)
  33. cmd = '''/opt/tool/send_tg.sh '' '' "%s"''' % text
  34. p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  35. p.communicate()
  36. # 每10秒执行一次,当获取新的时间不等于上一次的时间,重置计数
  37. def run():
  38. logerr = err(errlog, frequency)
  39. lastmytime = ""
  40. while True:
  41. mytime = logerr.strftime()
  42. if lastmytime != mytime:
  43. logerr.counter = 0
  44. logerr.tg_sender()
  45. lastmytime = mytime
  46. time.sleep(10)
  47. if __name__ == "__main__":
  48. run()