Prometheus+Grafana監控mysql主從故障告警


因業務需求,mysql主從常常出現同步故障,故研究了Prometheus來做mysql主從同步狀態監控報警。

1 Prometheus介紹

 

1.1什么是Prometheus?

Prometheus是由SoundCloud開發的開源監控報警系統和時序列數據庫(TSDB)。Prometheus使用Go語言開發,是Google BorgMon監控系統的開源版本。
2016年由Google發起Linux基金會旗下的原生雲基金會(Cloud Native Computing Foundation), 將Prometheus納入其下第二大開源項目。
Prometheus目前在開源社區相當活躍。
Prometheus和Heapster(Heapster是K8S的一個子項目,用於獲取集群的性能數據。)相比功能更完善、更全面。Prometheus性能也足夠支撐上萬台規模的集群。


1.2 Prometheus的特點

一個多維數據模型,具有由指標名稱和鍵/值對標識的時間序列數據
PromQL,一種 利用這種維度的靈活查詢語言
不依賴分布式存儲;單個服務器節點是自治的
時間序列收集通過 HTTP 上的拉模型發生
通過中間網關支持推送時間序列
通過服務發現或靜態配置發現目標
多種圖形和儀表板支持模式

1.3 Prometheus監控基本原理

Prometheus的基本原理是通過HTTP協議周期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就可以接入監控。不需要任何SDK或者其他的集成過程。這樣做非常適合做虛擬化環境監控系統,比如VM、Docker、Kubernetes等。輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系統信息(包括磁盤、內存、CPU、網絡等等)。

1.4 Prometheus服務過程

Prometheus Daemon 負責定時去目標上抓取metrics(指標)數據,每個抓取目標需要暴露一個http服務的接口給它定時抓取。Prometheus支持通過配置文件、文本文件、Zookeeper、Consul、DNS SRV Lookup等方式指定抓取目標。Prometheus采用PULL的方式進行監控,即服務器可以直接通過目標PULL數據或者間接地通過中間網關來Push數據。
Prometheus在本地存儲抓取的所有數據,並通過一定規則進行清理和整理數據,並把得到的結果存儲到新的時間序列中。
Prometheus通過PromQL和其他API可視化地展示收集的數據。Prometheus支持很多方式的圖表可視化,例如Grafana、自帶的Promdash以及自身提供的模版引擎等等。Prometheus還提供HTTP API的查詢方式,自定義所需要的輸出。
PushGateway支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。
Alertmanager是獨立於Prometheus的一個組件,可以支持Prometheus的查詢語句,提供十分靈活的報警方式。

下圖說明了 Prometheus 的架構及其一些生態系統組件

1.5 Prometheus 組件

Prometheus 生態圈中包含了多個組件,其中許多組件是可選的:

Prometheus Server: 用於收集和存儲時間序列數據。
Client Library: 客戶端庫,為需要監控的服務生成相應的 metrics 並暴露給 Prometheus server。當 Prometheus server 來 pull 時,直接返回實時狀態的 metrics。
Push Gateway: 主要用於短期的 jobs。由於這類 jobs 存在時間較短,可能在 Prometheus 來 pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端推送它們的 metrics。這種方式主要用於服務層面的 metrics,對於機器層面的 metrices,需要使用 node exporter。
Exporters: 用於暴露已有的第三方服務的 metrics 給 Prometheus。
Alertmanager: 從 Prometheus server 端接收到 alerts 后,會進行去除重復數據,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
一些其他的工具。

准備環境

2 安裝 Prometheus Server

 

2.1 運行用戶創建

groupadd prometheus
useradd -g prometheus -m -d /opt/prometheus/ -s /sbin/nologin prometheus

 

2.2 prometheus server安裝

下載地址:https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz
tar -xzvf prometheus-2.28.1.linux-amd64.tar.gz -C /opt/
ln -s /opt/prometheus-2.28.1* /opt/prometheus
chown prometheus:prometheus -R prometheus*
cd /opt/prometheus

 

2.3 prometheus配置語法校驗

建議每次修改prometheus配置之后, 都進行語法校驗, 以免導致 prometheus server無法啟動。
./promtool check config prometheus.yml

 

2.4 啟動Prometheus

此時采用默認配置啟動 prometheus server 看下界面, 稍后介紹如何監控Linux 服務器.
./prometheus --config.file=prometheus.yml

 

2.5 通過瀏覽器訪問prometheus

http:/localhost:9090/

 

 

發現 target 中只有 prometheus server, 因為我們還沒有加入其他監控, 下面進行介紹,

 

2.6 設置prometheus系統服務,並配置開機啟動

vim /usr/lib/systemd/system/prometheus.service
chown prometheus:prometheus /usr/lib/systemd/system/prometheus.service
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target

[Service]
Type=simple
User=prometheus
# --storage.tsdb.path是可選項,默認數據目錄在運行目錄的./dada目錄中
ExecStart=/opt/prometheus/prometheus --config.file=/opt/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.path=/opt/prometheus/data --storage.tsdb.retention=60d
Restart=on-failure

[Install]
WantedBy=multi-user.target

Prometheus啟動參數說明

--config.file -- 指明prometheus的配置文件路徑
--web.enable-lifecycle -- 指明prometheus配置更改后可以進行熱加載
--storage.tsdb.path -- 指明監控數據存儲路徑
--storage.tsdb.retention --指明數據保留時間

設置開機啟動

systemctl daemon-reload
systemctl enable prometheus.service
systemctl start prometheus.service
systemctl status prometheus.service

說明: prometheus在2.0之后默認的熱加載配置沒有開啟, 配置修改后, 需要重啟prometheus server才能生效, 這對於生產環境的監控是不可容忍的, 所以我們需要開啟prometheus server的配置熱加載功能。
在啟動prometheus時加上參數 web.enable
-lifecycle , 可以啟用配置的熱加載, 配置修改后, 熱加載配置: curl -X POST http://localhost:9090/-/reload

3 安裝mysql_exporter

 

3.1 下載mysql_exporter並解壓

下載地址:
https://github.com/prometheus/mysqld_exporter/releases/download/v0.12.1/mysqld_exporter-0.12.1.linux-amd64.tar.gz
tar -zxvf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /opt/
ln -s /opt/mysqld_exporter-0.12.1.linux-amd64 /opt/mysqld_exporter

 

3.2 需要授權用戶給exporter使用

create user 'exporter'@'localhost' identified by '123456';
grant process,replication client,select on *.* to 'exporter'@'localhost';

在mysqld_exporter路徑下創建my.cnf,添加剛才創建的exporter用戶和密碼
vim /opt/mysqld_exporter/my.cnf
[client]
user=exporter
password=123456

 

3.3 添加system系統服務

vim /usr/lib/systemd/system/mysqld_exporter.service
[Unit]
Description=mysqld_exporter
After=network.target

[Service]
User=root
Type=simple
ExecStart=/opt/mysqld_exporter/mysqld_exporter --config.my-cnf /opt/mysqld_exporter/my.cnf --collect.info_schema.processlist

Restart=on-failure

[Install]
WantedBy=multi-user.target

 

3.4 啟動添加的system服務

systemctl daemon-reload
systemctl start mysqld_exporter.service
netstat -lntup | grep "9104"

 

3.5 修改prometheus.yml

vim /opt/prometheus/prometheus.yml
- job_name: 'mysql-mater-slave'
scrape_interval: 5s
static_configs:
- targets: ['192.168.220.130:9104','192.168.220.131:9104']

重啟prometheus服務

systemctl restart prometheus.service

4 安裝Grafana

 

4.1 下載並安裝grafana

下載地址: https://grafana.com/grafana/download
wget https://dl.grafana.com/oss/release/grafana-7.2.1-1.x86_64.rpm
yum -y linstall grafana-7.2.1-1.x86_64.rpm
systemctl start grafana-server

granafa默認端口為3000,可以在瀏覽器中輸入http://localhost:3000/
granafa首次登錄賬戶名和密碼admin/admin,可以修改
配置數據源Data sources->Add data source -> Prometheus,輸入prometheus數據源的信息,主要是輸入name和url

4.2導入模板

下載模板
https://grafana.com/grafana/dashboards/7371
https://grafana.com/grafana/dashboards/7362
https://grafana.com/grafana/dashboards/7365

在grafana中導入dashboard

 

 

 

 

 

 

7371、7362兩個模板導入如上通。

7371

 

 

 5 安裝alertmanager

 

5.1下載安裝包並配置

下載地址:https://prometheus.io/download/
https://github.com/prometheus/alertmanager/releases/download/v0.21.0/alertmanager-0.21.0.linux-amd64.tar.gz

tar -xzvf alertmanager-0.21.0.linux-amd64.tar.gz -C /opt
ln -s /opt/alertmanager-0.21.0.linux-amd64 /opt/alertmanager
cd /opt/alertmanager && mv alertmanager.yml alertmanager.yml_bak

vim /opt/alertmanager/alertmanager.yml


global:
smtp_smarthost: smtp.exmail.xxx.com:465 # 發件人郵箱smtp地址
smtp_auth_username: xxxx@xxx.com # 發件人郵箱賬號
smtp_from: xxx@xxx.com # 發件人郵箱賬號
smtp_auth_password: xxxxxx # 發件人郵箱密碼(郵箱授權碼)
resolve_timeout: 5m
smtp_require_tls: false

 
         

route:
# group_by: ['alertname'] # 報警分組依據
group_wait: 10s # 最初即第一次等待多久時間發送一組警報的通知
group_interval: 10s # 在發送新警報前的等待時間
repeat_interval: 1m # 發送重復警報的周期 對於email配置中多頻繁
receiver: 'email'

 
         

receivers:
- name: email
email_configs:
- send_resolved: true
to: xxx@xxx.com, xxx@xxx.com # 收件人郵箱賬號

 
         

檢驗配置文件
[root@prometheus /opt/alertmanager]# ./amtool check-config conf/alertmanager.yml

 

5.2設置alertmanager系統服務,並配置開機啟動

vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/opt/alertmanager/alertmanager --config.file=/opt/alertmanager/alertmanager.yml --storage.path=/opt/alertmanager/data
Restart=on-failure
[Install]
WantedBy=multi-user.target

設置開機啟動
systemctl daemon-reload
systemctl enable alertmanager.service
systemctl start alertmanager.service
systemctl status alertmanager.service

 

5.3 prometheus配置

在prometheus目錄下編輯報警模版mysql_rules.yml,添加一些自定義報警項。

vim /opt/prometheus/rules/mysql_rules.yml
groups:
- name: MySQL-rules
  rules:
  - alert: MySQL Status 
    expr: up == 0
    for: 5s 
    labels:
      severity: warning
    annotations:
      summary: "{{$labels.instance}}: MySQL has stop !!!"
      description: "檢測MySQL數據庫運行狀態"

  - alert: MySQL Slave IO Thread Status
    expr: mysql_slave_status_slave_io_running == 0
    for: 5s 
    labels:
      severity: warning
    annotations: 
      summary: "{{$labels.instance}}: MySQL Slave IO Thread has stop !!!"
      description: "檢測MySQL主從IO線程運行狀態"

  - alert: MySQL Slave SQL Thread Status 
    expr: mysql_slave_status_slave_sql_running == 0
    for: 5s 
    labels:
      severity: warning
    annotations: 
      summary: "{{$labels.instance}}: MySQL Slave SQL Thread has stop !!!"
      description: "檢測MySQL主從SQL線程運行狀態"

  - alert: MySQL Slave Delay Status 
    expr: mysql_slave_status_sql_delay == 30
    for: 5s 
    labels:
      severity: warning
    annotations: 
      summary: "{{$labels.instance}}: MySQL Slave Delay has more than 30s !!!"
      description: "檢測MySQL主從延時狀態"


  - alert: Mysql_Too_Many_Connections
    expr: rate(mysql_global_status_threads_connected[5m]) > 200
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "{{$labels.instance}}: 連接數過多"
      description: "{{$labels.instance}}: 連接數過多,請處理 ,(current value is: {{ $value }})"  
 
  - alert: Mysql_Too_Many_slow_queries
    expr: rate(mysql_global_status_slow_queries[5m]) > 3
    for: 2m
    labels:
      severity: warning
    annotations:
      summary: "{{$labels.instance}}: 慢查詢有點多,請檢查處理"
      description: "{{$labels.instance}}: Mysql slow_queries is more than 3 per second ,(current value is: {{ $value }})"

prometheus目錄下編輯prometheus的配置文件,將監控的配置信息添加到prometheus.yml。如下圖所示

 

檢查配置
./promtool check config prometheus.yml
重啟Prometheus加載配置
systemctl restart prometheus.service

 

5.4 安裝sendmail服務

yum -y install sendmail

 

5.5 修改granfana.ini配置文件

vim /etc/grafana/grafana.ini 
#################################### SMTP / Emailing ##########################
[smtp]
;enabled = false
;host = localhost:25
;user =
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
;password =
;cert_file =
;key_file =
;skip_verify = false
;from_address = admin@grafana.localhost
;from_name = Grafana
# EHLO identity in SMTP dialog (defaults to instance_name)
;ehlo_identity = dashboard.example.com

enabled = true
host = smtp.exmail.qq.com:465
user = 發件郵箱地址
password = GIliu.123
skip_verify = true
from_address = 發件郵箱地址
from_name = Grafana

重啟grafana加載配置
systemctl restart grafana-server

 

5.6grafana上測試郵件發送

 

郵箱查驗

 

5.7驗證郵件報警

登陸prometheus的web頁面,查看報警信息。

瀏覽器輸入Prometheus_IP:9090 ,可以看到各個報警項的狀態。

停掉主從線程,模擬觸發報警

Slave SQL線程停掉后,報警項顏色變成黃色,持續時間超過定義的持續時間后,顏色變紅,並發送郵件。

 

收到報警郵件

 


免責聲明!

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



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