Telegraf和InfluxDB:如何将PROCSTAT的pid从字段转换为标签?

问题描述

摘要:我正在使用Telegraf将procstat放入InfluxDB。 我想将pid从整数字段转换为TAG,以便可以在Influx中对其进行分组。

详细信息: 经过大量搜索后,我在某个网站上发现了以下内容,但似乎与此相反(将标记转换为字段)。我不确定如何从中推断出相反的转换语法:

set.seed(123)
df <- data.frame(a = 1:10,lag = sample(3,10,replace = TRUE))
df
#    a lag
#1   1   3
#2   2   3
#3   3   3
#4   4   2
#5   5   3
#6   6   2
#7   7   2
#8   8   2
#9   9   3
#10 10   1

我正在使用Influx 1.7.9

解决方法

将pid转换为标签的正确处理器配置如下。

[processors]
  [[processors.converter]]
     namepass = [ "procstat"]

  [processors.converter.fields]
     tag = [ "pid"]

请参阅转换器处理器插件的文档 https://github.com/influxdata/telegraf/tree/master/plugins/processors/converter

在最新版本的telegraf中,可以通过在输入插件配置中指定pid来将其存储为标签。这里不需要转换器处理器。

配置pid_tag = true中的内容。但是,请注意,当进程短暂存在时,使用pid作为标记会对性能产生影响。

P.S:您应该尝试将telegraf版本升级到1.14.5。此版本中有针对procstat插件的性能改进修复程序。

插件配置参考https://github.com/influxdata/telegraf/tree/master/plugins/inputs/procstat

示例配置。

# Monitor process cpu and memory usage
[[inputs.procstat]]
  ## PID file to monitor process
  pid_file = "/var/run/nginx.pid"
  ## executable name (ie,pgrep <exe>)
  # exe = "nginx"
  ## pattern as argument for pgrep (ie,pgrep -f <pattern>)
  # pattern = "nginx"
  ## user as argument for pgrep (ie,pgrep -u <user>)
  # user = "nginx"
  ## Systemd unit name
  # systemd_unit = "nginx.service"
  ## CGroup name or path
  # cgroup = "systemd/system.slice/nginx.service"

  ## Windows service name
  # win_service = ""

  ## override for process_name
  ## This is optional; default is sourced from /proc/<pid>/status
  # process_name = "bar"

  ## Field name prefix
  # prefix = ""

  ## When true add the full cmdline as a tag.
  # cmdline_tag = false

  ## Add the PID as a tag instead of as a field.  When collecting multiple
  ## processes with otherwise matching tags this setting should be enabled to
  ## ensure each process has a unique identity.
  ##
  ## Enabling this option may result in a large number of series,especially
  ## when processes have a short lifetime.
  # pid_tag = false