【python 监控报警】错误日志监控并钉钉报警

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u013421629/article/details/86243001

将钉钉报警服务做出接口之后,对程序进行错误日志监控,一旦产出错误日志,立马报警出来。

# -*- encoding=utf-8 -*-
import requests
import os

"""
报警配置模板
"""
host_url="此次填写钉钉报警接口---"
text="DT-IMG-2:text_anti_spam_dongli出现错误日志@东篱"
url=host_url+text



# 检查是否出现error日志,若出现则钉钉报警出来~

# 先找到最新的error日志文件名
cmd1 = "ls /home/admin/anti_spam/text_anti_spam_dongli/log/ -l | grep error | tail -n 1 | awk '{print $9}'"
res1 = os.popen(cmd1)
res1_1=res1.read()

cmd2="tail /home/admin/anti_spam/text_anti_spam_dongli/log/%s" %res1_1
res2=os.popen(cmd2)
res2_1=res2.read()



# 如果有错误日志就报警出来
if len(res2_1)>0:
    # 发送钉钉报警
    content=requests.get(url).text


res1.close()
res2.close()

猜你喜欢

转载自blog.csdn.net/u013421629/article/details/86243001