import time import os import logging import pymysql import requests import json import multiprocessing api = 'https://zoom-api.cg.app/zoom/robotSendMsg' headers = {'Content-type': 'application/json', 'Accept': 'text/plain', 'apiKey':'4sdW06G6EbuahmPioVmr12'} addr="" user="" passwd="" date=time.strftime("%Y%m%d%H%M%S", time.localtime()) now=time.strftime("%Y%m%d %H:%M:%S", time.localtime()) print(date) def read_site_config(): # 读取站点配置文档,返回站点列表 site_list = [] with open("/home/tidb/scripts/analyze_stats/site_config.txt", "r") as file: for line in file: site = line.strip() site_list.append(site) return site_list def kill_process(process): # 终止指定进程 logging.info("Terminating make_analyze process: %s" % process.pid) process.terminate() def db_conn(addr, user, passwd, db): # 数据库连接 mydb = pymysql.connect( host=addr, port=4000, user=user, passwd=passwd, db=db ) return mydb def webhook(msg): # 发送成功信息到 zoom send_data = { "to_jid": "2d40f3b5b708471185197c14585ea7cf@conference.xmpp.zoom.us", "type": "message", "msg": msg } try: r = requests.post(url=api, headers=headers, json=send_data) if r.status_code == 200: print("send success") except Exception as e: print(e) print("bot send message err") def get_healthy_info(db): # 获取健康信息 conn = db_conn(addr, user, passwd, "information_schema") cursor = conn.cursor() cursor.execute("set session tidb_multi_statement_mode='on';") cursor.execute("set session max_execution_time=300000;") db_sql = "SHOW STATS_HEALTHY where db_name= '%s' and db_name != 'dh_app_default';" % (db) cursor.execute(db_sql) row_all = cursor.fetchall() conn.commit() cursor.close() conn.close() return row_all def analyze(dh_db, table, healthy_stat): # 执行统计信息收集 while True: conn = db_conn(addr, user, passwd, "information_schema") cursor = conn.cursor() db_sql = "select count(1) from PROCESSLIST where info like 'analyze%' limit 1;" cursor.execute(db_sql) row_all = cursor.fetchall() if row_all[0][0] <= 3: break elif row_all[0][0] > 3: cursor.close() conn.close() time.sleep(5) continue cursor.execute("set session tidb_multi_statement_mode='on';") cursor.execute("set session tidb_build_stats_concurrency=30;") cursor.execute("set session tidb_index_serial_scan_concurrency=10;") cursor.execute("set session tidb_distsql_scan_concurrency=30;") cursor.execute("show variables like 'tidb_build_stats_concurrency';") analyze_sql = "analyze table %s.%s with 256 buckets, 500 topn;" % (dh_db, table) start_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) cursor.execute(analyze_sql) end_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) logging.debug("%s-%s less %s" % (dh_db, table, healthy_stat)) logging.info("开始时间: %s %s-%s healthy is %s " % (start_time, dh_db, table, healthy_stat)) logging.info("结束时间: %s %s %s is finished\n" % (end_time,dh_db,table)) cursor.close() conn.close() def make_analyze(dh_db,healthy_info,table_list): logging.debug("%s is start" % (dh_db)) healthy_table_list=[] healthy_table_name_list=[] for healthy in healthy_info: if dh_db==healthy[0]: healthy_table_list.append(healthy) healthy_table_name_list.append(healthy[1]) for table in table_list: if table not in healthy_table_name_list: analyze(dh_db,table,'NULL') else: for healthy_table in healthy_table_list: if table==healthy_table[1] and healthy_table[3]<80: analyze(dh_db,table,healthy_table[3]) def start_process(site,table_list): # 启动一个进程,对指定站点运行 make_analyze logging.info("Starting make_analyze process for site: %s" % site) healthy_info = get_healthy_info(site) make_analyze(site, healthy_info, table_list) if __name__ == "__main__": mlog="/home/tidb/scripts/analyze_stats/analyze_logs/analyze_important_mprocess_less80_%s.log" % (date) logging.basicConfig(filename=mlog, level=logging.DEBUG) print(mlog) while True: now = time.strftime("%Y%m%d %H:%M:%S", time.localtime()) logging.info("Script start time: %s\n" % now) print('Parent process %s.' % os.getpid()) table_list=[] for table in open("/home/tidb/scripts/analyze_stats/frequently_table.txt"): table=table.strip("\n").strip() table_list.append(table) # 读取站点配置文档 site_list = read_site_config() print("Site list:", site_list) # 检查新站点加入 processes = [] for site in site_list: # 启动新站点的 make_analyze 进程 p = multiprocessing.Process(target=start_process, args=(site,table_list,)) print(p) processes.append(p) p.start() logging.info("Started make_analyze process for new site: %s" % site) for p in processes: p.join() print('Waiting for all subprocesses to finish...') logging.info("脚本结束时间 %s\n" % (time.strftime("%Y%m%d %H:%M:%S", time.localtime()))) end_time=time.strftime("%Y%m%d %H:%M:%S", time.localtime()) msg="""北美重要库重要表健康度小于80的统计信息收集,开始时间: %s 结束时间: %s""" % (now,end_time) logging.info("脚本执行信息 %s\n" % (msg)) time.sleep(300) # 等待5分钟