钉钉监控代码

import pymysql
import time
import requests

‘’’
kpi小姐姐接口
https://oapi.dingtalk.com/robot/send?access_token=8daebe660297f090e6839b6a4454ff05382b59f3d515b3d7c14bc07f5fb642dd
‘’’

class Moniter:

def __init__(self):

    self.conn_mysql()
    self.dingding()

def conn_mysql(self):

    #创建数据库连接对象
    self.conn=pymysql.Connect(host='127.0.0.1',user='root',password='admin',database='02180530',charset='utf8')
    self.cur=self.conn.cursor()

def dingding(self):

    day_ago = time.time()-86400
    # day_ago = 1560319001
    #获取需要发送的数据。
    #定义查询出总数和更新数量sql语句。
    sql = """
        SELECT count(*) FROM lianjia
        UNION 
        SELECT count(*) FROM lianjia WHERE refresh_time > {}
        UNION 
        SELECT phone FROM source WHERE source = 'xiaofang'
        """.format(day_ago)
    #执行sql语句并获取sql语句查询出来的数据
    self.cur.execute(sql)
    self.conn.commit()
    #获取所有的数据
    data = self.cur.fetchall()
    # print(data)
    #提取数据获取总数和更新数。
    total_count = int(data[0][0])
    refresh_count = int(data[1][0])
    phone = int(data[2][0])
    print(total_count,refresh_count,phone)
    #生成刷新数量的百分比
    per_no_refresh = refresh_count / total_count
    if per_no_refresh > 0.8:
        per_no_refresh = '%.2f%%'%(per_no_refresh * 100)
        print(per_no_refresh)
        # 发送钉钉消息
        self.dingding2(phone,per_no_refresh)

def dingding2(self,phone,per_no_refresh):

    dd_api = "https://oapi.dingtalk.com/robot/send?access_token=8daebe660297f090e6839b6a4454ff05382b59f3d515b3d7c14bc07f5fb642dd"
    print(phone,per_no_refresh)
    dd_json = {
"msgtype": "text",
"text": {
    "content": "你的代码有问题,请及时解决,比例为{}".format(per_no_refresh)
},
"at": {
    "atMobiles": [
        phone,
        "18679030315"
    ],
    "isAtAll": False
}
}

requests.post(dd_api,json=dd_json)
print('send success')

def __del__(self):

    self.cur.close()
    self.conn.close()

if __name__ == '__main__':

Moniter()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Python中,你可以使用`websocket`库来建立与WebSocket接口的连接,并使用`requests`库来发送钉钉机器人的消息。下面是一个示例代码: ```python import websocket import requests import json import time # WebSocket接口URL websocket_url = 'wss://your_websocket_url' # 钉钉机器人Webhook地址 webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=your_access_token' def send_dingding_message(content): headers = {'Content-Type': 'application/json;charset=utf-8'} data = { "msgtype": "text", "text": { "content": content } } response = requests.post(webhook_url, headers=headers, data=json.dumps(data)) if response.status_code == 200: print("Dingding message sent successfully.") else: print("Failed to send Dingding message.") def on_message(ws, message): # 在此处编写接收到消息后的处理逻辑 # 判断接收到的消息是否符合预期,如果不符合则发送钉钉消息告警 if message != 'expected_message': send_dingding_message("WebSocket接口异常:接收到的消息不符合预期") def on_error(ws, error): # WebSocket出现错误时的处理逻辑 send_dingding_message(f"WebSocket接口异常:{error}") def on_close(ws): # WebSocket连接关闭时的处理逻辑 send_dingding_message("WebSocket接口连接已关闭") def on_open(ws): # WebSocket连接建立后的处理逻辑 send_dingding_message("WebSocket接口连接已建立") # 连接WebSocket接口 websocket.enableTrace(True) # 如果需要调试,打开此行注释 ws = websocket.WebSocketApp(websocket_url, on_message=on_message, on_error=on_error, on_close=on_close) ws.on_open = on_open ws.run_forever() ``` 以上代码示例中,`on_message`函数用于处理接收到的消息,你可以在该函数中编写逻辑来判断接收到的消息是否符合预期。如果不符合预期,可以调用`send_dingding_message`函数发送钉钉消息进行告警。`on_error`函数用于处理WebSocket连接出现错误的情况,`on_close`函数用于处理WebSocket连接关闭的情况,`on_open`函数用于处理WebSocket连接建立的情况。 在实际使用中,你需要将`your_websocket_url`替换为实际的WebSocket接口URL,将`your_access_token`替换为实际的钉钉机器人的access_token。 请注意,以上代码仅为示例,具体实现还需要根据你的实际需求进行适配和调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值