- 軟件模塊:
* Prometheus及其對應的exporter:用於數據採集和存取
* grafana:用於數據展示的可視化面板
- 早期文章中的軟件版本:
* Prometheus:prometheus-2.1.0
* mysqld_exporter:mysqld_exporter-0.10.0
* node_exporter:node_exporter-0.15.2
* grafana:grafana-4.6.3
* grafana-dashboard:grafana-dashboards-1.6.1
- 本期文章中的軟件版本:
* Prometheus:prometheus-2.17.1
* mysqld_exporter:mysqld_exporter-0.12.1
* node_exporter:node_exporter-1.0.0-rc.0
* grafana:grafana-6.7.2
* grafana-dashboard:grafana-dashboards-2.4.0
* alertmanager:alertmanager-0.20.0
- 特別說明:本文僅從運維實操的角度對如何玩耍Prometheus + grafana進行闡述,並不涉及到詳細深入的原理和架構解讀,如有需求請移駕如下鏈接:
* Prometheus:https://prometheus.io/docs/
* grafana:https://grafana.com/docs/grafana/latest/
- 下面,我就開始我的表演了,首先列出本次操作演示的服務器環境信息
- prometheus+grafana server端(主機名:Slave3):192.168.2.181
- MySQL 和主機監控客戶端:
* 主庫(主機名:Master):192.168.2.135
* 從庫1(主機名:Slave1):192.168.2.108
* 從庫2(主機名:Slave2):192.168.2.156
1、概述
1.1. 什麼是Prometheus?
- Prometheus是一個開源的系統監控和報警工具包,最初是在SoundCloud開發的。自2012年項目啟動以來,已經有許多公司和組織採用了Prometheus,該項目擁有一個非常活躍的開發人員和用戶社區。它現在是一個獨立的開源項目,且獨立於任何公司進行維護。為了強調這一點,並闡明該項目的治理結構,Prometheus在2016年加入了雲原生計算基金會(Cloud Native Computing Foundation,簡稱CNCF),成為繼Kubernetes之後的第二個託管項目。
- Prometheus的主要特點如下:
- 多維數據模型,其中包含通過度量名稱和"鍵-值"對標識的時間序列數據
- 支持PromQL,PromQL是一種靈活的查詢語言
- 不依賴分佈式存儲,單個服務器節點可以自治
- 時間序列數據通過HTTP上的 pull 模型進行收集
- 通過中間網關支持推送時間序列數據
- 支持通過服務發現或靜態配置來發現目標
- 支持多種繪圖模式和多種儀表盤
- 組件組成
- Prometheus生態系統由多個組件組成,其中許多組件是可選的:
- 主要的Prometheus Server,它用於抓取和存儲時間序列數據
- 用於檢測應用程序代碼的客戶端庫
- 支持短暫工作的推送網關(pushgateway)
- 各種服務監控的專用exporter。官方認可的有:blackbox_exporter,consul_exporter,graphite_exporter,haproxy_exporter,memcached_exporter,mysqld_exporter,node_exporter,statsd_exporter,社區支持更多的exporter,詳情可參考鏈接:https://prometheus.io/docs/instrumenting/exporters/
- 負責處理警報的alertmanager
- 各種支持工具
- 大多數Prometheus 組件都是用Go語言編寫的,因此易於構建和部署為靜態二進制文件
- Prometheus體系結構如下圖(圖片來源:https://prometheus.io/docs/introduction/overview/),該圖展示了Prometheus的架構和它的生態系統的一些組件組成
- 以下是早期的架構圖,這裡一併給出方便大家做個參照對比
- Prometheus適用於什麼場景?
- Prometheus適用於記錄任何純數字的時間序列數據。它既適合面向主機的監控,也適合面向高動態的服務體系結構的監控。在微服務中,它對多維數據收集和查詢的支持具有獨特的優勢。
- Prometheus的設計旨在提高可靠性,它可以在停機時用來快速診斷出現問題的系統。每個Prometheus Server是獨立的,不依賴於網絡存儲或其他遠程服務。因此當基礎設施中的一部分被破壞時,其仍然可用,所以不需要專門為其規劃大量的基礎設施來運行它。
- Prometheus不合適什麼場景?
- Prometheus重視高可靠性,因此,即使在部分出現故障的情況下,也可以查看關於系統的可用統計信息。如果您需要100%的準確性,那麼Prometheus不是一個好的選擇,因為Prometheus收集到的數據可能不夠詳細和完整,也可能不夠即時。在這種情況下,最好使用其他系統來收集和分析數據以進行實時的監控,並使用Prometheus來完成剩下的監控工作(例如:當做grafana的展示數據源使用)。
1.2. 什麼是grafana?
- Grafana是一個可視化大型測量數據的開源程序,實際上就是一個可視化面板(Dashboard),有著非常漂亮的圖表和佈局展示,功能齊全的度量儀表盤和圖形編輯器,提供了強大和優雅的方式去創建、共享、瀏覽數據,grafana有熱插拔控制面板和可擴展的數據源,目前已經支持的數據源有如下一些:
- 時序數據庫:Prometheus、Graphite、OpenTSDB、InfluxDB
- 文檔數據庫:Loki、Elasticsearch
- 關係型數據庫:MySQL、PostgreSQL、Microsoft SQL Server
- 雲監控服務:Stackdriver、CloudWatch、Azure Monitor、Grafana Cloud
- 商業數據庫擴展插件:Splunk、ServiceNow、Oracle、New Relic、Dynatrace、DataDog
- 其他:TestData DB
- PS:隨著版本的迭代,支持接入的數據源將會更多
2、安裝部署
2.1. 安裝prometheus
- prometheus是一個監控採集與數據存儲框架(監控server端),具體採集什麼數據依賴於具體的exporter(監控client端),例如:採集MySQL的數據需要使用到mysql_exporter,prometheus調用mysql_expoter採集到mysql的監控指標之後,把mysql_exporter採集到的數據存放到prometheus所在服務器的TSDB中保存(詳情可參考鏈接:https://prometheus.io/docs/prometheus/latest/storage/)。
2.1.1. 下載安裝包
- 對於prometheus,假設我們需要監控MySQL,那麼我們需要下載至少3個組件,如下:
- prometheus程序包
- node_exporter:監控主機磁盤、內存、CPU等硬件性能指標的採集程序包
- mysql_exporter:監控mysql各種性能指標的採集程序包
- 下載鏈接(該頁面始終只有一個最新版本):https://prometheus.io/download/
- 下載prometheus,其中包含了監控採集框架和存儲採集數據的時序數據庫
- 下載mysqld_exporter和node_exporter,其中,mysqld_exporter是MySQL數據的採集端,node_exporter是主機負載數據的採集端
- PS:如果你還需要配置監控告警,需要下載alertmanager程序包
2.1.2. 解壓程序包
- 解壓prometheus
<code>[root@localhost ~]# mkdir /data [root@localhost ~]# tar xf prometheus-2.17.1.linux-amd64.tar.gz -C /data//<code>
- 解壓exporter:由於prometheus主機自身也需要監控,所以也至少需要在安裝prometheus的主機上解壓node_exporter包(安裝prometheus的主機上如果沒有數據庫則不需要安裝mysqld_exporter包)
<code>[root@localhost ~]# tar xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /data/ # 如果需要監控mysql,則繼續解壓mysql_exporter [root@localhost ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /data//<code>
- 在其他監控目標數據庫服務器上,分別解壓上述2個exporter包(略)
2.1.3. 啟動prometheus
- 進入prometheus的工作目錄
<code>[root@localhost ~]# cd /data/ [root@localhost data]# mv prometheus-2.17.1.linux-amd64/ prometheus [root@localhost ~]# cd /data/prometheus/<code>
- 配置 prometheus.yml配置文件
<code> [root@localhost data]# 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). # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: - file_sd_configs: # 注意,如果指定從某配置文件加載監控目標,則在prometheus啟動之前需要確保該文件在prometheus的工作目錄下事先存在,否則可能後續配置過程中出現報錯 - files: - host.yml job_name: Host metrics_path: /metrics relabel_configs: - source_labels: [__address__] regex: (.*) target_label: instance replacement: $1 - source_labels: [__address__] regex: (.*) target_label: __address__ replacement: $1:9100 - file_sd_configs: # 注意,如果指定從某配置文件加載監控目標,則在prometheus啟動之前需要確保該文件在prometheus的工作目錄下事先存在,否則可能後續配置過程中出現報錯 - files: - mysql.yml job_name: MySQL metrics_path: /metrics relabel_configs: - source_labels: [__address__] regex: (.*) target_label: instance replacement: $1 - source_labels: [__address__] regex: (.*) target_label: __address__ replacement: $1:9104 - job_name: prometheus static_configs: - targets: - localhost:9090/<code>
- 先創建2個空的主機和MySQL配置文件,以避免啟動報錯
<code>[root@localhost prometheus]# touch host.yml mysql.yml/<code>
- 啟動prometheus進程,30d表示prometheus只保留30天以內的數據(注意,如果直接使用prometheus命令啟動而不是使用service腳本,那麼你需要先切換到工作目錄下再啟動,例如,這裡工作目錄為/data/prometheus)
<code>[root@localhost prometheus]# /data/prometheus/prometheus --storage.tsdb.retention=30d &/<code>
- 如果是7.x系統,可以按照如下方式配置service啟動腳本
<code> # 修改WorkingDirectory參數為你的prometheus的工作目錄 [root@localhost prometheus]# cat /usr/lib/systemd/system/prometheus.service [Unit] Description=Prometheus instance Wants=network-online.target After=network-online.target After=postgresql.service mariadb.service mysql.service [Service] User=root Group=root Type=simple Restart=on-failure WorkingDirectory=/data/prometheus/ RuntimeDirectory=prometheus RuntimeDirectoryMode=0750 ExecStart=/data/prometheus/prometheus --storage.tsdb.retention=30d --config.file=/data/prometheus/prometheus.yml LimitNOFILE=10000 TimeoutStopSec=20 [Install] WantedBy=multi-user.target/<code>
- 注意:如果需要使用service啟動腳本,則需要先將手動啟動的prometheus進程停止,才能使用
<code>[root@localhost prometheus]# ps aux |grep prometheus root 10060 0.2 1.9 165356 35528 pts/2 Sl 19:41 0:00 /data/prometheus/prometheus --storage.tsdb.retention=30d root 10175 0.0 0.0 112728 976 pts/2 S+ 19:42 0:00 grep --color=auto prometheus [root@localhost prometheus]# kill 10060 [root@localhost prometheus]# level=warn ts=2020-04-11T11:43:08.058Z caller=main.go:512 msg="Received SIGTERM, exiting gracefully..." level=info ts=2020-04-11T11:43:08.058Z caller=main.go:535 msg="Stopping scrape discovery manager..." level=info ts=2020-04-11T11:43:08.058Z caller=main.go:549 msg="Stopping notify discovery manager..." level=info ts=2020-04-11T11:43:08.058Z caller=main.go:571 msg="Stopping scrape manager..." level=info ts=2020-04-11T11:43:08.058Z caller=main.go:531 msg="Scrape discovery manager stopped" level=info ts=2020-04-11T11:43:08.058Z caller=main.go:545 msg="Notify discovery manager stopped" level=info ts=2020-04-11T11:43:08.059Z caller=manager.go:882 component="rule manager" msg="Stopping rule manager..." level=info ts=2020-04-11T11:43:08.059Z caller=manager.go:892 component="rule manager" msg="Rule manager stopped" level=info ts=2020-04-11T11:43:08.059Z caller=main.go:565 msg="Scrape manager stopped" level=info ts=2020-04-11T11:43:08.061Z caller=notifier.go:598 component=notifier msg="Stopping notification manager..." level=info ts=2020-04-11T11:43:08.061Z caller=main.go:738 msg="Notifier manager stopped" level=info ts=2020-04-11T11:43:08.061Z caller=main.go:750 msg="See you next time!" [1]+ 完成 /data/prometheus/prometheus --storage.tsdb.retention=30d [root@localhost prometheus]# service prometheus start Redirecting to /bin/systemctl start prometheus.service [root@localhost prometheus]# [root@localhost prometheus]# service prometheus status Redirecting to /bin/systemctl status prometheus.service # prometheus.service - Prometheus instance Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled) Active: active (running) since 六 2020-04-11 19:43:36 CST; 2s ago Main PID: 10238 (prometheus) CGroup: /system.slice/prometheus.service └─10238 /data/prometheus/prometheus --storage.tsdb.retention=30d --config.file=/data/prometheus/prometheus.yml 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.317Z caller=web.go:514 component=web msg="Start listening for connections" address=0.0.0.0:9090 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.320Z caller=head.go:575 component=tsdb msg="replaying WAL, this may take awhile" 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.325Z caller=head.go:624 component=tsdb msg="WAL segment loaded" segment=0 maxSegment=1 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.325Z caller=head.go:624 component=tsdb msg="WAL segment loaded" segment=1 maxSegment=1 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.325Z caller=head.go:627 component=tsdb msg="WAL replay completed" duration=4.382388ms 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.326Z caller=main.go:683 fs_type=XFS_SUPER_MAGIC 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.326Z caller=main.go:684 msg="TSDB started" 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.326Z caller=main.go:788 msg="Loading configuration file" filename=/data/prometheus/prometheus.yml 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.327Z caller=main.go:816 msg="Completed loading of configuration file" filename=/data/prometheus/prometheus.yml 4月 11 19:43:36 localhost.localdomain prometheus[10238]: level=info ts=2020-04-11T11:43:36.327Z caller=main.go:635 msg="Server is ready to receive web requests." /<code>
- PS:prometheus默認的web訪問端口為9090,可以使用如下地址訪問(使用谷歌內核瀏覽器,注,此時由於還位添加任何主機和數據庫監控列表,頁面是沒有任何數據的,如果有需要,請跳轉到"3、監控節點部署")
- http://192.168.2.181:9090
2.2. 安裝grafana
- grafana是類似於一個出圖展示框架,grafana根據grafana-dashboards來進行展示,grafana-dashboards就類似於grafana的出圖配置文件,根據在grafana-dashboards中的定義來確定在頁面中需要展示什麼指標,需要如何展示等
2.2.1. 下載安裝包
- 對於grafana來說,需要下載一個程序包,一個grafana-dashboards包
- 下載鏈接
- grafana程序包:https://grafana.com/grafana/download
- grafana-dashboards包:https://github.com/percona/grafana-dashboards/releases
2.2.2. 解壓程序包
- 解壓grafana
<code>[root@localhost ~]# tar xf grafana-6.7.2.linux-amd64.tar.gz -C /data/prometheus/ [root@localhost ~]# cd /data/prometheus [root@localhost prometheus]# mv grafana-6.7.2/ grafana/<code>
2.2.3. 啟動grafana
- 進入grafana工作目錄,並啟動
<code>[root@localhost ]# cd /data/prometheus/grafana [root@localhost ]# ./bin/grafana-server &/<code>
- 如果是7.x系統,可以按照如下方式配置service啟動腳本
<code> [root@localhost service]# cat /usr/lib/systemd/system/grafana-server.service [Unit] Description=Grafana instance Documentation=http://docs.grafana.org Wants=network-online.target After=network-online.target After=postgresql.service mariadb.service mysql.service [Service] User=root Group=root Type=simple Restart=on-failure WorkingDirectory=/data/prometheus/grafana RuntimeDirectory=grafana RuntimeDirectoryMode=0750 ExecStart=/data/prometheus/grafana/bin/grafana-server LimitNOFILE=10000 TimeoutStopSec=20 [Install] WantedBy=multi-user.target/<code>
2.2.4. 配置grafana
2.2.4.1. 登錄grafana頁面
- 打開grafana頁面(默認帳號和密碼:admin/admin,默認的端口為3000,通過地址:http://192.168.2.181:3000 訪問。使用谷歌內核瀏覽器),配置數據來源
- 輸入原始賬號和密碼:admin/admin
- 輸入新密碼,重置初始密碼(這裡我們選擇跳過),注意,不要使用IE內核瀏覽器,否則可能出現無法修改密碼、也無法跳過修改密碼的步驟
- 到了這裡,可以發現新版本的配置步驟變成了嚮導式的了,點擊"Add data source"按鈕
- 在跳轉的頁面中,可以看到,新版本支持的數據源非常多,這裡我們選擇prometheus
- 指定prometheus地址,這裡我們把grafana裝在了同一臺機器,直接使用127.0.0.1的地址配置即可,如下圖
2.2.4.2. 在grafana中導入grafana-dashboards
- 解壓grafana-dashboards包,該包中提供了大量的json格式文件的grafana dashboards,根據需要自行選擇,我們這裡需要監控主機和MySQL,就選擇如下一些json文件
<code>[root@localhost ~]# tar xf grafana-dashboards-2.4.0.tar.gz [root@localhost ~]# cd grafana-dashboards-2.4.0/dashboards/ [root@localhost dashboards]# updatedb [root@localhost ~]# locate grafana-dashboards |grep dashboards |grep json ...... /root/grafana-dashboards-2.4.0/dashboards/Advanced_Data_Exploration.json /root/grafana-dashboards-2.4.0/dashboards/CPU_Utilization_Details.json /root/grafana-dashboards-2.4.0/dashboards/Disk_Details.json /root/grafana-dashboards-2.4.0/dashboards/Home_Dashboard.json /root/grafana-dashboards-2.4.0/dashboards/Memory_Details.json ...... /root/grafana-dashboards-2.4.0/dashboards/MySQL_Command_Handler_Counters_Compare.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_InnoDB_Compression_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_InnoDB_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Instance_Summary.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Instances_Compare.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Instances_Overview.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_MyISAM_Aria_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_MyRocks_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Performance_Schema_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Query_Response_Time_Details.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Replication_Summary.json /root/grafana-dashboards-2.4.0/dashboards/MySQL_Table_Details.json ...... /root/grafana-dashboards-2.4.0/dashboards/MySQL_Wait_Event_Analyses_Details.json ...... /root/grafana-dashboards-2.4.0/dashboards/Network_Details.json /root/grafana-dashboards-2.4.0/dashboards/Node_Summary.json /root/grafana-dashboards-2.4.0/dashboards/Node_Temperature_Details.json /root/grafana-dashboards-2.4.0/dashboards/Nodes_Compare.json /root/grafana-dashboards-2.4.0/dashboards/Nodes_Overview.json ...... /root/grafana-dashboards-2.4.0/dashboards/Processes_Details.json /<code>
- 在grafana頁面中,導入需要的json文件
- 在彈出的窗口中選擇你需要導入的json文件
- 然後,如果你的grafana中已經添加過主機,此時,就可以看到相應的json dashboard監控數據(注意:這裡還不會有數據,因為監控主機的exporter程序還未啟動,後續步驟馬上講到,這裡只需要模版添加成功即可)
- 至此,prometheus+grafana的基礎架構(server端)已經搭建好了,現在,你可以去給他們添加監控節點了(client端)
- PS:如果dashboard文件加載之後報如下錯誤,則說明缺少了digiapulssi-breadcrumb-panel面板插件
- 使用grafana-cli命令安裝該面板插件,然後重啟grafana-server服務即可
<code> [root@localhost ~]# cd /data/prometheus/grafana/ [root@localhost grafana]# ./bin/grafana-cli plugins install digiapulssi-breadcrumb-panel installing digiapulssi-breadcrumb-panel @ 1.1.6 from: https://grafana.com/api/plugins/digiapulssi-breadcrumb-panel/versions/1.1.6/download into: /var/lib/grafana/plugins ✔ Installed digiapulssi-breadcrumb-panel successfully Restart grafana after installing plugins . [root@localhost grafana]# ./bin/grafana-cli plugins install grafana-polystat-panel installing grafana-polystat-panel @ 1.1.0 from: https://grafana.com/api/plugins/grafana-polystat-panel/versions/1.1.0/download into: /var/lib/grafana/plugins ✔ Installed grafana-polystat-panel successfully Restart grafana after installing plugins . [root@localhost grafana]# service grafana-server restart Redirecting to /bin/systemctl restart grafana-server.service/<code>
- 如果還有其他面板插件需要安裝,可以通過如下命令查看可用的插件列表,然後進行安裝
<code>[root@localhost grafana]# cd /data/prometheus/grafana/ [root@localhost grafana]# ./bin/grafana-cli plugins list-remote id: abhisant-druid-datasource version: 0.0.5 id: agenty-flowcharting-panel version: 0.7.0 id: aidanmountford-html-panel version: 0.0.1 id: akumuli-datasource version: 1.3.11 id: alexanderzobnin-zabbix-app version: 3.11.0 id: alexandra-trackmap-panel version: 1.2.4 id: andig-darksky-datasource version: 1.0.1 id: ayoungprogrammer-finance-datasource version: 1.0.0 id: belugacdn-app version: 1.2.0 id: bessler-pictureit-panel version: 1.0.0 id: blackmirror1-singlestat-math-panel version: 1.1.7 id: blackmirror1-statusbygroup-panel version: 1.1.1 id: bosun-app version: 0.0.28 id: briangann-datatable-panel version: 0.0.9 id: briangann-gauge-panel version: 0.0.6 id: btplc-alarm-box-panel version: 1.0.8 id: btplc-peak-report-panel version: 0.2.4 id: btplc-status-dot-panel version: 0.2.4 id: btplc-trend-box-panel version: 0.1.9 id: camptocamp-prometheus-alertmanager-datasource version: 0.0.7 id: citilogics-geoloop-panel version: 1.1.1 id: cloudflare-app version: 0.1.4 id: cognitedata-datasource version: 1.0.1 id: corpglory-progresslist-panel version: 1.0.5 id: dalmatinerdb-datasource version: 1.0.5 id: ddurieux-glpi-app version: 1.3.0 id: devicehive-devicehive-datasource version: 2.0.1 id: devopsprodigy-kubegraf-app version: 1.3.0 id: digiapulssi-breadcrumb-panel version: 1.1.6 id: digiapulssi-organisations-panel version: 1.3.0 id: digrich-bubblechart-panel version: 1.1.0 id: doitintl-bigquery-datasource version: 1.0.7 id: farski-blendstat-panel version: 1.0.2 id: fastweb-openfalcon-datasource version: 1.0.0 id: fatcloud-windrose-panel version: 0.7.0 id: fetzerch-sunandmoon-datasource version: 0.1.5 id: flant-statusmap-panel version: 0.2.0 id: foursquare-clouderamanager-datasource version: 0.9.2 id: fzakaria-simple-annotations-datasource version: 1.0.0 id: gnocchixyz-gnocchi-datasource version: 1.7.0 id: goshposh-metaqueries-datasource version: 0.0.3 id: grafana-azure-data-explorer-datasource version: 2.0.5 id: grafana-azure-monitor-datasource version: 0.3.0 id: grafana-clock-panel version: 1.0.3 id: grafana-googlesheets-datasource version: 0.9.0 id: grafana-image-renderer version: 1.0.12 id: grafana-influxdb-08-datasource version: 1.0.2 id: grafana-influxdb-flux-datasource version: 5.4.1 id: grafana-kairosdb-datasource version: 3.0.1 id: grafana-kubernetes-app version: 1.0.1 id: grafana-piechart-panel version: 1.4.0 id: grafana-polystat-panel version: 1.1.0 id: grafana-sensu-app version: 1.0.6 id: grafana-simple-json-datasource version: 1.4.0 id: grafana-strava-datasource version: 1.1.1 id: grafana-worldmap-panel version: 0.2.1 id: gretamosa-topology-panel version: 1.0.0 id: gridprotectionalliance-openhistorian-datasource version: 1.0.2 id: gridprotectionalliance-osisoftpi-datasource version: 1.0.4 id: hawkular-datasource version: 1.1.1 id: ibm-apm-datasource version: 0.9.0 id: instana-datasource version: 2.7.0 id: jasonlashua-prtg-datasource version: 4.0.3 id: jdbranham-diagram-panel version: 1.6.1 id: jeanbaptistewatenberg-percent-panel version: 1.0.6 id: kentik-app version: 1.3.4 id: larona-epict-panel version: 1.2.2 id: linksmart-hds-datasource version: 1.0.1 id: linksmart-sensorthings-datasource version: 1.3.0 id: logzio-datasource version: 5.0.0 id: macropower-analytics-panel version: 0.0.1 id: marcuscalidus-svg-panel version: 0.3.3 id: michaeldmoore-annunciator-panel version: 1.0.4 id: michaeldmoore-multistat-panel version: 1.2.7 id: monasca-datasource version: 1.0.0 id: monitoringartist-monitoringart-datasource version: 1.0.0 id: moogsoft-aiops-app version: 7.3.1 id: mtanda-google-calendar-datasource version: 1.0.4 id: mtanda-heatmap-epoch-panel version: 0.1.7 id: mtanda-histogram-panel version: 0.1.6 id: mxswat-separator-panel version: 1.0.0 id: natel-discrete-panel version: 0.0.9 id: natel-influx-admin-panel version: 0.0.5 id: natel-plotly-panel version: 0.0.6 id: natel-usgs-datasource version: 0.0.2 id: neocat-cal-heatmap-panel version: 0.0.3 id: novalabs-annotations-panel version: 0.0.1 id: ns1-app version: 0.0.7 id: ntop-ntopng-datasource version: 1.0.0 id: oci-datasource version: 1.0.2 id: opennms-helm-app version: 5.0.1 id: ovh-warp10-datasource version: 2.1.2 id: paytm-kapacitor-datasource version: 0.1.2 id: percona-percona-app version: 1.0.0 id: petrslavotinek-carpetplot-panel version: 0.1.1 id: pierosavi-imageit-panel version: 0.1.3 id: pr0ps-trackmap-panel version: 2.0.4 id: praj-ams-datasource version: 1.2.0 id: pue-solr-datasource version: 1.0.2 id: quasardb-datasource version: 3.5.0 id: rackerlabs-blueflood-datasource version: 0.0.2 id: radensolutions-netxms-datasource version: 1.2.1 id: raintank-snap-app version: 0.0.5 id: raintank-worldping-app version: 1.2.7 id: ryantxu-ajax-panel version: 0.0.7-dev id: ryantxu-annolist-panel version: 0.0.1 id: satellogic-3d-globe-panel version: 0.1.0 id: savantly-heatmap-panel version: 0.2.0 id: sbueringer-consul-datasource version: 0.1.5 id: scadavis-synoptic-panel version: 1.0.4 id: sidewinder-datasource version: 0.2.0 id: simpod-json-datasource version: 0.1.7 id: skydive-datasource version: 1.2.0 id: smartmakers-trafficlight-panel version: 1.0.0 id: sni-pnp-datasource version: 1.0.5 id: sni-thruk-datasource version: 1.0.3 id: snuids-radar-panel version: 1.4.4 id: snuids-trafficlights-panel version: 1.4.5 id: spotify-heroic-datasource version: 0.0.1 id: stagemonitor-elasticsearch-app version: 0.83.2 id: udoprog-heroic-datasource version: 0.1.0 id: vertamedia-clickhouse-datasource version: 1.9.5 id: vertica-grafana-datasource version: 0.1.0 id: vonage-status-panel version: 1.0.9 id: voxter-app version: 0.0.1 id: xginn8-pagerduty-datasource version: 0.2.1 id: yesoreyeram-boomtable-panel version: 1.3.0 id: yesoreyeram-boomtheme-panel version: 0.1.0 id: zuburqan-parity-report-panel version: 1.2.1 Restart grafana after installing plugins . [root@localhost grafana]# ./bin/grafana-cli plugins install grafana-polystat-panel installing grafana-polystat-panel @ 1.1.0 from: https://grafana.com/api/plugins/grafana-polystat-panel/versions/1.1.0/download into: /var/lib/grafana/plugins ✔ Installed grafana-polystat-panel successfully/<code>
- PS1:如果是二進制包直接解壓安裝的,而不是rpm或者yum安裝的,那麼,你需要修改grafana的配置文件中的插件路徑,二進制包中的插件路徑默認為grafana根目錄下的data/plugins目錄下,而grafana-cli命令安裝的插件路徑位於/var/lib/grafana/plugins/下,修改grafana配置文件,更正插件路徑
<code> [root@localhost grafana]# vim /data/prometheus/grafana/conf/defaults.ini ...... # Directory where grafana will automatically scan and look for plugins #plugins = data/plugins plugins = /var/lib/grafana/plugins ...... # 重啟grafana-server [root@localhost grafana]# service grafana-server restart Redirecting to /bin/systemctl restart grafana-server.service/<code>
- PS2:grafana支持的插件有3種,應用程序插件(App plugin)、數據源插件(Data source plugin)、面板插件(Panel plugin)
- 應用程序插件:通過自定義的檢查方式來擴展Grafana。它包括一組面板和數據源插件,以及自定義頁面
- 數據源插件:擴展了Grafana的數據源,並在Grafana中支持其他數據源
- 面板插件:使用其他可視化選項擴展了Grafana
2.3. 監控節點部署
2.3.1. 添加主機監控
- 以添加prometheus主機(這裡我們以將所有的監控主機都一併添加)為例進行說明
- 解壓exporter壓縮包
<code>[root@localhost ~]# mkdir /data/ [root@localhost ~]# tar xf node_exporter-1.0.0-rc.0.linux-amd64.tar.gz -C /data/ [root@localhost ~]# cd /data/ [root@localhost data]# mv node_exporter-1.0.0-rc.0.linux-amd64 node_exporter/<code>
- 啟動node_exporter程序
<code>[root@localhost data]# cd node_exporter [root@localhost node_exporter]# nohup ./node_exporter --collector.processes --collector.mountstats --collector.interrupts &/<code>
- 配置prometheus主機監控配置列表文件,由於之前主配置文件prometheus.yml 中已經定義了監控主機的配置文件host.yml,這裡只需要把主機IP信息填入即可動態生效(注意,在該配置文件的labels的鍵值對,需要定義哪些值需要參考你加載的dashboard文件而定,具體詳見下文)
<code>[root@localhost node_exporter]# cat /data/prometheus/host.yml - labels: node_name: Host-Prometheus service_name: Host-Prometheus targets: - 192.168.2.181/<code>
- 然後,在grafana頁面中就可以看到你配置的主機
- PS:如果該文件中已經配置過lables且不需要使用獨立的labels標籤進行標記,則新添加的實例的IP可以直接放在同一個targets下,但這樣就無法在dashboard中進行篩選了(舊版本的不添加獨立的labels標籤是可以使用IP地址進行篩選的),未便於演示,這裡我們未每臺主機配置獨立的labels標籤,如下
<code> [root@localhost mysqld_exporter]# cat /data/prometheus/host.yml - labels: node_name: Host-Master service_name: Host-Master targets: - 192.168.2.135 - labels: node_name: Host-Slave1 service_name: Host-Slave1 targets: - 192.168.2.108 - labels: node_name: Host-Slave2 service_name: Host-Slave2 targets: - 192.168.2.156 - labels: node_name: Host-Prometheus service_name: Host-Prometheus targets: - 192.168.2.181/<code>
- 添加MySQL監控主機,這裡以添加192.168.2.135為例進行說明
- 解壓exporter壓縮包
<code>[root@localhost ~]# mkdir /data/ [root@localhost ~]# tar xf mysqld_exporter-0.12.1.linux-amd64.tar.gz -C /data/ [root@localhost ~]# cd /data/ [root@localhost ~]# mv mysqld_exporter-0.12.1.linux-amd64 mysqld_exporter/<code>
- 配置監控數據庫需要的主機IP、數據庫端口、數據庫賬號和密碼的環境變量
<code>[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:letsg0@(192.168.2.135:3306)/'[root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:letsg0@(192.168.2.135:3306)/'" >> /etc/profile/<code>
- 啟動exporter
# 由於目前最新的版本默認關閉了大量的mysql採集項,需要顯式使用相應的選項開啟(截止到寫稿時間,最新的開發版本可以通過prometheus端的配置項讓exporter端生效,而無需再exporter中使用大量的啟動選項開啟)
<code>[root@luoxiaobo-01 ~]# export DATA_SOURCE_NAME='admin:letsg0@(192.168.2.135:3306)/' [root@luoxiaobo-01 ~]# echo "export DATA_SOURCE_NAME='admin:letsg0@(192.168.2.135:3306)/'" >> /etc/profile/<code>
- 配置prometheus MySQL監控配置列表文件,由於之前主配置文件prometheus.yml 中已經定義了監控MySQL的配置文件mysql.yml,這裡只需要把主機IP信息填入即可動態生效
<code>[root@localhost ~]# cd mysqld_exporter [root@localhost mysqld_exporter]# nohup ./mysqld_exporter --collect.auto_increment.columns --collect.info_schema.tables --collect.perf_schema.eventsstatements --collect.perf_schema.eventsstatementssum --collect.perf_schema.eventswaits --collect.perf_schema.file_events --collect.perf_schema.file_instances --collect.perf_schema.indexiowaits --collect.perf_schema.tableiowaits --collect.perf_schema.tablelocks --collect.perf_schema.replication_group_member_stats --collect.perf_schema.replication_applier_status_by_worker --collect.slave_hosts --collect.info_schema.query_response_time --collect.info_schema.processlist --collect.info_schema.innodb_tablespaces --collect.info_schema.innodb_metrics --collect.engine_innodb_status --collect.binlog_size --collect.info_schema.clientstats & # 注意,新版本的mysqld_exporter可能不支持--collect.info_schema.processlist 選項,請自行使用./mysqld_exporter --help查看/<code>
- 然後,在grafana頁面中就可以看到你配置的MySQL實例
- PS:查看dashboard配置文件中的過濾選項,以便在prometheus的yml配置文件中添加對應的labels標籤,如下:
- 以上這幾個variable都是query類型的,根據具體的definition值可以看到,通過環境變量和labels標籤值來獲取,也就是說,這些就是可用於生成下拉篩選列表的值,我們將其variable名稱寫入到host.yml和mysql.yml文件中作為label的名稱,值自行根據需要取名即可(但是要注意,這些variables是有先後順序的,如果某一個variables引用了另外一個variables,那麼被引用的variables一定要在其之前定義,否則無法使用)。
- prometheus的targets信息(目標主機)可以通過如下頁面進行查看
2.3.3. grafana頁面dashboard切換
- 根據需要切換監控模板
- 然後,就能看到你想要的數據
- MySQL instances Overview dashboard展示
- Nodes Overview dashboard展示
- MySQL Table Details dashboard展示
- 切換到主機和MySQL的比較模板,可以清晰地看到多臺監控主機之間的差異
- 主機比較模板
- MySQL 比較模板
2.4. Prometheus採集頻率修改
- 以下以修改MySQL 監控採集頻率為例進行說明(注意:有需要才修改,通常不需要修改,修改MySQL的採集間隔可能會影響MySQL實例的性能)
- 如果不知道如何修改,則可打開如下頁面(Prometheus主機,訪問鏈接請對應修改為你的環境中的Prometheus主機IP),查看當前模板中生效的採集頻率(scrape_interval和scrape_timeout值)
- http://192.168.2.181:9090/config
- 在prometheus所在服務器中,找到你的prometheus配置文件,類似如下(配置文件的globalIxia的scrape_interval是全局採集的間隔時間,不要修改,找到job_name: MySQL,如果在該標籤下未找到scrape_interval,則添加一行信息,並修改為5s)
<code>[root@localhost ~]# cat /data/prometheus/config/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). # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: - file_sd_configs: - files: - host.yml job_name: Host metrics_path: /metrics relabel_configs: - source_labels: [__address__] regex: (.*) target_label: instance replacement: $1 - source_labels: [__address__] regex: (.*) target_label: __address__ replacement: $1:9100 - file_sd_configs: - files: - mysql.yml job_name: MySQL scrape_interval: 5s # 修改MySQL的採集間隔為5s scrape_timeout: 5s # 採集超時時間也需要設置,且不能大於採集間隔時間,需要設置為小於等於scrape_interval的值 metrics_path: /metrics relabel_configs: - source_labels: [__address__] regex: (.*) target_label: instance replacement: $1 - source_labels: [__address__] regex: (.*) target_label: __address__ replacement: $1:9104 - job_name: prometheus static_configs: - targets: - localhost:9090/<code>
- 重新加載prometheus配置文件
- [root@localhost ~]# kill -1 `pgrep prometheus`
- 然後 ,查看頁面中生效的採集時間間隔信息
- http://192.168.2.181:9090/config
- PS:經測試,發現grafana並不能展示低於15s的展示頻率,所以要展示低於15秒一次的採集數據,可能需要使用prometheus提供的API自己去獲取數據
2.5. 配置監控告警
- 採用alertmanager組件
2.5.1. 安裝alertmanager
- 解壓alertmanager包
<code> [root@Slave3 ~]# tar xf alertmanager-0.20.0.linux-amd64.tar.gz -C /data/prometheus/ [root@Slave3 ~]# cd /data/prometheus/ [root@Slave3 prometheus]# mv alertmanager-0.20.0.linux-amd64/ alertmanager/<code>
- 使用默認的配置文件啟動
<code>[root@Slave3 alertmanager]# nohup ./alertmanager --config.file=alertmanager.yml & ....../<code>
- 啟動之後,可以通過如下鏈接查看監控節點的狀態以及告警項等信息
- http://192.168.2.181:9093
- 修改prometheus的配置文件,添加alertmanager的配置信息,添加到global配置段之後,scrape_configs配置段之前,如下
<code>[root@Slave3 alertmanager]# cat /data/prometheus/prometheus.yml 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). alerting: alertmanagers: - static_configs: - targets: - 192.168.2.181:9093 rule_files: - "first_rules.yml" # - "second_rules.yml" # A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs: ...... 添加告警規則文件,這裡以添加一個監控node_exporter進程的存活為例 [root@Slave3 alertmanager]# cat /data/prometheus/first_rules.yml groups: - name: test_alert_up rules: - alert: node_time:sum:error expr: up < 1 labels: severity: warnging action: text kind: node_export annotations: summary: not up last: "{{ $value }}"/<code>
- 添加告警規則文件,這裡以添加一個監控node_exporter進程的存活為例
<code>[root@Slave3 alertmanager]# cat /data/prometheus/first_rules.yml groups: - name: test_alert_up rules: - alert: node_time:sum:error expr: up < 1 labels: severity: warnging action: text kind: node_export annotations: summary: not up last: "{{ $value }}"/<code>
- 重啟prometheus服務
<code>[root@Slave3 alertmanager]# service prometheus status Redirecting to /bin/systemctl status prometheus.service/<code>
- alertmanager模塊還支持集群化部署,避免單點故障而無法及時發出告警,關於集群化部署,可參考鏈接:https://blog.csdn.net/u014686399/article/details/88902340
2.6. 自定義dashboard
- 在grafana頁面,可以自定義dashboard,將自己關心的監控指標自定義一個儀表盤,並設置好每一個告警項的樣式、相應的告警閥值等,非常靈活,有興趣的童鞋可自行研究,不過,這裡特別說明一點,自己純手工拼湊一個dashboard繁瑣不說,而且學習成本非常高,通常,grafana-dashboard包中自帶的dashboard模板導入之後是隻讀權限,但是我們可以對這些模板進行拷貝(也可以通過模板設置界面添加具有編輯或管理權限的賬號,就可以對模板進行編輯,但是通常不建議這麼做,因為在有編輯權限的用戶下,很容易誤操作誤修改),拷貝的副本是可以自由編輯的,如下
- 切換到你想要抄襲的dashboard模板,然後點擊右上角的"Dashboard settings"按鈕
- 點擊左側的"Save as..."按鈕
- 然後,在彈出的文本框中輸入一個dashboard名字
- 然後,切換到你拷貝的dashboard模板中,你會發現,dashboard中的所有元素都可以自由修改了
- 如果你確實需要直接編輯dashboard,那麼,可以按照如下步驟添加一個具有編輯或者管理權限的賬號,然後退出設置界面,你就會發現,可以對模板進行編輯了
- 如果你想自定義dashboard,且只需要提取某幾個dashboard中的少數監控項面板,並設置頁面告警列表(注意,這裡不是郵件告警,而是頁面告警),那麼,可以直接從其他dashboard模板中拷貝看中的監控項面板,直接導入自定義dashboard裡即可,下面以設置從庫複製延遲為例進行說明:
- 現在切換到MySQL Replication Summary dashboard(需要先按照PS1裡介紹的方法完整拷貝其他模板或者添加編輯權限),點擊如圖所示MySQL Replication Delay監控項面板標題旁邊的"倒三角"圖標,選擇"more",點擊"Copy"
- copy成功時能看到提示信息,這個時候,就可以創建自定義dashboard了,按照下圖標記處操作(當然,如果你沒有編輯或管理權限,那麼,在上一步驟的more中點擊"Panel JSON",在彈出的文本框中複製下所有的JSON文本,然後,粘貼到你自定義的dashboard的"JSON Model"標籤頁中的代碼段的Panels標籤下,注意,要針貼到中括號中,多個監控項的panel json之間要用逗號分割 )
- 現在,面板沒有任何東西,一塊白板(窗口可以在鼠標指向面板時通過右下角的小符號拖動調整)
- 現在,我們還要在dashboard settings面板中拷貝一些variables設置才能夠查詢到數據
- 在跳轉的窗口中,點擊"JSON Model",將其中的templating部分全部copy(templating)
- 粘貼到自定義的dashboard settings的JSON Model窗口中的templating花括號下,然後點擊下方的"Save Changes"按鈕
- 然後,在Variables標籤頁下就可以看到一堆variables了,而且,剛添加的複製延遲監控項的面板也有數據了,而且,下拉列表也有了(除了直接拷貝json代碼之外,你當然也可以對照著其他模板的每一個variables,一個一個手工照著配置,只是比較麻煩)
- 現在,我們調整一下該面板的一些屬性,如下圖,Metrics輸入框中的2個花括號刪除,時期不安列表切換,所有監控數據庫在一個面板中可以看到
- 現在,我們為這個面板添加一個頁面視覺閥值(Visual thresholds,即,相當於可以直接肉眼觀察的一個閥值水位線)
- 設置完成之後,我們回到dashboard,可以看到如下效果
- 現在,我們對主庫使用sysbench 1.0版本加一些寫壓力(oltp_write_only),持續幾分鐘之後觀察效果,如下圖,可以看到,MySQL-Slave1超過了嚴重級別的告警線,MySQL-Slave2超過了警告級別的告警線
- 如果你的在面板中看到的數值很久沒動靜,那麼很可能是你的刷新間隔被關掉了,可以通過右上角的下拉列表進行調整
- 到這裡,dashboard的面板中直接配置視覺閥值線完結,除此之外,我們還可以單獨配置一個告警列表面板,但是,由於監控複製延遲的面板配置中使用了模板中的變量,因此不支持alert列表,它與視覺閥值是互斥的。這裡不再贅述,如果有需要的童鞋請自行研究(如果某個監控項的面板可以配置alert,則可以看到類似如下按鈕)
- PS1:如果你想要查看某個監控指標是採集的什麼值,那麼可以查看URL信息,通常一個外網的鏈接就是真實的,如果是一個內部URL,可能無法查看有效信息
- 通常將鼠標指向左側的"i"圖標,就能看到彈出一個藍色框,點擊下方的鏈接就可以進行跳轉,需要注意的是,這些鏈接有些是外部鏈接,有些是內部鏈接
- 進入編輯模式,就能看到具體的URL,帶https開頭的都是外部的,帶/graph開頭的就是內部鏈接,只有外部鏈接能正常跳轉,內部鏈接通常不能用
- PS2:
- 關於單機多實例部署MySQL的監控,可參考"沃趣技術"微信公眾號中的 "prometheus配置監控多個MySQL實例"
- 更多關於配置郵件告警的步驟,可參考"沃趣技術"微信公眾號中的 "prometheus配置MySQL郵件報警配置"
- 更多關於prometheus的原理和架構,可參考"沃趣技術"微信公眾號中的"開源監控系統Prometheus的前世今生"
- 參考鏈接:
- https://prometheus.io/docs/
- https://grafana.com/docs/
- https://www.cnblogs.com/biaopei/p/12096705.html
- zabbix + grafana:https://cloud.tencent.com/developer/article/1352735
- 全文完。