您现在的位置是:首页 > 文章详情

使用 Promwww9992019comI8I2222IIIIetheus 监控 Ceph

日期:2018-12-30点击:396

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

原文链接:https://yq.aliyun.com/articles/683637
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章