title: Telegraf简介 toc: true cover: 'https://img.paulzzh.com/touhou/random?11' date: 2023-06-27 12:09:27 categories: 技术杂谈 tags: [技术杂谈, Telegraf]
Telegraf 是一个用 Go 编写的代理程序,可收集系统和服务的统计数据,并写入到 InfluxDB 数据库;
本文简单介绍了 Telegraf 的使用;
<br/>
<!--more-->Telegraf 是一个用 Go 编写的代理程序,可收集系统和服务的统计数据,并写入到 InfluxDB 数据库; 官网:
Telegraf是TICK Stack的一部分,是一个插件驱动的服务器代理,用于收集和报告指标。
Telegraf 集成了直接从其运行的容器和系统中提取各种指标,事件和日志,从第三方API提取指标,甚至通过StatsD和Kafka消费者服务监听指标。
它还具有输出插件,可将指标发送到各种其他数据存储,服务和消息队列,包括InfluxDB,Graphite,OpenTSDB,Datadog,Librato,Kafka,MQTT,NSQ等等。
Telegraf作为数据采集模块,需要安装至被监控的目标主机上。Telegraf设计目标是较小的内存使用,通过插件来构建各种服务和第三方组件的metrics收集;
Telegraf由4个独立的插件驱动:
为什么要用telegraf和influxdb?
<br/>
使用 apt 安装即可:
apt install telegraf
安装完成后,telegraf 创建一个后台的 service,因此可以使用 systemctl 管理:
启动命令:
systemctl start telegraf
重启命令:
systemctl restart telegraf
<br/>
Telegraf 提供了大量的配置,配置文件在: vim /etc/telegraf/telegraf.conf
;
配置说明:
yum 安装后在 /etc/telegraf 下会生成一个 telegraf.conf 文件,在配置文件中可以使用 “$ENV_ITEM”
的形式使用环境变量;
下面是一些主要的配置项:
这里记录的内容将作为 Tags 保存到 InfluxDB 的每个 Item 中;
<br/>
这一部分内容是数据搜集服务的行为定义;
更多参数见:
<br/>
查看帮助:
telegraf --help
生成配置文件:
# 比如在当前目录下生成mysql相关的配置文件
telegraf config > telegraf-mysql.conf
建议生成的配置放置在 /etc/telegraf/telegraf.d目录下;
telegraf 支持读取多个配置文件,可将多个配置文件放置在 /etc/telegraf/telegraf.d
目录下;
生成指定输入和输出插件的配置文件:
telegraf --input-filter <pluginname>[:<pluginname>] --output-filter <outputname>[:<outputname>] config > telegraf.conf
例如,生成带 cpu、memroy、disk、diskio、net 和 influxdb 插件的配置文件 telegraf.conf,指定输出到 influxdb 和 opentsdb:
telegraf --input-filter cpu:mem:disk:diskio:net --output-filter influxdb:opentsdb config > telegraf.conf
也可使用默认的配置文件:
telegraf --input-filter cpu:mem:http_listener --output-filter influxdb config
测试配置是否成功:
测试 /etc/telegraf/telegraf.conf
配置文件中输入 cpu 配置是否正确:
telegraf -config /etc/telegraf/telegraf.conf -input-filter cpu -test
测试 /etc/telegraf/telegraf.conf
输出 influxdb 配置是否正确:
telegraf -config /etc/telegraf/telegraf.conf -output-filter influxdb -test
测试 /etc/telegraf/telegraf.d/mysql.conf
输入 cpu 和 输出 influxdb 配置是否正确:
telegraf -config /etc/telegraf/telegraf.d/mysql.conf -input-filter cpu -output-filter influxdb -test
配置文件保存修改后,记得要重启 telegraf:
service telegraf restart
<br/>
telegraf 日志目录:/var/log/telegraf/telegraf.log
;
<br/>
Telegraf 可以结合 InfluxDB、Kafka 等存储来使用;
在使用时只需要编写相应的配置文件,使用不同的插件进行实现即可;
例如,输入插件:
输出插件:
<br/>
编写配置文件,创建 /etc/telegraf/telegraf.d/cpu.conf
:
[global_tags]
ip = "127.0.0.1"
[agent]
interval = "5s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 10000
collection_jitter = "0s"
flush_interval = "5s"
flush_jitter = "0s"
precision = "1ms"
logtarget = "file"
logfile = "/tmp/telegraf-cpu.log"
logfile_rotation_max_size = "10MB"
logfile_rotation_max_archives = 10
hostname = ""
omit_hostname = false
[[inputs.cpu]]
## Whether to report per-cpu stats or not
percpu = true
## Whether to report total system cpu stats or not
totalcpu = true
## If true, collect raw CPU time metrics.
collect_cpu_time = false
## If true, compute and report the sum of all non-idle CPU states.
report_active = false
[[outputs.file]]
## Files to write to, "stdout" is a specially handled file.
files = ["stdout", "/tmp/metrics.out"]
## Use batch serialization format instead of line based delimiting. The
## batch format allows for the production of non line based output formats and
## may more efficiently encode and write metrics.
# use_batch_format = false
## The file will be rotated after the time interval specified. When set
## to 0 no time based rotation is performed.
# rotation_interval = "0h"
## The logfile will be rotated when it becomes larger than the specified
## size. When set to 0 no size based rotation is performed.
# rotation_max_size = "0MB"
## Maximum number of rotated archives to keep, any older logs are deleted.
## If set to -1, no archives are removed.
# rotation_max_archives = 5
## Data format to output.
## Each data format has its own unique set of configuration options, read
## more about them here:
## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md
data_format = "json"
具体的 input、output 插件参数可以参考:
校验配置文件
可以使用 telegraf -config xxx.config -test
来校验:
telegraf -config cpu.conf -test
输出如下:
2023-05-22T06:30:06Z I! Starting Telegraf 1.21.4+ds1-0ubuntu2
> cpu,cpu=cpu0,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu1,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu2,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu3,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu4,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu5,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu6,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu7,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
> cpu,cpu=cpu8,host=ubuntu,ip=127.0.0.1 usage_guest=0,usage_guest_nice=0,usage_idle=100,usage_iowait=0,usage_irq=0,usage_nice=0,usage_softirq=0,usage_steal=0,usage_system=0,usage_user=0 1684737007400000000
检验没有问题后,重启 telegraf 服务:
systemctl restart telegraf
查看输出:
cat /tmp/metrics.out
输出如下:
{"fields":{"active":234635264,"available":68976783360,"available_percent":98.43237993042763,"buffered":20967424,"cached":475803648,"commit_limit":39332610048,"committed_as":390959104,"dirty":0,"free":69279043584,"high_free":0,"high_total":0,"huge_page_size":2097152,"huge_pages_free":0,"huge_pages_total":0,"inactive":306810880,"low_free":0,"low_total":0,"mapped":129556480,"page_tables":1888256,"shared":737280,"slab":101105664,"sreclaimable":48656384,"sunreclaim":52449280,"swap_cached":0,"swap_free":4294963200,"swap_total":4294963200,"total":70075297792,"used":299483136,"used_percent":0.42737333330917343,"vmalloc_chunk":0,"vmalloc_total":35184372087808,"vmalloc_used":20594688,"write_back":0,"write_back_tmp":0},"name":"mem","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
{"fields":{"boot_time":1684724406,"context_switches":1591707,"entropy_avail":256,"interrupts":1575945,"processes_forked":1567},"name":"kernel","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
{"fields":{"free":4294963200,"total":4294963200,"used":0,"used_percent":0},"name":"swap","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
{"fields":{"in":0,"out":0},"name":"swap","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
{"fields":{"load1":0,"load15":0,"load5":0,"n_cpus":16,"n_users":1},"name":"system","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
{"fields":{"uptime":12294},"name":"system","tags":{"host":"ubuntu","ip":"127.0.0.1"},"timestamp":1684736700}
......
<br/>
参考文章
<br/>