Prometheus监控Haproxy

11 篇文章 1 订阅
3 篇文章 0 订阅

一、概述

从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导入大体可以了,后续再微调。
在这里插入图片描述
在这里插入图片描述效果图:在这里插入图片描述

一个典型的Haproxy_exporter的监控metric例子 包含指标 haproxy_backend_bytes_in_total haproxy_backend_bytes_out_total haproxy_backend_client_aborts_total haproxy_backend_compressor_bytes_bypassed_total haproxy_backend_compressor_bytes_in_total haproxy_backend_compressor_bytes_out_total haproxy_backend_connection_errors_total haproxy_backend_current_queue haproxy_backend_current_server haproxy_backend_current_session_rate haproxy_backend_current_sessions haproxy_backend_http_connect_time_average_seconds haproxy_backend_http_queue_time_average_seconds haproxy_backend_http_response_time_average_seconds haproxy_backend_http_responses_compressed_total haproxy_backend_http_responses_total haproxy_backend_http_total_time_average_seconds haproxy_backend_response_errors_total haproxy_backend_retry_warnings_total haproxy_backend_server_aborts_total haproxy_backend_server_selected_total haproxy_backend_sessions_total haproxy_backend_up haproxy_backend_weight haproxy_exporter_build_info haproxy_exporter_csv_parse_failures haproxy_exporter_total_scrapes haproxy_frontend_bytes_in_total haproxy_frontend_bytes_out_total haproxy_frontend_compressor_bytes_bypassed_total haproxy_frontend_compressor_bytes_in_total haproxy_frontend_compressor_bytes_out_total haproxy_frontend_connections_total haproxy_frontend_current_session_rate haproxy_frontend_current_sessions haproxy_frontend_http_requests_total haproxy_frontend_http_responses_compressed_total haproxy_frontend_http_responses_total haproxy_frontend_limit_session_rate haproxy_frontend_limit_sessions haproxy_frontend_max_session_rate haproxy_frontend_max_sessions haproxy_frontend_request_errors_total haproxy_frontend_requests_denied_total haproxy_frontend_sessions_total haproxy_server_bytes_in_total haproxy_server_bytes_out_total haproxy_server_check_duration_seconds haproxy_server_check_failures_total haproxy_server_client_aborts_total haproxy_server_connection_errors_total haproxy_server_current_queue haproxy_server_current_session_rate haproxy_server_current_sessions haproxy_server_downtime_seconds_total haproxy_server_http_responses_total haproxy_server_max_queue haproxy_server_max_session_rate haproxy_server_max_sessions haproxy_server_redispatch_warnings_total haproxy_server_response_errors_total haproxy_server_retry_warnings_total haproxy_server_server_aborts_total haproxy_server_server_selected_total haproxy_server_sessions_total haproxy_server_up haproxy_server_weight haproxy_up process_cpu_seconds_total process_max_fds process_open_fds process_resident_memory_bytes process_start_time_seconds process_virtual_memory_bytes promhttp_metric_handler_requests_in_flight promhttp_metric_handler_requests_total
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Energet!c

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值