zabbix 监控mysql

 

1. ping检测

1.1 创建密码文件,如果是二进制安装,需要将该文件存放etc/目录下

cat /etc/zabbix/.my.cnf

[client]
user=
password=

[mysql]
user=
password=

[mysqladmin]
user=
password=

1.2. 配置监控项,不建议对本机进行ping测试,应至少两台数据库服务器进行ping测试

UserParameter=mysql_ping,HOME=/etc/zabbix /usr/bin/mysqladmin  -h172.16.xxx.xxx ping | grep -c alive

1.3.  重启zabbix-agent

1.4 创建模板或监控项-触发器等

2. 状态监测

2.1 脚本

import json
import sys
import pymysql
from cryptography.hazmat.backends import default_backend

host = "192.168.20.232"
password = "Mysql@123456.."
conn = pymysql.connect(host=host, user='root', password=password)
cursor = conn.cursor()


class MysqlGlobalStatus:
    def __init__(self, status):
        self.status = status

    def show_status(self):
        cursor.execute("show global status")
        data = dict(cursor.fetchall())
        return data[self.status]


if __name__ == '__main__':
    # status = sys.argv[1]
    # x = MysqlGlobalStatus(status)
    # print(int(x.show_status()))
    x = MysqlGlobalStatus("Max_execution_time_exceeded")
    print(int(x.show_status()))
    cursor.close()
    conn.close()

2.2. 配置监控参数

UserParameter=mysql_get_status[*],python3 -W ignore  /etc/zabbix/zabbix_agentd.conf.d/MysqlGlobalStatus.py "$1"

2.3. 创建监控模板

其他

非交互式执行命令

mysql -uroot -p -e "show global status" >sql.txt

一些参数解释

-- 查看查询时间超过long_query_time秒的查询的个数。
Slow_queries

-- 查看创建时间超过slow_launch_time秒的线程数。
Slow_launch_threads

Threads_connected  当前打开的连接数。

--查看线程缓存内的线程的数量。
Threads_cached

--查看创建用来处理连接的线程数。如果Threads_created较大,你可能要增加thread_cache_size值。
Threads_created

--查看激活的(非睡眠状态)线程数。
Threads_running

-- 查看不能立即获得的表的锁的次数。如果该值较高,并且有性能问题,你应首先优化查询,然后拆分表或使用复制。
Table_locks_waited

--查看立即获得的表的锁的次数。
Table_locks_immediate

-- 查看创建时间超过slow_launch_time秒的线程数。
Slow_launch_threads

-- 锁定状态
mysql> show global  status like '%lock%';

Table_locks_waited/Table_locks_immediate=0.3% 如果这个比值比较大的话,说明表锁造成的阻塞比较严重
Innodb_row_lock_waits innodb行锁,太大可能是间隙锁造成的

-- Binlog Cache 使用状况
 show status like 'Binlog_cache%';

如果Binlog_cache_disk_use值不为0 ,可能需要调大 binlog_cache_size大小


-- Innodb_log_waits 量
Innodb_log_waits   日志缓冲区太小而需要等待刷新才能继续的次数,Innodb_log_waits值不等于0的话,表明 innodb log buffer 因为空间不足而等待

Aborted_connects   连接到 MySQL 服务器的失败尝试次数

Connection_errors_max_connections    由于 max_connections达到服务器限制而拒绝的连接数。

Mysqlx_connection_errors  导致错误的连接数。

Connection_errors_accept  accept()在侦听端口上 调用期间发生的错误数 。

Connection_errors_internal  由于服务器内部错误(例如无法启动新线程或内存不足情况)而被拒绝的连接数。

Mysqlx_sessions    已打开的会话数

Max_execution_time_exceeded SELECT超过执行超时 的语句数。

官方文档:MySQL :: MySQL 8.0 Reference Manual :: 5.1.10 Server Status Variables

MySQL :: MySQL 5.7 参考手册 :: 19.4.2.3 X 插件状态变量

 

posted @ 2022-12-05 14:13  不会跳舞的胖子  阅读(969)  评论(0编辑  收藏  举报