当前位置: 移动技术网 > IT编程>脚本编程>Python > python统计mysql数据量变化并调用接口告警的示例代码

python统计mysql数据量变化并调用接口告警的示例代码

2020年09月22日  | 移动技术网IT编程  | 我要评论
统计每天的数据量变化,数据量变动超过一定范围时,进行告警。告警通过把对应的参数传递至相应接口。python程序如下#!/usr/bin/python# coding=utf-8import pymys

统计每天的数据量变化,数据量变动超过一定范围时,进行告警。告警通过把对应的参数传递至相应接口。

python程序如下

#!/usr/bin/python
# coding=utf-8
import pymysql as mdb
import os
import sys
import requests
import json


tar_conn = mdb.connect(host='192.168.56.128',port=3306,user='xxx',passwd='xxx123',db='bak_db')
tar_cur = tar_conn.cursor()

v_sql_dt =" select date_format(current_date(),'%y-%m-%d')t1 ,date_format(subdate(current_date(),interval 1 day),'%y-%m-%d')t2,date_format(subdate(current_date(),interval 1 week),'%y-%m-%d')t3,date_format(subdate(current_date(),interval 1 month),'%y-%m-%d %h:%i:00')t4"
v_extract_rows=tar_cur.execute(v_sql_dt)
v_res=tar_cur.fetchone()
v_dt1=v_res[0]
v_dt2=v_res[1]
v_dt3=v_res[2]
v_dt4=v_res[3]

print v_dt1,v_dt2,v_dt3,v_dt4
#v_start_time='2020-09-10'
#v_end_time='2020-09-11'
def get_cnt(v_dt):

 v_sql1="select tb_rows from bak_db.tb_size where dt='%s';"%(v_dt)

 v_extract_rows=tar_cur.execute(v_sql1)
 v_res=tar_cur.fetchone()
 v_cnt1=v_res[0]

 return(v_cnt1)

(v_cnt_now)=get_cnt(v_dt1)
(v_cnt_1d)=get_cnt(v_dt2)
(v_cnt_1w)=get_cnt(v_dt3)
(v_cnt_1m)=get_cnt(v_dt4)

def f_notify(v_cnt_now,v_cnt_before,v_message):
 v_rate1=abs(((v_cnt_before-v_cnt_now)*1.00/v_cnt_before*1.00)*100)

# print v_rate1,v_rate2
 if (v_rate1>100 ) and (v_cnt_now>500 or v_cnt_before>500) :
  v_level=1
  v_list=[v_message,',','当前量:',str(v_cnt_now),',','前期量:',str(v_cnt_before)]
  v_message1=''.join(v_list)
  print v_message1
  url = 'http://192.168.56.128:9000/api/v1/alarm'        # 接口地址
  body ={"level": v_level, "group": ["dba"], "msg": {"content": v_message1}} 
  headers = {'content-type': "application/json"}        # 如有认证信息,添加认证信息即可,例如'authorization': 'app appid = xxx,token = xxxxxxxxxxxxxxxx'
  response = requests.post(url, data = json.dumps(body), headers = headers) # body是json格式的,用 json.dumps(body)方式进行处理
  print response.text
  print response.status_code
   

f_notify(v_cnt_now,v_cnt_1d,'数据量与前一天相比波动超过100%')
f_notify(v_cnt_now,v_cnt_1w,'数据量与前一周相比波动超过100%')
f_notify(v_cnt_now,v_cnt_1m,'数据量与前一月相比波动超过100%')



tar_conn.close()

以上就是python统计mysql数据量变化并调用接口告警的示例代码的详细内容,更多关于python统计mysql数据量变化的资料请关注移动技术网其它相关文章!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网