Prometheus監控


Prometheus介紹

(1)Prometheus由來

普羅米修斯的靈感來自於谷歌的Borgmon。它最初是由馬特·t·普勞德(Matt T. Proud)作為一個研究項目開發的,普勞德曾是谷歌(google)的一名雇員。在普勞德加入SoundCloud之后,他與另一位工程師朱利葉斯•沃爾茲(Julius Volz)合作, 認真開發普羅米修斯。其他開發人員也參與了這項工作,並繼續在SoundCloud內部進行開發,最終於2015年1月公開 發布。


(2)Prometheus架構

Prometheus架構

  • Prometheus Server:用於收集和存儲時間序列數據。
  • PushGateway:主要用於短期的 jobs。由於這類 jobs 存在時間較短,可能在 Prometheus 來 pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端push metrics。
  • Exporters:Exporter是Prometheus的一類數據采集組件的總稱。它負責從目標處搜集數據,並將其轉化為Prometheus支持的格式。與傳統的數據采集組件不同的是,它並不向中央服務器發送數據,而是等待中央服務器主動前來抓取。
  • Alertmanager:從 Prometheus server 端接收到 alerts 后,會進行去除重復數據,分組,並路由到對收的接受方式,發出報警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。

(3)Prometheus基本原理

Prometheus的基本原理是通過HTTP協議周期性抓取被監控組件的狀態,任意組件只要提供對應的HTTP接口就可以接入監控。不需要任何SDK或者其他的集成過程。這樣做非常適合做虛擬化環境監控系統,比如VM、Docker、Kubernetes等。輸出被監控組件信息的HTTP接口被叫做exporter 。目前互聯網公司常用的組件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux系統信息(包括磁盤、內存、CPU、網絡等等)。


Prometheus安裝

Prometheus Server端安裝

1.下載:
wget https://github.com/prometheus/prometheus/releases/download/v2.8.0/prometheus-2.8.0.linux-amd64.tar.gz
tar xf prometheus-2.8.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/prometheus-2.8.0.linux-amd64 /usr/local/prometheus
mkdir /usr/local/prometheus/data     #數據存放目錄
2.使用screen來管理Prometheus
yum -y install screen
screen     #打開一個新的窗口
/usr/local/prometheus/prometheus --web.listen-address="0.0.0.0:9090" --web.read-timeout=5m --web.max-connections=10 --storage.tsdb.retention=15d  --storage.tsdb.path="data/"   --query.max-concurrency=20   --query.timeout=2m   #C-a d  退出窗口,screen -ls查看后台進程
3.啟動參數說明
--web.read-timeout=5m #請求鏈接的最⼤等待時間,防⽌太多的空閑鏈接 占⽤資源
--web.max-connections=512 #最⼤鏈接數
--storage.tsdb.retention=15d  #prometheus開始采集監控數據后,對於保留期限的設置
--storage.tsdb.path="data/"  #存儲數據路徑,wal目錄保存着按照⼀定間隔的內存中近期的監控數據
--query.timeout=2m   #防⽌單個⽤戶執⾏過慢的查詢
--query.max-concurrency=20  #允許多少用戶同時查詢
注:prometheus 對系統時間⾮常敏感,⼀定要時刻保證系統時間同步,不然曲線是亂的

Prometheus Client端安裝 node_export插件

wget https://github.com/prometheus/node_exporter/releases/download/v0.17.0/node_exporter-0.17.0.linux-amd64.tar.gz
tar xf node_exporter-0.17.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/node_exporter-0.17.0.linux-amd64 /usr/local/node_exporter
2.使用screen來管理Prometheus
yum -y install screen
screen  #打開一個新的窗口
./node_exporter --collector.systemd

Prometheus配置文件說明

# 全局配置
global:
  scrape_interval:     15s   # 多長時間抓取一次數據
  evaluation_interval: 15s   # 多長時間評估一次報警規則
  scrape_timeout:      10s   # 每次抓取數據的超時時間
# 告警配置
alerting:
  ...  #這里我們不使用prometheus自帶的告警,使用無需關注
# 告警規則
rule_files:
  ...  #制定了規則所在的位置,prometheus可以根據這個配置加載規則
# 定義Promeetheus監控那些資源
scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']   #監控prometheus本身的健康情況
#添加客戶端監控
  - job_name: 'test'
    static_configs:
    - targets: ['jenkins:9100','gitlab:9100']   #此處主機名需要在/etc/hosts上定義。

注: 修改完配置文件需要重啟prometheus, web上輸入PrometheusIP:Prot查看頁面。
Prometheus主頁面

Pushgateway

(1) pushgateway介紹

Pushgateway是Prometheus 生態中一個重要工具,使用它的原因主要是:

  1. Prometheus 采用 pull 模式,可能由於不在一個子網或者防火牆原因,導致 Prometheus 無法直接拉取各個 target 數據。
  2. 在監控業務數據的時候,需要將不同數據匯總, 由 Prometheus 統一收集。

Pushgateway缺點:

  1. Prometheus拉取狀態UP只能針對Pushgateway,無法做到對每個節點有效。
  2. 將多個節點數據匯總到pushgateway, 如果pushgateway宕機,受影響比多個target大。

Pushgateway的客戶端采用push方式將數據發送到服務端,Prometheus只需要到Pushgateway拉取數據即可。Pushgateway可以單獨運⾏在任何節點上的插件(並不⼀定要在被監控客戶端)

(2) pushgateway安裝

wget http://github.com/prometheus/pushgateway/releases/download/v0.7.0/pushgateway-0.7.0.linux-amd64.tar.gz
tar xf pushgateway-0.7.0.linux-amd64.tar.gz -C /usr/local/
mv /usr/local/pushgateway-0.7.0.linux-amd64 /usr/local/pushgateway
screen
/usr/local/pushgateway/pushgateway

(3) Prometheus配置文件引用pushgateway

[root@nagios ~]# tail -3 /usr/local/prometheus/prometheus.yml
  - job_name: 'pushgateway'
    static_configs:
    - targets: ['localhost:9091']       
#因為我將pushgateway裝到了prometheus機器上所以使用的主機名是localhost,端口默認是9091。
#需要重啟prometheus。

(4) 客戶端自定義腳本推送數據到pushgateway

我們來寫一個監控客戶端主機登陸用戶數量的腳本,將數據推送到pushgateway
[root@jenkins_test ~]# cat user_login.sh 
#!/bin/bash
count=$(w| awk 'NR==1{print $4}')
label="Count_login_users"
instance_name=$(hostname)
echo "$label $count" | curl --data-binary @- http://192.168.18.213:9091/metrics/job/pushgateway/instance/$instance_name

#job/pushgateway  推送到prometheus.yml的哪一個job⾥。
#instance/$instance_name 推送后顯⽰的機器名是什么。

(5) 客戶端定時推送數據

編寫的監控bash腳本是⼀次性執⾏的bash,我們需要按時間段反復執⾏,所以呢?⾃然就得結合contab了。但是crontab默認只能最短⼀分鍾的間隔,如果希望⼩於⼀分鍾的間隔15s,可以使用如下方法:

[root@jenkins_test ~]# cat user_login.sh 
#!/bin/bash
for((i=1;i<=4;i++));
  do 
  count=$(w| awk 'NR==1{print $4}')
  label="Count_login_users"
  instance_name=$(hostname)
  echo "$label $count" | curl --data-binary @- http://192.168.18.213:9091/metrics/job/pushgateway/instance/$instance_name
  sleep 15        #等待15秒
done

[root@jenkins_test ~]# crontab -l
* * * * * /bin/bash /root/user_login.sh  &>/dev/null

(6) Prometheus頁面查看數據

Grafana

(1) Grafana介紹

Grafana是一個跨平台的開源的度量分析和可視化工具,可以通過將采集的數據查詢然后可視化的展示,並及時通知。它主要有以下幾個特點:

  • 展示方式:快速靈活的客戶端圖表,面板插件有許多不同方式的可視化指標和日志,官方庫中具有豐富的儀表盤插件,比如熱圖、折線圖、圖表等多種展示方式;
  • 數據源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
  • 通知提醒:4.0之后的添加了報警功能,可以以可視方式定義最重要指標的警報規則,Grafana將不斷計算並發送通知,在數據達到閾值時通過Slack、PagerDuty等獲得通知;
  • 混合展示:在同一圖表中混合使用不同的數據源,可以基於每個查詢指定數據源,甚至自定義數據源;
  • 注釋:使用來自不同數據源的豐富事件注釋圖表,將鼠標懸停在事件上會顯示完整的事件元數據和標記;

(2) Grafana安裝(安裝特別簡單)

wget https://dl.grafana.com/oss/release/grafana-6.0.1-1.x86_64.rpm  #最新版本
yum localinstall -y grafana-6.0.1-1.x86_64.rpm
#安裝餅圖插件
cd /var/lib/grafana/plugins/
git clone https://github.com/grafana/piechart-panel.git
#修改配置文件
vim /etc/grafana/grafana.ini  
root_url = http://192.168.18.213:3000    #將localhost改為grafana服務端地址
#啟動Grafana
systemctl start grafana-server.service
systemctl enable grafana-server.service

注: 默認運行在3000端口,web上輸入IP:Prot查看頁面,初始賬號密碼為admin/admin。
Grafana主頁面

(3) Grafana配置連接Prometheus數據源

連接Prometheus數據源

(4) Grafana導入儀表盤

導入儀表盤
編輯儀表盤屬性
編輯屬性

(5) 查看頁面展示效果

頁面效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM