1. 程式人生 > 實用技巧 >利用Prometheus + Grafana 對伺服器效能視覺化監控

利用Prometheus + Grafana 對伺服器效能視覺化監控

Prometheus介紹

Prometheus 是一款用於事件監控告警的開源免費應用程式, 採用Go編寫。
Prometheus 工作時通過HTTP的方式週期性抓取被監控元件的效能資料,任意想要被監控的元件只需要提供對應的HTTP介面即可接入監控,不需要額外的SDK支援或者其他的整合過程,輸出被監控元件效能資訊的HTTP介面被叫做exporter。
其中常用的exporter有node_exporter,可以用來輸出伺服器的CPU使用率,磁碟佔用情況,網路頻寬使用情況,等基本效能資訊。

Grafana 介紹

Grafana是一個跨平臺的開源的度量分析和視覺化工具,可以通過將採集的資料查詢然後視覺化的展示,並及時通知。它主要有以下六大特點:

1、展示方式:快速靈活的客戶端圖表,面板外掛有許多不同方式的視覺化指標和日誌,官方庫中具有豐富的儀表盤外掛,比如熱圖、折線圖、圖表等多種展示方式;
2、資料來源:Graphite,InfluxDB,OpenTSDB,Prometheus,Elasticsearch,CloudWatch和KairosDB等;
3、通知提醒:以可視方式定義最重要指標的警報規則,Grafana將不斷計算併發送通知,在資料達到閾值時通過Slack、PagerDuty等獲得通知;
4、混合展示:在同一圖表中混合使用不同的資料來源,可以基於每個查詢指定資料來源,甚至自定義資料來源;
5、註釋:使用來自不同資料來源的豐富事件註釋圖表,將滑鼠懸停在事件上會顯示完整的事件元資料和標記;
6、過濾器:Ad-hoc過濾器允許動態建立新的鍵/值過濾器,這些過濾器會自動應用於使用該資料來源的所有查詢。

node_exporter 安裝

新增使用者

useradd --no-create-home --shell /bin/false node_exporter

下載安裝

curl -fsSL https://github.com/prometheus/node_exporter/releases/download/v1.0.1/node_exporter-1.0.1.linux-amd64.tar.gz \
  | sudo tar -zxvf - -C /usr/local/bin --strip-components=1 node_exporter-1.0.1.linux-amd64/node_exporter \
  && sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter

建立服務

tee /etc/systemd/system/node_exporter.service <<"EOF"
[Unit]
Description=Node Exporter

[Service]
User=node_exporter
Group=node_exporter
EnvironmentFile=-/etc/sysconfig/node_exporter
ExecStart=/usr/local/bin/node_exporter $OPTIONS

[Install]
WantedBy=multi-user.target
EOF

開機自啟並啟動服務

systemctl daemon-reload && \
systemctl start node_exporter && \
systemctl status node_exporter && \
systemctl enable node_exporter

node_exporter服務預設監控在9100埠,訪問http://服務IP:9100/metrics 正常情況下出現如下畫面

docker安裝Prometheus + Grafana

Prometheus 配置檔案 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).

# Alertmanager configuration
alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# 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'

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
    - targets: ['localhost:9090']

   # 主要是新增了node_exporter的job,如果有多個node_exporter,在targets陣列後面加即可

  - job_name: 'node_exporter'
    static_configs:
      - targets: ['伺服器IP:9100']

docker-compose.yml 檔案

version: '3'
services:
    grafana:
        image: grafana/grafana
        container_name: grafana
        restart: always
        ports:
          - "3000:3000"

    prometheus:
        image: prom/prometheus
        container_name: prometheus
        restart: always
        ports:
          - "9090:9090"
        volumes:
          - ./prometheus.yml:/etc/prometheus/prometheus.yml

docker-compose up -d 啟動
訪問http://docker伺服器IP:9090/targets 應該出現如下頁面

建立Dashboard

登入進Grafana 3000埠, 預設憑證(admin/admin)
建立資料來源, 由於是Docker執行的,可以填服務名稱。

建立Dashboard 時候,匯入以下模版即可,可以填入URL, 最後選擇建立的資料來源即可。

https://grafana.com/api/dashboards/8919/revisions/24/download

展示

主機效能視覺化

Mongo效能展示

參考

https://www.jianshu.com/p/821bf7d2bbef
https://grafana.com/grafana/dashboards/8919
https://gist.github.com/jarek-przygodzki/735e15337a3502fea40beba27e193b04