使用json通過telegraf生成metrics(摘自telegraf github文件)

大囚長發表於2018-04-10

JSON:
The JSON data format flattens JSON into metric fields. NOTE: Only numerical values are converted to fields, and they are converted into a float. strings are ignored unless specified as a tag_key (see below).

So for example, this JSON:

{
“a”: 5,
“b”: {
“c”: 6
},
“ignored”: “I’m a string”
}
Would get translated into fields of a measurement:

myjsonmetric a=5,b_c=6
The measurement name is usually the name of the plugin, but can be overridden using the name_override config option.

JSON Configuration:
The JSON data format supports specifying “tag keys”. If specified, keys will be searched for in the root-level of the JSON blob. If the key(s) exist, they will be applied as tags to the Telegraf metrics.

For example, if you had this configuration:

[[inputs.exec]]
## Commands array
commands = [“/tmp/test.sh”, “/usr/bin/mycollector –foo=bar”]

## measurement name suffix (for separating different commands)
name_suffix = “_mycollector”

## Data format to consume.
## 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_INPUT.md
data_format = “json”

## List of tag names to extract from top-level of JSON server response
tag_keys = [
“my_tag_1”,
“my_tag_2”
]
with this JSON output from a command:

{
“a”: 5,
“b”: {
“c”: 6
},
“my_tag_1”: “foo”
}
Your Telegraf metrics would get tagged with “my_tag_1”

exec_mycollector,my_tag_1=foo a=5,b_c=6
If the JSON data is an array, then each element of the array is parsed with the configured settings. Each resulting metric will be output with the same timestamp.

For example, if the following configuration:

[[inputs.exec]]
## Commands array
commands = [“/usr/bin/mycollector –foo=bar”]

## measurement name suffix (for separating different commands)
name_suffix = “_mycollector”

## Data format to consume.
## 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_INPUT.md
data_format = “json”

## List of tag names to extract from top-level of JSON server response
tag_keys = [
“my_tag_1”,
“my_tag_2”
]
with this JSON output from a command:

[
{
“a”: 5,
“b”: {
“c”: 6
},
“my_tag_1”: “foo”,
“my_tag_2”: “baz”
},
{
“a”: 7,
“b”: {
“c”: 8
},
“my_tag_1”: “bar”,
“my_tag_2”: “baz”
}
]
Your Telegraf metrics would get tagged with “my_tag_1” and “my_tag_2”

exec_mycollector,my_tag_1=foo,my_tag_2=baz a=5,b_c=6
exec_mycollector,my_tag_1=bar,my_tag_2=baz a=7,b_c=8

相關文章