统计每天的数据量变化,数据量变动超过一定范围时,进行告警。告警通过把对应的参数传递至相应接口。
python程序如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/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数据量变化的资料请关注服务器之家其它相关文章!
原文链接:https://www.cnblogs.com/gjc592/p/13673429.html