0基础上手python3编程,通用Mysql数据库字段监控,企业微信告警

0基础上手python3编程,通用Mysql数据库字段监控,企业微信告警

王忘杰
2023-07-10 / 0 评论 / 162 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2023年07月10日,已超过313天没有更新,若内容或图片失效,请留言反馈。

架构图
通过redis缓存mysql数据库字段进行对比,当发现内容变动时,进行企业微信告警
ljwluq0t.png

预览
ljwltbm8.png

系统组成
由两部分组成 check.py和redis数据库,从mysql中读取指定字段存储到redis中

代码

import requests,redis
import mysql.connector
def post_weixin(data):

    url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=用自己的'
    body = {
        "msgtype": "news",
        "news": {
            "articles": [
                {
                    "title": "卡号监控机器人",
                    "description": data,
                    "url": "90apt.com",
                    "picurl": "https://www.zkteco.com/cn/uploads/image/20210301/3e1adaa2dce94812e658c5d42afc1525.png"
                }
            ]
        }
    }
    headers = {"Content-Type": "application/json"}
    response = requests.post(url, json=body)
    print(response.text)
    print(response.status_code)

def sqlread():
    mqdb = mysql.connector.connect(
        host="127.0.0.1",
        user="admin",
        passwd="password",
        database="mysqldata"
    )
    mqcursor = mqdb.cursor()
    getconfig_sql = "SELECT number,card,name FROM userinfo where ifnull(name, '') <> ''"
    mqcursor.execute(getconfig_sql)
    mqconfig = mqcursor.fetchall()
    global cardchange
    cardchange = ""
    readredis = redis.Redis(connection_pool=redis.ConnectionPool(host="127.0.0.1", port="6379", password="password",decode_responses=True))
    for i in mqconfig:
        if readredis.get(i[0]) == str(i[1]):
            None
        else:
            cardchange = cardchange + i[2]+readredis.get(i[0])+"变为"+str(i[1])+"\n"
            readredis.set(i[0], str(i[1]))
sqlread()
post_weixin(cardchange)

总结
简单

3

评论

博主关闭了所有页面的评论