@Giovanni_Luisotto
I have tried to run it both ways, and both ways run as system.
Telegraf Config File
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 cache metric_buffer_limit metrics for each output, and will
flush this buffer on a successful write.
metric_buffer_limit = 1000
Flush the buffer whenever full, regardless of flush_interval.
flush_buffer_when_full = true
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”
Default flushing interval for all outputs. You shouldn’t set this below
interval. 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”
Logging configuration:
Run telegraf in debug mode
debug = true
Run telegraf in quiet mode
quiet = false
Specify the log file name. The empty string means to log to stdout.
logfile = “/Telegraf/telegraf.log”
Override default hostname, if empty use os.Hostname()
hostname = “”
###############################################################################
OUTPUTS
###############################################################################
Configuration for influxdb server to send metrics to
[[outputs.influxdb]]
The full HTTP or UDP endpoint URL for your InfluxDB instance.
Multiple urls can be specified but it is assumed that they are part of the same
cluster, this means that only ONE of the urls will be written to each interval.
urls = [“udp://127.0.0.1:8089”] # UDP endpoint example
urls = [“http://influxdb:8086”]
The target database for metrics (telegraf will create it if not exists)
database = “Test”
Precision of writes, valid values are “ns”, “us” (or “µs”), “ms”, “s”, “m”, “h”.
note: using second precision greatly helps InfluxDB compression
precision = “s”
Write timeout (for the InfluxDB client), formatted as a string.
If not provided, will default to 5s. 0s means no timeout (not recommended).
timeout = “5s”
#username = “telegraf”
password = “metricsmetricsmetricsmetrics”
Set the user agent for HTTP POSTs (can be useful for log differentiation)
user_agent = “telegraf”
Set UDP payload size, defaults to InfluxDB UDP Client default (512 bytes)
udp_payload = 512
###############################################################################
INPUTS
###############################################################################
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:/telegraf/DiskSpace.ps1”’
]
Timeout for each command to complete.
timeout = “2m”
data format options:
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