ZABBIX监控mysql主从状态
模板如下
<zabbix_export> <version>3.4</version> <date>2018-11-30T08:28:28Z</date> <groups> <group> <name>自定义</name> </group> </groups> <templates> <template> <template>Mysql-slave-info</template> <name>Mysql-slave-info</name> <description/> <groups> <group> <name>自定义</name> </group> </groups> <applications/> <items> <item> <name>Mysql-Slave 读取错误数</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>slave_status[Last_Errno]</key> <delay>30s</delay> <history>7d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>Mysql-Slave 慢多少秒</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>slave_status[Seconds_Behind_Master]</key> <delay>30s</delay> <history>7d</history> <trends>365d</trends> <status>0</status> <value_type>0</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>Mysql-Slave 跳过错误数</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>slave_status[Skip_Counter]</key> <delay>30s</delay> <history>7d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>Mysql-Slave IO线程状态</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>slave_status[Slave_IO_Running]</key> <delay>30s</delay> <history>7d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> <item> <name>Mysql-Slave sql线程状态</name> <type>0</type> <snmp_community/> <snmp_oid/> <key>slave_status[Slave_SQL_Running]</key> <delay>30s</delay> <history>7d</history> <trends>365d</trends> <status>0</status> <value_type>3</value_type> <allowed_hosts/> <units/> <snmpv3_contextname/> <snmpv3_securityname/> <snmpv3_securitylevel>0</snmpv3_securitylevel> <snmpv3_authprotocol>0</snmpv3_authprotocol> <snmpv3_authpassphrase/> <snmpv3_privprotocol>0</snmpv3_privprotocol> <snmpv3_privpassphrase/> <params/> <ipmi_sensor/> <authtype>0</authtype> <username/> <password/> <publickey/> <privatekey/> <port/> <description/> <inventory_link>0</inventory_link> <applications/> <valuemap/> <logtimefmt/> <preprocessing/> <jmx_endpoint/> <master_item/> </item> </items> <discovery_rules/> <httptests/> <macros/> <templates/> <screens/> </template> </templates> <triggers> <trigger> <expression> {Mysql-slave-info:slave_status[Slave_IO_Running].last()}=1 </expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>Mysql-Slave IO线程中断</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression> {Mysql-slave-info:slave_status[Slave_SQL_Running].last()}=1 </expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>Mysql-Slave Sql线程中断</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression> {Mysql-slave-info:slave_status[Seconds_Behind_Master].last()}>600 </expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>Mysql-Slave 慢超过600秒</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression> {Mysql-slave-info:slave_status[Skip_Counter].last()}=1 </expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>Mysql-Slave 读取错误数超过0个</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> <trigger> <expression> {Mysql-slave-info:slave_status[Skip_Counter].last()}=1 </expression> <recovery_mode>0</recovery_mode> <recovery_expression/> <name>Mysql-Slave 跳过错误数超过0个</name> <correlation_mode>0</correlation_mode> <correlation_tag/> <url/> <status>0</status> <priority>3</priority> <description/> <type>0</type> <manual_close>0</manual_close> <dependencies/> <tags/> </trigger> </triggers> </zabbix_export>
如果mysql是源码安装,而且是环境变量的mysql命令,需要将脚本里mysql写成绝对路径
#vim mysql-slave.sh
#!/bin/bash #监控mysql从状态脚本 commnd() { /usr/local/mysql/bin/mysql -uroot -p'123456' -e "show slave status\G;" 2>/dev/null | grep -w $1 | awk -F': ' '{print $2}' } if [[ $1 == "Slave_IO_Running" ]];then a=`commnd Slave_IO_Running` #判断是否为Yes,如果是Yes,输出0,否则输出1,触发报警 [[ "$a" == "Yes" ]] && echo 0 || echo 1 elif [[ $1 == "Slave_SQL_Running" ]];then a=`commnd Slave_SQL_Running` [[ "$a" == "Yes" ]] && echo 0 || echo 1 elif [[ $1 == "Seconds_Behind_Master" ]];then a=`commnd Seconds_Behind_Master` b=`cat /tmp/sbm.tmp` if [[ $a -ge 600 ]] && [[ $b -ge 600 ]];then echo $a else echo 0 fi echo "$a" > /tmp/sbm.tmp elif [[ $1 == "Last_Errno" ]];then a=`commnd Last_Errno` [[ $a -eq 0 ]] && echo 0 || echo 1 elif [[ $1 == "Skip_Counter" ]];then a=`commnd Skip_Counter` [[ $a -eq 0 ]] && echo 0 || echo 1 else echo "Slave_IO_Running | Slave_SQL_Running | Seconds_Behind_Master | Last_Errno | Skip_Counter" fi
#vim zabbix.agent.conf
UserParameter=slave_status[*],/etc/zabbix/script/mysql-slave.sh $1
验证
# zabbix_get -s 10.0.0.52(监控的主机) -k'slave_status[Slave_IO_Running]'
web界面创建监控项
web界面创建触发值
依次创建监控项跟触发值
键值:
触发器:
图:为0则是正常的,Seconds_Behind_Master是延迟多少秒。超过600也就是10分钟会报警