當前位置:
首頁 > 最新 > 顏值實力派—打造MySQL運行監控環境

顏值實力派—打造MySQL運行監控環境

關注我們獲得更多內容

關注我們獲得更多精彩

作者 | 陳龍,雲和恩墨西區工程師,一線服務過金融等行業,精通 oracle 性能優化,故障診斷,特殊恢復領域。

摘要

本文給大家推薦一款顏值實力派的監控展示軟體 Grafana,圍繞 MySQL,快速打造 Prometheus+Grafana 監控系統。

提起監控軟體,可能是這樣的:

也有可能是這樣的:

那麼今天給大家推薦一款顏值實力派的監控展示軟體Grafana 。

本文圍繞 MySQL,快速打造Prometheus+Grafana 監控系統

所需工具:

Prometheus

Grafana

mysqld_exporter

01

工具介紹

1)Prometheus,是一套開源的監控&報警&時間序列資料庫的組合,起始是由SoundCloud 公司開發的。成立於2012年,之後許多公司和組織接受和採用prometheus,他們便將它獨立成開源項目,並且有公司來運作.該項目有非常活躍的社區和開發人員,目前是獨立的開源項目,任何公司都可以使用它。

官方 GitHub 地址為:https://github.com/prometheus/prometheus

官方地址:https://prometheus.io/

主要功能:

多維 數據模型(時序由 metric 名字和 k/v 的 labels 構成)。

靈活的查詢語句(PromQL)。

無依賴存儲,支持 local 和 remote 不同模型。

採用 http 協議,使用 pull 模式,拉取數據,簡單易懂。

監控目標,可以採用服務發現或靜態配置的方式。

支持多種統計數據模型,圖形化友好。

-架構拓撲圖-

功能簡述:

prometheus server: 定期從靜態配置的 targets 或者服務發現targets 抽取的數據。

exporters:負責向prometheus server傳輸抽取數據。比如監控主機有node-exporters,mysql有MySQL server exporter。

pushgateway:主要使用場景為:Prometheus 採用 pull 模式,可能由於不在一個子網或者防火牆原因,導致 Prometheus 無法直接拉取各個 target 數據。在監控業務數據的時候,需要將不同數據匯總, 由 Prometheus 統一收集。

Alertmanager:實現prometheus的告警功能。

webui:主要通過grafana來實現webui展示。

2)Grafana

grafana 是一款採用 go 語言編寫的開源應用,主要用於大規模指標數據的可視化展現,基於商業友好的 Apache License 2.0 開源協議。

在網路架構和應用分析中最流行的時序數據展示工具,並且也在工業控制、自動化監控和過程管理等領域有著廣泛的應用。

grafana 有熱插拔控制面板和可擴展的數據源,已經支持絕大部分常用的時序資料庫

包含以下:Graphite、Elasticsearch、CloudWatch、InfluxDB、OpenTSDB、Prometheus

3)mysqld_exporter

mysqld_exporter主要用於抽取MySQL數據,回傳到Prometheus service

02

快速部署

1)安裝 grafana

登錄 grafana 官網下載界面(https://grafana.com/get)選擇下載。

下載到本地進行 yum 安裝

[root@albert monitor]# yum localinstall grafana-5.2.1-1.x86_64.rpm

Loaded plugins: refresh-packagekit, security

Setting up Local Package Process

Examining grafana-5.2.1-1.x86_64.rpm: grafana-5.2.1-1.x86_64

Marking grafana-5.2.1-1.x86_64.rpm to be installed

file:///media/disk/repodata/repomd.xml: [Errno 14] Could not open/read file:///media/disk/repodata/repomd.xml

Trying other mirror.

Resolving Dependencies

--> Running transaction check

---> Package grafana.x86_64 0:5.2.1-1 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

=================================================================================================================================================================================================

Package Arch Version Repository Size

=================================================================================================================================================================================================

Installing:

grafana x86_64 5.2.1-1 /grafana-5.2.1-1.x86_64 158 M

Transaction Summary

=================================================================================================================================================================================================

Install 1 Package(s)

Total size: 158 M

Installed size: 158 M

Is this ok [y/N]: y

Downloading Packages:

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

Installing : grafana-5.2.1-1.x86_64 1/1

### NOT starting grafana-server by default on bootup, please execute

sudo /sbin/chkconfig --add grafana-server

### In order to start grafana-server, execute

sudo service grafana-server start

Verifying : grafana-5.2.1-1.x86_64

Installed:

grafana.x86_64 0:5.2.1-1

Complete!

[root@albert monitor]#

將 grafana-server 加入開機啟動

[root@albert monitor]# service grafana-server start

Starting Grafana Server: ... [ OK ]

[root@albert monitor]# chkconfig --add grafana-server

[root@albert monitor]# service grafana-server status

grafana-server (pid 128243) is running...

埠:

[root@albert monitor]# netstat -plntu | grep grafana-serv

tcp 0 0 :::3000 :::* LISTEN 128243/grafana-serv

登錄控制台,本地 IP+3000 埠(默認)

用戶:admin

密碼:admin(默認)

主菜單:

2)部署 Prometheus

解壓安裝包:

[root@albert monitor]# tar -zxvf prometheus-2.3.1.linux-amd64.tar.gz

prometheus-2.3.1.linux-amd64/

prometheus-2.3.1.linux-amd64/console_libraries/

prometheus-2.3.1.linux-amd64/console_libraries/menu.lib

prometheus-2.3.1.linux-amd64/console_libraries/prom.lib

prometheus-2.3.1.linux-amd64/prometheus

prometheus-2.3.1.linux-amd64/consoles/

prometheus-2.3.1.linux-amd64/consoles/node.html

prometheus-2.3.1.linux-amd64/consoles/prometheus.html

prometheus-2.3.1.linux-amd64/consoles/node-disk.html

prometheus-2.3.1.linux-amd64/consoles/node-overview.html

prometheus-2.3.1.linux-amd64/consoles/node-cpu.html

prometheus-2.3.1.linux-amd64/consoles/index.html.example

prometheus-2.3.1.linux-amd64/consoles/prometheus-overview.html

prometheus-2.3.1.linux-amd64/LICENSE

prometheus-2.3.1.linux-amd64/promtool

prometheus-2.3.1.linux-amd64/NOTICE

prometheus-2.3.1.linux-amd64/prometheus.yml

配置 Prometheus:

[root@albert prometheus-2.3.1.linux-amd64]# cat prometheus.yml

# my global config

global:

scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

# scrape_timeout is set to the global default (10s).

# Alertmanager configuration

alerting:

alertmanagers:

- static_configs:

- targets:

# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global "evaluation_interval".

rule_files:

# - "first_rules.yml"

# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:

# Here it"s Prometheus itself.

scrape_configs:

# The job name is added as a label `job=` to any timeseries scraped from this config.

- job_name: "prometheus"

# metrics_path defaults to "/metrics"

# scheme defaults to "http".

static_configs:

- targets: ["172.16.94.181:9090"]

運行 Prometheus:

[root@albert prometheus-2.3.1.linux-amd64]# ./prometheus --config.file=prometheus.yml

level=info ts=2018-07-11T09:16:00.496842912Z caller=main.go:222 msg="Starting Prometheus" version="(version=2.3.1, branch=HEAD, revision=188ca45bd85ce843071e768d855722a9d9dabe03)"

level=info ts=2018-07-11T09:16:00.496913131Z caller=main.go:223 build_context="(go=go1.10.3, user=root@82ef94f1b8f7, date=20180619-15:56:22)"

level=info ts=2018-07-11T09:16:00.496933529Z caller=main.go:224 host_details="(Linux 2.6.32-431.el6.x86_64 #1 SMP Wed Nov 20 23:56:07 PST 2013 x86_64 albert (none))"

level=info ts=2018-07-11T09:16:00.496949832Z caller=main.go:225 fd_limits="(soft=1024, hard=4096)"

level=info ts=2018-07-11T09:16:00.505128258Z caller=main.go:514 msg="Starting TSDB ..."

level=info ts=2018-07-11T09:16:00.509703595Z caller=web.go:415 component=web msg="Start listening for connections" address=0.0.0.0:9090

level=info ts=2018-07-11T09:16:00.5495258Z caller=main.go:524 msg="TSDB started"

level=info ts=2018-07-11T09:16:00.549583533Z caller=main.go:603 msg="Loading configuration file" filename=prometheus.yml

level=info ts=2018-07-11T09:16:00.570661335Z caller=main.go:500 msg="Server is ready to receive web requests."

登錄Prometheus控制台,由於還沒有部署MySQL_EXPORTER,所以只有Prometheus。

3)安裝 mysqld_exporter

mysqld_exporter,負責抽取 MySQL 基礎性能數據。

下載:mysqld_exporter(prometheus.io/download/#mysqld_exporter)。當然也要下載對應系統的版本。

解壓後的內容如下:

[root@albert mysqld_exporter-0.11.0.linux-amd64]# ls -l

total 14064

-rw-r--r--. 1 3434 3434 11325 Jun 29 11:08 LICENSE

-rwxr-xr-x. 1 3434 3434 14381431 Jun 29 11:00 mysqld_exporter

-rw-r--r--. 1 3434 3434 65 Jun 29 11:08 NOTICE

進行mysqld_exporter 配置:

這裡配置需要連接監控的MySQL資料庫信息

[root@albert mysqld_exporter-0.11.0.linux-amd64]# cat .my.cnf

[client]

host = 172.16.94.181

user=root

password=xxxxx

啟動 mysqld_exporter:

[root@albert mysqld_exporter-0.11.0.linux-amd64]# ./mysqld_exporter --config.my-cnf="/monitor/mysqld_exporter-0.11.0.linux-amd64/.my.cnf"

INFO[0000] Starting mysqld_exporter (version=0.11.0, branch=HEAD, revision=5d7179615695a61ecc3b5bf90a2a7c76a9592cdd) source="mysqld_exporter.go:206"

INFO[0000] Build context (go=go1.10.3, user=root@3d3ff666b0e4, date=20180629-15:00:35) source="mysqld_exporter.go:207"

INFO[0000] Enabled scrapers: source="mysqld_exporter.go:218"

INFO[0000] --collect.global_status source="mysqld_exporter.go:222"

INFO[0000] --collect.global_variables source="mysqld_exporter.go:222"

INFO[0000] --collect.slave_status source="mysqld_exporter.go:222"

INFO[0000] --collect.info_schema.tables source="mysqld_exporter.go:222"

INFO[0000] Listening on :9104 source="mysqld_exporter.go:232"

登錄到埠 9104 的界面,可以看到抽取的數據信息。

4)在 Prometheus 中配置 mysqld_exporter

exporter啟動後,需要在 Prometheus 中正確的配置。修改 prometheus 目錄中的prometheus.yml,增加配置如下:

[root@albert prometheus-2.3.1.linux-amd64]# cat prometheus.yml

# my global config

global:

scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.

evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

# scrape_timeout is set to the global default (10s).

# Alertmanager configuration

alerting:

alertmanagers:

- static_configs:

- targets:

# - alertmanager:9093

# Load rules once and periodically evaluate them according to the global "evaluation_interval".

rule_files:

# - "first_rules.yml"

# - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:

# Here it"s Prometheus itself.

scrape_configs:

# The job name is added as a label `job=` to any timeseries scraped from this config.

- job_name: "prometheus"

# metrics_path defaults to "/metrics"

# scheme defaults to "http".

static_configs:

- targets: ["172.16.94.181:9090"]

# - job_name: mysql

- job_name: mysql

static_configs:

- targets: ["172.16.94.181:9104"]

labels:

instance: db1

備註:

job_name,當前執行job的名字

targets,連接的主機

instance,資料庫實例的標籤明

重啟 prometheus,點擊導航欄中的 status->targets 可以看到,mysql 的 exporter 已經集成進來了。

5) Grafana 和 Prometheus 關聯

現在增加數據源將 Grafana 和 Prometheus 關聯起來。點擊 Add data source,如下填寫數據保存即可:

出現 DATA SOURCE IS WORKING 表示關聯成功

6)添加儀錶盤

Grafana,提供了大量的儀錶盤展示 json,可以到官網進行選擇。

本文在 Grafana GitHub 上隨意選擇了一個 json

把 json 導入到本地

到這裡,大功完成了。一起來看一下監控儀錶盤吧!

03

錶盤內容

還可以設置告警信息:

關於定製化 Grafana 展示界面,可以參考官方文檔進行修改,裡面有具體的規則及其粒度設置信息。

下期預告:如何監控 docker

原創:陳龍。

投稿:有投稿意向技術人請在公眾號對話框留言。

轉載:意向文章下方留言。


喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 雲和恩墨 的精彩文章:

解鎖不可見索引新特性,處理ORA-01555故障
ProxySQL!像C羅一樣的強大!

TAG:雲和恩墨 |