Zabbix monitors the Master/Slave status of MySQL (Python script)

Source: Internet
Author: User

1. Place the check script on the monitored machine (Mysql Slave:

/Usr/local/zabbix/bin/check_mysql_repl.py

  1. #! /Usr/bin/python
  2. # Coding: UTF-8
  3. Import MySQLdb
  4. Import sys
  5. Class check_mysql_repl ():
  6. Def _ init _ (self ):
  7. Self. dbhost = 'localhost'
  8. Self. dbuser = 'root'
  9. Self. dbpass = 'wisp888'
  10. Self. dbport = 3306
  11. Self. sock = "/data/db_misc/mysql_3306.sock"
  12. Self. conn = MySQLdb. connect (unix_socket = self. sock) # connect based on the actual situation
  13. Self. cursor = self. conn. cursor (cursorclass = MySQLdb. cursors. DictCursor)
  14. Self. SQL = 'show slave status'
  15. Self.cursor.exe cute (self. SQL)
  16. Self. data = self. cursor. fetchall ()
  17. Self. io = self. data [0] ['slave _ io_running']
  18. Self. SQL = self. data [0] ['slave _ SQL _running']
  19. Self. conn. close ()
  20. Def get_io_status (self ):
  21. Ifself. io = 'yes ':
  22. Return1
  23. Else:
  24. Return0
  25. Def get_ SQL _status (self ):
  26. Ifself. io = 'yes ':
  27. Return1
  28. Else:
  29. Return0
  30. If _ name _ = "_ main __":
  31. If len (sys. argv )! = 2:
  32. Print "Usage: % s [io | SQL]" % sys. argv [0]
  33. Sys. exit (1)
  34. Mysql = check_mysql_repl ()
  35. If sys. argv [1] = "io ":
  36. Print mysql. get_io_status ()
  37. Elif sys. argv [1] = "SQL ":
  38. Print mysql. get_ SQL _status ()

2. Configure zabbix_agent.conf and add the Key and monitoring script:

Vi/usr/local/zabbix/etc/zabbix_agentd.conf. Add the following two lines at the end of the file:

UserParameter = mysql. repl_io,/usr/local/zabbix/bin/check_mysql_repl.py io UserParameter = mysql. repl_ SQL,/usr/local/zabbix/bin/check_mysql_repl.py SQL

UserParameter is fixed

Mysql. repl_io is the custom Key, which is used when the metric item is added below

/Usr/local/zabbix/bin/check_mysql_repl.py io is the above script and Parameter

Restart zabbix_agentd

3. Add metric items:

4. Add a trigger:

This trigger is used to trigger an alarm if the returned value of the script is not 1.

This is a simple master-slave monitoring configuration !~~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.