MySQL system running status real-time monitoring (python version)

In yesterday's article, I wrote a simple template for real-time monitoring of MySQL system operating status using shell, "Real-time monitoring of MySQL system operating status (shell version) ", for this operation, any language can be completed, and today I will use python to write Please correct me if it is not elegantly written.


First of all, in order for python to connect to the MySQL database, some third-party libraries are needed. Since I am using the python 2.3 version, I can use mysqldb, (if python 3.x, I can use PyMySQL), which can be downloaded and compressed from the following link , the latest version is 1.2.5,

https://pypi.python.org/pypi/MySQL-python/1.2.3



If you are not sure whether the machine is installed, you can use,

python

>> import MySQLdb


See if an error is reported, if prompted,

ImportError: No module named MySQLdb

It means it is not installed.


Unzip MySQL-python-1.2.3.tar.gz,



Enter the directory and execute the following command to complete the installation of mysqldb,

python setup.py install


Next, start coding, first define an enumeration class to facilitate constant calls, here are five state parameters,

def enum(**enums):

return type('Enum', (), enums)


Status=enum(QPS="queries", Commit="com_commit",

Rollback="com_rollback", Threads_con="Threads_connected",

Threads_run="Threads_running")


Python connects to the database, which is indeed simpler than languages ​​such as java.

dbConn=MySQLdb.connect(

host='x.x.x.x',

port=3306,

user='bisal',

passwd='xxxxx',

db='mysql')

cursor=dbConn.cursor()


比如我要检索QPS这个参数,执行以下SQL,由于是肯定只返回一条数据,所以用了fetchone()函数,为了只要返回值,使用str[a:b]进行了字符串截取。

sql='show global status like \'' + Status.QPS + '\''

cursor.execute(sql)

result=cursor.fetchone()

str= ''.join(result)

q=str[len(Status.QPS):len(str)]


接下来就可以格式化打印,同样输出10次,重新打印表头,显示sleep一秒,

if (count==0):

print "|QPS        |Commit     |Rollback   |TPS        |Threads_con  |Threads_run |"

print "------------------------------------------------------------------------------"

if (count>=10):

count=0

print "------------------------------------------------------------------------------"

print "|QPS        |Commit     |Rollback   |TPS        |Threads_con  |Threads_run |"

print "------------------------------------------------------------------------------"

print "|%-10s |%-10s |%-10s |%-10s |%-12s |%-12s|" % (q,c,r,c+r,tc,tr)

else:

print "|%-10s |%-10s |%-10s |%-10s |%-12s |%-12s|" % (q,c,r,c+r,tc,tr)

count+=1

time.sleep(1)


另外,记得需要关闭数据库连接,

cursor.close()

dbConn.close()


整个流程,其实很简单,就是执行show status语句,进行一些字符串处理,格式化输出,以上完整代码,可以下载:

https://github.com/bisal-liu/mysql/blob/master/mysql_per_monitor_1.py


要说可以优化,就是上面这种方法中,对于每一个状态参数,都要执行一次show status,有些浪费,可以一次执行,多次解析,使用IN子句,实现执行一次SQL,返回不同参数,

sql='show global status where variable_name in (\'' + Status.QPS + '\',\'' + Status.Commit \

+ '\',\'' + Status.Rollback + '\',\'' + Status.Threads_con + '\',\'' + Status.Threads_run + '\')'


由于返回不止一条记录,因此需要使用for,if中根据字符串做匹配,以下写法中,

1. 若使用了''.join(line),则需要使用q=k1[len(Status.QPS):len(k1)]截取字符串。

2. 若使用了str(line),则需要使用q=k2.split('\'')[3]截取字符串。

for line in result:

k1=''.join(line).lower()

k2=str(line).lower()


if (Status.QPS in k1):

q=k1[len(Status.QPS):len(k1)]

elif (Status.Commit in k1):

c=k1[len(Status.Commit):len(k1)]

elif (Status.Rollback in k1):

r=k1[len(Status.Rollback):len(k1)]

elif (Status.Threads_con in k1):

tc=k1[len(Status.Threads_con):len(k1)]

elif (Status.Threads_run in k1):

tr=k1[len(Status.Threads_run):len(k1)]


if (Status.QPS in k2):

q=k2.split('\'')[3]

elif (Status.Commit in k2):

c=k2.split('\'')[3]

elif (Status.Rollback in k2):

r=k2.split('\'')[3]

elif (Status.Threads_con in k2):

tc=k2.split('\'')[3]

elif (Status.Threads_run in k2):

tr=k2.split('\'')[3]


以上完整代码,可以下载,

https://github.com/bisal-liu/mysql/blob/master/mysql_per_monitor_2.py


以上两种写法,效果一样,

1. 每隔1秒,刷新一次,

2. 每隔10次,重新打印表头,



如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325642293&siteId=291194637