Zabbix automatically discovers and monitors MySQL_MySQL

Source: Internet
Author: User
Zabbix automatically discovers and monitors MySQL I. solution requirements and ideas

Because there are many servers running the MySQL service, and each server may run multiple databases with different ports, if you manually modify the agent one by one. conf, web add monitoring, which is too troublesome and time-consuming. There are two solutions. one is to deploy an automatic detection script on each server running mysql and modify the agent. conf and custom KEY. second, a single server is used to monitor all running mysql services on all servers. It seems that solution 2 is easier.

Solution 2:

Assume that there are five servers, namely A, B, C, D, and E. mysql is run on them, and two or more mysql instances are run on each server, assume that server A monitors the status of mysql on all servers. In this case, you need to record the IP addresses of the five servers and the port occupied by the mysql service on them, and write them into a file: eg:

  1. 192.168.12.14 3306
  2. 192.168.12.14 3307
  3. 192.168.12.15 3308
  4. 192.168.12.15 3380
  5. ......

Put the file to a directory under the zabbix Directory, grant permissions, and write a json script to retrieve the file, in addition, when executing the script file, you need to pass the IP address value and output all the ports on the IP address.

Add a custom key. Define an automatically discovered key, and then define a key that queries the mysql status of all ports on the server based on the IP address. Restart zabbix_agentd and use zabbix_get to obtain the parameters to check whether they are normal.

Configure the web interface. On the web configuration page, create a host. the ip address of the HOST points to the IP address of server A, and switch the [HOST. NAME] points to the IP address of the mysql to be monitored, ([HOST. NAME] generally refers to "Visable name"). the server that monitors mysql points to a group and then calls the module to automatically discover the mysql template. Then you can create the remaining servers.

This solution is not a good solution, but it is relatively good. if a new mysql server is added in the future, you only need to modify the list file with IP addresses and ports, you do not need to restart the agent.

2. configure the Agent:

1. edit the mysqld file

  1. # Cat/usr/local/zabbix/bin/mysqld
  2. 172.16.8.250 3306
  3. 172.16.8.250 3309
  4. 172.16.8.252 3306
  5. 172.16.8.252 3307

Note: This article implements monitoring of databases on 8.250 and local databases on 252.

2. grant permissions to mysql

Each of the four databases is assigned to the 172.16.8.250 host with the zabbix, 111 password, and show global status permission.

3. edit discovery_mysql.sh.

  1. # Cat/usr/local/zabbix/bin/discovery_mysql.sh
  2. #! /Bin/bash
  3. # Mysql low-level discovery
  4. # Script_name discovery_mysql.sh
  5. Res = 'cat/usr/local/zabbix/bin/mysqld | grep $1 | grep-v "^ #" | awk '{print $2 }''
  6. Port = ($ res)
  7. Printf' {/N'
  8. Printf'/t "data": [/N'
  9. For key in $ {! Port [@]}
  10. Do
  11. If [["$ {# port [@]}"-gt 1 & "$ {key}"-ne "$ ($ {# port [@]}-1 )) "]; then
  12. Printf'/t {/N'
  13. Printf "/t/" {# MYSQLPORT}/":/" $ {port [$ {key}]}/"},/n"
  14. Else [["$ {key}"-eq "($ {# port [@]}-1)"]
  15. Printf'/t {/N'
  16. Printf "/t/" {# MYSQLPORT}/":/" $ {port [$ {key}]}/"}/n"
  17. Fi
  18. Done
  19. Printf'/t]/N'
  20. Printf'}/N'

Grant execution permission:

  1. # Chmod + x/usr/local/zabbix/bin/discovery_mysql.sh

Test execution result:

  1. # Sh/usr/local/zabbix/bin/discovery_mysql.sh 172.16.8.250
  2. {
  3. "Data ":[
  4. {
  5. "{# MYSQLPORT}": "3306 "},
  6. {
  7. "{# MYSQLPORT}": "3309 "}
  8. ]
  9. }

Paste the above results to the json detection site to see if the results are correct http://jsonlint.com/

4. modify agentd. conf

Enable include

  1. Include =/usr/local/zabbix/etc/zabbix_agentd/

Add mysql automatic discovery rules and customize keys

  1. # Cat/usr/local/zabbix/etc/zabbix_agentd/mysql_discovery.conf
  2. #### $1 = IP $2 = PORT $3 = COMMAND
  3. UserParameter = zabbix_low_discovery [*],/bin/bash/usr/local/zabbix/bin/discovery. sh $1
  4. UserParameter = mysql_stats [*], mysql-uzabbix-p111-P $2-h $1-e "show global status" | grep "/<$3/>" | cut-f2
  5. UserParameter = mysql. alive [*], mysqladmin-uzabbix-p111-h $1-P $2 ping | grep-c alive

5. test:

Restart zabbix_agentd and use zabbix_get on zabbix server to test whether the zabbix mysql automatically finds that the rule is correct.

Note: 172.16.8.250 is the zabbix server and the agent address is 127.0.0.1. modify the following command as needed.

  1. # Zabbix_get-s 127.0.0.1-k zabbix_log_discovery [172.16.8.250]
  2. {
  3. "Data ":[
  4. {
  5. "{# MYSQLPORT}": "3306 "},
  6. {
  7. "{# MYSQLPORT}": "3309 "}
  8. ]
  9. }
  10. # Zabbix_get-s 127.0.0.1-k mysql_stats [172.16.8.250, 3306, Uptime]

Data can be obtained correctly,

Reprinted please indicate the original source: http://www.minunix.com/2014/04/zabbix_low_discovery_mysql_01/

3. add hosts to Zabbix WEB

1. import the template discovery_mysql.xml

2. add hosts:

Link template:

Note: Because the template definition update time is 3600 s, if you need to quickly see the effect, you can first modify the "Interval" update time of the template custom rule to 60, then you can see the data in "last data.

Monitor 172.16.8.252. you only need to create a host and change the value of visable name 172.16.8.250 to 172.16.8.252.

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.