監控神器-Prometheus(服務端搭建)

優勢

  1. 易於管理
  2. 輕易獲取服務內部狀態
  3. 高效靈活的查詢語句
  4. 支持本地和遠程存儲
  5. 採用http協議,默認pull模式拉取數據,也可以通過中間網關push數據
  6. 支持自動發現
  7. 可擴展
  8. 易集成(客戶端分爲不同語言的SDK和不同用途的exporter兩種)

Prometheus運行流程 

    prometheus根據配置定時去拉取各個節點的數據,默認使用的拉取方式是pull,也可以使用pushgateway提供的push方式獲取各個監控節點的數據。將獲取到的數據存入TSDB,一款時序型數據庫。此時prometheus已經獲取到了監控數據,可以使用內置的PromQL進行查詢。它的報警功能使用Alertmanager提供,Alertmanager是prometheus的告警管理和發送報警的一個組件。prometheus原生的圖標功能過於簡單,可將prometheus數據接入grafana,由grafana進行統一管理。 

搭建GO語言環境

下載go安裝包

wget https://dl.google.com/go/go1.12.3.linux-amd64.tar.gz

其他安裝包可在此挑選: https://golang.google.cn/dl/

解壓&配置

tar -C /usr/local -zxvf  go1.12.3.linux-amd64.tar.gz

修改配置文件

vim /etc/profile

在末尾新增兩行

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin

刷新配置

source /etc/profile

檢測是否安裝成功

go version

安裝Grafana

獲取安裝包

下載地址:https://grafana.com/grafana/download

在Linux聯網環境下可以直接獲取

wget https://dl.grafana.com/oss/release/grafana-6.3.5-1.x86_64.rpm
sudo yum localinstall grafana-6.3.5-1.x86_64.rpm

接着把Grafana加入到系統服務,將服務啓動

sudo /sbin/chkconfig --add grafana-server
sudo service grafana-server start

安裝Prometheus

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

解壓

tar  -vxf prometheus-2.12.0.linux-amd64.tar.gz  -C /usr/local/

製作軟連接

ln  -sv  /usr/local/prometheus-2.12.0.linux-amd64/  /usr/local/prometheus

進入目錄後執行命令

./prometheus 

node_exporter安裝

tar xvf node_exporter-0.18.1.linux-amd64.tar.gz -C  /usr/local/
cd /usr/local/node_exporter-0.18.1.linux-amd64/ 
nohup ./node_exporter >/dev/null 2>&1 &

修改 prometheus.yml

cd /usr/local/prometheus
vim prometheus.yml
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: ['IP:9090']
  - job_name: 'server'
    static_configs:
      - targets: ['IP:9100']

重啓!! !

 

如果需要配置監控對象則配置(prometheus.yml)!!!!

配置文件大致可以分爲四部分:

  • global:全局配置,其中scrape_interval表示抓取一次數據的間隔時間,evaluation_interval表示進行告警規則檢測的間隔時間;
  • alerting:告警管理器(Alertmanager)的配置,目前還沒有安裝Alertmanager;
  • rule_files:告警規則有哪些;
  • scrape_configs:抓取監控信息的目標。一個job_name就是一個目標,其targets就是採集信息的IP和端口。這裏默認監控了Prometheus自己,可以通過修改這裏來修改Prometheus的監控端口。Prometheus的每個exporter都會是一個目標,它們可以上報不同的監控信息,比如機器狀態,或者mysql性能等等,不同語言sdk也會是一個目標,它們會上報你自定義的業務監控信息。

 整合Grafana

到設置頁面配置Prometheus的連接信息

 例子:計算出整個CPU的工作狀態

(((count(count(node_cpu_seconds_total) by (cpu))) - avg(sum by (mode)(irate(node_cpu_seconds_total{mode='idle'}[5m])))) * 100) / count(count(node_cpu_seconds_total) by (cpu))

訪問該地址https://grafana.com/grafana/dashboards

使用官方推薦儀表盤!

推薦:https://www.jianshu.com/p/413fd42ae660

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章