钉钉监控代码

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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值