日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

python写mysql脚本_使用python写一个监控mysql的脚本,在zabbix web上加上模板

發布時間:2024/9/18 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python写mysql脚本_使用python写一个监控mysql的脚本,在zabbix web上加上模板 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用python寫一個監控mysql的腳本,在zabbix web上加上模板:

##先使用MySQLdb的接口關聯數據庫。

[root@cml python]# cat check_Mysql_custom.py

#!/usr/local/bin/python

'''author = chenmingle'''

'''Description:get mysql status'''

import os

import sys

try:

import MySQLdb as mysql

except Exception, e:

print e

print "pip install MySQL-python"

sys.exit(1)

con = mysql.connect(host='localhost',user='root',passwd='passwd',)

def processlist():

cur = con.cursor()

sql1 = 'show processlist'

a = str(cur.execute(sql1))

print a

def slave_status():

cur = con.cursor()

sql2 = cur.execute('show status like "%Slave_running%";')

status2 = str(cur.fetchall())

check2 = status2.split("'")[3]

if check2 == 'ON':

print 0

else:

print 1

def show_status(type):

cur = con.cursor()

b = cur.execute('show status like "%s";' %(type))

for i in cur.fetchall():

cat = str(i)

check = str(cat.split("'")[3])

print check

def main(type):

if type == 'processlist':

processlist()

elif type == 'slave_status':

slave_status()

else:

show_status(type)

if __name__ == '__main__':

try:

type = sys.argv[1]

except Exception, e:

print "Usage: python %s type" % sys.argv[0]

sys.exit(1)

main(type)

假如執行測試時報以下錯誤:

[root@cml python]# python check_Mysql_custom.py slave_status

Traceback (most recent call last):

File "test.py", line 14, in

con = mysql.connect(user='zabbix',passwd='',port=3306)

File "/usr/local/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect

return Connection(*args, **kwargs)

File "/usr/local/lib/python2.7/site-packages/MySQLdb/connections.py", line 193, in __init__

super(Connection, self).__init__(*args, **kwargs2)

_mysql_exceptions.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)")

執行以下命令可以解決:

[root@cml python]# mkdir /var/lib/mysql

[root@cml python]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

##測試一下腳本:

[root@cml python]# python check_Mysql_custom.py processlist

3

[root@cml python]# python check_Mysql_custom.py Connections

55876

##測試顯示所有在mysql中show status的都可以輸出信息出來:

##寫一個模板配置文件(自定義key值):

[root@cml zabbix_agentd.d]# cat Mysql_Custom.conf

UserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1

##在zabbix web界面上加入模板:

##增加一個Appaliactions:

##加入item值:

總結

以上是生活随笔為你收集整理的python写mysql脚本_使用python写一个监控mysql的脚本,在zabbix web上加上模板的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。