
zabbix监控mysql的方法
使用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)
编写脚本获取mysql性能指标数据:
[root@master scripts]# pwd /etc/zabbix/scripts 脚本权限: -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 操作的数量zabbix监控mysql,一次查询只累加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 操作删除的行数;
通过以上几个参数zabbix监控mysql,可以很容易的知道当前的应用是以插入更新为主还是以查询操作为主,以及各类类型的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
为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}'
修改/etc/sudoers( chmod +s /bin/netstat也可以,省去了修改/etc/sudoers的麻烦)
#注释掉此行

Defaults requiretty#在最终加入如下行:
zabbix ALL=(ALL) NOPASSWD: /bin/netstat
重启zabbix_agentd
killall zabbix_agentd
systemctl restart zabbix-agent
在server端测试能否能获得到数据
$ zabbix_get -s10.10.50.50 -k"mysql.status[Com_rollback]"
0
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
连接模版:
本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-139160-1.html
喝了睡觉好
犯我华夏者
这样的事件最好不好在发生了
不知道在中国如何
而不是人为故意或无意放进去的