zabbix 监控MySQL

现在我来说一下我的监控环境

zabbix-3.0.3

MySQL-5.6.23

1.首先我们要登录MySQL,创建一个监控MySQL的用户

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

2。根据zabbix监控MySQL的key改写脚本

[root@DBSlave-live etc]# cd zabbix_agentd.conf.d/
[root@DBSlave-live zabbix_agentd.conf.d]# ls
mysql_status.conf
[root@DBSlave-live zabbix_agentd.conf.d]# cat mysql_status.conf
### MySQL DB Infomation
UserParameter=mysql.status[*],echo "show global status where Variable_name='$1';"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}'
UserParameter=mysql.variables[*],echo "show global variables where Variable_name='$1';"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|awk '{print $$2}'
UserParameter=mysql.ping,/data/usr/local/mysql/bin/mysqladmin --defaults-file=/data/usr/local/zabbix/etc/.my.cnf ping|grep -c alive
UserParameter=mysql.version,echo "select version();"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N #### MySQL Slave Information
UserParameter=mysql.slave.Seconds_Behind_Master,echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep "Seconds_Behind_Master"|awk '{print $2}'
#UserParameter=mysql.slave.Slave_IO_Running,echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep "Slave_IO_Running"|awk '{print $2}'
#UserParameter=mysql.slave.Slave_SQL_Running,echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep "Slave_SQL_Running"|awk '{print $2}'
#UserParameter=mysql.replication[*],echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep "Slave_IO_Running"|awk '{print $2}'|grep -c Yes
UserParameter=mysql.replication[*],echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes
UserParameter=mysql.slave.Relay_Log_Pos,echo "show slave status\G"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/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"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep "Exec_Master_Log_Pos"|awk '{print $2}'
UserParameter=mysql.process,echo "show processlist"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf|grep -v "Id"|wc -l #### MySQL InnoDB Information
#
UserParameter=mysql.innodb[*],/usr/local/zabbix/bin/mysql_innodb_status.sh $1
[root@DBSlave-live bin]# pwd
/data/usr/local/zabbix/bin
[root@DBSlave-live bin]# ls
mysql_health.sh mysql_innodb_status.sh zabbix_get zabbix_sender
[root@DBSlave-live bin]# cat mysql_innodb_status.sh
#!/bin/bash #Get InnoDB Row Lock Details and InnoDB Transcation Lock Memory
#/data/usr/local/mysql/bin/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
#/data/usr/local/mysql/bin/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. #/data/usr/local/mysql/bin/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 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;"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;"|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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;'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -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'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "History list length"|awk '{print $4}'
;;
Innodb_last_checkpoint_at)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Last checkpoint at"|awk '{print $4}'
;; Innodb_log_sequence_number)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Log sequence number"|awk '{print $4}'
;;
Innodb_log_flushed_up_to)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Log flushed up to"|awk '{print $5}'
;;
Innodb_open_read_views_inside_innodb)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "read views open inside InnoDB"|awk '{print $1}'
;;
Innodb_queries_inside_innodb)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "queries inside InnoDB"|awk '{print $1}'
;;
Innodb_queries_in_queue)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "queries in queue"|awk '{print $5}'
;;
Innodb_hash_seaches)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "hash searches"|awk '{print $1}'
;;
Innodb_non_hash_searches)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "non-hash searches/s"|awk '{print $4}'
;;
Innodb_node_heap_buffers)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "node heap"|awk '{print $8}'
;;
Innodb_mutex_os_waits)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Mutex spin waits"|awk '{print $9}'
;;
Innodb_mutex_spin_rounds)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Mutex spin waits"|awk '{print $6}'|tr -d ','
;;
Innodb_mutex_spin_waits)
echo 'show engine innodb status\G'|/data/usr/local/mysql/bin/mysql --defaults-file=/data/usr/local/zabbix/etc/.my.cnf -N|grep "Mutex spin waits"|awk '{print $4}'|tr -d ','
;; *)
echo "wrong parameter"
;; esac

要是不定义下面这个文件的话,直接在配置文件里面直接写用户名和密码的话会报错

/etc/zabbix/my.conf 这个文件定义的是zabbix这个mysql用户的信息,然后直接指定这个文件,如果我们在命令行直接输入zabbix的用户密码的话会一行提示,影响我们zabbix取值,例如:

zabbix 监控MySQL

如果我们直接指定了这个文件,那么那行提示就不会出现

zabbix 监控MySQL

[root@DBSlave-live etc]# ls
zabbix_agentd.conf zabbix_agentd.conf.d
[root@DBSlave-live etc]# ls -al
total 20
drwxr-xr-x 3 root root 4096 Nov 17 16:42 .
drwxr-xr-x 7 root root 4096 Nov 17 16:41 ..
-rw-r--r-- 1 root root 99 Nov 2 15:35 .my.cnf
-rw-r--r-- 1 root root 182 Nov 2 15:32 zabbix_agentd.conf
drwxr-xr-x 2 root root 4096 Nov 18 11:16 zabbix_agentd.conf.d
[root@DBSlave-live etc]# cat .my.cnf
[mysql]
user=zabbixagent
password=konzh123
[mysqladmin]
user=zabbixagent
password=konzh123

这里我们还需要修改一下zabbix客户端的配置文件

[root@DBSlave-live etc]# vim zabbix_agentd.conf

LogFile=/tmp/zabbix_agentd.log
Server=13.59.15.9
ServerActive=13.59.15.9
Hostname=DB_Slave_245
Include=/data/usr/local/zabbix/etc/zabbix_agentd.conf.d/*
UnsafeUserParameters=1 #允许自定义key
~

下面是在MySQLserver端测试一下,看一下自定义的key能不能获取到值

[root@openvpn_nagios_cacti src]# zabbix_get -s 13.59.15.45 -k "mysql.replication"
2
[root@openvpn_nagios_cacti src]# zabbix_get -s 13.59.15.45 -k "mysql.replication"
1
[root@openvpn_nagios_cacti src]# zabbix_get -s 13.59.15.45 -k "mysql.process"
4

我们都配置完成之后,我们重启一下zabbix客户端

*************************************************************************************************************

我们的脚本都写好了,通过zabbix_get也是可以获取到数据的,现在我们在zabbix上来干了

配置--->主机--->DB_Slave_245--->监控项--->创建监控项

zabbix 监控MySQL

这里我们自定义的key 需要手动输入key名称(就是你在客户端自己定义的)

然后创建触发器(triggers)

点击创建触发器--->名称--->表达式构造器--->构造

zabbix 监控MySQL

创建表达式的时候,上面的Item就是刚刚之前创建的item,自己选择一下

检测它最后一次的取值是不是小于2,定义N的值为2(因为有两个线程,IO线程和SQL线程都是YES的时候这个值才会是2),如果取得的值小于2就说明有问题啦

这里我们insert之后,点击一下增加表达式(Add),是expression下面的Add,最后才点击最下面的Add(添加触发器)

还有就是这里的严重性选择一下,这里要是不选的话,是不会触发报警的,我这里一开始忘了选,我测试的时候,明明是主从不同步的,但是就是不报警,回头看才知道这里的严重性没有选择

文章摘自:

http://blog.sctux.com/?p=396

http://www.tuicool.com/articles/eEJJzi3

http://john88wang.blog.51cto.com/2165294/1596272?utm_source=tuicool&utm_medium=referral

上一篇:iOS推送小结(证书的生成、客户端的开发、服务端的开发)


下一篇:navicat使用跳板机连接数据库-ssh