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
|
def ExecutePtkillCmd(**info): try: ptkillCmd = "/opt/soft/percona-toolkit-2.2.14/bin/pt-kill --no-version-check " \ "--host {rsHost} --port {rsPort} --user 'dba' --password '5d63f33c10b8f430'" \ " --busy-time 2 --match-state='Sending data|Sorting result' --victim all " \ "--interval 1 --run-time 60 --daemonize --kill --print --log={ptkillLogFile}".format(**info) status, ret = executeSSH('10.148.16.25', ptkillCmd) if status == 0: msg = "Execute {rsHost}:{rsPort} pt-kill command success.".format(**info) return True, msg else: msg = "Execute {rsHost}:{rsPort} pt-kill command failed, Cmd:".format(**info), ptkillCmd return False, msg except Exception as err: msg = "Execute {rsHost}:{rsPort} pt-kill command error.".format(**info), err return False, msg
def GetFileRegixCount(**info): try: logPwd = info['ptkillLogFile'] process = Popen(['grep', '^# [0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}', logPwd], stdout=PIPE) info['slowSQLCount'] = len((process.stdout).readlines()) msg = "Get {rsHost}:{rsPort} slow log count success.".format(**info) return True, info except Exception as err: msg = "Get {rsHost}:{rsPort} slow log count err:".format(**info), err log.error(msg) return False, msg
{'rsHost': '1.1.1.1', 'rsPort': 3306, 'rsRole': 'master', 'logFile': 'm_3306_11_20200810084828_slow.log', 'ptkillLogFile': '/path/SlowLog/m_3306_11_20200810084828_slow.log', 'slowSQLCount': 0}
from time import sleep def killSlowSql(info): try: status, msg = ExecutePtkillCmd(**info) if status is False: return status, msg sleep(60) return GetFileRegixCount(**info) except Exception as err: msg = "kill {rsHost}:{rsPort} {ptkillLogFile} err:".format(**info), err return False, msg
from multiprocessing import Pool def main(*HostInfoList): try: pool = Pool(8) res_l = [] infoList = [] for info in HostInfoList: base = pool.apply_async(killSlowSql, (info, )) res_l.append(base) pool.close() pool.join() for res in res_l: ret = res.get() infoList.append(ret[1]) return True, infoList except Exception as err: return False, err
|