一、概述
从prometheus的官网可以看到,官方已经支持很多exporter,其中就包含了Haproxy-exporter。exporter使用非常地简单,在linux机器上解压后直接后台运行就能跑起来。
二、指标获取原理
haproxy-exporter是对uri地址获取到的csv内容进行了就解析,理论上的haproxy界面是这个样子。
在其后缀上添加如图字段后会变成这个样子
因此,如果使用haproxy-exporter收集指标,需要配置haproxy本身来暴露元数据。
三、haproxy配置
haproxy的安装不在赘述,通过yum安装的haproxy默认是在/etc/haproxy
,配置文件为haproxy.cfg
1、配置文件内容概览:
# listen 后边名字自定义
listen admin_stat
#haproxy的web管理端口 8888,自行设置
bind 0.0.0.0:8888
mode http
stats refresh 30s
#haproxy web管理url,自行设置
stats uri /haproxy_stats
stats realm Haproxy\ Statistics
#haproxy web管理用户名密码,自行设置
stats auth admin:admin
stats hide-version
根据实际需求添加负载均衡,生效配置文件就可以了。
# haproxy -f /etc/haproxy/haproxy.cfg
通过执行命令查看haproxy工作状态
# netstat -tlunap |grep haproxy
做完上边的步骤就可以看到上边的挂的两幅图片了。那么也就可以配置haproxy-exporter了
四、haproxy-exporter
haproxy-exporter下载地址在官网,下载后解压到合适位置即可。
# tar -zxvf haproxy_exporter-0.12.0.linux-amd64.tar.gz -C /opt/
# mv haproxy_exporter-0.12.0.linux-amd64.tar.gz haproxy_exporter
1、haproxy-exporter配置概览
[root@node01 haproxy_exporter]# ./haproxy_exporter --help
usage: haproxy_exporter [<flags>]
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
--web.listen-address=":9101"
Address to listen on for web interface and telemetry.
--web.telemetry-path="/metrics"
Path under which to expose metrics.
--haproxy.scrape-uri="http://localhost/;csv"
URI on which to scrape HAProxy.
--haproxy.ssl-verify Flag that enables SSL certificate verification for the scrape URI
--haproxy.server-metric-fields="2,3,4,5,6,7,8,9,13,14,15,16,17,18,21,24,30,33,35,38,39,40,41,42,43,44,49,50"
Comma-separated list of exported server metrics. See http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#9.1
--haproxy.server-exclude-states=""
Comma-separated list of exported server states to exclude. See https://cbonte.github.io/haproxy-dconv/1.8/management.html#9.1,
field 17 statuus
--haproxy.timeout=5s Timeout for trying to get stats from HAProxy.
--haproxy.pid-file="" Path to HAProxy pid file.
If provided, the standard process metrics get exported for the HAProxy
process, prefixed with 'haproxy_process_...'. The haproxy_process exporter
needs to have read access to files owned by the HAProxy process. Depends on
the availability of /proc.
https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics.
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
--version Show application version.
[root@node01 haproxy_exporter]#
这是官方的帮助手册,根据需求进行配置。举个例子如下
# ./haproxy_exporter --web.listen-address=":5674" --haproxy.scrape-uri="http://admin:admin@ipaddress:8888/haproxy_stats;csv"
使用用户admin密码admin登录要收取指标的haproxy获取暴露的csv格式指标"http://admin:admin@172.16.20.197:8888/haproxy_stats;csv"
暴露在5674这个自定义的端口,一会儿用prometheus监控起来
五、prometheus配置
在prometheus.yml配置文件中加入如下配置
- job_name: 'haproxy'
static_configs:
- targets: ['0.0.0.0:5674','0.0.0.0:5674']
这里地址和名字根据需要改。
重新启用一下prometheus,如下图
六、grafana配置
添加prometheus数据源
方便一点的话grafana官网找个dashboard导入大体可以了,后续再微调。
效果图: