使用python寫一個監控mysql的指令碼,在zabbix web上自訂模板

來源:互聯網
上載者:User

標籤:python指令碼自訂zabbix監控模

##先使用MySQLdb的介面關聯資料庫。

[[email protected] python]# cat check_Mysql_custom.py#!/usr/local/bin/python'''author = chenmingle''''''Description:get mysql status'''import osimport systry:    import MySQLdb as mysqlexcept 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 adef 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 1def 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 checkdef 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)



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

[[email protected] python]# python check_Mysql_custom.py slave_status

Traceback (most recent call last):

  File "test.py", line 14, in <module>

    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)")


[[email protected] python]# mkdir /var/lib/mysql[[email protected] python]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock




##測試一下指令碼:

[[email protected] python]# python check_Mysql_custom.py processlist3[[email protected] python]# python check_Mysql_custom.py Connections55876

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



##寫一個模板設定檔(自訂key值):

[[email protected] zabbix_agentd.d]# cat Mysql_Custom.confUserParameter=mysql.Custom[*],/usr/local/bin/python /home/python/check_Mysql_custom.py $1




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

##增加一個Appaliactions:


##加入item值: 

使用python寫一個監控mysql的指令碼,在zabbix web上自訂模板

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.