一、方案需求及思路
因跑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即可。

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools
