前言
最近愛上了研究各種雜七雜八的技術。
Prometheus是現如今最火的監控軟件之一。做為一個運維DBA,不會這個可就OUT了。
本篇博客,演示一下prometheus之通過mysql exporter+node exporter監控mysql,並使用grafana進行圖表展示。
概述
prometheus是由SoundCloud開發的開源監控告警系統並且自帶時序數據庫,基於Go語言。Prometheus根據配置的任務(job)以周期性pull的方式獲取指定目標(target)上的指標(metric)。
Prometheus 生態圈中包含了多個組件:
Prometheus Server: 根據配置完成數據采集, 服務發現以及數據存儲。
Push Gateway : 為應對部分push場景提供的插件,監控數據先推送到 Push Gateway 上,然后再由 Prometheus Server 端采集 pull 。用於存在時間較短,可能在 Prometheus 來 pull 之前就消失了的 jobs (若 Prometheus Server 采集間隔期間,Push Gateway 上的數據沒有變化, Prometheus Server 將采集到2次相同的數據,僅時間戳不同)
Exporters(探針): 是Prometheus的一類數據采集組件的總稱。它負責從目標處搜集數據,並將其轉化為Prometheus支持的格式。與傳統的數據采集組件不同的是,它並不向中央服務器發送數據,而是等待中央服務器主動前來抓取。
Alertmanager: Prometheus server 主要負責根據基於PromQL的告警規則分析數據,如果滿足PromQL定義的規則,則會產生一條告警,並發送告警信息到Alertmanager,Alertmanager則是根據配置處理告警信息並發送。常見的接收方式有:電子郵件,webhook 等。Alertmanager三種處理告警信息的方式:分組,抑制,靜默。
開始演示
測試機器
prometheus-server 192.168.56.140
MySQL host01 192.168.56.103
MySQL host02 192.168.56.104
配置mysql host01
MySQL使用版本
8.0.25 MySQL Community Server
創建exporter帳號
mysqld_exporter通過查詢mysql的狀態表及狀態命令獲取數據。所以,需要先在mysql內,創建相應帳號
create user 'exporter'@'%' identified by 'Xiaopang*803'; GRANT REPLICATION CLIENT, PROCESS ON *.* TO 'exporter'@'%'; GRANT SELECT ON performance_schema.* TO 'exporter'@'%'; flush privileges;
下載,安裝mysqld_exporter
wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.13.0/mysqld_exporter-0.13.0.linux-amd64.tar.gz
tar xvzf mysqld_exporter-0.13.0.linux-amd64.tar.gz -C /usr/local/.
cd /usr/local && ln -s mysqld_exporter-0.13.0.linux-amd64/ mysqld_exporter
編緝如下文件,輸入exporter用戶句與密碼(與前面mysql內創建的帳號密碼一致)
[root@host01 mysqld_exporter]# vi .my.cnf
[client]
user=exporter
password=Xiaopang*803
添加啟動服務文件
[root@host01 ~]# vi /etc/systemd/system/mysqld_exporter.service
[Unit] Description=mysqld_exporter After=network.target [Service] Type=simple ExecStart=/usr/local/mysqld_exporter/mysqld_exporter --config.my-cnf=/usr/local/mysqld_exporter/.my.cnf Restart=on-failure [Install] WantedBy=multi-user.target
啟動mysqld_exporter
service mysqld_exporter start
測試驗證
mysqld_exporter默認使用9104端口,我們可以在瀏覽器內輸入如下地址。查看是否有數據輸出。
輸入 http://192.168.56.103:9104/metrics
輸出信息類似如下
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 2.5395e-05 go_gc_duration_seconds{quantile="0.25"} 3.5372e-05 go_gc_duration_seconds{quantile="0.5"} 3.9393e-05 go_gc_duration_seconds{quantile="0.75"} 5.5068e-05 go_gc_duration_seconds{quantile="1"} 0.062537624 go_gc_duration_seconds_sum 0.453204071 go_gc_duration_seconds_count 2131 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge
下載,安裝node_exporter
如果只安裝mysqld_exporter則無法監控OS相關的數據,所以需要安裝node_exporter進行OS監控。
wget https://github.com/prometheus/node_exporter/releases/download/v1.2.2/node_exporter-1.2.2.linux-amd64.tar.gz
tar xvzf node_exporter-1.2.2.linux-amd64.tar.gz -C /usr/local/.
cd /usr/local && ln -s node_exporter-1.2.2.linux-amd64/ node_exporter
添加啟動服務文件
[root@host01 ~]# vi /etc/systemd/system/node_exporter.service
[Unit] Description=node_export Documentation=https://github.com/prometheus/node_exporter After=network.target [Service] Type=simple User=root Group=root ExecStart=/usr/local/node_exporter/node_exporter Restart=on-failure [Install] WantedBy=multi-user.target
啟動node_exporter
service node_exporter start
測試驗證
node_exporter默認使用9100端口,我們可以在瀏覽器內輸入如下地址。查看是否有數據輸出。
輸入 http://192.168.56.103:9100/metrics
輸出結果類似如下
# HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 2.5934e-05 go_gc_duration_seconds{quantile="0.25"} 4.0072e-05 go_gc_duration_seconds{quantile="0.5"} 4.7616e-05 go_gc_duration_seconds{quantile="0.75"} 6.726e-05 go_gc_duration_seconds{quantile="1"} 0.228887598 go_gc_duration_seconds_sum 0.550266258 go_gc_duration_seconds_count 793 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge
安裝prometheus+grafana
使用版本
prometheus 2.28
grafana 6.7.6
安裝過程
下載軟件包
wget https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz
解壓並添加軟鏈接
tar xvzf prometheus-2.28.1.linux-amd64.tar.gz -C /usr/local/. cd /usr/local/ ln -s prometheus-2.28.1.linux-amd64/ prometheus
增加啟動服務
[root@prometheus-server prometheus]# vi /etc/systemd/system/prometheus.service
[Unit] Description=Prometheus Monitoring System Documentation=Prometheus Monitoring System [Service] Type=simple User=root Group=root ExecStart=/usr/local/prometheus/prometheus \ --config.file=/usr/local/prometheus/prometheus.yml \ --storage.tsdb.path="data/" \ --storage.tsdb.retention.time=15d \ --web.max-connections=512 \ --web.listen-address=:9090
添加mysql監控
vi /usr/local/prometheus/prometheus.yml
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'mysql' static_configs: - targets: ['192.168.56.103:9104'] labels: instance: mysql_instance1 - job_name: 'linux' static_configs: - targets: ['192.168.56.103:9100'] labels: instance: mysql_instance1
啟動prometheus
service prometheus start
查看prometheus
prometheus默認監控端口
http://192.168.56.140:9090/
點擊status->target。如果一切正常,可以看到如下mysql/linux的state為UP
下載,安裝grafana
wget https://dl.grafana.com/oss/release/grafana-6.7.6-1.x86_64.rpm
rpm -ivh grafana-6.7.6-1.x86_64.rpm
訪問grafana
prometheus的展示功能很弱,為了更好的進行圖形展示,所以我們需要grafana
輸入 http://192.168.56.140:3000/
配置data source為proemtheus的HTTP鏈接(注意是HTTP,而不是HTTPS)
導入mysql監控模板
grafana數據的展示是通過模板實現的。grafana網站上面有很多共享的模板,你可以自行探索。
本例模板,我是從如下鏈接下載的。
https://grafana.com/api/dashboards/9623/revisions/4/download
版本不匹配問題
因為版本不太匹配的原因,完成后有些項目如法正常顯示。它使用的版本是grafana5.0版本,我的是6.x版本。
但是這點小問題,難不倒我,我自己修改了一下。就能正常顯示了,如下是修改后的JSON文件。
https://pan.baidu.com/s/1d8Nwdzn9_J9qAjHR44mM3w 提取碼:abcd
修改過程
很多時候,很多東西並不完全能拿來即用。我們需要根據自己的需要進行一些修改。
接下來大概花了半個多小時,弄清楚了如何修改了。而后大概又花了兩小時,修改完成的相應的項目。
修改過程中,碰到的問題,主要就兩類:
1)grafana 5.x和6.x組件的名稱發生了變化。
"Buffer Pool Size of Total RAM"無法正常顯示,原因是6.0和5.0組件名不同。
替換 pmm-singlestat-panel -> singlestat搞定
2)exporter提取的屬性名字發生了變化
我用的是node_exporter-1.2.2,這里面關於OS提取的屬性與JSON文件的定義屬性名不匹配。
方法是直接在“http://192.168.56.103:9100/metrics”里面搜索新的屬性名,替換JSON文件里面的舊的屬性名。
例如:
替換 node_memory_MemTotal->node_memory_MemTotal_bytes
替換 node_memory_MemTotal->node_memory_MemTotal_bytes
進行導入
因為我進行了一些修改,你可以import的時候,直接把JSON的內容輸入進去。
點擊Load加載,接下來,選擇數據源為prometheus。
啟動sysbench壓測工具
開啟sysbench工具的目的是通過壓測生成有數據變化的圖表(不然,沒有流量,數據也不會動)。
這里,我從遠端壓測(在另一台機器host02上運行sysbench)。目的是為了生成網絡流量數據。
[root@host02 ~]# sysbench /usr/share/sysbench/oltp_read_write.lua --time=9180 --mysql-host=host01 --mysql-port=3306 --mysql-user=dbusr --mysql-password=Xiaopang*803 --mysql-db=db1 --table-size=50000 --tables=15 --threads=15 --report-interval=10 run sysbench 1.0.20 (using bundled LuaJIT 2.1.0-beta2) Running the test with following options: Number of threads: 15 Report intermediate results every 10 second(s) Initializing random number generator from current time Initializing worker threads... Threads started! [ 10s ] thds: 15 tps: 112.68 qps: 2268.92 (r/w/o: 1589.76/452.30/226.85) lat (ms,95%): 277.21 err/s: 0.00 reconn/s: 0.00 [ 20s ] thds: 15 tps: 113.91 qps: 2282.81 (r/w/o: 1598.47/456.52/227.81) lat (ms,95%): 211.60 err/s: 0.00 reconn/s: 0.00 [ 30s ] thds: 15 tps: 109.80 qps: 2192.95 (r/w/o: 1536.66/436.69/219.59) lat (ms,95%): 240.02 err/s: 0.00 reconn/s: 0.00 [ 40s ] thds: 15 tps: 112.70 qps: 2265.36 (r/w/o: 1583.17/456.79/225.40) lat (ms,95%): 193.38 err/s: 0.00 reconn/s: 0.00 [ 50s ] thds: 15 tps: 101.00 qps: 2013.42 (r/w/o: 1413.32/398.10/202.00) lat (ms,95%): 325.98 err/s: 0.00 reconn/s: 0.00
查看grafana,完成后效果
這里只貼出了部分圖表。
MySQL信息展示
OS信息展示
接下來
有空的時候,下一節我再給大家演示一下prometheus如何配置告警相關內容。
參考文檔
https://grafana.com/grafana/https://prometheus.io/docs/introduction/overview/
https://blog.csdn.net/weixin_38645718/article/details/85111527
https://blog.51cto.com/u_1000682/2374386
https://blog.csdn.net/eastyell/article/details/112232691