试着通过Telegraf将HAProxy的统计信息输入到InfluxDB中

首先

最近考虑使用InfluxDB来监控日志和资源状态,并且在@takaidohigasi的介绍下选择了InfluxDB提供的度量收集工具Telegraf,做个备忘。

请参考以下链接,获取更详细的Telegraf介绍。
http://qiita.com/takaidohigasi/items/fdc81db8336aa7601fc9

Telegraf是什么?

根据所述,这是一个用于将数据注册到InfluxDB的GO制收集工具。它支持以下指标。

    • disque

 

    • elasticsearch

 

    • exec (generic JSON-emitting executable plugin)

 

    • haproxy

 

    • httpjson (generic JSON-emitting http service plugin)

 

    • kafka_consumer

 

    • leofs

 

    • lustre2

 

    • memcached

 

    • mongodb

 

    • mysql

 

    • nginx

 

    • postgresql

 

    • prometheus

 

    • rabbitmq

 

    • redis

 

    • rethinkdb

 

    system (mem, CPU, load, etc.)

尽管InfluxDB是一个工具,但据说它支持以下输出目标。

    • influxdb

 

    • kafka

 

    datadog

环境

    • OS: CentOS7.1

 

    • InfluxDB: 0.9.3

 

    HAProxy: 1.5.4

安装

由于提供了RPM包,可以使用以下命令进行安装。

$ sudo yum -y install http://get.influxdb.org/telegraf/telegraf-0.1.7-1.x86_64.rpm
$

设定

可以使用下面的命令创建一个示例配置文件。

$ telegraf -sample-config > telegraf.toml

HAProxy插件的配置

可以通过以下命令确认telegraf的各种插件的设置方法,这是telegraf的一个有用功能。

$ /opt/telegraf/telegraf -usage haproxy
# Read metrics of haproxy, via socket or csv stats page
[haproxy]
# An array of address to gather stats about. Specify an ip on hostname
    # with optional port. ie localhost, 10.10.3.33:1936, etc.
    #
    # If no servers are specified, then default to 127.0.0.1:1936
    servers = ["http://myhaproxy.com:1936", "http://anotherhaproxy.com:1936"]
    # Or you can also use local socket(not work yet)
    # servers = ["socket:/run/haproxy/admin.sock"]$

参考 ^,创建一个设置文件如下。

请注意:在此指定的URI是统计信息的CSV文件,而不是统计信息页面。

# Telegraf configuration
# Telegraf is entirely plugin driven. All metrics are gathered from the
# declared plugins.
# Even if a plugin has no configuration, it must be declared in here
# to be active. Declaring a plugin means just specifying the name
# as a section with no variables. To deactivate a plugin, comment
# out the name and any variables.
# Use 'telegraf -config telegraf.toml -test' to see what metrics a config
# file would generate.
# One rule that plugins conform to is wherever a connection string
# can be passed, the values '' and 'localhost' are treated specially.
# They indicate to the plugin to use their own builtin configuration to
# connect to the local system.
# NOTE: The configuration has a few required parameters. They are marked
# with 'required'. Be sure to edit those to make this configuration work.
# Tags can also be specified via a normal map, but only one form at a time:
[tags]
    # dc = "us-east-1"
# Configuration for telegraf itself
[agent]
    interval = "10s"
    # debug = false
    hostname = "telegraf01"
# Read metrics of haproxy, via socket or csv stats page
###############################################################################
# OUTPUTS #
###############################################################################
[outputs]
# Configuration for influxdb server to send metrics to
[outputs.influxdb]
    # The full HTTP endpoint URL for your InfluxDB instance
    url = "http://localhost:8086" # required.
    # The target database for metrics. This database must already exist
    database = "telegraf" # required.
    # Connection timeout (for the connection with InfluxDB), formatted as a string.
    # Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
    # If not provided, will default to 0 (no timeout)
    # timeout = "5s"
    # username = "telegraf"
    # password = "metricsmetricsmetricsmetrics"
    # Set the user agent for the POSTs (can be useful for log differentiation)
    # user_agent = "telegraf"
###############################################################################
# PLUGINS #
###############################################################################
[haproxy]
# An array of address to gather stats about. Specify an ip on hostname
    # with optional port. ie localhost, 10.10.3.33:1936, etc.
    #
    # If no servers are specified, then default to 127.0.0.1:1936
    servers = ["http://localhost:8080/haproxy/stats;csv"]
    # Or you can also use local socket(not work yet)
    # servers = ["socket:/run/haproxy/admin.sock"]

启动/配置HAProxy

在 HAProxy 的配置文件中添加以下内容并启动。

# admin #
listen hastats
 bind *:8080
 mode http
 stats enable
 stats show-legends
 stats uri /haproxy/stats

当您启动后,在浏览器中访问*: 8080 /haproxy/stats,将会显示以下屏幕。

haproxy_stats.png

当点击右上角的CSV export链接时,会显示CSV文件,将此链接指定到telegraf的设置文件中。

haproxy_stats_csv.png

试试启动

我会用以下的命令在前台启动。

$ /opt/telegraf/telegraf -config=telegraf.toml
2015/09/01 12:03:47 Starting Telegraf (version 0.1.7)
2015/09/01 12:03:47 Loaded outputs: influxdb
2015/09/01 12:03:47 Loaded plugins: haproxy
2015/09/01 12:03:47 Tags enabled: host=telegraf01

经过一段时间后,我在InfluxDB中检查发现,各种measurements已经存储在名为telegraf的数据库中。

influxdb_telegraf.png

我們試著用Grafana來看一下。

如果在Dashboard上添加名为telegraf的数据库作为数据源,并按照http的响应码进行显示,将会显示如下内容。

grafana_telegraf.png

它以Responsecode的累计值进行显示,这很实用呢。

结束了。

广告
将在 10 秒后关闭
bannerAds