Fluentd部署:通过Prometheus监控Fluentd
安装fluent-plugin-prometheus $ sudo td-agent-gem install fluent-plugin-prometheus --version='~>1.6.1'Fluentd示例配置 为了向Prometheus暴露Fluentd的统计指标,我们需要对Fluentd做一些配置,主要包含3步: 使用Prometheus过滤插件对输入消息进行计数 首先,按照如下配置添加<filter>配置项,该过滤器会按照tag对输入消息计数。 # source<source>@type forwardbind 0.0.0.0port 24224</source># count number of incoming records per tag<filter company.*>@type prometheus<metric>name fluentd_input_status_num_records_totaltype counterdesc The total number of incoming records<labels>tag ${tag}hostname ${hostname}</labels></metric></filter>使用Prometheus输出插件对输出消息进行计数 接着,使用copy插件处理输出数据,其中使用Prometheus对输出消息按照tag进行计数。 # count number of outgoing records per tag<match company.*>@type copy<store>@type forward<server>name myserver1hostname 192.168.1.3port 24224weight 60</server></store><store>@type prometheus<metric>name fluentd_output_status_num_records_totaltype counterdesc The total number of outgoing records<labels>tag ${tag}hostname ${hostname}</labels></metric></store></match>使用Prometheus输入插件通过HTTP暴露统计指标 最后,增加如下输入配置,使得Prometheus可以通过HTTP访问Fluentd内部统计信息。 # expose metrics in prometheus format<source>@type prometheusbind 0.0.0.0port 24231metrics_path /metrics</source><source>@type prometheus_output_monitorinterval 10<labels>hostname ${hostname}</labels></source>配置检查
完成以上3步配置之后,重启fluentd。
# For stand-alone Fluentd installations$ fluentd -c fluentd.conf# For td-agent users$ sudo systemctl restart td-agent向fluentd发送测试数据
$ echo '{"message":"hello"}' | bundle exec fluent-cat company.test1$ echo '{"message":"hello"}' | bundle exec fluent-cat company.test1$ echo '{"message":"hello"}' | bundle exec fluent-cat company.test1$ echo '{"message":"hello"}' | bundle exec fluent-cat company.test2访问http://localhost:24231/metrics,可以看到Prometheus格式的Fluentd相关统计指标。
curl http://localhost:24231/metrics# TYPE fluentd_input_status_num_records_total counter# HELP fluentd_input_status_num_records_total The total number of incoming recordsfluentd_input_status_num_records_total{tag="company.test",host="KZK.local"} 3.0fluentd_input_status_num_records_total{tag="company.test2",host="KZK.local"} 1.0# TYPE fluentd_output_status_num_records_total counter# HELP fluentd_output_status_num_records_total The total number of outgoing recordsfluentd_output_status_num_records_total{tag="company.test",host="KZK.local"} 3.0fluentd_output_status_num_records_total{tag="company.test2",host="KZK.local"} 1.0# TYPE fluentd_output_status_buffer_queue_length gauge# HELP fluentd_output_status_buffer_queue_length Current buffer queue length.fluentd_output_status_buffer_queue_length{hostname="KZK.local",plugin_id="object:3fcbccc6d388",type="forward"} 1.0....Prometheus示例配置 现在需要对Prometheus进行配置,准备以下配置文件prometheus.yml global:scrape_interval: 10s # Set the scrape interval to every 10 seconds. Default is every 1 minute.# A scrape configuration containing exactly one endpoint to scrape:# Here it's Prometheus itself.scrape_configs:- job_name: 'fluentd'static_configs:- targets: ['localhost:24231']启动prometheus进程 $ ./prometheus --config.file="prometheus.yml"打开浏览器,访问
http://localhost:9090/。在Prometheus中查看Fluentd监控数据 Fluentd节点列表 打开http://localhost:9090/targets,就会看到Fluentd节点及其运行状态 
Fluentd运作指标列表 打开http://localhost:9090/graph,就会看到Fluentd运行指标 
可以看到如下指标:
fluentd_input_status_num_records_totalfluentd_output_status_buffer_queue_lengthfluentd_output_status_buffer_total_bytesfluentd_output_status_emit_countfluentd_output_status_num_errorsfluentd_output_status_num_records_totalfluentd_output_status_retry_countfluentd_output_status_retry_wait选择fluentd_input_status_num_records_total,就会看到输入的消息计数。 
这里,我们只对Prometheus的用法做简单介绍,感兴趣的话可以去官网做进一步了解。
赞 (0)

