本文是在 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.YAML --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.YAML /etc/prometheus/
- $ sudo mkdir /var/lib/prometheus/
- $ sudo systemctl daemon-reload
- $ sudo systemctl enable prometheus.service
- $ sudo systemctl start prometheus.service
在下一步装好 ceph_exporter 后, 还需要在 Promethues 中添加相应配置, 不过现在执行到这一步就可以了.
2. 安装 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/<user-name>/go
- export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
- $ source /etc/profile.d/go.sh
已经配置好 Go 环境了, 接下来创建 GOPATH 指定的目录:
$ mkdir /home/<user-name>/go
2.2 安装 ceph_exporter
Go 环境安装好后, 我们接下来下载 ceph_exporter 代码, 然后编译出可执行程序.
- $ mkdir -p /home/<user-name>/go/src/GitHub.com/digitalocean
- $ cd /home/<user-name>/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/<user-name>/go/bin
- $ cp ceph_exporter /home/<user-name>/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=<user-name>
- Group=<group-name>
- ExecStart=/home/<user-name>/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.YAML
- ...
- scrape_configs:
- - job_name: 'ceph_exporter'
- static_configs:
- - targets: ['localhost:9128']
- labels:
- alias: ceph_exporter
- ...
改好后, 重启:
$ sudo systemctl restart prometheus.service
3. 安装 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
访问 http://localhost:3000 来登录 Grafana, 默认用户为 admin, 密码也是 admin.
登录后首先需要配置 data source, 访问地址 http://localhost:3000/datasources, 会出现如上图所示的界面, 按照图中显示的信息配置即可.
最后需要导入 Ceph 相关的界面, 如图所示, 导入的是编号为 917 的 dashboard(从 grafana.com 上, 导入编号为 917 的 dashboard).
完成后, 终于可以看到 Ceph 的监控信息了.
4. 告警系统
现在已经有了图形化界面的状态监控, 但出现紧急情况我们肯定不希望要登录到界面上才能察觉到, 在 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.YAML /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.YAML
- 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.YAML
- ...
- alerting:
- alertmanagers:
- - static_configs:
- - targets: ["localhost:9093"]
- $ sudo systemctl restart prometheus.service
4.2 获取钉钉的 webhook
要往钉钉发消息, 当然要先知道 webhook 是多少, 首先是在钉钉群里添加一个机器人, 然后查看机器人的设置, 就可以看到 webhook:
4.3 配置消息转发的 API
配置 Prometheus 直接向钉钉 Webhook 发消息应该是发不过去的, Prometheus 的消息格式和钉钉 webhook 并不兼容, 而且就算是拿到消息中的字符串再发过去, 没经过格式化的消息也太难看了. 截个未经处理的钉钉消息的图给大家感受一下:
所以我们需要配置一个转发并格式化 Prometheus 消息的 API 服务器, 在网上搜了一下还真的找到一个已经做好的格式化 Prometheus 消息的开源项目, 完全满足需求:, 感谢 Timon Wong http://theo.im/ 的贡献. 接下来介绍一下如何以 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
启动镜像.
这里解释一下两个变量:
- <Web-hook-name>
- :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 <dingtalk-webhook>: 这个就是之前获取的钉钉 webhook. $ sudo docker run -d --restart always -p 8060:8060 timonwong/prometheus-webhook-dingtalk:v0.3.0 --ding.profile="<web-hook-name>=<dingtalk-webhook>"
4.4 配置 AlertManager 告警规则
首先修改 alertmanager.YAML, 在下面这个例子中指定了名为 Web.hook 的消息接收方, url 为刚刚启动的 prometheus-webhook-dingtalk 的地址.
$ cat /etc/prometheus/alertmanager.YAML 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: - url: 'http://localhost:8060/dingtalk/web.hook/send' inhibit_rules: - source_match: severity: 'critical' target_match: severity: 'warning' equal: ['alertname', 'dev', 'instance']
然后修改 /etc/prometheus/prometheus.YAML, 添加告警规则文件.
$ cat /etc/prometheus/prometheus.YAML ...... 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
最后我们来看看发出来的消息效果如何, 确实比之前好看多了, 总算是没有白费一番功夫.
好了, Prometheus 监控 Ceph 到这里就结束了, 多谢各位看官, 下期见.
5. 参考文档
Installing on Debian / Ubuntu http://docs.grafana.org/installation/debian/
[Ubuntu 系统安装 go 语言的简单方法
]( https://www.jianshu.com/p/82f5a3928897)
Ceph 集群监控 Prometheus+Grafana https://www.jianshu.com/p/5c66d2d0bd3f/
使用 prometheus + grafana 监控 ceph 集群
Prometheus 报警 AlertManager 实战
https://prometheus.io/docs/alerting/alertmanager/
将钉钉接入 Prometheus AlertManager WebHook http://theo.im/blog/categories/prometheus/
来源: https://yq.aliyun.com/articles/683444