Grafana+Prometheus打造全方位立體監控系統

  • 2019 年 12 月 5 日
  • 筆記

前言

本文主要介紹如何使用Grafana和Prometheus以及node_exporter對Linux伺服器性能進行監控。下面兩張圖分別是兩台伺服器:

伺服器A

伺服器B

概述

Prometheus是一個開源的服務監控系統,它通過HTTP協議從遠程的機器收集數據並存儲在本地的時序資料庫上。

  • 多維數據模型(時序列數據由metric名和一組key/value組成)
  • 在多維度上靈活的查詢語言(PromQl)
  • 不依賴分散式存儲,單主節點工作.
  • 通過基於HTTP的pull方式採集時序數據
  • 可以通過push gateway進行時序列數據推送(pushing)
  • 可以通過服務發現或者靜態配置去獲取要採集的目標伺服器
  • 多種可視化圖表及儀錶盤支援

Prometheus通過安裝在遠程機器上的exporter來收集監控數據,後面我們將使用到node_exporter收集系統數據。

架構

Grafana 是一個開箱即用的可視化工具,具有功能齊全的度量儀錶盤和圖形編輯器,有靈活豐富的圖形化選項,可以混合多種風格,支援多個數據源特點。

架構

安裝

Exporter

下載並解壓:

#下載  wget https://github.com/prometheus/node_exporter/releases/download/v0.14.0/node_exporter-0.15.0.linux-amd64.tar.gz -O node_exporter-0.15.0.linux-amd64.tar.gz  # 可自定義解壓目錄  tar -xvf node_exporter-0.15.0.linux-amd64.tar.gz

運行node_exporter:

# 後台運行  ./node_exporter &

Prometheus

下載地址:https://prometheus.io/download

執行以下命令:

## 下載  wget https://github.com/prometheus/prometheus/releases/download/v2.0.0-rc.3/prometheus-2.0.0-rc.3.linux-amd64.tar.gz  ## 可自定義解壓目錄  tar -xvf prometheus-2.0.0-rc.3.linux-amd64.tar.gz

配置prometheus,vi prometheus.yml

global:    scrape_interval:     15s    evaluation_interval: 15s      - job_name: prometheus      static_configs:        - targets: ['localhost:9090']          labels:            instance: prometheus      - job_name: linux1      static_configs:        - targets: ['192.168.1.120:9100']          labels:            instance: sys1      - job_name: linux2      static_configs:        - targets: ['192.168.1.130:9100']          labels:            instance: sys2

IP對應的是我們內網的伺服器,埠則是對應的exporter的監聽埠。

運行Prometheus

./prometheus  level=info ts=2017-11-07T02:39:50.220187934Z caller=main.go:215 msg="Starting Prometheus" version="(version=2.0.0-rc.2, branch=HEAD, revision=ce63a5a8557bb33e2030a7756c58fd773736b592)"  level=info ts=2017-11-07T02:39:50.22025258Z caller=main.go:216 build_context="(go=go1.9.1, user=root@a6d2e4a7b8da, date=20171025-18:42:54)"  level=info ts=2017-11-07T02:39:50.220270139Z caller=main.go:217 host_details="(Linux 3.10.0-514.16.1.el7.x86_64 #1 SMP Wed Apr 12 15:04:24 UTC 2017 x86_64 iZ2ze74fkxrls31tr2ia2fZ (none))"  level=info ts=2017-11-07T02:39:50.223171565Z caller=web.go:380 component=web msg="Start listening for connections" address=0.0.0.0:9090  ......

啟動成功以後我們可以通過Prometheus內置了web介面訪問,http://ip:9090 ,如果出現以下介面,說明配置成功

Grafana

執行以下安裝命令:

## 安裝依賴grafana運行需要go環境  yum install  go -y  ## 安裝 grafana  yum install https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana-4.6.1-1.x86_64.rpm -y

安裝包資訊:

二進位文件: /usr/sbin/grafana-server

init.d 腳本: /etc/init.d/grafana-server 環境變數文件: /etc/sysconfig/grafana-server 配置文件: /etc/grafana/grafana.ini 啟動項: grafana-server.service 日誌文件:/var/log/grafana/grafana.log 默認配置的sqlite3資料庫:/var/lib/grafana/grafana.db

你可以執行以下啟動命令:

service grafana-server start

啟動grafana,並設置開機啟動:

systemctl daemon-reload  systemctl start grafana-server  systemctl status grafana-server  systemctl enable grafana-server.service

伺服器端影像(PNG)渲染是可選的功能,但在共享可視化時非常有用,例如在警報通知中。 如果影像缺少文本,請確保已安裝字體包。

yum install fontconfig  yum install freetype*  yum install urw-fonts

訪問Grafana通過Nginx代理,默認登錄用戶名密碼:admin/admin,需及時修改。

server {          listen       80;          server_name  grafana.52itstyle.com;            charset utf-8;            location / {              default_type text/html;              proxy_pass http://127.0.0.1:3000;          }    }

編輯配置文件/etc/grafana/grafana.ini ,修改dashboards.json段落下兩個參數的值:

[dashboards.json]  enabled = true  path = /var/lib/grafana/dashboards

安裝儀錶盤JSON模版:

git clone https://github.com/percona/grafana-dashboards.git  cp -r grafana-dashboards/dashboards /var/lib/grafana/

最後,通過service grafana-server start命令啟動服務,訪問地址:http://grafana.52itstyle.com

然後在Data Sources選項中添加數據源:

添加成功以後,我們就可以查看到文章開頭的效果圖了。

總結

講道理,這一套東西還是很強大的,各種開源組間一整合完美搭建出一套監控系統。當然了以上僅僅是系統的一個監控,Grafana以及exporter組間還可以實現對Nginx、MySql、Redis以及MongDB的監控。

監控不是目的,目的是出現問題能夠及時發現並解決問題。

參考資料

https://grafana.com/

https://prometheus.io/

https://github.com/prometheus

https://github.com/prometheus/node_exporter

https://github.com/percona/grafana-dashboards

https://www.percona.com/blog/2016/02/29/graphing-mysql-performance-with-prometheus-and-grafana/

https://www.cnblogs.com/davygeek/p/6668706.html

Tags: