Deploying Telegraf on Windows as a Service

<< Deploying Telegraf on Linux

Most people reading this guide series probably use Windows. So I figured I’d add this quick page that explains how to deploy Telegraf on Windows as a service.

We will be using both PowerShell and Visual Studio Code for this portion of the guide. To get started, download Telegraf via a web request in Powershell

You will need to be in an Admin session to download to C:\!

# PowerShell [Hyper-V Host]
Invoke-WebRequest https://dl.influxdata.com/telegraf/releases/telegraf-1.13.0_windows_amd64.zip -OutFile C:\telegraf.zip -UseBasicParsing

Next unzip the telegraf.zip file and delete the old zip file.

# PowerShell [Hyper-V Host]
Expand-Archive C:\telegraf.zip -DestinationPath C:\

rm C:\telegraf.zip

Once completed you should now have a folder in C:\ called telegraf. In this folder you should see two files: telegraf.exe and telegraf.conf.

Configuring Telegraf for Windows

Now that we have Telegraf downloaded, we need to configure it for Windows monitoring.

First create the telegraf.d folder for our inputs and our log folder.

# PowerShell [Hyper-V Host]
mkdir C:\telegraf\telegraf.d
mkdir C:\telegraf\logs

Now delete the telegraf.conf file and recreate it.

# PowerShell [Hyper-V Host]
del C:\telegraf\telegraf.conf
# PowerShell [Hyper-V Host]
# Check and make sure Visual Studio Code is installed
code --version

OUTPUT EXAMPLE:
PS C:\telegraf> code --version
1.31.1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x64
# PowerShell [Hyper-V Host]
code C:\telegraf\telegraf.conf

Paste the following into the new telegraf.conf file:

# Telegraf Configuration
#
[global_tags]
[agent]
  interval = "10s"
  round_interval = true
  metric_batch_size = 1000
  metric_buffer_limit = 10000
  collection_jitter = "0s"
  flush_interval = "10s"
  flush_jitter = "0s"
  precision = ""
  debug = false
  quiet = false
  logfile = "C:/telegraf/logs/telegraf.log"
  logfile_rotation_interval = "1d"
  logfile_rotation_max_size = "10MB"
  logfile_rotation_max_archives = 5
  hostname = ""
  omit_hostname = false


###############################################################################
#                                  OUTPUTS                                    #
###############################################################################

# Configuration for sending metrics to InfluxDB
[[outputs.influxdb]]
  ## The full HTTP or UDP URL for your InfluxDB instance.
   urls = ["http://CHANGEME:8086"]
   database = "telegraf"
   skip_database_creation = true
  # retention_policy = ""
  # write_consistency = "any"
   timeout = "5s"
   username = "CHANGEME"
   password = "CHANGEME"
   user_agent = "telegraf"
  # udp_payload = "512B"
  # tls_ca = "/etc/telegraf/ca.pem"
  # tls_cert = "/etc/telegraf/cert.pem"
  # tls_key = "/etc/telegraf/key.pem"
  # insecure_skip_verify = false
  # http_proxy = "http://corporate.proxy:3128"
  # http_headers = {"X-Special-Header" = "Special-Value"}
  # content_encoding = "identity"
  # influx_uint_support = false

Edit the CHANGEME values in the [[outputs.influxdb]] to match your environment setup.

Once completed we can add our win_perf counters input to a telegraf.d config file.

code C:\telegraf\telegraf.d\win_perf_counters.conf

Paste the following in the new file:

[[inputs.win_perf_counters]]
  [[inputs.win_perf_counters.object]]
    ObjectName = "Processor"
    Instances = ["*"]
    Counters = [
      "% Idle Time",
      "% Interrupt Time",
      "% Privileged Time",
      "% User Time",
      "% Processor Time",
      "% DPC Time",
    ]
    Measurement = "win_cpu"
    IncludeTotal=true

  [[inputs.win_perf_counters.object]]
    ObjectName = "LogicalDisk"
    Instances = ["*"]
    Counters = [
      "% Idle Time",
      "% Disk Time",
      "% Disk Read Time",
      "% Disk Write Time",
      "Current Disk Queue Length",
      "% Free Space",
      "Free Megabytes",
    ]
    Measurement = "win_disk"

  [[inputs.win_perf_counters.object]]
    ObjectName = "PhysicalDisk"
    Instances = ["*"]
    Counters = [
      "Disk Read Bytes/sec",
      "Disk Write Bytes/sec",
      "Current Disk Queue Length",
      "Disk Reads/sec",
      "Disk Writes/sec",
      "% Disk Time",
      "% Disk Read Time",
      "% Disk Write Time",
    ]
    Measurement = "win_diskio"

  [[inputs.win_perf_counters.object]]
    ObjectName = "Network Interface"
    Instances = ["*"]
    Counters = [
      "Bytes Received/sec",
      "Bytes Sent/sec",
      "Packets Received/sec",
      "Packets Sent/sec",
      "Packets Received Discarded",
      "Packets Outbound Discarded",
      "Packets Received Errors",
      "Packets Outbound Errors",
    ]
    Measurement = "win_net"

  [[inputs.win_perf_counters.object]]
    ObjectName = "System"
    Counters = [
      "Context Switches/sec",
      "System Calls/sec",
      "Processor Queue Length",
      "System Up Time",
    ]
    Instances = ["------"]
    Measurement = "win_system"

  [[inputs.win_perf_counters.object]]
    ObjectName = "Memory"
    Counters = [
      "Available Bytes",
      "Cache Faults/sec",
      "Demand Zero Faults/sec",
      "Page Faults/sec",
      "Pages/sec",
      "Transition Faults/sec",
      "Pool Nonpaged Bytes",
      "Pool Paged Bytes",
      "Standby Cache Reserve Bytes",
      "Standby Cache Normal Priority Bytes",
      "Standby Cache Core Bytes",

    ]
    Instances = ["------"]
    Measurement = "win_mem"
    IncludeTotal=true

  [[inputs.win_perf_counters.object]]
    ObjectName = "Paging File"
    Counters = [
      "% Usage",
    ]
    Instances = ["_Total"]
    Measurement = "win_swap"

Save and close the file.

Installing the Telegraf service

First we need to test our config. This can be done by running the following in the same directory as the telegraf.exe.

# PowerShell [Hyper-V Host]
C:\telegraf\telegraf.exe --config=C:\telegraf\telegraf.conf --config-directory=C:\telegraf\telegraf.d --test

You should see something like this output if your config is correct.

If its successful we can install the Telegraf service.

# PowerShell [Hyper-V Host]
C:\telegraf.exe --service install --config C:\telegraf\telegraf.conf --config-directory C:\telegraf\telegraf.d

Once the install is completed we can start the service.

# PowerShell [Hyper-V Host]
net start telegraf

If you encounter the following error while trying to start Telegraf, make sure to check your install command includes the full path to your config dir and conf file!


Importing the Windows Dashboard

This section is to be followed after you deploy Grafana.

Open Grafana and navigate to your dashboard import screen. In the Grafana.com Dashboard box type: 10411. Make sure to select your Telegraf database and click Import.

The dashboard has a dropdown selection for various Windows hosts. It works via selecting hostnames in InfluxDB via a regex for ALL CAPS hostnames. You can change this in the dashboard variables config screen.


<< Deploying Telegraf on Linux