SQL 输入插件
此插件通过针对 SQL 服务器执行 SQL 查询来读取指标。支持不同的服务器类型,它们的设置可能不同(特别是连接参数)。请查看 支持的 SQL 驱动程序 列表以了解 driver 名称和数据源名称 (dsn) 选项的设置。
引入于: Telegraf v1.19.0 标签: datastore 操作系统支持: all
全局配置选项
插件支持其他全局和插件配置设置,用于修改指标、标签和字段,创建别名以及配置插件顺序等任务。更多详情请参阅 CONFIGURATION.md。
Secret-store 支持
此插件支持来自 secret-stores 的 dsn 选项的 secrets。有关如何使用它们的更多详细信息,请参阅 secret-store 文档。
配置
# Read metrics from SQL queries
[[inputs.sql]]
## Database Driver
## See https://github.com/influxdata/telegraf/blob/master/docs/SQL_DRIVERS_INPUT.md for
## a list of supported drivers.
driver = "mysql"
## Data source name for connecting
## The syntax and supported options depends on selected driver.
dsn = "username:password@tcp(mysqlserver:3307)/dbname?param=value"
## Timeout for any operation
## Note that the timeout for queries is per query not per gather.
# timeout = "5s"
## Connection time limits
## By default the maximum idle time and maximum lifetime of a connection is unlimited.
## Connections will not be closed automatically. If you specify a positive time, the connections will be closed after
## idling or existing for at least that amount of time, respectively.
# connection_max_idle_time = "0s"
# connection_max_life_time = "0s"
## Connection count limits
## By default the number of open connections is not limited and the number of maximum idle connections
## will be inferred from the number of queries specified. If you specify a positive number for any of the
## two options, connections will be closed when reaching the specified limit. The number of idle connections
## will be clipped to the maximum number of connections limit if any.
# connection_max_open = 0
# connection_max_idle = auto
## Specifies plugin behavior regarding disconnected servers
## Available choices :
## - error: telegraf will return an error on startup if one the servers is unreachable
## - ignore: telegraf will ignore unreachable servers on both startup and gather
# disconnected_servers_behavior = "error"
[[inputs.sql.query]]
## Query to perform on the server
query="SELECT user,state,latency,score FROM Scoreboard WHERE application > 0"
## Alternatively to specifying the query directly you can select a file here containing the SQL query.
## Only one of 'query' and 'query_script' can be specified!
# query_script = "/path/to/sql/script.sql"
## Name of the measurement
## In case both measurement and 'measurement_col' are given, the latter takes precedence.
# measurement = "sql"
## Column name containing the name of the measurement
## If given, this will take precedence over the 'measurement' setting. In case a query result
## does not contain the specified column, we fall-back to the 'measurement' setting.
# measurement_column = ""
## Column name containing the time of the measurement
## If omitted, the time of the query will be used.
# time_column = ""
## Format of the time contained in 'time_col'
## The time must be 'unix', 'unix_ms', 'unix_us', 'unix_ns', or a golang time format.
## See https://golang.ac.cn/pkg/time/#Time.Format for details.
# time_format = "unix"
## Column names containing tags
## An empty include list will reject all columns and an empty exclude list will not exclude any column.
## I.e. by default no columns will be returned as tag and the tags are empty.
# tag_columns_include = []
# tag_columns_exclude = []
## Column names containing fields (explicit types)
## Convert the given columns to the corresponding type. Explicit type conversions take precedence over
## the automatic (driver-based) conversion below.
## NOTE: Columns should not be specified for multiple types or the resulting type is undefined.
# field_columns_float = []
# field_columns_int = []
# field_columns_uint = []
# field_columns_bool = []
# field_columns_string = []
## Column names containing fields (automatic types)
## An empty include list is equivalent to '[*]' and all returned columns will be accepted. An empty
## exclude list will not exclude any column. I.e. by default all columns will be returned as fields.
## NOTE: We rely on the database driver to perform automatic datatype conversion.
# field_columns_include = []
# field_columns_exclude = []驱动程序
driver 和 dsn 选项指定了如何连接到数据库。由于 dsn 格式和值尤其与 driver 相关,请参阅支持的 SQL 驱动程序列表以获取可能的值和更多详细信息。
连接限制
使用这些选项,您可以限制此插件保持打开的连接数。有关确切工作原理的详细信息,请参阅 golang sql 文档。
查询部分
可以为此插件指定多个 query 部分。每个指定的查询将首先在服务器上进行准备,然后使用指定的列映射在每个间隔中执行。请注意,tag 和 field 列不是互斥的(一列可以同时添加到两者中)。当使用 include 和 exclude 列表时,exclude 列表优先于 include 列表。例如,如果您在两个列表中都指定了 foo,foo 将从不通过过滤器。如果 measurement_col 或 time_col 中指定的任何列未由查询返回,插件将回退到文档中的默认值。在选项的 includes 中指定但在返回的查询中丢失的字段或标签将被静默忽略。
类型
此插件依赖驱动程序进行类型转换。对于指标的不同属性,接受以下类型。
Measurement
只接受 string 类型的列。
时间
对于指标时间列,直接接受 time 类型的列。对于数字列,应相应地将 time_format 设置为 unix、unix_ms、unix_ns 或 unix_us。默认情况下,预计时间戳为 unix 格式。对于字符串列,请相应地指定 time_format。有关详细信息,请参阅 golang time 文档。
标签
对于具有文本值(string 和 bytes)、有符号和无符号整数(8、16、32 和 64 位)、浮点数(32 和 64 位)、boolean 和 time 值的标签列,都可接受。这些值将被转换为字符串。
字段
对于具有文本值(string 和 bytes)、有符号和无符号整数(8、16、32 和 64 位)、浮点数(32 和 64 位)、boolean 和 time 值的字段列,都可接受。此处 bytes 将转换为 string,有符号和无符号整数值将分别转换为 int64 或 uint64。浮点值将转换为 float64,time 将转换为 int64 类型的纳秒时间戳。
Metrics
此插件生成的指标的格式取决于文件的内容和数据格式。
示例输出
使用 MariaDB 示例数据库 和配置
[[inputs.sql]]
driver = "mysql"
dsn = "root:password@/nation"
[[inputs.sql.query]]
query="SELECT * FROM guests"
measurement = "nation"
tag_columns_include = ["name"]
field_columns_exclude = ["name"]Telegraf 将输出以下指标
nation,host=Hugin,name=John guest_id=1i 1611332164000000000
nation,host=Hugin,name=Jane guest_id=2i 1611332164000000000
nation,host=Hugin,name=Jean guest_id=3i 1611332164000000000
nation,host=Hugin,name=Storm guest_id=4i 1611332164000000000
nation,host=Hugin,name=Beast guest_id=5i 1611332164000000000此页面是否有帮助?
感谢您的反馈!
支持和反馈
感谢您成为我们社区的一员!我们欢迎并鼓励您对 Telegraf 和本文档提出反馈和 bug 报告。要获取支持,请使用以下资源
具有年度合同或支持合同的客户可以 联系 InfluxData 支持。