本文給大家推薦一款顏值實(shí)力派的監(jiān)控展示軟件 Grafana,圍繞 MySQL,快速打造 Prometheus+Grafana 監(jiān)控系統(tǒng)。 提起監(jiān)控軟件,可能是這樣的: 也有可能是這樣的: 那么今天給大家推薦一款顏值實(shí)力派的監(jiān)控展示軟件 Grafana 。 本文圍繞 MySQL,快速打造 Prometheus+Grafana 監(jiān)控系統(tǒng)。 所需工具: Prometheus Grafana mysqld_exporter
1)Prometheus,是一套開源的監(jiān)控&報(bào)警&時(shí)間序列數(shù)據(jù)庫(kù)的組合,起始是由SoundCloud 公司開發(fā)的。成立于2012年,之后許多公司和組織接受和采用prometheus,他們便將它獨(dú)立成開源項(xiàng)目,并且有公司來運(yùn)作.該項(xiàng)目有非?;钴S的社區(qū)和開發(fā)人員,目前是獨(dú)立的開源項(xiàng)目,任何公司都可以使用它。 官方 GitHub 地址為:https://github.com/prometheus/prometheus 官方地址:https:/// 主要功能: 多維 數(shù)據(jù)模型(時(shí)序由 metric 名字和 k/v 的 labels 構(gòu)成)。 靈活的查詢語句(PromQL)。 無依賴存儲(chǔ),支持 local 和 remote 不同模型。 采用 http 協(xié)議,使用 pull 模式,拉取數(shù)據(jù),簡(jiǎn)單易懂。 監(jiān)控目標(biāo),可以采用服務(wù)發(fā)現(xiàn)或靜態(tài)配置的方式。 支持多種統(tǒng)計(jì)數(shù)據(jù)模型,圖形化友好。
-架構(gòu)拓?fù)鋱D- 功能簡(jiǎn)述: prometheus server: 定期從靜態(tài)配置的 targets 或者服務(wù)發(fā)現(xiàn)targets 抽取的數(shù)據(jù)。 exporters:負(fù)責(zé)向prometheus server傳輸抽取數(shù)據(jù)。比如監(jiān)控主機(jī)有node-exporters,mysql有MySQL server exporter。 pushgateway:主要使用場(chǎng)景為:Prometheus 采用 pull 模式,可能由于不在一個(gè)子網(wǎng)或者防火墻原因,導(dǎo)致 Prometheus 無法直接拉取各個(gè) target 數(shù)據(jù)。在監(jiān)控業(yè)務(wù)數(shù)據(jù)的時(shí)候,需要將不同數(shù)據(jù)匯總, 由 Prometheus 統(tǒng)一收集。 Alertmanager:實(shí)現(xiàn)prometheus的告警功能。 webui:主要通過grafana來實(shí)現(xiàn)webui展示。
2)Grafana grafana 是一款采用 go 語言編寫的開源應(yīng)用,主要用于大規(guī)模指標(biāo)數(shù)據(jù)的可視化展現(xiàn),基于商業(yè)友好的 Apache License 2.0 開源協(xié)議。 在網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時(shí)序數(shù)據(jù)展示工具,并且也在工業(yè)控制、自動(dòng)化監(jiān)控和過程管理等領(lǐng)域有著廣泛的應(yīng)用。 grafana 有熱插拔控制面板和可擴(kuò)展的數(shù)據(jù)源,已經(jīng)支持絕大部分常用的時(shí)序數(shù)據(jù)庫(kù) 包含以下:Graphite、Elasticsearch、CloudWatch、InfluxDB、OpenTSDB、Prometheus 3)mysqld_exporter 1)安裝 grafana 登錄 grafana 官網(wǎng)下載界面(https:///get)選擇下載。 下載到本地進(jìn)行 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 加入開機(jī)啟動(dòng) [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
登錄控制臺(tái),本地 IP+3000 端口(默認(rèn)) 用戶:admin
密碼:admin(默認(rèn)) 主菜單: 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=<job_name>` 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']
運(yùn)行 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控制臺(tái),由于還沒有部署MySQL_EXPORTER,所以只有Prometheus。 3)安裝 mysqld_exporter mysqld_exporter,負(fù)責(zé)抽取 MySQL 基礎(chǔ)性能數(shù)據(jù)。 下載:mysqld_exporter(/download/#mysqld_exporter)。當(dāng)然也要下載對(duì)應(yīng)系統(tǒng)的版本。 解壓后的內(nèi)容如下: [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 進(jìn)行mysqld_exporter 配置: 這里配置需要連接監(jiān)控的MySQL數(shù)據(jù)庫(kù)信息 [root@albert mysqld_exporter-0.11.0.linux-amd64]# cat .my.cnf [client] host = 172.16.94.181 user=root password=xxxxx 啟動(dòng) 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 的界面,可以看到抽取的數(shù)據(jù)信息。 4)在 Prometheus 中配置 mysqld_exporter exporter啟動(dòng)后,需要在 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=<job_name>` 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
備注: 重啟 prometheus,點(diǎn)擊導(dǎo)航欄中的 status->targets 可以看到,mysql 的 exporter 已經(jīng)集成進(jìn)來了。 5) Grafana 和 Prometheus 關(guān)聯(lián) 現(xiàn)在增加數(shù)據(jù)源將 Grafana 和 Prometheus 關(guān)聯(lián)起來。點(diǎn)擊 Add data source,如下填寫數(shù)據(jù)保存即可: 出現(xiàn) DATA SOURCE IS WORKING 表示關(guān)聯(lián)成功 6)添加儀表盤 Grafana,提供了大量的儀表盤展示 json,可以到官網(wǎng)進(jìn)行選擇。 本文在 Grafana GitHub 上隨意選擇了一個(gè) json 把 json 導(dǎo)入到本地 到這里,大功完成了。一起來看一下監(jiān)控儀表盤吧! 還可以設(shè)置告警信息: 關(guān)于定制化 Grafana 展示界面,可以參考官方文檔進(jìn)行修改,里面有具體的規(guī)則及其粒度設(shè)置信息。 下期預(yù)告:如何監(jiān)控 docker 原創(chuàng):陳龍。 投稿:有投稿意向技術(shù)人請(qǐng)?jiān)诠娞?hào)對(duì)話框留言。
|