Telegraf

1、产品概述

1.1 Telegraf

Telegraf 是一个开源代理工具,用于收集、处理和传输度量数据,由 InfluxData 开发。

Telegraf 有以下这些特点:

  • 插件体系结构:Telegraf 的强大之处在于其广泛的插件生态系统。它支持多种输入、输出和处理器插件,可以与各种数据源和目标无缝集成。
  • 数据收集:Telegraf 擅长从不同来源收集度量数据,例如系统指标、日志、数据库等。其多功能性使其适用于监视应用程序、基础架构和物联网设备。
  • 输出目标:一旦收集到数据,可以将其发送到各种输出目标,包括流行的数据库如 InfluxDB。这种灵活性使 Telegraf 适应不同的监视和分析设置。
  • 配置简易:Telegraf 的配置使用 TOML 文件进行。这种简单性使用户能够轻松定义输入、输出和处理器,使定制变得简单明了。
  • 社区与支持:作为开源项目,Telegraf 受益于活跃的社区。用户可以通过论坛和文档贡献插件、报告问题并寻求帮助。

1.2 Telegraf-IoTDB 插件

Telegraf-IoTDB 插件可以将保存在 Telegraf 中的监控信息输出存储到 IoTDB,输出插件使用了 IoTDB session 进行连接和数据写入。

Telegraf插件 - 图1

2、安装要求

Telegraf支持多种操作系统,包括Linux、Windows、macOS,Telegraf 的安装推荐使用root管理员权限才能成功完成,具体安装要求请查看 安装要求

3、安装步骤

具体安装步骤请查看 安装步骤

  • 注:此插件为Telegraf内置插件,无需进行二次安装

4、使用说明

4.1 设置输入源

在“telegraf.conf”配置文件中找到“INPUT PLUGINS”来配置输入源,具体配置内容如下表所示

配置项 说明 备注
alias 命名插件的实例
interval 收集此指标的频率。普通插件使用单个全局间隔,但如果某个特定输入的运行频率应更低或更高,您可以在此处进行配置。interval可以增加间隔以减少数据输入速率限制。
precision 覆盖precision代理的设置。收集的指标四舍五入到指定的精度interval。当在服务输入上设置此值(例如statsd:)时,输出数据库可能会合并在同一时间戳发生的多个事件。
collection_jitter 覆盖collection_jitter代理的设置。Collection jitter 用于通过随机的interval
name_override 输出到 IoTDB 时使用的自定义时间序列路径名 输出的路径名称需满足“语法要求”要求
name_prefix 指定附加到测量名称的前缀
name_suffix 指定附加到测量名称的后缀

Telegraf插件 - 图2

4.2 设置输出源

在“telegraf.conf”配置文件中找到“outputs.iotdb”来配置输出源,具体配置内容如下表所示,具体输入源示例可查看 输出源示例

配置项 说明 修改前 修改后 备注
host IoTDB 的 host # host = “127.0.0.1” host = “部署 IoTDB 的 host” 默认为 127.0.0.1
port IoTDB 的端口号 # port = “6667” port = “部署 IoTDB 的端口号” 默认为 6667
user IoTDB 的用户名 # user = “root” user = “IoTDB 的用户名” 默认为 root
password IoTDB 的密码 # password = “root” password= “IoTDB 的密码” 默认为 root
timestamp_precision 时间戳精度 timestamp_precision = “millisecond” timestamp_precision = “与 IoTDB 相同的时间戳精度” 您可以通过查看”iotdb-system.properties”中”timestamp_precision”字段即可
sanitize_tag 数据库版本 无 sanitize_tag = “0.13/1.0/1.1/1.2/1.3”

Telegraf插件 - 图3

4.3 启动Telegraf服务

1. telegraf -config /path/to/telegraf.conf

5、使用示例

以下是一个使用 Telegraf 收集 CPU 数据并使用 Telegraf-IoTDB 输出到 IoTDB 的示例。使用 telegraf 命令生成配置文件

1. telegraf --sample-config --input-filter cpu --output-filter iotdb > cpu_iotdb.conf
  1. 在 cpu_iotdb.conf 中修改 input cpu 插件的配置。其中,“name_override” 字段为输出到 IoTDB 时使用的自定义时间序列路径名
1. # Read metrics about cpu usage
2. [[inputs.cpu]]
3. ## Whether to report per-cpu stats or not
4. percpu = true
5. ## Whether to report total system cpu stats or not
6. totalcpu = true
7. ## If true, collect raw CPU time metrics
8. collect_cpu_time = false
9. ## If true, compute and report the sum of all non-idle CPU states
10. report_active = false
11. ## If true and the info is available then add core_id and physical_id tags
12. core_tags = false
13. name_override = "root.demo.telgraf.cpu"
  1. 在 cpu_iotdb.conf 中修改 output iotdb 插件的配置
配置项 说明 修改前 修改后 备注
host IoTDB 的 host # host = “127.0.0.1” host = “部署 IoTDB 的 host” 默认为 127.0.0.1
port IoTDB 的端口号 # port = “6667” port = “部署 IoTDB 的端口号” 默认为 6667
user IoTDB 的用户名 # user = “root” user = “IoTDB 的用户名” 默认为 root
password IoTDB 的密码 # password = “root” password= “IoTDB 的密码” 默认为 root
timestamp_precision 时间戳精度 timestamp_precision = “millisecond” timestamp_precision = “与 IoTDB 相同的时间戳精度” 您可以通过查看”iotdb-system.properties”中”timestamp_precision”字段即可
sanitize_tag 数据库版本 无 sanitize_tag = “0.13/1.0/1.1/1.2/1.3”
1. # Save metrics to an IoTDB Database
2. [[outputs.iotdb]]
3. ## Configuration of IoTDB server connection
4. host = "127.0.0.1"
5. # port = "6667"

7. ## Configuration of authentication
8. # user = "root"
9. # password = "root"

11. ## Timeout to open a new session.
12. ## A value of zero means no timeout.
13. # timeout = "5s"

15. ## Configuration of type conversion for 64-bit unsigned int
16. ## IoTDB currently DOES NOT support unsigned integers (version 13.x).
17. ## 32-bit unsigned integers are safely converted into 64-bit signed integers by the plugin,
18. ## however, this is not true for 64-bit values in general as overflows may occur.
19. ## The following setting allows to specify the handling of 64-bit unsigned integers.
20. ## Available values are:
21. ##   - "int64"       --  convert to 64-bit signed integers and accept overflows
22. ##   - "int64_clip"  --  convert to 64-bit signed integers and clip the values on overflow to 9,223,372,036,854,775,807
23. ##   - "text"        --  convert to the string representation of the value
24. # uint64_conversion = "int64_clip"

26. ## Configuration of TimeStamp
27. ## TimeStamp is always saved in 64bits int. timestamp_precision specifies the unit of timestamp.
28. ## Available value:
29. ## "second", "millisecond", "microsecond", "nanosecond"(default)
30. timestamp_precision = "millisecond"

32. ## Handling of tags
33. ## Tags are not fully supported by IoTDB.
34. ## A guide with suggestions on how to handle tags can be found here:
35. ##     https://iotdb.apache.org/UserGuide/Master/API/InfluxDB-Protocol.html
36. ##
37. ## Available values are:
38. ##   - "fields"     --  convert tags to fields in the measurement
39. ##   - "device_id"  --  attach tags to the device ID
40. ##
41. ## For Example, a metric named "root.sg.device" with the tags `tag1: "private"`  and  `tag2: "working"` and
42. ##  fields `s1: 100`  and `s2: "hello"` will result in the following representations in IoTDB
43. ##   - "fields"     --  root.sg.device, s1=100, s2="hello", tag1="private", tag2="working"
44. ##   - "device_id"  --  root.sg.device.private.working, s1=100, s2="hello"
45. # convert_tags_to = "device_id"
46. ## Handling of unsupported characters
47. ## Some characters in different versions of IoTDB are not supported in path name
48. ## A guide with suggetions on valid paths can be found here:
49. ## for iotdb 0.13.x           -> https://iotdb.apache.org/UserGuide/V0.13.x/Reference/Syntax-Conventions.html#identifiers
50. ## for iotdb 1.x.x and above  -> https://iotdb.apache.org/UserGuide/V1.3.x/User-Manual/Syntax-Rule.html#identifier
51. ##
52. ## Available values are:
53. ##   - "1.0", "1.1", "1.2", "1.3"  -- enclose in `` the world having forbidden character
54. ##                                    such as @ $ # : [ ] { } ( ) space
55. ##   - "0.13"                      -- enclose in `` the world having forbidden character
56. ##                                    such as space
57. ##
58. ## Keep this section commented if you don't want to sanitize the path
59. sanitize_tag = "1.3"
  1. 使用 cpu_iotdb.conf 配置文件运行 Telegraf:运行一段时间后,可以在 IoTDB 中查询到 Telegraf 收集上报的数据