创建监控用户(权限usage):
mysql> grant usage on *.* to zabbix@127.0.0.1 identified by 'zabbix@123';
mysql> flush privileges;
监控脚本:
# cat /etc/zabbix/alertscripts/mysql_status.sh
#!/bin/bash
#Desc:zabbix 监控 MySQL 状态
#Date:2021-3-1
#主机
HOST="127.0.0.1"
#用户
USER="zabbix"
#密码
PASSWORD="zabbix@123"
#端口
PORT="22809"
#MySQL连接
CONNECTION="/usr/local/mysql/bin/mysqladmin -h ${HOST} -u ${USER} -P ${PORT} -p${PASSWORD}"
if [ $# -ne "1" ];then
echo "arg error!"
fi
case $1 in
Uptime)
result=`${CONNECTION} status 2>/dev/null |awk '{print $2}'`
echo $result
;;
Questions)
result=`${CONNECTION} status 2>/dev/null |awk '{print $6}'`
echo $result
;;
Com_update)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_update" |awk '{print $4}'`
echo $result
;;
Slow_queries)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Slow_queries" |awk '{print $4}'`
echo $result
;;
Com_select)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_select" |awk '{print $4}'`
echo $result
;;
Com_rollback)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_rollback" |awk '{print $4}'`
echo $result
;;
Com_insert)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_insert" |awk '{print $4}'`
echo $result
;;
Com_delete)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_delete" |awk '{print $4}'`
echo $result
;;
Com_commit)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_commit" |awk '{print $4}'`
echo $result
;;
Bytes_sent)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_sent" |awk '{print $4}'`
echo $result
;;
Bytes_received)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_received" |awk '{print $4}'`
echo $result
;;
Com_begin)
result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_begin" |awk '{print $4}'`
echo $result
;;*)
echo "Usage:$0(Uptime|Questions|Com_update|Slow_queries|Com_select|Com_rollback|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac
zabbix配置
#mysql监控
UserParameter=mysql.status[*],/etc/zabbix/alertscripts/mysql_status.sh $1
#获取MySQL运行状态(与脚本无关,可以自信忽略)
UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -u zabbix -h"127.0.0.1" -P22809 -p"zabbix@123" ping 2>/dev/null | grep -c alive
zabbix模板:
# zbx_export_templates.xml
<?xml version="1.0" encoding="UTF-8"?>
<zabbix_export>
<version>5.0</version>
<date>2021-11-11T08:35:01Z</date>
<groups>
<group>
<name>Templates/Databases</name>
</group>
</groups>
<templates>
<template>
<template>Template DB MySQL custom</template>
<name>Template DB MySQL custom</name>
<groups>
<group>
<name>Templates/Databases</name>
</group>
</groups>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<items>
<item>
<name>MySQL status</name>
<key>mysql.ping</key>
<history>1w</history>
<description>It requires user parameter mysql.ping, which is defined in userparameter_mysql.conf.
0 - MySQL server is down
1 - MySQL server is up</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<valuemap>
<name>Service state</name>
</valuemap>
<triggers>
<trigger>
<expression>{last(0)}=0</expression>
<name>MySQL is down</name>
<priority>WARNING</priority>
</trigger>
</triggers>
</item>
<item>
<name>MySQL bytes received per second</name>
<key>mysql.status[Bytes_received]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>Bps</units>
<description>The number of bytes received from all clients.
It requires user parameter mysql.status[*], which is defined in
userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL bytes sent per second</name>
<key>mysql.status[Bytes_sent]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>Bps</units>
<description>The number of bytes sent to all clients.
It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL begin operations per second</name>
<key>mysql.status[Com_begin]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL commit operations per second</name>
<key>mysql.status[Com_commit]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL delete operations per second</name>
<key>mysql.status[Com_delete]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL insert operations per second</name>
<key>mysql.status[Com_insert]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL rollback operations per second</name>
<key>mysql.status[Com_rollback]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL select operations per second</name>
<key>mysql.status[Com_select]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL update operations per second</name>
<key>mysql.status[Com_update]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL queries per second</name>
<key>mysql.status[Questions]</key>
<history>1w</history>
<value_type>FLOAT</value_type>
<units>qps</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
<preprocessing>
<step>
<type>CHANGE_PER_SECOND</type>
<params/>
</step>
</preprocessing>
</item>
<item>
<name>MySQL slow queries</name>
<key>mysql.status[Slow_queries]</key>
<history>1w</history>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
</item>
<item>
<name>MySQL uptime</name>
<key>mysql.status[Uptime]</key>
<history>1w</history>
<units>uptime</units>
<description>It requires user parameter mysql.status[*], which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
</item>
<item>
<name>MySQL version</name>
<key>mysql.version</key>
<delay>1h</delay>
<history>1w</history>
<trends>0</trends>
<value_type>CHAR</value_type>
<description>It requires user parameter mysql.version, which is defined in userparameter_mysql.conf.</description>
<applications>
<application>
<name>MySQL</name>
</application>
</applications>
</item>
</items>
<screens>
<screen>
<name>MySQL performance</name>
<hsize>2</hsize>
<vsize>1</vsize>
<screen_items>
<screen_item>
<resourcetype>0</resourcetype>
<style>0</style>
<resource>
<name>MySQL operations</name>
<host>Template DB MySQL</host>
</resource>
<width>500</width>
<height>200</height>
<x>0</x>
<y>0</y>
<colspan>1</colspan>
<rowspan>1</rowspan>
<elements>0</elements>
<valign>1</valign>
<halign>0</halign>
<dynamic>0</dynamic>
<sort_triggers>0</sort_triggers>
<url/>
<application/>
<max_columns>3</max_columns>
</screen_item>
<screen_item>
<resourcetype>0</resourcetype>
<style>0</style>
<resource>
<name>MySQL bandwidth</name>
<host>Template DB MySQL</host>
</resource>
<width>500</width>
<height>270</height>
<x>1</x>
<y>0</y>
<colspan>1</colspan>
<rowspan>1</rowspan>
<elements>0</elements>
<valign>1</valign>
<halign>0</halign>
<dynamic>0</dynamic>
<sort_triggers>0</sort_triggers>
<url/>
<application/>
<max_columns>3</max_columns>
</screen_item>
</screen_items>
</screen>
</screens>
</template>
</templates>
<value_maps>
<value_map>
<name>Service state</name>
<mappings>
<mapping>
<value>0</value>
<newvalue>Down</newvalue>
</mapping>
<mapping>
<value>1</value>
<newvalue>Up</newvalue>
</mapping>
</mappings>
</value_map>
</value_maps>
</zabbix_export>