使用zabbix低级发现功能监控MySQL

在有些情况下,我们不能对MySQL实例所在的主机上安装zabbix agent,只能访问MySQL实例,例如使用Ucloud提供的UDB.这时我们就需要使用Zabbix 的LLD功能来监控MySQL

参见使用zabbix全方位监控MySQL


1.添加MySQL监控账号

GRANT USAGE,PROCESS,SUPER,REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'zabbixagent' IDENTIFIED BY 'zabbixagent';

flush privileges;


在/usr/local/zabbix/etc/目录下创建需要监控MySQL的连接信息

发现脚本会根据是否以Master和Slave来识别并传递不同的宏

.my.cnf10.10.41.7:3306:Master

.my.cnf10.10.41.8:3306:Slave


[mysql]
user=zabbixagent
password=zabbixagent
host=10.10.41.8
[mysqladmin]
user=zabbixagent
password=zabbixagent
host=10.10.41.8


具体连接信息根据情况而定


2.添加MySQL实例,MySQL Master和MySQL Slave的发现脚本

# python mysql_discovery.py 
{
    "data":[
        {
            "{#MYSQL_DB}":"10.10.41.7:3306:Master"
        },
        {
            "{#MYSQL_DB}":"10.10.41.8:3306:Slave"
        }
    ]
}
#/usr/bin/python
#This script is used to discovery disk on the server
import subprocess
import json
args='''ls -al /usr/local/zabbix/etc/.my.cnf*|awk '{print $NF}'|sed "s;/usr/local/zabbix/etc/.my.cnf;;g"'''
t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
mysql_dbs=[]

for mysql in t.split('\n'):
    if len(mysql) != 0:
       mysql_dbs.append({'{#MYSQL_DB}':mysql})
print json.dumps({'data':mysql_dbs},indent=4,separators=(',',':'))



# python mysql_master_discovery.py 
{
    "data":[
        {
            "{#MYSQL_MASTER}":"10.10.41.7:3306:Master"
        }
    ]
}
#/usr/bin/python
#This script is used to discovery disk on the server
import subprocess
import json
args='''ls -al /usr/local/zabbix/etc/.my.cnf*|awk '{print $NF}'|sed "s;/usr/local/zabbix/etc/.my.cnf;;g"'''
t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
mysql_dbs=[]

for mysql in t.split('\n'):
    if len(mysql) != 0 and mysql.endswith(tuple(['Master','master'])):
       mysql_dbs.append({'{#MYSQL_MASTER}':mysql})

if len(mysql_dbs) == 0:
   print "No MySQL Master Found" 
else:
   print json.dumps({'data':mysql_dbs},indent=4,separators=(',',':'))


# python mysql_slave_discovery.py 
{
    "data":[
        {
            "{#MYSQL_SLAVE}":"10.10.41.8:3306:Slave"
        }
    ]
}
#/usr/bin/python
#This script is used to discovery disk on the server
import subprocess
import json
args='''ls -al /usr/local/zabbix/etc/.my.cnf*|awk '{print $NF}'|sed "s;/usr/local/zabbix/etc/.my.cnf;;g"'''
t=subprocess.Popen(args,shell=True,stdout=subprocess.PIPE).communicate()[0]
mysql_dbs=[]

for mysql in t.split('\n'):
    if len(mysql) != 0 and mysql.endswith(tuple(['Slave','slave'])):
       mysql_dbs.append({'{#MYSQL_SLAVE}':mysql})

if len(mysql_dbs) == 0:
   print "No MySQL Slave Found" 
else:
   print json.dumps({'data':mysql_dbs},indent=4,separators=(',',':'))


传递不同的宏值避免添加不需要的模板



3.添加zabbix配置文件discovery_mysql_status.conf

##discovery mysql instance
UserParameter=mysql.discovery,/usr/bin/python /usr/local/zabbix/bin/mysql_discovery.py
UserParameter=mysql.innodb.discovery,/usr/bin/python /usr/local/zabbix/bin/mysql_discovery.py
UserParameter=mysql.master.discovery,/usr/bin/python /usr/local/zabbix/bin/mysql_master_discovery.py
UserParameter=mysql.slave.discovery,/usr/bin/python /usr/local/zabbix/bin/mysql_slave_discovery.py


### MySQL DB Infomation
#
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$2 -N|awk '{print $$2}'
UserParameter=mysql.variables[*],echo "show global variables where Variable_name='$1';"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$2 -N|awk '{print $$2}'
UserParameter=mysql.ping[*],mysqladmin --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 ping|grep -c  alive

#UserParameter=mysql.version,echo "select version();"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 -N

#### MySQL Master Information
UserParameter=mysql.master.Slave_count[*],echo "show slave hosts;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 -N|wc -l
UserParameter=mysql.master.Binlog_file[*],echo "show master status;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 -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$1 -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$1 -N|awk '{print $$2}'
UserParameter=mysql.master.Binlog_count[*],echo "show binary logs;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 -N|wc -l
UserParameter=mysql.master.Binlog_total_size[*],echo "show binary logs;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$1 -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$1|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$1|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$1|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$1|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$1|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$1|grep "Read_Master_Log_Pos"|awk '{print $$2}'



#### MySQL InnoDB Information
#
UserParameter=mysql.innodb[*],/usr/local/zabbix/bin/discovery_mysql_innodb_status.sh $1 $2


####MySQL MyISAM Information
#


InnoDB部分监控项目需要使用单独的脚本discovery_mysql_innodb_status.sh



#!/bin/bash

#Get InnoDB Row Lock Details and InnoDB Transcation Lock Memory
#mysql> SELECT SUM(trx_rows_locked) AS rows_locked, SUM(trx_rows_modified) AS rows_modified, SUM(trx_lock_memory_bytes) AS lock_memory FROM information_schema.INNODB_TRX;
#+-------------+---------------+-------------+
#| rows_locked | rows_modified | lock_memory |
#+-------------+---------------+-------------+
#|        NULL |          NULL |        NULL |
#+-------------+---------------+-------------+
#1 row in set (0.00 sec)

#+-------------+---------------+-------------+
#| rows_locked | rows_modified | lock_memory |
#+-------------+---------------+-------------+
#|           0 |             0 |         376 |
#+-------------+---------------+-------------+

#Get InnoDB Compression Time
#mysql> SELECT SUM(compress_time) AS compress_time, SUM(uncompress_time) AS uncompress_time FROM information_schema.INNODB_CMP;
#+---------------+-----------------+
#| compress_time | uncompress_time |
#+---------------+-----------------+
#|             0 |               0 |
#+---------------+-----------------+
#1 row in set (0.00 sec)


#Get InnoDB Transaction states

#TRX_STATE	Transaction execution state. One of RUNNING, LOCK WAIT, ROLLING BACK or COMMITTING.

#mysql> SELECT LOWER(REPLACE(trx_state, " ", "_")) AS state, count(*) AS cnt from information_schema.INNODB_TRX GROUP BY state;
#+---------+-----+
#| state   | cnt |
#+---------+-----+
#| running |   1 |
#+---------+-----+
#1 row in set (0.00 sec)





innodb_metric=$1
mysql_host=$2

case $innodb_metric in
   Innodb_rows_locked)
                      value=$(echo "SELECT SUM(trx_rows_locked) AS rows_locked, SUM(trx_rows_modified) AS rows_modified, SUM(trx_lock_memory_bytes) AS lock_memory FROM information_schema.INNODB_TRX;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N| awk '{print $1}')
                      if [ "$value" == "NULL" ];then
                         echo 0
                      else
                         echo $value
                      fi
                    ;;
   Innodb_rows_modified)
                      value=$(echo "SELECT SUM(trx_rows_locked) AS rows_locked, SUM(trx_rows_modified) AS rows_modified, SUM(trx_lock_memory_bytes) AS lock_memory FROM information_schema.INNODB_TRX;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N| awk '{print $2}')
                      if [ "$value" == "NULL" ];then
                         echo 0
                      else
                         echo $value
                      fi
                    ;;
   Innodb_trx_lock_memory)
                      value=$(echo "SELECT SUM(trx_rows_locked) AS rows_locked, SUM(trx_rows_modified) AS rows_modified, SUM(trx_lock_memory_bytes) AS lock_memory FROM information_schema.INNODB_TRX;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N| awk '{print $3}')
                      if [ "$value" == "NULL" ];then
                         echo 0
                      else
                         echo $value
                      fi
                    ;;
      Innodb_compress_time)
                      value=$(echo "SELECT SUM(compress_time) AS compress_time, SUM(uncompress_time) AS uncompress_time FROM information_schema.INNODB_CMP;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|awk '{print $1}')
                      echo $value
                      ;;
       
     Innodb_uncompress_time)
                      value=$(echo "SELECT SUM(compress_time) AS compress_time, SUM(uncompress_time) AS uncompress_time FROM information_schema.INNODB_CMP;"|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|awk '{print $2}')
                      echo $value
                      ;;   
         Innodb_trx_running)
                         value=$(echo 'SELECT LOWER(REPLACE(trx_state, " ", "_")) AS state, count(*) AS cnt from information_schema.INNODB_TRX GROUP BY state;'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep running|awk '{print $2}')
                         if [ "$value" == "" ];then
                            echo 0
                         else
                            echo $value
                         fi
                        ;;
       Innodb_trx_lock_wait)
                         value=$(echo 'SELECT LOWER(REPLACE(trx_state, " ", "_")) AS state, count(*) AS cnt from information_schema.INNODB_TRX GROUP BY state;'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep lock_wait|awk '{print $2}')
                         if [ "$value" == "" ];then
                            echo 0
                         else
                            echo $value
                         fi
                        ;;
    Innodb_trx_rolling_back)
                         value=$(echo 'SELECT LOWER(REPLACE(trx_state, " ", "_")) AS state, count(*) AS cnt from information_schema.INNODB_TRX GROUP BY state;'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep rolling_back|awk '{print $2}')
                         if [ "$value" == "" ];then
                            echo 0
                         else
                            echo $value
                         fi
                        ;;
    Innodb_trx_committing)
                         value=$(echo 'SELECT LOWER(REPLACE(trx_state, " ", "_")) AS state, count(*) AS cnt from information_schema.INNODB_TRX GROUP BY state;'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep committing|awk '{print $2}')
                         if [ "$value" == "" ];then
                            echo 0
                         else
                            echo $value
                         fi
                        ;;
 Innodb_trx_history_list_length)
                         echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "History list length"|awk '{print $4}'
                        ;;
    Innodb_last_checkpoint_at)
                         echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Last checkpoint at"|awk '{print $4}'
                        ;;

   Innodb_log_sequence_number)
                         echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Log sequence number"|awk '{print $4}'
                        ;;
    Innodb_log_flushed_up_to)
                         echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Log flushed up to"|awk '{print $5}'
                        ;;
   Innodb_open_read_views_inside_innodb)
                         echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "read views open inside InnoDB"|awk '{print $1}'
                        ;;
        Innodb_queries_inside_innodb)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "queries inside InnoDB"|awk '{print $1}'
                        ;;
        Innodb_queries_in_queue)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "queries in queue"|awk '{print $5}'
                        ;;
        Innodb_hash_seaches)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "hash searches"|awk '{print $1}'
                        ;;
       Innodb_non_hash_searches)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "non-hash searches/s"|awk '{print $4}'
                        ;;
       Innodb_node_heap_buffers)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "node heap"|awk '{print $8}'
                       ;;
       Innodb_mutex_os_waits)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Mutex spin waits"|awk '{print $9}'
                       ;;
       Innodb_mutex_spin_rounds)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Mutex spin waits"|awk '{print $6}'|tr -d ','
                       ;;
       Innodb_mutex_spin_waits)
                        echo 'show engine innodb status\G'|mysql --defaults-file=/usr/local/zabbix/etc/.my.cnf$mysql_host -N|grep "Mutex spin waits"|awk '{print $4}'|tr -d ','
                       ;; 

                   *)
                    echo "wrong parameter"
                    ;;

esac


3.添加zabbix监控模板

wKiom1ShQDiiqyj2AAL8qvGiSk0806.jpg

wKioL1ShQOuRSWyyAALO7vL5Rjc291.jpg

wKiom1ShQDmDeyvHAALPHcvB84w299.jpg

wKioL1ShQOyiTTJhAALV9KWiIzA076.jpg


你可能感兴趣的:(mysql)