python2.7.5监控php慢日志报警到钉钉机器人

一、在钉钉建立自定义机器人webhook
在这里插入图片描述
点击“复制”按钮,便可得到这个机器人对应的Webhook地址,其格式以下:
https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxphp

二、安装python依赖包python

pip install pyinotify
pip install requests

三、准备 py_inotify.py,监控php慢日志,若是慢日志有变更,将变更内容发送到钉钉web

# !/usr/bin/python
#coding:utf-8
import sys, os, pyinotify
import dingding
notifier = None
monfile = None
lastsize = 0
wm = None
wd = 0
def roll_file(filename):
    global lastsize
    fd = os.open(filename, os.O_RDONLY)
    try:
        newsize = os.fstat(fd).st_size
        if newsize <= lastsize: return
        os.lseek(fd, lastsize, os.SEEK_SET)
        while True:
            data = os.read(fd, 4096)
            if not data: break
            dingding.dingmessage(data)
        sys.stdout.flush()
        pos = os.lseek(fd, 0, os.SEEK_CUR)
        lastsize = pos if pos != lastsize else newsize
    finally:
        os.close(fd)
class EventHandler(pyinotify.ProcessEvent):
    def process_IN_CREATE(self, event):
        if monfile == event.pathname:
            global wd
            wd = wm.add_watch(monfile, pyinotify.IN_MODIFY).values()[0]
            roll_file(monfile)
    def process_IN_DELETE(self, event):
        global wd, lastsize
        if monfile == event.pathname:
            if wd > 0:
                try:
                    wm.rm_watch(wd, quiet=False)
                except pyinotify.WatchManagerError:
                    pass
                wd = 0
            lastsize = 0
    def process_IN_MOVED_FROM(self, event):
        self.process_IN_DELETE(event)
    def process_IN_MOVED_TO(self, event):
        self.process_IN_DELETE(event)
        self.process_IN_CREATE(event)
    def process_IN_MODIFY(self, event):
        roll_file(monfile)
def main():
    global notifier, lastsize, wm, wd, monfile
    monfile = "a.txt"
    #print "path={0}".format(monfile)
    lastsize = os.stat(monfile).st_size
    wm = pyinotify.WatchManager()
    notifier = pyinotify.Notifier(wm, EventHandler())
    wd = wm.add_watch(monfile, pyinotify.IN_MODIFY).values()[0]
    wm.add_watch(os.path.dirname(monfile),
                 pyinotify.IN_DELETE | pyinotify.IN_CREATE | pyinotify.IN_MOVED_FROM | pyinotify.IN_MOVED_TO)
    #print "watching {0} ...".format(monfile)
    while True:
        notifier.process_events()
        if notifier.check_events():
            notifier.read_events()
if __name__ == "__main__":
    try:
        main()
    finally:
        if notifier: notifier.stop()

四、在py_inotify.py同级目录建立dingding.pyjson

#coding:utf-8
import requests
import json
def dingmessage(message):
# 请求的URL,WebHook地址
    webhook = "https://oapi.dingtalk.com/robot/send?access_token=xxxxxx"
#构建请求头部
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
}
#构建请求数据
    tex = message
    message ={
        "msgtype": "text",
        "text": {
            "content": tex
        },
        "at": {
            "isAtAll": True
        }
    }
#对请求的数据进行json封装
    message_json = json.dumps(message)
#发送请求
    info = requests.post(url=webhook,data=message_json,headers=header)
#打印返回的结果
    print(info.text)

五、测试将文件变更内容发送到钉钉机器人:
python py_inotify.py
钉钉返回结果:
{“errcode”:0,“errmsg”:“ok”}
打开另外一个终端:
echo “test dingding” >> a.txt
钉钉接受到消息通知:
在这里插入图片描述api