Zabbix 自动发现并监控 MySQL_MySQL
一、方案需求及思路
因跑MySQL服务的服务器比较多,并且每台服务器可能会运行多个不同端口的数据库,如果单独手动一台一台去修改agent.conf,web添加监控,这样太麻烦,费时费力。此时有两种方案;其一:在每台跑mysql的服务器上部署一个自动发现脚本,修改agent.conf 并自定义KEY;其二:统一由一台服务器来监控所有服务器上所有运行的mysql服务。发现好像方案二比较更简便些。
方案二思路:
假设有A、B、C、D、E五台服务器,上面都跑有mysql,并且每台都跑有2+个mysql,在此假定由A服务器来监控所有服务器上mysql的状态。此时需要先把五台服务器的IP及其上mysql服务所占用的port 记录下来,统一写到一个文件里:eg:
- 192.168.12.14 3306
- 192.168.12.14 3307
- 192.168.12.15 3308
- 192.168.12.15 3380
- ……
把该文件放到zabbix 目录下的某一个目录,并赋权限,再写一个json的脚本来调取此文件,并且需要在执行脚本文件的时候来进行IP传值并输出该IP上所有的port。
添加自定义key。其中需要定义一个自动发现的key,然后定义一个根据IP查询此服务器上所有端口的mysql状态的key。重启zabbix_agentd,通过zabbix_get来获取参数看看是否正常。
配置web界面。在web配置界面中,创建一个主机,该主机的ip指向A服务器的IP,切该服务器的[HOST.NAME] 指向所要监控mysql的IP,([HOST.NAME]一般指的是”Visable name”),把监控mysql的服务器都指向一个组,然后调用自动发现mysql模版。之后在创建剩下的几台服务器即可。
此方案也不是一个很好的方案,但相对来说还是不错的,以后若有新增加的mysql服务器,只需要修改写有IP和PORT的列表文件即可,不需要重启agent。
二、配置Agent:
1、编辑mysqld文件
- # cat /usr/local/zabbix/bin/mysqld
- 172.16.8.250 3306
- 172.16.8.250 3309
- 172.16.8.252 3306
- 172.16.8.252 3307
注:本文均在8.250上实现监控252上的数据库及本地250的数据库
2、mysql赋权限
分别将四个数据库都赋给172.16.8.250主机以用户zabbix,密码111的访问,show global status权限。
3、编辑discovery_mysql.sh
- # cat /usr/local/zabbix/bin/discovery_mysql.sh
- #!/bin/bash
- #mysql low-level discovery
- #Script_name discovery_mysql.sh
- res=`cat /usr/local/zabbix/bin/mysqld|grep $1|grep -v "^#"|awk '{print $2}'`
- port=($res)
- printf '{/n'
- printf '/t"data":[/n'
- for key in ${!port[@]}
- do
- if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
- printf '/t {/n'
- printf "/t/t/t/"{#MYSQLPORT}/":/"${port[${key}]}/"},/n"
- else [[ "${key}" -eq "((${#port[@]}-1))" ]]
- printf '/t {/n'
- printf "/t/t/t/"{#MYSQLPORT}/":/"${port[${key}]}/"}/n"
- fi
- done
- printf '/t ]/n'
- printf '}/n'
赋执行权限:
- # chmod +x /usr/local/zabbix/bin/discovery_mysql.sh
测试执行效果:
- # sh /usr/local/zabbix/bin/discovery_mysql.sh 172.16.8.250
- {
- "data":[
- {
- "{#MYSQLPORT}":"3306"},
- {
- "{#MYSQLPORT}":"3309"}
- ]
- }
把上述结果粘贴到json检测网站,查看结果是否正确 http://jsonlint.com/
4、修改agentd.conf
开启include选项
- Include=/usr/local/zabbix/etc/zabbix_agentd/
添加mysql自动发现规则,自定义key
- # cat /usr/local/zabbix/etc/zabbix_agentd/mysql_discovery.conf
- #### $1 ==IP $2 == PORT $3==COMMAND
- UserParameter=zabbix_low_discovery[*],/bin/bash /usr/local/zabbix/bin/discovery.sh $1
- UserParameter=mysql_stats[*],mysql -uzabbix -p111 -P$2 -h$1 -e "show global status"|grep "/"|cut -f2
- UserParameter=mysql.alive[*],mysqladmin -uzabbix -p111 -h$1 -P$2 ping|grep -c alive
5、测试:
重启zabbix_agentd,在zabbix server中通过zabbix_get测试zabbix mysql自动发现规则是否正确。
注:172.16.8.250是zabbix server,agent地址为127.0.0.1,请根据自己情况修改下面命令。
- # zabbix_get -s 127.0.0.1 -k zabbix_log_discovery[172.16.8.250]
- {
- "data":[
- {
- "{#MYSQLPORT}":"3306"},
- {
- "{#MYSQLPORT}":"3309"}
- ]
- }
- # zabbix_get -s 127.0.0.1 -k mysql_stats[172.16.8.250,3306,Uptime]
可正确取得数据便为正确,
转载请注明原文出处:http://www.minunix.com/2014/04/zabbix_low_discovery_mysql_01/
三、Zabbix WEB 添加主机
1、导入模版discovery_mysql.xml
2、添加主机:
链接模版:
注:因模版定义的更新时间为3600s,如需快速看到效果,可先修改模版自定义规则的“Interval”更新时间改为60,之后便可再”last data”中看到数据。
监控172.16.8.252,只需要创建主机,将visable name 的值172.16.8.250 改为172.16.8.252即可。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

In MySQL, the function of foreign keys is to establish the relationship between tables and ensure the consistency and integrity of the data. Foreign keys maintain the effectiveness of data through reference integrity checks and cascading operations. Pay attention to performance optimization and avoid common errors when using them.

The main difference between MySQL and MariaDB is performance, functionality and license: 1. MySQL is developed by Oracle, and MariaDB is its fork. 2. MariaDB may perform better in high load environments. 3.MariaDB provides more storage engines and functions. 4.MySQL adopts a dual license, and MariaDB is completely open source. The existing infrastructure, performance requirements, functional requirements and license costs should be taken into account when choosing.

SQL is a standard language for managing relational databases, while MySQL is a database management system that uses SQL. SQL defines ways to interact with a database, including CRUD operations, while MySQL implements the SQL standard and provides additional features such as stored procedures and triggers.

Abstract of the first paragraph of the article: When choosing software to develop Yi framework applications, multiple factors need to be considered. While native mobile application development tools such as XCode and Android Studio can provide strong control and flexibility, cross-platform frameworks such as React Native and Flutter are becoming increasingly popular with the benefits of being able to deploy to multiple platforms at once. For developers new to mobile development, low-code or no-code platforms such as AppSheet and Glide can quickly and easily build applications. Additionally, cloud service providers such as AWS Amplify and Firebase provide comprehensive tools

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...
