使用 Promwww9992019comI8I2222IIIIetheus 監控 Ceph

Prometheus

本文是在 Ubuntu 16.04 最新版基礎上安裝 Prometheus 監控系統,Ceph 版本爲 Luminous 12.2.8。

  1. 安裝 Prometheus
    直接使用 apt 安裝的 Prometheus 版本較低,很多新的配置選項都已不再支持,建議使用 Prometheus 的安裝包,接下來看看安

裝包部署的步驟。

先下載安裝包,這裏用的是 2.0.0 版本,目前爲止,最新的應該爲 2.4.0,安裝方法都是一樣的。

$ wget https://github.com/prometheus/prometheus/releases/download/v2.0.0/prometheus-2.0.0.linux-amd64.tar.gz
$ tar zxvf prometheus-2.0.0.linux-amd64.tar.gz
$ cd prometheus-2.0.0.linux-amd64/
$ sudo cp prometheus /usr/bin/
$ sudo cp promtool /usr/bin/
$ vim /lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus: the monitoring system
Documentation=http://prometheus.io/docs/

[Service]
ExecStart=/usr/bin/prometheus --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/var/lib/prometheus --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/prometheus/console_libraries --web.listen-address=0.0.0.0:9090 --web.external-url=
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target
$ sudo mkdir /etc/prometheus/
$ sudo cp -R consoles console_libraries prometheus.yml /etc/prometheus/
$ sudo mkdir /var/lib/prometheus/
$ sudo systemctl daemon-reload
$ sudo systemctl enable prometheus.service
$ sudo systemctl start prometheus.service
在下一步裝好 ceph_exporter 後,還需要在 Promethues 中添加相應配置,不過現在執行到這一步就可以了。

  1. 安裝 ceph_exporter
    2.1 安裝 Go 語言環境

導出 Ceph 信息到 Prometheus 有多種方式,本文采用的是 DigitalOcean 的 ceph_exporter,ceph_exporter 使用 go 語言編寫的,所以需要先安裝 go 語言環境。還是一條命令解決:

$ sudo apt install -y golang
安裝好後執行 $ go env 命令驗證並查看一下 go 環境信息。

$ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go-1.6"
GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"
GO15VENDOREXPERIMENT="1"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++"
CGO_ENABLED="1"
然後需要設置 Go 環境變量:

$ cat /etc/profile.d/go.sh
export GOROOT=/usr/lib/go-1.6
export GOBIN=$GOROOT/bin
export GOPATH=/home//go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

$ source /etc/profile.d/go.sh
已經配置好 Go 環境了,接下來創建 GOPATH 指定的目錄:

$ mkdir /home//go
2.2 安裝 ceph_exporter
Go 環境安裝好後,我們接下來下載 ceph_exporter 代碼,然後編譯出可執行程序。

$ mkdir -p /home//go/src/github.com/digitalocean
$ cd /home//go/github.com/src/digitalocean
$ git clone https://github.com/digitalocean/ceph_exporter
$ cd ceph_exporter
$ go build
這時編譯會報錯,原因是需要依賴 ceph rados 相關的頭文件,需要安裝 librados-dev 包。

$ sudo apt install -y librados-dev
安裝好後,在編譯,複製可執行文件到對應目錄完成安裝。
再運行 go build 完成安裝。

$ go get
$ go build
$ mkdir /home//go/bin
$ cp ceph_exporter /home//go/bin
執行 ceph_exporter 來驗證一下是否可以正常使用

$ ceph_exporter --help
Usage of ceph_exporter:
-ceph.config string

    path to ceph config file

-ceph.user string

    Ceph user to connect to cluster. (default "admin")

-exporter.config string

    Path to ceph exporter config. (default "/etc/ceph/exporter.yml")

-rgw.mode int

    Enable collection of stats from RGW (0:disabled 1:enabled 2:background)

-telemetry.addr string

    host:port for ceph exporter (default ":9128")

-telemetry.path string

    URL path for surfacing collected metrics (default "/metrics")

接下來要配置 ceph_exporter 的自動啓動:

$ cat /lib/systemd/system/ceph_exporter.service
[Unit]
Description=Prometheus's ceph metrics exporter
After=prometheus.ervice

[Service]
User=
Group=
ExecStart=/home//go/bin/ceph_exporter

[Install]
WantedBy=multi-user.target
Alias=ceph_exporter.service

$ sudo systemctl daemon-reload
$ sudo systemctl enable ceph_exporter.service
$ sudo systemctl start ceph_exporter.service
2.3 修改 Promethues 配置
接下來需要修改 Prometheus 的配置,添加一會要安裝的 ceph_exporter 的相關信息:

$ sudo vim /etc/prometheus/prometheus.yml
...
scrape_configs:

  • job_name: 'ceph_exporter'
    static_configs:

    • targets: ['localhost:9128']
      labels:

      alias: ceph_exporter

      ...

改好後,重啓:

$ sudo systemctl restart prometheus.service

  1. 安裝 Grafana
    3.1 安裝

Grafana 也不推薦使用 APT 安裝,原因也是版本太低,安裝官方打包好的版本是更優的選擇。

$ sudo apt-get install -y adduser libfontconfig
$ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/grafana_5.2.4_amd64.deb
$ sudo dpkg -i grafana_5.2.4_amd64.deb
$ sudo systemctl enable grafana-server
$ sudo systemctl start grafana-server
至此 Grafana 也已經安裝好了,接下來登錄 grafana 界面。

3.2 配置 dashboard
Grafana

訪問 http://localhost:3000 來登錄 Grafana,默認用戶爲 admin,密碼也是 admin。

data-source

登錄後首先需要配置 data source,訪問地址 http://localhost:3000/datasources,會出現如上圖所示的界面,按照圖中顯示的信息配置即可。

import

最後需要導入 Ceph 相關的界面,如圖所示,導入的是編號爲 917 的 dashboard(從 grafana.com 上,導入編號爲 917 的 dashboard)。

ceph-cluster

完成後,終於可以看到 Ceph 的監控信息了。

  1. 告警系統
    現在已經有了圖形化界面的狀態監控,但出現緊急情況我們肯定不希望要登錄到界面上才能察覺到,在 Prometheus 系統中,這個工作由 AlertManager 組件負責,接下來我們就以釘釘消息通知爲例,看一下如何配置告警系統。

4.1 安裝 AlertManager
AlertManager 的安裝流程和 Prometheus 很像,也是需要下載對應的安裝包。

$ wget https://github.com/prometheus/alertmanager/releases/download/v0.15.2/alertmanager-0.15.2.linux-amd64.tar.gz
$ tar zxvf alertmanager-0.15.2.linux-amd64.tar.gz
$ cd alertmanager-0.15.2.linux-amd64
$ sudo cp alertmanager amtool /usr/bin/
$ sudo cp alertmanager.yml /etc/prometheus/
接下來配置 systemd 的 unit 文件。

$ cat /lib/systemd/system/alertmanager.service
[Unit]
Description=Prometheus: the alerting system
Documentation=http://prometheus.io/docs/
After=prometheus.service

[Service]
ExecStart=/usr/bin/alertmanager --config.file=/etc/prometheus/alertmanager.yml
Restart=always
StartLimitInterval=0
RestartSec=10

[Install]
WantedBy=multi-user.target
啓動 alermanager 服務,並配置開機啓動。

$ sudo systemctl enable alertmanager.service
$ sudo systemctl start alertmanager.service
在 Prometheus 中添加 AlertManager 的信息,並重啓 Prometheus。

$ cat /etc/prometheus/prometheus.yml
...
alerting:
alertmanagers:

- static_configs:
  - targets: ["localhost:9093"]

$ sudo systemctl restart prometheus.service
4.2 獲取釘釘的 webhook
要往釘釘發消息,當然要先知道 webhook 是多少,首先是在釘釘羣裏添加一個機器人,然後查看機器人的設置,就可以看到 webhook:

Dingtalk robot API

4.3 配置消息轉發的 API
配置 Prometheus 直接向釘釘 Webhook 發消息應該是發不過去的,Prometheus 的消息格式和釘釘 webhook 並不兼容,而且就算是拿到消息中的字符串再發過去,沒經過格式化的消息也太難看了。截個未經處理的釘釘消息的圖給大家感受一下:

Unformatted prometheus message

所以我們需要配置一個轉發並格式化 Prometheus 消息的 API 服務器,在網上搜了一下還真的找到一個已經做好的格式化 Prometheus 消息的開源項目,完全滿足需求:https://github.com/timonwong/prometheus-webhook-dingtalk,感謝 Timon Wong 的貢獻。接下來介紹一下如何以 Docker 形式部署該 API 服務。

4.3.1 安裝 Docker
首先當然是要先安裝 Docker ,並配置 Docker 從國內鏡像源下載鏡像。

$ sudo apt install -y docker.io
$ sudo vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"]
}
$ sudo systemctl restart docker.service
4.3.2 啓動 prometheus-webhook-dingtalk
先下載鏡像。

$ sudo docker pull timonwong/prometheus-webhook-dingtalk:v0.3.0
啓動鏡像。
這裏解釋一下兩個變量:

:prometheus-webhook-dingtalk 支持多個釘釘 webhook,不同 webhook 就是靠名字對應到 URL 來做映射的。要支持多個釘釘 webhook,可以用多個 --ding.profile 參數的方式支持,例如:sudo docker run -d --restart always -p 8060:8060 timonwong/prometheus-webhook-dingtalk:v0.3.0 --ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=token1" --ding.profile="webhook2=https://oapi.dingtalk.com/robot/send?access_token=token2"。而名字和 URL 的對應規則如下,ding.profile="webhook1=......",對應的 API URL 爲:http://localhost:8060/dingtalk/webhook1/send
:這個就是之前獲取的釘釘 webhook。
$ sudo docker run -d --restart always -p 8060:8060 timonwong/prometheus-webhook-dingtalk:v0.3.0 --ding.profile="="
4.4 配置 AlertManager 告警規則
首先修改 alertmanager.yml,在下面這個例子中指定了名爲 web.hook 的消息接收方,url 爲剛剛啓動的 prometheus-webhook-dingtalk 的地址。

$ cat /etc/prometheus/alertmanager.yml
global:
resolve_timeout: 5m

route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 1h
receiver: 'web.hook'
receivers:

  • name: 'web.hook'
    webhook_configs:

    然後修改 /etc/prometheus/prometheus.yml,添加告警規則文件。

$ cat /etc/prometheus/prometheus.yml
......
rule_files:

- /etc/prometheus/rules/ceph.yaml

接下來輪到剛剛提到的告警規則文件了,下面這個例子中定義了在 ceph 可用存儲空間小於總存儲空間 70% 的情況下,發出告警消息。

$ cat /etc/prometheus/rules/ceph.yaml
groups:

  • name: ceph-rule
    rules:

    • alert: CephCapacityUsage
      expr: ceph_cluster_available_bytes / ceph_cluster_capacity_bytes * 100 > 70

    for: 2m
    labels:

    product: ceph

    annotations:

    summary: "{{$labels.instance}}: Not enough capacity in Ceph detected"
    description: "{{$labels.instance}}: Available capacity is used up to 70% (current value is: {{ $value }}"

    好了,最後重啓 AlertManager 和 Prometheus 就大功告成了。

$ sudo systemctl restart alertmanager.service
$ sudo systemctl restart prometheus.service
最後我們來看看發出來的消息效果如何,確實比之前好看多了,總算是沒有白費一番功夫。

formatted prometheus message

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