之前聽別人說使用Prometheus做監控,最近自己也在測試環境試用了一下。目前總的體會是安裝簡單方便,不需要額外的數據庫服務;yaml格式的配置文件,簡單易讀;提供的採集用的exporter是二進制文件,運行即可;配合Grafana展示,有Prometheus專用dashboards可用,一目瞭然。
相關環境
工具 | 版本 |
---|---|
操作系統 | centos7 |
Prometheus | prometheus-1.7.1.linux-amd64 |
node_exporter | node_exporter-0.14.0.linux-amd64 |
mysqld_exporter | mysqld_exporter-0.10.0.linux-amd64 |
Grafana | 4.3.1 |
安裝運行
安裝方法
在官網上下載Prometheus的包之後,解壓即可
運行方法
進入解壓後的目錄,裏面有個名爲prometheus可執行文件和prometheus.yml配置文件,執行如下命令即可運行
./prometheus -config.file=prometheus.yml
之後訪問http://localhost:9090/ 就可以訪問到prometheus的頁面了。
配置說明
默認的prometheus.yml配置文件內容如下:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['localhost:9090']
配置文件爲yaml格式,配合上面的註釋,很易讀。global 配置一些全局信息, scrape_configs 配置具體想要抓取的目標。這裏默認是配置了一個名爲prometheus的監控自己localhost:9090的抓取任務,並且抓取間隔設置的5s會覆蓋全局配置中的15s。targets後面的值除了用中括號外,還可以用下面這種寫法:
- targets:
- 10.20.30.100:9100
- 10.20.30.101:9100
- 10.20.30.102:9100
- 10.20.30.103:9100
個人喜歡這種寫法,感覺比中括號寫的形式更直觀、方便。
更詳細的配置說明,還是建議去看官方配置說明,會有很多收穫。
重新加載
當配置文件修改後,需要重新加載才能生效。prometheus提供了api可供reload操作;同時prometheus也接受命令信號進行加載。下面是我使用的一個reload操作的腳本:
#!/bin/bash
PID=`ps -ef | grep "prometheus -config.file=prometheus.yml" | grep -v grep |awk '{print $2}'`
if [ ! -z $PID ];then
echo "----- reload prometheus ------"
kill -HUP $PID > /dev/null
fi
# 或者通過api方式reload
# curl -X POST http://localhost:9090/-/reload
使用exporter採集數據
待補充
使用Grafana展示
待補充