1. 程式人生 > >zabbix監控mysql主從

zabbix監控mysql主從

環境說明:

主機名 ip 服務
zhu 192.168.100.233 主資料庫
cong 192.168.100.234 從資料庫
zabbix 192.168.100.11 zabbix

關閉防火牆、selinux

搭建zabbix

[[email protected] ~]# rpm -ivh http://mirrors.aliyun.com/zabbix/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
[[email protected] ~]# yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent mariadb-server
[
[email protected]
~]# systemctl start mariadb.service [[email protected] ~]# mysql_secure_installation [[email protected] ~]# mysql -uroot -p123.com MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> grant all privileges on zabbix.* to
[email protected]
identified by '123.com'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) [[email protected] ~]# cd /usr/share/doc/zabbix-server-mysql-3.0.23/ [[email protected] zabbix-server-mysql-3.0.23]# zcat create.sql.gz |mysql -p -u zabbix zabbix [
[email protected]
zabbix-server-mysql-3.0.23]# tail -1 /etc/zabbix/zabbix_server.conf DBPassword=123.com [[email protected] zabbix-server-mysql-3.0.23]# systemctl start zabbix-server.service [[email protected] zabbix-server-mysql-3.0.23]# ss -anlt State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 128 *:10051 *:* LISTEN 0 50 *:3306 *:* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 128 :::10051 :::* [[email protected] zabbix-server-mysql-3.0.23]# vim /etc/httpd/conf.d/zabbix.conf <IfModule mod_php5.c> php_value max_execution_time 300 php_value memory_limit 128M php_value post_max_size 16M php_value upload_max_filesize 2M php_value max_input_time 300 php_value max_input_vars 10000 php_value always_populate_raw_post_data -1 # php_value date.timezone Europe/Riga php_value date.timezone Asia/Shanghai </IfModule> [[email protected] zabbix-server-mysql-3.0.23]# systemctl start httpd

在這裡插入圖片描述 在這裡插入圖片描述

監控MySQL主從

[[email protected] ~]# yum -y install zabbix-agent
[[email protected] ~]# vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.100.11
...
ServerActive=192.168.100.11
...
Hostname=192.168.100.233
[[email protected] ~]# systemctl start zabbix-agent.service
[[email protected] ~]# ss -anlt
State      Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
LISTEN     0      128                      *:22                                   *:*                  
LISTEN     0      100              127.0.0.1:25                                   *:*                  
LISTEN     0      128                      *:10050                                *:*                  
LISTEN     0      50                       *:3306                                 *:*                  
LISTEN     0      128                     :::22                                  :::*                  
LISTEN     0      100                    ::1:25                                  :::*                  
LISTEN     0      128                     :::10050           

在這裡插入圖片描述 在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述

建立觸發器

編輯從庫指令碼檢測

\\重寫記錄
[[email protected] opt]# cat ckread.sh 
#!/bin/bash

A=$(mysql -uroot -p123.com -e "show slave status\G" | grep "Read_Master_Log_Pos"|awk -F':' '{print $2}')
B=$(mysql -uroot -p123.com -e "show slave status\G" | grep "Exec_Master_Log_Pos"|awk -F':' '{print $2}')

if [ $A == $B ];then
    echo '1'
else
    echo '0'
fi
\\定義key值
[[email protected] opt]# tail -5 /etc/zabbix/zabbix_agentd.conf 
#
UserParameter=ckmysql,/bin/bash /opt/ckmysql.sh mysql
UserParameter=ckread,/bin/bash /opt/ckread.sh
\\重啟zabbix客戶端服務端

服務端檢測

[[email protected] ~]# zabbix_get -s 192.168.100.116 -k 'ckread'
1

web頁面 新增專案一 在這裡插入圖片描述在這裡插入圖片描述 建立觸發器

新增專案二

新增觸發器

新增專案三

在這裡插入圖片描述 在這裡插入圖片描述 測試

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.100.33
                  Master_User: dsb
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000005
          Read_Master_Log_Pos: 245
               Relay_Log_File: mysql-relay-bin.000014
                Relay_Log_Pos: 529
        Relay_Master_Log_File: mysql-bin.000005
             Slave_IO_Running: No
            Slave_SQL_Running: No

IO執行緒和SQL寫同一指令碼實現監控

[[email protected] opt]# cat /opt/ckIS.sh 
#!/bin/bash
A=$1
B=$2

stat=$(mysql -uroot -p123.com -e 'show slave status\G'|egrep "$A|$B"|awk -F ':' '{print $2}'|grep -c Yes)

if [ $stat -eq 2 ];then
    echo "1"
else
    echo "0"
fi
[[email protected] opt]# tail -1 /etc/zabbix/zabbix_agentd.conf 
UserParameter=ckIS,/bin/bash /opt/ckIS.sh Slave_IO_Running Slave_SQL_Running
\\這裡需要重啟服務

監控端使用zabbix_get工具測試

[[email protected] ~]# zabbix_get -s 192.168.100.234 -p10050 -k 'ckIS'
1

在web頁面新增監控項 在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述在這裡插入圖片描述 模擬故障檢測

MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.02 sec)

在這裡插入圖片描述