钉钉监控代码
import pymysqlimport timeimport requests‘’’kpi小姐姐接口“https://oapi.dingtalk.com/robot/send?access_token=8daebe660297f090e6839b6a4454ff05382b59f3d515b3d7c14bc07f5fb642dd”‘’’class Moniter:def __in...
·
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条内容
所有评论(0)