Inputs Exec Powershell

I had a similar need few weeks ago and solved it with the following approach:

  • Powershell will return objects
  • Add/rename some properties
  • Using the Influx Powershell Module you can serialize the data in Influx format
  • feed the data to Telegraf

Here is the script and the corresponding conf (I will put them on Github someday
Configuration:

# Read metrics from one or more commands that can output to stdout
[[inputs.exec]]

  ## Override default gathering interval
  interval = "3h"

  ## Commands array
  commands = [
     'powershell "C:\Monitoring\telegraf\Configuration\telegraf.d\custom_input_scripts\Get_DiskSpace.ps1 -HostList SQLCSRV04"'
  ]
  
  ## Timeout for each command to complete.
  timeout = "2m"

  ## data format options:
  ## https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_INPUT.md
  data_format = "influx"

Powershell script:

# ===================================================
# Script Requirements
# ===================================================
# 1. Powershell remoting enabled, even on local machine. run as admin -> Enable-PSRemoting
# 2. Influx Powershell Module, run as admin -> Install-Module Influx

param(
    [String[]] $HostList = [Environment]::MachineName
)

# only DriveType = 3 → Local hard disk
$VolumeData = Get-CimInstance CIM_LogicalDisk -ComputerName $HostList | Where-Object {$_.DriveType -eq 3}

# ===================================================
# Set calculated/renamed Fields
# ===================================================

$Server = @{label="Server";expression={$_.SystemName}}
$Volume = @{label="VolumeId";expression={$_.DeviceID}}

$TotalSize = @{label="TotalSpace(MB)";expression={[math]::Round($_.Size/1MB, 2)}}
$FreeSpace = @{label="FreeSpace(MB)";expression={[math]::Round($_.FreeSpace/1MB, 2)}}
$FreeSpacePerc = @{label="FreeSpace(%)";expression={[math]::Round(1-($_.Size - $_.FreeSpace)/$_.Size , 4)}}

# $_.Description may contain the same value
$VolumeTypeC = @{label="VolumeType";expression={
    switch ($_.DriveType) {
        1 {"No root directory"; break}
        2 {"Removable drive"; break}
        3 {"Local hard disk"; break}
        4 {"Network disk"; break}
        5 {"Compact disk"; break}
        6 {"RAM disk"; break}
        default {"Unknown"; break}
    }
}}

# ===================================================
# Define Tags and Metrics property mapping
# ===================================================
$Measurement = "ServerDisk"
$TagList = "Server","VolumeId","VolumeName"
$MetricList = "TotalSpace(MB)","FreeSpace(MB)","FreeSpace(%)"

# ===================================================
# Output
# ===================================================
# Warnings can't be sent to the output therefore "-WarningAction SilentlyContinue" is used
$VolumeData | Select-Object $Server,$Volume,VolumeName,$VolumeTypeC,$TotalSize,$FreeSpace,$FreeSpacePerc | ConvertTo-Metric -Measure $Measurement -MetricProperty $MetricList -TagProperty $TagList | ConvertTo-InfluxLineString -WarningAction SilentlyContinue

Notes:

  • that the Input server list is passed manually, you may want to fetch it from somewhere else (ie a file, or some other Powershell command that returns a list of server)
  • The influx ps module can post the data directly to influxdc
1 Like