Skip to content

InfluxDB

Cookbook

Telegraf

Docker

Implementation:

  • Needs to have a .env file next to docker-compose.yml. The token comes from creating the Telegraf configuration in the InfluxDB web interface
    INFLUX_TOKEN="<TOKEN>"
    
  • Replace the URL in the command with the one from the Telegraf configuration in the InfluxDB web interface
Docker compose service example
telegraf:
    image: telegraf
    container_name: telegraf-docker-lxc
    restart: unless-stopped
    user: root:996
    volumes:
    - /var/run/docker.sock:/var/run/docker.sock
    env_file:
    - .env
    command: ["--config", "http://docker-lxc.localdomain:8086/api/v2/telegrafs/0bf0754463950000"]

Windows Performance Counters

sequenceDiagram
    telegraf->>influxdb: Request config with API token
    influxdb->>telegraf: Respond with config
    telegraf->>telegraf: Loads config
    telegraf->>influxdb: Writes to bucket with API token

Powershell Snippets

Example Installation
wget https://dl.influxdata.com/telegraf/releases/telegraf-1.28.2_windows_amd64.zip -UseBasicParsing -OutFile telegraf-1.28.2_windows_amd64.zip
Expand-Archive .\telegraf-1.28.2_windows_amd64.zip -DestinationPath 'C:\Program Files\InfluxData\telegraf'
Install service
$serviceName = 'telegraf'
$url = "http://docker-lxc.localdomain:8086/api/v2/telegrafs/0be5d1b85c53c000"

.\telegraf.exe --config $url `
--service install --service-name $serviceName `
--service-display-name "Telegraf - Windows Performance Counters" `
--service-auto-restart --service-restart-delay 1m

Providing the API token to the service

This makes the token available as an environment variable to the service when it runs.

  • Use regedit to add an entry to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\telegraf
  • Edit -> New -> Multi-String Value
    • Value Name: Environment
    • Value Data:
      INFLUX_TOKEN=<token>
      

Must run as admin

Check service
Get-Service -Name $serviceName
Restart service
$serviceName = 'telegraf'
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue

if ($null -ne $service) {
    try {
        Restart-Service -Name $serviceName -Force -ErrorAction Stop
        Write-Output "Service '$serviceName' has been restarted."
    } catch {
        Write-Error "Failed to restart service '$serviceName'. Error: $_"
    }
} else {
    Write-Output "Service '$serviceName' is not installed."
}
Stop service
$serviceName = 'telegraf'
$service = Get-Service -Name $serviceName -ErrorAction SilentlyContinue

if ($null -ne $service) {
    try {
        Stop-Service -Name $serviceName -Force -ErrorAction Stop
        Write-Output "Service '$serviceName' has been stopped."
    } catch {
        Write-Error "Failed to stop service '$serviceName'. Error: $_"
    }
} else {
    Write-Output "Service '$serviceName' is not installed."
}

References: