利用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


免責聲明!

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



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