Zabbix监控MySQL数据库实战

Zabbix 监控mysql数据库系统性能及mysql主从状态

zabbix监控mysql的方式

  • 只是安装agent
  • 启用模板监控
  • 启用自定义脚本的模板监控

使用zabbix模版及结合shell脚本监控mysql

创建mysql的zabbix授权用户

mysql> grant all PRIVILEGES on *.* to zabbix@'localhost' identified by 'zabbix'; 

###创建一个有权限的访问用户lqb密码设置zabbix

Query OK, 0 rows affected (0.04 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)flush privileges;

Query OK, 0 rows affected (0.01 sec)

编写脚本获取mysql性能指标数据:

[root@master scripts]# pwd
/etc/zabbix/scripts

脚本权限:

-rwxr-xr-x. 1 root root 2695 Jun  6 13:39 chk_mysql.sh脚本权限:

-rwxr-xr-x. 1 root root 2695 Jun  6 13:39 chk_mysql.sh
vim chk_mysql.sh

# 用户名
#MYSQL_USER='zabbix'

# 密码
#MYSQL_PWD='zabbix' #在脚本中输入密码mysql会提示安全告警信息#Warning: Using a password on the command line interface can be #insecure,需要将帐号密码等配置添加到mysql配置文件my.cnf中即可,脚本中#不用输入账号密码

# 主机地址/IP
MYSQL_HOST='127.0.0.1'

# 端口
MYSQL_PORT='3306'

# 数据连接
MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"

# 参数是否正确
if [ $# -ne "1" ];then
echo "arg error!"
fi

# 获取数据
case $1 in
Uptime)
result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
echo $result
;;
Com_update)
result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
echo $result
;;
Slow_queries)
result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
echo $result
;;
Com_select)
result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
echo $result
;;
Com_rollback)
result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
echo $result
;;
Questions)
result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
echo $result
;;
Com_insert)
result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
echo $result
;;
Com_delete)
result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
echo $result
;;
Com_commit)
result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
echo $result
;;
Bytes_sent)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
echo $result
;;
Bytes_received)
result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
echo $result
;;
Com_begin)
result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
echo $result
;;

*)
echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
;;
esac

以下几个参数对Myisam 和Innodb 存储引擎都计数:
1. Com_select 执行select 操作的次数,一次查询只累加1;
2. Com_insert 执行insert 操作的次数,对于批量插入的insert 操作,只累加一次;
3. Com_update 执行update 操作的次数;
4. Com_delete 执行delete 操作的次数;
以下几个参数是针对Innodb 存储引擎计数的:
1. Innodb_rows_read select 查询返回的行数;
2. Innodb_rows_inserted 执行Insert 操作插入的行数;
3. Innodb_rows_updated 执行update 操作更新的行数;
4. Innodb_rows_deleted 执行delete 操作删除的行数;
通过以上几个参数,可以很容易的了解当前数据库的应用是以插入更新为主还是以查询操作为主,以及各种类型的SQL 大致的执行比例是多少。对于更新操作的计数,是对执行次数的计数,不论提交还是回滚都会累加。对于事务型的应用,通过Com_commit 和Com_rollback 可以了解事务提交和回滚的情况,对于回滚操作非常频繁的数据库,可能应用编写存在问题。
另外还有几个参数可以了解数据库的基本信息:
1. Connections 试图连接Mysql 服务器的次数
2. Uptime 服务器工作时间
3. Slow_queries 慢查询的次数

在脚本中输入密码mysql会提示安全告警信息使zabbix获取不到正确数据,(Warning: Using a password on the command line interface can be insecure),需要将帐号密码等配置添加到mysql配置文件my.cnf中即可,脚本中不用输入账号密码,重启mysql配置生效

vim /etc/my.cnf

[client]

user=zabbix

password=zabbix

[mysqladmin]

host=localhost

user=zabbix

password=zabbix[client]

user=zabbix

password=zabbix

[mysqladmin]

host=localhost

user=zabbix

password=zabbix

为zabbix_agent添加用户参数

vim /usr/local/etc/zabbix_agentd.conf

UnsafeUserParameters=1
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/opt/scripts/chk_mysql.sh $1
#UserParameter=mysql.ping,mysqladmin -P3306 -h127.0.0.1 ping | grep -c alive
UserParameter=mysql.ping,sudo netstat -ntpl |grep 3306|grep mysql|wc |awk '{print $1}'UnsafeUserParameters=1
UserParameter=mysql.version,mysql -V
UserParameter=mysql.status[*],/opt/scripts/chk_mysql.sh $1
#UserParameter=mysql.ping,mysqladmin -P3306 -h127.0.0.1 ping | grep -c alive
UserParameter=mysql.ping,sudo netstat -ntpl |grep 3306|grep mysql|wc |awk '{print $1}'
修改/etc/sudoers( chmod +s /bin/netstat也可以,省去了修改/etc/sudoers的麻烦)

#注释掉此行

Defaults    requiretty   
#在最后加入如下行:
zabbix ALL=(ALL)   NOPASSWD:  /bin/netstat

重启zabbix_agentd

<span style="color:#ff0000">killall zabbix_agentd
systemctl restart zabbix-agent</span>

在server端测试是否能获取到数据

$ zabbix_get -s10.10.50.50 -k"mysql.status[Com_rollback]"

00

mysql主从状态监控

写一个脚本实现主从监控:

这个脚本主要用于获取MYSQL主从同步信息;

我们先执行一个命令

mysql -u zabbix -e 'show slave status\G'

我们在输出的信息中选择

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

这两项来监控,我测试了一下,当操作的数据出现异常的时候,Slave_SQL_Running就会变成No,当执行slave stop的时候,两个都会变成No;

授权:

grant all PRIVILEGES on *.* to zabbix@'localhost' identified by 'zabbix';

flush privileges;


编写主从监控脚本:

[root@master scripts]# pwd

/etc/zabbix/scripts

vim mysql_master.sh

#!/bin/bash
mysql -uzabbix -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes

为agent添加用户参数:

vim /usr/local/etc/zabbix_agentd.conf

UserParameter=system.mysql,/etc/zabbix/scripts/mysql_master.sh

重启agent

systemctl restart zabbix-agent

连接模版:

<span style="color:#33cc00">https://download.csdn.net/download/abel_dwh/10510113</span>

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

abel_dwh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值