安装 Telegraf

1、下载 Telegraf 二进制文件。
从浏览器下载 Telegraf 二进制文件。
如果需要,将下载的存档展开 C:\Program Files\InfluxData\ 并重命名文件。
  • Windows Binaries (64-bit) - using PowerShell
> Expand-Archive .\telegraf-1.25.1_windows_amd64.zip -DestinationPath 'C:\Program Files\InfluxData\'
> mv 'C:\Program Files\InfluxData\telegraf-1.25.1' 'C:\Program Files\InfluxData\telegraf'
启动 Telegraf

在Powershell中,通过运行守护进程导航 C:\Program Files\InfluxData\telegraf 并启动 Telegraf

> cd -Path 'C:\Program Files\InfluxData\telegraf'
> ./telegraf -h

通过提示信息,我们可了解到启动命令,接着我们配置启动配置

NAME:
   Telegraf - The plugin-driven server agent for collecting & reporting metrics.

USAGE:
   Telegraf [global options] command [command options] [arguments...]

COMMANDS:
   config   print out full sample configuration to stdout
   version  print current version to stdout
   secrets  commands for listing, adding and removing secrets on all known secret-stores
   help, h  Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --config value [ --config value ]                      configuration file to load
   --config-directory value [ --config-directory value ]  directory containing additional *.conf files
   --test-wait value                                      wait up to this many seconds for service inputs to complete in test mode (default: 0)
   --usage value                                          print usage for a plugin, ie, 'telegraf --usage mysql'
   --pprof-addr value                                     pprof host/IP and port to listen on (e.g. 'localhost:6060')
   --watch-config value                                   monitoring config changes [notify, poll]
   --pidfile value                                        file to write our pid to
   --once                                                 run one gather and exit (default: false)
   --debug                                                turn on debug logging (default: false)
   --quiet                                                run in quiet mode (default: false)
   --test                                                 enable test mode: gather metrics, print them out, and exit. Note: Test mode only runs inputs, not processors, aggregators, or outputs (default: false)
   --deprecation-list                                     print all deprecated plugins or plugin options (default: false)
   --input-list                                           print available input plugins (default: false)
   --output-list                                          print available output plugins (default: false)
   --version                                              DEPRECATED: display the version and exit (default: false)
   --sample-config                                        DEPRECATED: print out full sample configuration (default: false)
   --plugin-directory value                               DEPRECATED: path to directory containing external plugins
   --section-filter value                                 filter the sections to print, separator is ':'. Valid values are 'agent', 'global_tags', 'outputs', 'processors', 'aggregators' and 'inputs'
   --input-filter value                                   filter the inputs to enable, separator is ':'
   --output-filter value                                  filter the outputs to enable, separator is ':'
   --aggregator-filter value                              filter the aggregators to enable, separator is ':'
   --processor-filter value                               filter the processors to enable, separator is ':'
   --secretstore-filter value                             filter the secret-stores to enable, separator is ':'
   --service value                                        operate on the service (windows only)
   --service-name value                                   service name (windows only) (default: "telegraf")
   --service-display-name value                           service display name (windows only) (default: "Telegraf Data Collector Service")
   --service-restart-delay value                          (default: "5m")
   --service-auto-restart                                 auto restart service on failure (windows only) (default: false)
   --console                                              run as console application (windows only) (default: false)
   --help, -h                                             show help (default: false)

基于配置文件启动:

> ./telegraf -config 'C:\Program Files\InfluxData\telegraf\telegraf.conf'

上面的提示表示我们还没配置过监控数据输出信息,需要修改 telegraf.conf 文件中的配置;

InfluxDB 1.x 需要修改[[outputs.influxdb]] 段落的配置:

# # Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
  ## The full HTTP or UDP URL for your InfluxDB instance.
  ##
  ## Multiple URLs can be specified for a single cluster, only ONE of the
  ## urls will be written to each interval.
  # urls = ["unix:///var/run/influxdb.sock"]
  # urls = ["udp://127.0.0.1:8089"]
  urls = ["http://127.0.0.1:8086"]

  ## The target database for metrics; will be created as needed.
  ## For UDP url endpoint database needs to be configured on server side.
  database = "telegraf"

  ## The value of this tag will be used to determine the database.  If this
  ## tag is not set the 'database' option is used as the default.
   # database_tag = ""

  ## If true, the 'database_tag' will not be included in the written metric.
  # exclude_database_tag = false

  ## If true, no CREATE DATABASE queries will be sent.  Set to true when using
  ## Telegraf with a user without permissions to create databases or when the
  ## database already exists.
  # skip_database_creation = false

  ## Name of existing retention policy to write to.  Empty string writes to
  ## the default retention policy.  Only takes effect when using HTTP.
  # retention_policy = ""

  ## The value of this tag will be used to determine the retention policy.  If this
  ## tag is not set the 'retention_policy' option is used as the default.
  # retention_policy_tag = ""

  ## If true, the 'retention_policy_tag' will not be included in the written metric.
  # exclude_retention_policy_tag = false

  ## Write consistency (clusters only), can be: "any", "one", "quorum", "all".
  ## Only takes effect when using HTTP.
  # write_consistency = "any"

  ## Timeout for HTTP messages.
  timeout = "5s"

  ## HTTP Basic Auth
  # username = "telegraf"
  # password = "metricsmetricsmetricsmetrics"

  ## HTTP User-Agent
  # user_agent = "telegraf"

  ## UDP payload size is the maximum packet size to send.
  # udp_payload = "512B"

  ## Optional TLS Config for use on HTTP connections.
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  ## Use TLS but skip chain & host verification
  # insecure_skip_verify = false

  ## HTTP Proxy override, if unset values the standard proxy environment
  ## variables are consulted to determine which proxy, if any, should be used.
  # http_proxy = "http://corporate.proxy:3128"

  ## Additional HTTP headers
  # http_headers = {"X-Special-Header" = "Special-Value"}

  ## HTTP Content-Encoding for write request body, can be set to "gzip" to
  ## compress body or "identity" to apply no encoding.
  # content_encoding = "gzip"

  ## When true, Telegraf will output unsigned integers as unsigned values,
  ## i.e.: "42u".  You will need a version of InfluxDB supporting unsigned
  ## integer values.  Enabling this option will result in field type errors if
  ## existing data has been written.
  # influx_uint_support = false

InfluxDB 2.x 需要修改[[outputs.influxdb_v2]] 段落的配置:

# # Configuration for sending metrics to InfluxDB 2.0
[[outputs.influxdb_v2]]
#   ## The URLs of the InfluxDB cluster nodes.
#   ##
#   ## Multiple URLs can be specified for a single cluster, only ONE of the
#   ## urls will be written to each interval.
#   ##   ex: urls = ["https://us-west-2-1.aws.cloud2.influxdata.com"]
    urls = ["http://127.0.0.1:8086"]
#
#   ## Token for authentication.
   token = "eCnJJ3FhiIt45q_6DDlQEt5QifhXNZ3IbYGgcjfpNgJ8VCxMjrRRMj6WWNRFmuAYUvjx2TkmAdlYmCP3wTVkog=="
#
#   ## Organization is the name of the organization you wish to write to.
   organization = "test"
#
#   ## Destination bucket to write into.
   bucket = "telegraf"
#
#   ## The value of this tag will be used to determine the bucket.  If this
#   ## tag is not set the 'bucket' option is used as the default.
#   # bucket_tag = ""
#
#   ## If true, the bucket tag will not be added to the metric.
#   # exclude_bucket_tag = false
#
#   ## Timeout for HTTP messages.
    timeout = "5s"
#
#   ## Additional HTTP headers
#   # http_headers = {"X-Special-Header" = "Special-Value"}
#
#   ## HTTP Proxy override, if unset values the standard proxy environment
#   ## variables are consulted to determine which proxy, if any, should be used.
#   # http_proxy = "http://corporate.proxy:3128"
#
#   ## HTTP User-Agent
#   # user_agent = "telegraf"
#
#   ## Content-Encoding for write request body, can be set to "gzip" to
#   ## compress body or "identity" to apply no encoding.
#   # content_encoding = "gzip"
#
#   ## Enable or disable uint support for writing uints influxdb 2.0.
#   # influx_uint_support = false
#
#   ## Optional TLS Config for use on HTTP connections.
#   # tls_ca = "/etc/telegraf/ca.pem"
#   # tls_cert = "/etc/telegraf/cert.pem"
#   # tls_key = "/etc/telegraf/key.pem"
#   ## Use TLS but skip chain & host verification
#   # insecure_skip_verify = false

为了获取更多的客户端信息我们需要进一步修改 telegraf.conf 文件中的配置;

# Configuration for telegraf agent
[agent]
  ## Default data collection interval for all inputs
  interval = "10s"
  ## Rounds collection interval to 'interval'
  ## ie, if interval="10s" then always collect on :00, :10, :20, etc.
  round_interval = true

  ## Telegraf will send metrics to outputs in batches of at most
  ## metric_batch_size metrics.
  ## This controls the size of writes that Telegraf sends to output plugins.
  metric_batch_size = 1000

  ## Maximum number of unwritten metrics per output.  Increasing this value
  ## allows for longer periods of output downtime without dropping metrics at the
  ## cost of higher maximum memory usage.
  metric_buffer_limit = 10000

  ## Collection jitter is used to jitter the collection by a random amount.
  ## Each plugin will sleep for a random time within jitter before collecting.
  ## This can be used to avoid many plugins querying things like sysfs at the
  ## same time, which can have a measurable effect on the system.
  collection_jitter = "0s"

  ## Collection offset is used to shift the collection by the given amount.
  ## This can be be used to avoid many plugins querying constraint devices
  ## at the same time by manually scheduling them in time.
  # collection_offset = "0s"

  ## Default flushing interval for all outputs. Maximum flush_interval will be
  ## flush_interval + flush_jitter
  flush_interval = "10s"
  ## Jitter the flush interval by a random amount. This is primarily to avoid
  ## large write spikes for users running a large number of telegraf instances.
  ## ie, a jitter of 5s and interval 10s means flushes will happen every 10-15s
  flush_jitter = "0s"

  ## Collected metrics are rounded to the precision specified. Precision is
  ## specified as an interval with an integer + unit (e.g. 0s, 10ms, 2us, 4s).
  ## Valid time units are "ns", "us" (or "µs"), "ms", "s".
  ##
  ## By default or when set to "0s", precision will be set to the same
  ## timestamp order as the collection interval, with the maximum being 1s:
  ##   ie, when interval = "10s", precision will be "1s"
  ##       when interval = "250ms", precision will be "1ms"
  ##
  ## Precision will NOT be used for service inputs. It is up to each individual
  ## service input to set the timestamp at the appropriate precision.
  precision = "0s"

  ## Log at debug level.
  # debug = false
  ## Log only error level messages.
  # quiet = false

  ## Log target controls the destination for logs and can be one of "file",
  ## "stderr" or, on Windows, "eventlog".  When set to "file", the output file
  ## is determined by the "logfile" setting.
  # logtarget = "file"

  ## Name of the file to be logged to when using the "file" logtarget.  If set to
  ## the empty string then logs are written to stderr.
  # logfile = ""

  ## The logfile will be rotated after the time interval specified.  When set
  ## to 0 no time based rotation is performed.  Logs are rotated only when
  ## written to, if there is no log activity rotation may be delayed.
  # logfile_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.
  # logfile_rotation_max_size = "0MB"

  ## Maximum number of rotated archives to keep, any older logs are deleted.
  ## If set to -1, no archives are removed.
  # logfile_rotation_max_archives = 5

  ## Pick a timezone to use when logging or type 'local' for local time.
  ## Example: America/Chicago
  # log_with_timezone = ""

  ## Override default hostname, if empty use os.Hostname()
  ## 修改hosename为主机IP;格式如:127.0.0.1
  hostname = "127.0.0.1"
  ## If set to true, do no set the "host" tag in the telegraf agent.
  omit_hostname = false

  ## Method of translating SNMP objects. Can be "netsnmp" which
  ## translates by calling external programs snmptranslate and snmptable,
  ## or "gosmi" which translates using the built-in gosmi library.
  # snmp_translator = "netsnmp"

将 Telegraf 注册为本地服务

Telegraf 提供了安装为服务的能力,可通过下面的命令注册服务:

> .\telegraf.exe --service install --config "C:\Program Files\InfluxData\telegraf\telegraf.conf"

请确保提供telegraf.conf配置文件的绝对路径,否则 Windows 服务可能无法启动。

要测试安装是否有效,请运行:

> C:\"Program Files"\InfluxData\telegraf\telegraf.exe --config C:\"Program Files"\InfluxData\telegraf\telegraf.conf --test
日志记录和故障排除

当 Telegraf 作为 Windows 服务运行时,Telegraf 会将消息记录到 Windows 事件日志中。如果 Telegraf 服务无法启动,请通过选择事件查看器→ Windows 日志→应用程序查看错误日志。

Windows 服务命令

以下命令可用:

命令 说明
telegraf -service install -config %TELEGRAF_CONFIG_PATH% 安装服务
telegraf -service start 启动服务
telegraf -service stop 停止服务
telegraf -service uninstall 卸载服务
作者:Jeebiz  创建时间:2019-10-20 22:08
最后编辑:Jeebiz  更新时间:2024-02-26 11:18