mysql_exporter的安裝和告警、面板設置


 

 

MysQL數據庫安裝(略)

 

mysql_exporter安裝

 

一、創建用於監視數據庫的用戶exporter

mysql -u root -p

mysql> set global validate_password_policy=LOW;   # 降低MySQL8 密碼規則策略,或者按規則設置密碼

mysql> show variables like "%validate%";

 

mysql> CREATE USER 'exporter'@'localhost' IDENTIFIED BY 'Prometheus' WITH MAX_USER_CONNECTIONS 5;

mysql> GRANT PROCESS, REPLICATION CLIENT, SELECT ON *.* TO 'exporter'@'localhost';

說明:使用max_user_connections參數來限制exporter用戶最大連接數,避免監控引起數據庫過載,需要注意的是該參數並不是MySQL/Mariadb每個版本都支持

 

二、安裝mysqld_exporter

下載

# wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz

tar xvf mysqld_exporter-0.12.1.linux-amd64.tar.gz  -C /usr/local/

cd /usr/local/

ln -s mysqld_exporter-0.12.1.linux-amd64/ mysqld_exporter

cd /usr/local/mysqld_exporter

 

# cat > .my.cnf <<EOF

[client]

user=exporter

password=Prometheus

EOF

 

使用systemd方式啟動 

# cat >/usr/lib/systemd/system/mysqld_exporter.service  <<EOF

[Unit]

Description=Prometheus

[Service]

Environment=DATA_SOURCE_NAME=exporter:Prometheus@(localhost:3306)/

ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf --web.listen-address=:9104

Restart=on-failure

[Install]

WantedBy=multi-user.target

EOF

 

# systemctl enable mysqld_exporter

# systemctl start mysqld_exporter

 

mysqld_exporter默認運行端口是:9104

可以通過http://數據庫服務器:9104訪問MySQLD Exporter暴露的服務

可以通過/metrics查看mysql_up指標判斷當前MySQLD Exporter是否正常連接到了MySQL實例

 

http://xxxxxxxxxx:9104/           注意不是https

 

三、在prometheus.yaml中添加mysqld_exporter的配置

- job_name: 'mysqld_exporter'

  scheme: http

  static_configs:

  - targets: ['192.168.40.200:9104']

    labels:

      app: mysqld_exporter

      role: mysqld_exporter

 

重新加載配置prometheus配置

# curl -X POST http://xxxxxxxxx:9090/-/reload

 

  

四、編寫告警規則

../prometheus/etc/

本文將規則分開寫:第一個文件用於記錄規則,第二個是告警規則。

#1.記錄規則

cat > rules/mysql_record_rule.yml <<'EOF'

groups:

- name: mysql_rules

  rules:

  - record: mysql:status

    expr: mysql_up{instance=~".*9104"}

  - record: mysql:uptime

    expr: mysql_global_status_uptime{job=~"mysqld_exporter.*"}

  - record: mysql:mysql_threads_connected

    expr: mysql_global_status_threads_connected{job=~"mysqld_exporter.*"}

  - record: mysql:mysql_threads_running

    expr: mysql_global_status_threads_running{job=~"mysqld_exporter.*"}

  - record: mysql:mysql_aborted_connects

    expr: increase(mysql_global_status_aborted_connects{job=~"mysqld_exporter.*"}[2m])

  - record: mysql:mysql_slow_queries

    expr: increase(mysql_global_status_slow_queries{job=~"mysqld_exporter.*"}[2m])

  - record: mysql:mysql_table_locks

    expr: increase(mysql_global_status_table_locks_waited{job=~"mysqld_exporter.*"}[2m])

  - record: mysql:mysql_qps

    expr: rate(mysql_global_status_queries{job=~"mysqld_exporter.*"}[2m])

EOF

 

# 2.告警規則 

cat > rules/mysql_alert_rule.yml<<'EOF'

groups:

- name: mysql_alerts

  rules:

  - alert: MySQL_Down_Alert

    expr: mysql:status==0

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 數據庫 出現異常!

      description: 主機 {{ $labels.nodename }} 上的 {{ $labels.job }} 可能存在異常,請檢查!

  - alert: MySQL_uptime_Alert

    expr: mysql:uptime<1

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 數據庫 出現異常!

      description: 主機 {{ $labels.nodename }} 數據庫狀態異常,請檢查!

  - alert: MySQL_threads_connected_Alert

    expr: mysql:mysql_threads_connected > 100

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 threads_connected 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 threads_connected 超出閾值,當前值為{{humanize $value}},請檢查!

  - alert: MySQL_threads_running_Alert

    expr: mysql:mysql_threads_running > 200

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 threads_running 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 threads_connected 超出閾值,當前值為{{humanize $value}},請檢查!

  - alert: MySQL_aborted_connects_Alert

    expr: mysql:mysql_aborted_connects > 10

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 aborted_connects 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 aborted_connects 超出閾值,當前值為{{humanize $value}},請檢查!

  - alert: MySQL_slow_queries_Alert

    expr: mysql:mysql_slow_queries > 1

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 slow_queries 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 slow_queries 超出閾值,當前值為{{humanize $value}},請檢查!

  - alert: MySQL_table_locks_Alert

    expr: mysql:mysql_table_locks > 1

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 table_locks 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 table_locks 超出閾值,當前值為{{humanize $value}},請檢查!

  - alert: MySQL_qps_Alert

    expr: mysql:mysql_qps > 500

    for: 1m

    labels:

      metric_type: db_monitor

      resource: db

      severity: critical

    annotations:

      summary: 主機 {{ $labels.nodename }} 上的數據庫指標 qps 超出閾值!

      description: 主機 {{ $labels.nodename }} 上的數據庫指標 qps 超出閾值,當前值為{{humanize $value}},請檢查!

EOF

 

Prometheus中引入相關的rule_files文件

mysql_alert_rule.yml

mysql_record_rule.yml

支持通配符

 

 重新加載配置prometheus配置

# curl -X POST http://xxxxxxxxx:9090/-/reload

 

排錯1

 多個rule_file都要以- 開頭

  

排錯2

curl -X POST http://prometheus.zq.com/-/reload

failed to reload config: one or more errors occurred while applying the new configuration (--config.file="/data/etc/prometheus.yml")

 需要對規則進行檢查

# promtool check rules /data/etc/rules/mysql_record_rule.yml

 

 告警郵件效果

 

 

 

五.自定義Dashboard展示MySQL相關指標:

根據grafana官網大牛分享的7362面板修改,https://grafana.com/grafana/dashboards/7362

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM