■ Pymysql 설치
yum -y install epel-release
yum -y install python2 python2-pip
pip2 install --upgrade pip==20.3
pip2 install pymysql
■ Log를 담을 Table 생성
- AS-IS에서 Data를 가져와 TO-BE ( LOG DB ) 에 데이터를 적재 할 예정이다.
create database gm_state;
mysql> create table state(date datetime,state varchar(100),cnt int,avg int);
■ Python 코드
- 나의 경우에는 AS-IS의 thread들의 상태를 로그에 넣기 위해서 아래와 같이 하였다.
[root@gm-git01 ~]# cat gm.py
#!/bin/python2
import pymysql
import sys
import os
#### SELECT STATE
db=pymysql.connect(
user='repl',
password='repl',
host='192.168.100.54',
db='information_schema',
port=3306
)
cursor = db.cursor()
sql = "select state,substr(info,1,70),count(*),avg(time) from information_schema.processlist where state not in ('InnoDB purge worker','Master has sent all binlog to slave; waiting for binlog to be up','InnoDB purge coordinator','InnoDB shutdown handler','NULL','') group by 1,2 order by 3 desc;"
cursor.execute(sql)
result=cursor.fetchall()
db.close
### INSERT STATE
zabbix=pymysql.connect(
user='root',
password='root',
host='localhost',
db='gm_state',
port=3306
)
cursor = zabbix.cursor()
sql = "insert into state(date,state,cnt,avg) values(now(),%s,%s,%s)"
for (a,b,c) in result:
cursor.execute(sql,(a,b,c))
zabbix.commit()
zabbix.close()