python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼
統(tǒng)計(jì)每天的數(shù)據(jù)量變化,數(shù)據(jù)量變動超過一定范圍時,進(jìn)行告警。告警通過把對應(yīng)的參數(shù)傳遞至相應(yīng)接口。
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,',','當(dāng)前量:',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"} # 如有認(rèn)證信息,添加認(rèn)證信息即可,例如'Authorization': 'APP appid = xxx,token = xxxxxxxxxxxxxxxx'
response = requests.post(url, data = json.dumps(body), headers = headers) # body是json格式的,用 json.dumps(body)方式進(jìn)行處理
print response.text
print response.status_code
f_notify(v_cnt_now,v_cnt_1d,'數(shù)據(jù)量與前一天相比波動超過100%')
f_notify(v_cnt_now,v_cnt_1w,'數(shù)據(jù)量與前一周相比波動超過100%')
f_notify(v_cnt_now,v_cnt_1m,'數(shù)據(jù)量與前一月相比波動超過100%')
tar_conn.close()
以上就是python統(tǒng)計(jì)mysql數(shù)據(jù)量變化并調(diào)用接口告警的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于python統(tǒng)計(jì)mysql數(shù)據(jù)量變化的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python編程測試電腦開啟最大線程數(shù)實(shí)例代碼
這篇文章主要介紹了python編程測試電腦開啟最大線程數(shù)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02
Python Pandas批量讀取csv文件到dataframe的方法
這篇文章主要介紹了Python Pandas批量讀取csv文件到dataframe的方法,需要的朋友可以參考下2018-10-10
Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情
這篇文章主要介紹了Python數(shù)據(jù)分析應(yīng)用之Matplotlib數(shù)據(jù)可視化詳情,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06
python實(shí)現(xiàn)簡單的tcp 文件下載
這篇文章主要介紹了python如何實(shí)現(xiàn)簡單的tcp文件下載,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09

