Skip to main content

Dynamically Deploy Telegraf and Windows Monitoring Configurations with PowerShell

·651 words·4 mins
Table of Contents

I recently made this script to deploy the Telegraf metrics agent to Windows systems.

This is primarily designed for Windows Server but can also be pushed to workstations.

Overview
#

This is a Telegraf deployment script for Windows Server* environments.

By default, the script will install a base configuration which includes:

It will then scan the system to determine candidacy for the following additional input plugins:

RoleConfiguration Applied
Active Directory Domain Servicestelegraf-adds.conf **
DFS Namespacestelegraf-dfsn.conf
DFS Replicationtelegraf-dfsr.conf
DNS Servertelegraf-dns.conf

I am happy to add further configurations upon request; please raise an issue or pull request, ideally with an example Telegraf configuration.

* While this script is primarily designed for Windows Server, it will still work for Windows clients, the only difference being the additional configurations in the table above will never be installed (i.e. only the agent, output plugin, and system metrics input plugin).

** All of the above configurations are typically (but not always) applied to domain controllers since they commonly have all of the mentioned roles installed.

Suggested script deployment method: Group Policy computer start-up script.
It is flexible in this regard though, the only requirement being that it is run on the device itself.

Requirements
#

  • PowerShell 5.1 or higher.
  • Administrator privileges.
  • A network share with the following files in a flat structure:
    • telegraf.exe
    • telegraf.exe.sha256sum *
    • telegraf.conf
    • telegraf-adds.conf
    • telegraf-dfsn.conf
    • telegraf-dfsr.conf
    • telegraf-dns.conf
    • telegraf-system-metrics.conf

* This file must contain only the SHA256 sum of telegraf.exe.

Setup
#

  1. Download all the files in the repository to your desired source location (i.e. a network path). This can be done by running the commands below in PowerShell*.
cd \\path\to\share # Make sure you change this
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest "https://github.com/tigattack/Install-Telegraf-PoSH/archive/refs/heads/main.zip" -OutFile "$env:TEMP\Install-Telegraf-PoSH.zip"
Expand-Archive -Path "$env:TEMP\Install-Telegraf-PoSH.zip" -DestinationPath "$env:TEMP\"
Remove-Item "$env:TEMP\Install-Telegraf-PoSH.zip"
Remove-Item "$env:TEMP\Install-Telegraf-PoSH-main\.github" -Recurse
Move-Item "$env:TEMP\Install-Telegraf-PoSH-main\" ".\Install-Telegraf"
  1. Download the latest Telegraf release for Windows from Telegraf’s GitHub releases.
  2. Extract the EXE from the downloaded ZIP and move it to the source location.
  3. Rename the EXE to telegraf.exe.
  4. Open PowerShell, cd to the source directory (e.g. cd \\path\to\share) ,and run the following command:
(Get-FileHash -Algorithm SHA256 ".\telegraf.exe").Hash | Out-File ".\telegraf.exe.sha256sum"
  1. Configure your output plugin and any other Telegraf agent settings in telegraf.conf.

Ready to go!

* This little script is entirely safe. It instructs PowerShell to use TLS1.2 (allowing it to download from a secure (HTTPS) URL), downloads a ZIP of this repository to a temporary location, extracts the ZIP, removes the ZIP, moves this project into your destination directory, then removes the extracted directory.
However, please also feel free to perform a manual download of all the files in the repository (except README.md and .github).

Usage
#

Example
#

Screenshots
#

First install
First install

Update
Update

Commands
#

Standalone:

InstallTelegraf.ps1 -Source '\\path\to\share' `
    -Destination 'C:\custom\path' `
    -ServiceName 'my-telegraf' `
    -ServiceDisplayName 'My Telegraf' `
    -LogPath 'C:\Windows\TEMP\InstallTelegraf.log'

Standalone dry run:

InstallTelegraf.ps1 -Source '\\path\to\share' `
    -Destination 'C:\custom\path' `
    -ServiceName 'my-telegraf' `
    -ServiceDisplayName 'My Telegraf' `
    -LogPath 'C:\Windows\TEMP\InstallTelegraf.log' `
    -WhatIf

Automated Deployment:

PowerShell.exe -WindowStyle Hidden -File '\\path\to\InstallTelegraf.ps1' `
    -Source '\\path\to\share' `
    -Destination 'C:\custom\path' `
    -ServiceName 'my-telegraf' `
    -ServiceDisplayName 'My Telegraf' `
    -LogPath 'C:\Windows\TEMP\InstallTelegraf.log'

Parameters
#

ParameterTypeDescription
SourceStringPath to network share containing Telegraf source (agent, configurations, etc.). Defaults to the script’s parent directory.
DestinationStringPath to Telegraf destination directory. Defaults to ‘C:\Program Files\Telegraf’.
InstallServiceSwitchDefaults to true but can be used to disable the installation of the Telegraf service (i.e. -InstallService:$false).
ServiceNameStringTelegraf service name. Defaults to ’telegraf'.
ServiceDisplayNameStringTelegraf service display name. Defaults to ‘Telegraf’.
LogPathStringPath to log file. Defaults to ‘C:\InstallTelegraf.log’.
WhatIfSwitchPerforms a dry run.
ConfirmSwitchPrompts user for confirmation before taking action.
VerboseSwitchEnables verbose output.

I always welcome feedback on my posts, please contact me if you have any. I’m also happy to answer any related questions if I know the answer.

tigattack
Author
tigattack