下載Prometheus
下載使用你平台的最新版本的Prometheus,然后解壓:
curl -LO https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz tar xvfz prometheus-*.tar.gz cd prometheus-*
prometheus服務是一個單一的二進制文件。我們運行這個二進制文件,通過可選擇項--help
尋求幫助
./prometheus --help usage: prometheus [<flags>] The Prometheus monitoring server . . .
配置Prometheus
prometheus配置文件是yaml文件。prometheus安裝包里面有一個簡單的配置文件模版叫prometheus.yml
,我們從這里開始。
為了直觀,下面的prometheus.yml
內容省去了大多注釋的內容(通過#
可以增加注釋)。
global: scrape_interval: 15s evaluation_interval: 15s rule_files: # - "first.rules" # - "second.rules" scrape_configs: - job_name: prometheus static_configs: - targets: ['localhost:9090']
在上述配置文件中有三個區塊:global
, rule_files
and scrape_configs
。
global
控制prometheus服務的全局配置。當前配置文件配置了兩項。scrape_interval
控制prometheus抓取目標metrics的頻率(每15s采集一次目標metrics),當然對個別的目標可以覆蓋該全局配置。evaluation_interval
控制評估規則的頻率,prometheus使用規則產生新的時間序列數據或者產生警報。
rule_files
塊制定了規則所在的位置,prometheus可以根據這個配置加載規則,當前我們還沒有配置任何規則。
scrape_configs
控制prometheus監控哪些資源。由於prometheus通過HTTP endpoint暴露的它本身的監控數據,prometheus也能夠監控本身的健康情況。在默認的配置里有一個單獨的job,叫做prometheus
,它采集prometheus服務本身輸出的時間序列數據。這個job包含了一個單獨的、靜態配置的目標:在端口9090上的localhost
。prometheus默認會通過目標的/metrics
路徑采集metrics。所以,默認的job通過URL:http://localhost:9090/metrics
采集metrics。收集到的時間序列會詳述prometheus服務的狀態和性能。
For a complete specification of configuration options, see the configuration documentation.
eg:
global: evaluation_interval: 10s scrape_interval: 10s scrape_timeout: 5s rule_files: - /usr/local/zstack/prometheus/rules/hosts/collectd.rule scrape_configs: - file_sd_configs: - files: - /usr/local/zstack/prometheus/discovery/hosts/*.json refresh_interval: 10s job_name: collectd scrape_interval: 10s scrape_timeout: 5s
開始運行Prometheus
使用我們的配置文件運行prometheus,cd到prometheus目錄,運行prometheus:
./prometheus --config.file=prometheus.yml
運行prometheus之后,你可以通過http://localhost:9090訪問prometheus的狀態頁面。你也可以通過本地訪問metrics endpoint:http://localhost:9090/metrics確認prometheus在工作,並在產生它自己的metrics。
使用表達式瀏覽器
我們首先展示prometheus自己采集自己的數據,prometheus自帶展示界面。打開瀏覽器,訪問http://localhost:9090/graph,點擊Console
按鈕
其中一個prometheus輸出的metric是http_requests_total
(prometheus服務接收的http請求總數)。在瀏覽器輸入http_requests_total
,會返回許多不同的時間序列(每個序列按照時間排序)。所有的序列有着同一個metric namehttp_requests_total
,不同的標簽;這些標簽標明不同類型的請求。如果我們僅僅對狀態碼200的請求感興趣,可以使用下面的查詢表達式查詢相應的信息:
http_requests_total{code="200"}
為了計算序列的數量,可以使用如下查詢表達式:
count(http_requests_total)
For more about the expression language, see the expression language documentation.
使用繪圖界面
訪問http://localhost:9090/graph,點擊Graph
按鈕。
輸入如下表達式,繪制每秒HTTP請求率:
rare(http_requests_total[1m])
安裝Node Exporter
收集本身的metrics不能很好的表現Prometheus的能力。所以,接下來我們使用Node Exporter監控我們第一個資源Linux Host,這個樣例是監控prometheus所在的host,不過你可以監控任意一個host(只要prometheus能夠訪問)。你也可以使用VMI Exporter
采集windows主機。
Download the latest release of the Node Exporter of Prometheus for your platform, then extract it:
curl -LO https://github.com/prometheus/node_exporter/releases/download/v0.15.2/node_exporter-0.15.2.linux-amd64.tar.gz tar xvfz node_exporter-*.tar.gz cd node_exporter-*
Node Exporter是一個單獨的二進制文件,node_exporter
,有一組可配置的收集器用於手機各種基於主機的metrics。默認,收集器會收集cpu、內存、磁盤和其它metrics,暴露,使prometheus能夠抓取監控數據。
啟動Node Exporter:
./node_exporter
Node Exporter的metrics可以通過訪問9100端口的/metrics路徑獲取。本例中,訪問地址:http://localhost:9100/metrics。
現在我們需要配置Prometheus,使Prometheus能夠發現這個exporter。
配置prometheus監控Host
我們將會配置prometheus抓取新的目標metrics。我們需要在prometheus.yml
配置的scrape_configs
區增加一個job。
- job_name: node static_configs: - targets: ['localhost:9100']
我們新的job叫做“node”。它抓取一個靜態的目標,在端口9100上的loclahost。你可以用hostname或者IP地址替換localhost。
現在我們需要重新加載Prometheus配置來激活這個新的job。reload配置有兩種方式:
- send SIGHUP signal
kill -HUP <pid>
- send a HTTP POST to the Prometheus web server
使用API來reload prometheus需要在啟動prometheus時,開啟web.enable-lifecycle配置參數 --web.enable-lifecycle Enable shutdown and reload via HTTP request. /prometheus --config.file=prometheus.yml --web.enable-lifecycle
curl -X POST http://localhost:9090/-/reload
訪問prometheus,在“Execute”按鈕旁邊有一個下拉框,在下拉框中可以看到prometheus采集的指標列表。在這個列表中能夠看到以node_
開頭metrics,這些metrics就是Node Exporter收集的。比如通過node_cpu
查看節點CPU使用率。
一個很有用的metric是up
metric。up
metric能夠標識目標的狀態。如果值是1,那么能夠成功的從目標抓取metrics;如果值是0,那么則標明從該目標抓取metrics失敗。在一定程度上能夠通過改指標判斷目標的狀態。當前只能看到兩個up
metric,一個是關於Node Exporter的,一個是關於prometheus的。
總結
本文簡單介紹了如何安裝、配置prometheus來監控資源信息。同時,也安裝了第一個exporter,介紹了時間序列的基本查詢方法。
You can find more documentation and guides to help you continue to learn more about Prometheus.
雜記:
Storage
自身包含了一個基於本地磁盤的時間序列數據庫,但是也是支持其他遠程的存儲系統。
Local storage
- --storage.tsdb.path 數據庫位置,默認 data/
- --storage.tsdb.retention 數據保留時間,默認15d
sudo /usr/local/zstack/apache-tomcat/webapps/zstack/WEB-INF/classes/tools/prometheus -config.file /usr/local/zstack/prometheus/conf.yaml -storage.local.path /var/lib/zstack/prometheus/data -storage.local.retention 720h0m0s -query.timeout 2m0s -web.listen-address 192.168.211.5:9090 -query.max-concurrency 20 -alertmanager.url http://192.168.211.5:8080/zstack/prometheus
命令行啟動存儲位置,存儲天數以及報警位置等