zabbix是如何实现监控MySQL
admin
2023-05-19 16:42:46
0

下文内容主要给大家带来zabbix是如何实现监控MySQL,所讲到的知识,与书籍不同,都是专业技术人员在与用户接触过程中,总结出来的,具有一定的经验分享价值,希望给广大读者带来帮助。

一、linux环境下监控MySQL

Zabbix Server自带了MySQL插件来监控mysql数据库的模板,只需要配置好agent客户端,然后在web端给主机增加模板就行了

监控项目:

Com_update:     mysql执行的更新个数

Com_select:     mysql执行的查询个数

Com_insert:     mysql执行插入的个数

Com_delete:     执行删除的个数

Com_rollback:   执行回滚的操作个数

Bytes_received:  接受的字节数

Bytes_sent:     发送的字节数

Slow_queries:   慢查询语句的个数

Com_commit:     确认的事物个数

Com_begin:      开始的事物个数

Uptime:        云服务器已启动的秒数

Questions:      客户端发送到服务器的语句个数

监控模板下载地址:http://www.zabbix.org/wiki/Zabbix_Templates#External_template_resources

脚本下载地址:https://github.com/itnihao/zabbix-book


1)创建zabbix链接MySQL的用户名,密码并授予权限。

mysql> grant all on *.* to zabbix@'localhost'
identified by "123456”;
mysql> flush privileges;

2)在zabbix_agent服务目录下创建 .my.cnf 连接文件

cd /usr/local/zabbix/etc/
vim .my.cnf
[client]
user=zabbix
password=123456

注意:
如果在数据库grant授权时,针对的是localhost,这个.my.cnf里面就不用加host参数了【如上配置】
但如果grant授权时针对的是本机的ip(如192.168.1.25),那么在.my.cnf文件里就要加上host参数进行指定:host=192.168.1.25


3)配置MySQL的key文件

这个可以从zabbix安装时的解压包里拷贝过来:

cp 
/usr/local/src/zabbix3.0.3/conf/
zabbix_agentd/userparameter_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/

zabbix是如何实现监控MySQL

4)替换zabbix安装路径,注意如果MySQL没配置好环境变量可能找不到MySQL命令,可以用MySQL全路径

看到类似 HOME=/var/lib/zabbix 的路径设置,把路径全都替换为 /usr/local/zabbix/etc/,也就是上面的.my.cnf文件所在的目录路径。

cd /usr/local/zabbix/etc/zabbix_agentd.conf.d/
vim userparameter_mysql.conf
# For all the following commands HOME should be set to the directory that has .my.cnf file with password information.
# Flexible parameter to grab global variables. On the frontend side, use keys like mysql.status[Com_insert].
# Key syntax is mysql.status[variable].
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';" | HOME=/usr/local/zabbix/etc/ mysql -N | awk '{print $$2}'
# Flexible parameter to determine database or table size. On the frontend side, use keys like mysql.size[zabbix,history,data].
# Key syntax is mysql.size[,,].
# Database may be a database name or "all". Default is "all".
# Table may be a table name or "all". Default is "all".
# Type may be "data", "index", "free" or "both". Both is a sum of data and index. Default is "both".
# Database is mandatory if a table is specified. Type may be specified always.
# Returns value in bytes.
# 'sum' on data_length or index_length alone needed when we are getting this information for whole database instead of a single table
UserParameter=mysql.size[*],bash -c 'echo "select sum($(case "$3" in both|"") echo "data_length+index_length";; data|index) echo "$3_length";; free) echo "data_free";; esac)) from information_schema.tables$([[ "$1" = "all" || ! "$1" ]] || echo " where table_schema=\"$1\"")$([[ "$2" = "all" || ! "$2" ]] || echo "and table_name=\"$2\"");" | HOME=/usr/local/zabbix/etc/ mysql -N'
UserParameter=mysql.ping,HOME=/usr/local/zabbix/etc/ mysqladmin ping | grep -c alive
UserParameter=mysql.version,mysql -V

%s#/var/lib/zabbix#/usr/local/zabbix/etc/#      #用命令替换

也可以用这个shell脚本监控,包括主从监控:

### MySQL DB Infomation
UserParameter=mysql.status[*],echo"show global status where Variable_name='$1';"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}'
UserParameter=mysql.variables[*],echo"show global variables where Variable_name='$1';"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}'
UserParameter=mysql.ping,mysqladmin--defaults-file=/usr/local/zabbix/etc/.my.cnf ping|grep -c  alive
UserParameter=mysql.version,echo"select version();"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N
 
#### MySQL Master Information
UserParameter=mysql.master.Slave_count,echo"show slave hosts;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|wc -l
UserParameter=mysql.master.Binlog_file,echo"show master status;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $1}'|awk -F.'{print $1}'
UserParameter=mysql.master.Binlog_number,echo"show master status;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{print $1}'|awk -F.'{print $2}'
UserParameter=mysql.master.Binlog_position,echo"show master status;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf-N|awk '{print $2}'
UserParameter=mysql.master.Binlog_count,echo"show binary logs;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|wc -l
UserParameter=mysql.master.Binlog_total_size,echo"show binary logs;"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf -N|awk '{sum+=$NF}END{print  sum}'
 
#### MySQL Slave Information
UserParameter=mysql.slave.Seconds_Behind_Master,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep "Seconds_Behind_Master"|awk'{print $2}'
UserParameter=mysql.slave.Slave_IO_Running,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Slave_IO_Running"|awk '{print $2}'
UserParameter=mysql.slave.Slave_SQL_Running,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Slave_SQL_Running"|awk '{print $2}'
UserParameter=mysql.slave.Relay_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Relay_Log_Pos"|awk '{print $2}'
UserParameter=mysql.slave.Exec_Master_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Exec_Master_Log_Pos"|awk '{print $2}'
UserParameter=mysql.slave.Read_Master_Log_Pos,echo"show slave status\G"|mysql--defaults-file=/usr/local/zabbix/etc/.my.cnf|grep"Read_Master_Log_Pos"|awk '{print $2}'



5)修改zabbix_agentd.conf配置文件,开启额外加载,就是去掉前面的#号

vim zabbix_agentd.conf
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/

6)重启zabbix_agentd服务

/etc/init.d/zabbix_agentd restart

7)测试看能否获取到数据

# zabbix_get -s 127.0.0.1 -p 10050 -k mysql.status[Com_select]
200661

8)登陆zabbix监控界面,在“配置”里为主机添加模板,完成监控。

zabbix是如何实现监控MySQL

zabbix是如何实现监控MySQL

zabbix是如何实现监控MySQL


二、windows下的MySQL监控

要想在Windows上取得MySQL的状态数据,可以用vbs脚本运行mysql命令

1)在d:\Zabbix\Scripts\目录下新建两个脚本文件内容如下:

mysql_ping.vbs

Set objFS =CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
str1 = getCommandOutput("D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-ucactiuser -pcactiuser ping")     //修改对应数据库路径,用户名和密码
 
If Instr(str1,"alive") > 0Then
WScript.Echo 1
Else
WScript.Echo 0
End If
 
Function getCommandOutput(theCommand)
 
Dim objShell, objCmdExec
Set objShell =CreateObject("WScript.Shell")
Set objCmdExec = objshell.exec(thecommand)
getCommandOutput =objCmdExec.StdOut.ReadAll
end Function


 MYSQL-status.vbs

Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
str1 = getCommandOutput("D:\SOFT_PHP_PACKAGE\mysql\bin\mysqladmin-u cactiuser -pcactiuser extended-status")      //修改对应数据库路径,用户名和密码
Arg = objArgs(0)
str2 = Split(str1,"|")
 
For i = LBound(str2) to UBound(str2)
 
If Trim(str2(i)) = Arg Then   
WScript.Echo TRIM(str2(i+1))
Exit For
End If
next
 
 
Function getCommandOutput(theCommand)
 
Dim objShell, objCmdExec
Set objShell =CreateObject("WScript.Shell")
Set objCmdExec = objshell.exec(thecommand)
getCommandOutput =objCmdExec.StdOut.ReadAll
 
end Function



2)修改windows上的zabbix_agentd.comf文件,设置key值。在UserParameter下面加两句;

UserParameter=mysql.status[*], cscript/nologo d:\Zabbix\Scripts\MySQL_Ext-Status_Script.vbs $1 
UserParameter=mysql.ping, cscript /nologo d:\Zabbix\Scripts\MySql_Ping.vbs

3)重启zabbix_agentd,并给主机添加MySQL模版,查看items状态。

zabbix是如何实现监控MySQL

对于以上关于zabbix是如何实现监控MySQL,如果大家还有更多需要了解的可以持续关注我们的行业推新,如需获取专业解答,可在官网联系售前售后的,希望该文章可给大家带来一定的知识更新。

相关内容

热门资讯

让数据“说话”、AI“拿主意” 在德赛西威的智能化产线上,AI让流水线告别传统人工模式,精度与效率双双跃升 集成供应链的...
星河动力智神星一号 8 月中下... 8 月 3 日消息,星河动力航天将于近期择机实施“智神星一号(遥一)”商业运载火箭发射任务,今日发布...
伊朗称在霍尔木兹海峡上空击落一... 新华社德黑兰8月3日电 据伊朗媒体3日报道,伊朗伊斯兰革命卫队在霍尔木兹海峡上空击落一架MQ-9无人...
“地表最强男人”,遇难 奇迹没有发生,在经过几天的救援之后,正式消息发出,世界著名登山家尼尔马尔·普尔贾(Nirmal Pu...
精神科医生“十级美颜”证件照刷... 近日,山西医科大学第一医院精神科医生郎小娥的证件照,因美颜效果突出打破大众固有印象而意外出圈。8月1...
凤凰直击日本熊本震区:刚修缮好... 日本熊本强震重创当地历史建筑。位于熊本县八代市的八代宫神社受损惨重。凤凰卫视特派记者蒋文彬采访了神社...
考勤、工资、社保都要查,中国企... 编者按7月23日,欧盟第21轮对俄制裁,将14家中国内地及香港企业列入名单;不到24小时,中国商务部...
两名俄公民在泰国遇害,泰总理道... 综合泰国《曼谷邮报》等媒体8月2日报道,对于两名俄罗斯公民近期在泰国遇害,泰国总理阿努廷作出表态并进...
总书记的人民情怀|“坚持高质量... 原标题:“坚持高质量发展要成为领导干部政绩观的重要内容”(总书记的人民情怀)本报记者 赵 成 史一棋
马科斯遭痛批“亲美上头”:瞧瞧... 菲律宾主流英文报刊《马尼拉时报》8月3日刊发评论文章,批评菲律宾政府近年来过度依赖美国安全承诺,对华...