一、介紹Prometheus
Prometheus(普羅米修斯)是一套開源的監控&報警&時間序列數據庫的組合,起始是由SoundCloud公司開發的。隨着發展,越來越多公司和組織接受采用Prometheus,社會也十分活躍,他們便將它獨立成開源項目,並且有公司來運作。Google SRE的書內也曾提到跟他們BorgMon監控系統相似的實現是Prometheus。現在最常見的Kubernetes容器管理系統中,通常會搭配Prometheus進行監控。
Prometheus基本原理是通過HTTP協議周期性抓取被監控組件的狀態,這樣做的好處是任意組件只要提供HTTP接口就可以接入監控系統,不需要任何SDK或者其他的集成過程。這樣做非常適合虛擬化環境比如VM或者Docker 。
Prometheus應該是為數不多的適合Docker、Mesos、Kubernetes環境的監控系統之一。
輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系統信息 (包括磁盤、內存、CPU、網絡等等),具體支持的源看:https://github.com/prometheus。
與其他監控系統相比,Prometheus的主要特點是:
- 一個多維數據模型(時間序列由指標名稱定義和設置鍵/值尺寸)。
- 非常高效的存儲,平均一個采樣數據占~3.5bytes左右,320萬的時間序列,每30秒采樣,保持60天,消耗磁盤大概228G。
- 一種靈活的查詢語言。
- 不依賴分布式存儲,單個服務器節點。
- 時間集合通過HTTP上的PULL模型進行。
- 通過中間網關支持推送時間。
- 通過服務發現或靜態配置發現目標。
- 多種模式的圖形和儀表板支持。
二、Prometheus架構概覽
該圖說明了普羅米修斯(Prometheus)及其一些生態系統組件的整體架構:
它的服務過程是這樣的Prometheus daemon負責定時去目標上抓取metrics(指標) 數據,每個抓取目標需要暴露一個http服務的接口給它定時抓取。
Prometheus:支持通過配置文件、文本文件、zookeeper、Consul、DNS SRV lookup等方式指定抓取目標。支持很多方式的圖表可視化,例如十分精美的Grafana,自帶的Promdash,以及自身提供的模版引擎等等,還提供HTTP API的查詢方式,自定義所需要的輸出。
Alertmanager:是獨立於Prometheus的一個組件,可以支持Prometheus的查詢語句,提供十分靈活的報警方式。
PushGateway:這個組件是支持Client主動推送metrics到PushGateway,而Prometheus只是定時去Gateway上抓取數據。
如果有使用過statsd的用戶,則會覺得這十分相似,只是statsd是直接發送給服務器端,而Prometheus主要還是靠進程主動去抓取。
大多數Prometheus組件都是用Go編寫的,它們可以輕松地構建和部署為靜態二進制文件。訪問prometheus.io以獲取完整的文檔,示例和指南。
三、Prometheus的數據模型
Prometheus從根本上所有的存儲都是按時間序列去實現的,相同的metrics(指標名稱) 和label(一個或多個標簽) 組成一條時間序列,不同的label表示不同的時間序列。為了支持一些查詢,有時還會臨時產生一些時間序列存儲。
metrics name&label指標名稱和標簽
每條時間序列是由唯一的”指標名稱”和一組”標簽(key=value)”的形式組成。
指標名稱:一般是給監測對像起一名字,例如http_requests_total這樣,它有一些命名規則,可以包字母數字_之類的的。通常是以應用名稱開頭_監測對像_數值類型_單位這樣。例如:push_total、userlogin_mysql_duration_seconds、app_memory_usage_bytes。
標簽:就是對一條時間序列不同維度的識別了,例如一個http請求用的是POST還是GET,它的endpoint是什么,這時候就要用標簽去標記了。最終形成的標識便是這樣了:http_requests_total{method=”POST”,endpoint=”/api/tracks”}。
記住,針對http_requests_total這個metrics name無論是增加標簽還是刪除標簽都會形成一條新的時間序列。
查詢語句就可以跟據上面標簽的組合來查詢聚合結果了。
如果以傳統數據庫的理解來看這條語句,則可以考慮http_requests_total是表名,標簽是字段,而timestamp是主鍵,還有一個float64字段是值了。(Prometheus里面所有值都是按float64存儲)。
四、Prometheus四種數據類型
Counter
Counter用於累計值,例如記錄請求次數、任務完成數、錯誤發生次數。一直增加,不會減少。重啟進程后,會被重置。
例如:http_response_total{method=”GET”,endpoint=”/api/tracks”} 100,10秒后抓取http_response_total{method=”GET”,endpoint=”/api/tracks”} 100。
Gauge
Gauge常規數值,例如 溫度變化、內存使用變化。可變大,可變小。重啟進程后,會被重置。
例如: memory_usage_bytes{host=”master-01″} 100 < 抓取值、memory_usage_bytes{host=”master-01″} 30、memory_usage_bytes{host=”master-01″} 50、memory_usage_bytes{host=”master-01″} 80 < 抓取值。
Histogram
Histogram(直方圖)可以理解為柱狀圖的意思,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。它特別之處是可以對記錄的內容進行分組,提供count和sum全部值的功能。
例如:{小於10=5次,小於20=1次,小於30=2次},count=7次,sum=7次的求和值。
Summary
Summary和Histogram十分相似,常用於跟蹤事件發生的規模,例如:請求耗時、響應大小。同樣提供 count 和 sum 全部值的功能。
例如:count=7次,sum=7次的值求值。
它提供一個quantiles的功能,可以按%比划分跟蹤的結果。例如:quantile取值0.95,表示取采樣值里面的95%數據。
五、安裝運行Prometheus(二進制版)
下面介紹如何使用Prometheus和Grafana對MySQL服務器性能進行監控。
我們用到了以下兩個exporter:
- node_exporter – 用於機器系統數據收集
- mysqld_exporter – 用於MySQL服務器數據收集
Grafana是一個開源的功能豐富的數據可視化平台,通常用於時序數據的可視化。它內置了以下數據源的支持:
下面是我們安裝時用到的架構圖:
首先安裝GO
|
1
2
3
|
$ yum install go
$ go version
go version go1.6.3 linux/amd64
|
下載安裝Prometheus(https://prometheus.io/download/)
|
1
2
3
4
|
$ wget https://github.com/prometheus/prometheus/releases/download/v1.6.2/prometheus-1.6.2.linux-amd64.tar.gz
$ tar xvf prometheus-1.6.2.linux-amd64.tar.gz -C /usr/local/
$ ln -sv /usr/local/prometheus-1.6.2.linux-amd64/ /usr/local/prometheus
$ cd /usr/local/prometheus
|
首先,在創造上的主機文件系統的最小Prometheus配置文件prometheus.yml (替換你要監控的IP地址):
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
global:
scrape_interval: 60s
evaluation_interval: 60s
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['localhost:9090']
labels:
instance: prometheus
- job_name: linux
static_configs:
- targets: ['10.10.0.186:9100']
labels:
instance: db1
- job_name: mysql
static_configs:
- targets: ['10.10.0.186:9104']
labels:
instance: db1
|
10.10.0.186是我們數據庫主機的IP,端口則是對應的exporter的監聽端口。
啟動Prometheus
|
1
2
3
4
5
6
7
8
|
$ ./prometheus -config.file=prometheus.yml
INFO[0000] Starting prometheus (version=1.6.2, branch=master, revision=b38e977fd8cc2a0d13f47e7f0e17b82d1a908a9a) source=main.go:88
INFO[0000] Build context (go=go1.8.1, user=root@c99d9d650cf4, date=20170511-12:59:13) source=main.go:89
INFO[0000] Loading configuration file prometheus.yml source=main.go:251
INFO[0000] Loading series map and head chunks... source=storage.go:421
INFO[0000] 1032 series loaded. source=storage.go:432
INFO[0000] Starting target manager... source=targetmanager.go:61
INFO[0000] Listening on :9090 source=web.go:259
|
Prometheus內置了一個web界面,我們可通過http://monitor_host:9090進行訪問:
在Status->Targets頁面下,我們可以看到我們配置的兩個Target,它們的State為DOWN。
下一步我們需要安裝並運行exporter,下載exporters並解壓到被監控端服務器:
|
1
2
|
$ wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.14.0.linux-amd64.tar.gz
$ wget https://github.com/prometheus/mysqld_exporter/releases/download/v0.10.0/mysqld_exporter-0.10.0.linux-amd64.tar.gz
|
被監控安裝GO環境
|
1
2
3
|
$ yum install go -y
$ go version
go version go1.6.3 linux/amd64
|
安裝運行node_exporter
|
1
2
|
$ tar xvf node_exporter-0.14.0.linux-amd64.tar.gz -C /usr/local/
$ nohup /usr/local/node_exporter-0.14.0.linux-amd64/node_exporter &
|
安裝運行mysqld_exporter
mysqld_exporter需要連接到Mysql,所以需要Mysql的權限,我們先為它創建用戶並賦予所需的權限.
|
1
2
|
mysql> GRANT REPLICATION CLIENT,PROCESS ON *.* TO 'mysql_monitor'@'localhost' identified by 'mysql_monitor';
mysql> GRANT SELECT ON *.* TO 'mysql_monitor'@'localhost';
|
創建.my.cnf文件並運行mysqld_exporter:
|
1
2
3
4
|
$ cat /usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf
[client]
user=mysql_monitor
password=mysql_monitor
|
|
1
2
|
$ tar xvf mysqld_exporter-0.10.0.linux-amd64.tar.gz -C /usr/local/
$ /usr/local/mysqld_exporter-0.10.0.linux-amd64/mysqld_exporter -config.my-cnf="/usr/local/mysqld_exporter-0.10.0.linux-amd64/.my.cnf" &
|
我們再次回到Status->Targets頁面,可以看到兩個Target的狀態已經變成UP了:
接下來就可以看圖形了
Prometheus自帶的圖形並不夠強大,於是我們可以使用Grafana作為Prometheus的Dashboard。
六、安裝運行Grafana
|
1
2
|
$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.2.0-1.x86_64.rpm
$ sudo yum localinstall grafana-4.2.0-1.x86_64.rpm
|
編輯配置文件/etc/grafana/grafana.ini,修改dashboards.json段落下兩個參數的值:
|
1
2
3
|
[dashboards.json]
enabled = true
path = /var/lib/grafana/dashboards
|
安裝儀表盤(Percona提供)
|
1
2
|
$ git clone https://github.com/percona/grafana-dashboards.git
$ cp -r grafana-dashboards/dashboards /var/lib/grafana
|
運行以下命令為Grafana打個補丁,不然圖表不能正常顯示:
|
1
2
|
$ sed -i 's/expr=\(.\)\.replace(\(.\)\.expr,\(.\)\.scopedVars\(.*\)var \(.\)=\(.\)\.interval/expr=\1.replace(\2.expr,\3.scopedVars\4var \5=\1.replace(\6.interval, \3.scopedVars)/' /usr/share/grafana/public/app/plugins/datasource/prometheus/datasource.js
$ sed -i 's/,range_input/.replace(\/"{\/g,"\\"").replace(\/}"\/g,"\\""),range_input/; s/step_input:""/step_input:this.target.step/' /usr/share/grafana/public/app/plugins/datasource/prometheus/query_ctrl.js
|
最后我們運行Grafana服務
|
1
2
3
|
$ systemctl daemon-reload
$ systemctl start grafana-server
$ systemctl status grafana-server
|
我們可通過http://monitor_host:3000訪問Grafana網頁界面(缺省的帳號/密碼為admin/admin):
然后我們到Data Sources頁面添加數據源:
系統監控概覽
MySQL監控概覽
<參考資料>
https://github.com/percona/grafana-dashboards
https://www.percona.com/blog/…
如果只是想監控MySQL或MongoDB,那么請使用PMM(Percona Monitoring and Management)監控工具,在Prometheus+Granafa基礎之上添加了慢查詢收集等額外功能,更專業的MySQL&MongoDB監控系統。
完結。。。










