# Promethues监控MYSQL,Redis ##

1. MYSQL监控

1. 官网下载MYSQL-exporter

[root@autoops ~]# mkdir mysql_exporter
[root@autoops mysql_exporter]#wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.15.1/mysqld_exporter-0.15.1.linux-amd64.tar.gz

[root@autoops mysql_exporter]# tar xvfz mysqld_exporter-0.15.1.linux-amd64.tar.gz 
mysqld_exporter-0.15.1.linux-amd64/
mysqld_exporter-0.15.1.linux-amd64/LICENSE
mysqld_exporter-0.15.1.linux-amd64/mysqld_exporter
mysqld_exporter-0.15.1.linux-amd64/NOTICE

2. 关闭防火墙以及selinux

#查看防火墙状态
systemctl status firewalld
#关闭防火墙
systemctl stop firewalld
#查看selinux
getenforce
#永久关闭selinux
vi /etc/selinux/config
#将SELINUX=enforcing改为SELINUX=disabled

3. 登录MYSQL创建监控需要的用户

[root@autoops mysqld_exporter-0.15.1.linux-amd64]# mysql -usspaas -pSspaas@1233
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 133082
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'Admin@1233';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

4. 书写exporter配置文件

[root@autoops mysqld_exporter-0.15.1.linux-amd64]# vim .my.cnf
[client]
user=exporter
password=Admin@1233ca

5. 配置systemd启动

[root@autoops mysqld_exporter]# cat /etc/systemd/system/mysqld_exporter.service
[Unit]
Description=Prometheus MySQL Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=root
Group=root
Type=simple
ExecStart=/root/mysql_exporter/mysqld_exporter/mysqld_exporter --config.my-cnf=/root/mysql_exporter/mysqld_exporter/.my.cnf
Restart=always

[Install]
WantedBy=multi-user.target

[root@autoops mysqld_exporter]# systemctl daemon-reload
[root@autoops mysqld_exporter]# systemctl start mysqld_exporter
[root@autoops mysqld_exporter]# systemctl enable mysqld_exporter

6. promethues配置

[root@k8s-master-1 jianshao]# cat prometheus/prometheus-configmap.yaml
****
    - job_name: 'mysql'
      static_configs:
      - targets: ['10.101.0.91:9104']

****
    - name: mysql_alerts
      rules:
      - alert: MySQL实例宕机
        expr: up{job="mysql"} == 0
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "MySQL实例宕机"
          description: "MySQL实例已经宕机超过5分钟。"

      - alert: MySQL高CPU使用率
        expr: rate(process_cpu_seconds_total{job="mysql"}[1m]) > 0.8
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "MySQL高CPU使用率"
          description: "MySQL实例的CPU使用率超过80%超过5分钟。"

      - alert: MySQL高内存使用率
        expr: process_resident_memory_bytes{job="mysql"} / process_virtual_memory_bytes{job="mysql"} > 0.8
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "MySQL高内存使用率"
          description: "MySQL实例的内存使用率超过80%超过5分钟。"

      - alert: MySQL慢查询
        expr: rate(mysql_global_status_slow_queries{job="mysql"}[5m]) > 10
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "MySQL慢查询"
          description: "MySQL实例的慢查询数量每分钟超过10次,持续5分钟。"

      - alert: MySQL线程连接数高
        expr: mysql_global_status_threads_connected{job="mysql"} / mysql_global_variables_max_connections{job="mysql"} > 0.9
        for: 5m
        labels:
          severity: critical
        annotations:
          summary: "MySQL线程连接数高"
          description: "MySQL实例的连接数超过最大连接数的90%,持续5分钟。"

[root@k8s-master-1 prometheus]# curl -X POST http://10.0.200.6:9090/-/reload

7.查看指标

image-20240722142605694

2. Redis监控

1. 容器启动Redis_exporter监控

docker run -d --name redis_exporter -p 9121:9121 registry.cn-hangzhou.aliyuncs.com/beitangxingyun/redis_exporter --redis.addr=redis://10.101.0.91:6379 --redis.password='123456' --skip-tls-verification

2. 配置promethues

****
    - job_name: 'redis'
      static_configs:
      - targets: ['10.101.0.91:9121']
****
    - name: redis_alerts
      rules:
      - alert: Redis实例宕机
        expr: redis_up == 0
        for: 1m
        labels:
          severity: critical
        annotations:
          summary: "Redis实例宕机"
          description: "Redis实例已经宕机超过1分钟。"

      - alert: Redis连接数过多
        expr: redis_connected_clients > 200
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis连接数过多"
          description: "Redis实例的连接数超过200,持续5分钟。"

      - alert: Redis CPU使用率过高
        expr: rate(redis_cpu_sys_seconds_total[5m]) > 0.8
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis CPU使用率过高"
          description: "Redis实例的CPU使用率超过80%超过5分钟。"

      - alert: Redis实例重启
        expr: changes(redis_uptime_in_seconds[5m]) > 0
        for: 1m
        labels:
          severity: warning
        annotations:
          summary: "Redis实例重启"
          description: "Redis实例在过去1分钟内重启过。"

      - alert: Redis阻塞命令过多
        expr: redis_blocked_clients > 50
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis阻塞命令过多"
          description: "Redis实例的阻塞命令数超过50,持续5分钟。"

      - alert: Redis内存碎片率异常
        expr: (redis_mem_fragmentation_ratio > 4) or (redis_mem_fragmentation_ratio < 0.5)
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "Redis内存碎片率异常"
          description: "Redis实例的内存碎片率超过4或低于0.5,持续5分钟。" 

3. 查看监控指标

image-20240722140115896

3. 确保能查询到指标

image-20240722142546492
image-20240722142629464

posted @ 2024-07-22 14:36  整点薯条儿  阅读(17)  评论(0)    收藏  举报