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
63
64
65
66
67
68
69
70
71
72
73
# coding:utf-8
import psycopg2
import pymysql
from dingtalkchatbot.chatbot import DingtalkChatbot
DATABASES = {
'slave': { # mysql
'ENGINE': 'django.db.backends.mysql',
'NAME': 'zhengxing',
'USER': 'zhengxing',
'PASSWORD': 'ZX0*n4vgtdN%t1VE',
'HOST': '172.16.30.141',
'PORT': '3306',
'OPTIONS': {
"init_command": "SET foreign_key_checks = 0;",
"charset": "utf8mb4",
},
},
}
def main():
slave = DATABASES["slave"]
my_db = pymysql.connect(host=slave["HOST"], user=slave["USER"], passwd=slave["PASSWORD"], db=slave["NAME"])
pg_db = psycopg2.connect(host="172.16.50.153", user="brain_worker", password="zbVas87dtLhY", database="brain")
my_cursor = my_db.cursor()
pg_cursor = pg_db.cursor()
zhengxing_table_list = ["api_city", "api_province", "auth_user", "api_userextra", "api_userblacklist",
"api_userextratocollege",
"api_doctor", "api_person", "social_userfollow", "api_userinnerinfo", "api_hospital",
"statistic_device",
"statistic_device_user", "api_bdtransfer"]
msg = "数据同步监控, 有{}张表在同步\n".format(len(zhengxing_table_list))
delay_table_num = 0
for table in zhengxing_table_list:
sql = "select count(*) from %s" % table
pg_sql = "select count(*) from zhengxing.%s" % table
try:
my_cursor.execute(sql)
my_num = my_cursor.fetchone()[0]
except:
my_num = 0
try:
pg_cursor.execute(pg_sql)
pg_num = pg_cursor.fetchone()[0]
except:
pg_num = 0
print(table, my_num, pg_num)
if my_num > pg_num:
delay_table_num += 1
msg += "zhengxing.{}延迟的数据量为{}条记录, \nmysql: {}, \npgsql: {};\n".format(table, my_num - pg_num, my_num, str(pg_num))
if delay_table_num > 0:
msg += "有{}张表存在延迟".format(delay_table_num)
webhook = 'https://oapi.dingtalk.com/robot/send?access_token={}'.format(
"1475fb9e6beff63126fef5464a378b6b77fa8655933a407d8159a2a2b1b8c869")
xiaoding = DingtalkChatbot(webhook)
# at_mobiles = ["17794411132", "15624986543"]
at_mobiles = []
xiaoding.send_text(msg=msg, at_mobiles=at_mobiles)
if __name__ == '__main__':
main()