Powershell Script in Telegraf

@Giovanni_Luisotto

I couldn’t comment to your thread, as it was closed. But, I wanted to see if you can help me out. I am using your script to pull the storage data with powershell from telegraf and write it into inflluxdb. When i run the telegraf in test mode, i do see the output of the systems disk info displayed on the screen. But when i try to run it normally, i do not see any data being written in influxdb. Everything i am using is pretty much exactly, just replaced the server name,

Thank You.
George

Are you running Telegraf as a service on windows? is it running as a different user?
can you test it with the user taht actually runs telegraf?

There might be some problems related to the Powershell execution policy, but since it runs with the --test flag probably that’s not the problem.

Can you share the Powershell script and your telegraf configuration?

EDIT

pls encapsulate your code in triple backticks (`) to keep it properly formatted

@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:

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

My guess is that there is something wrong when writing the points to influxDB, since that’s the only part the --test flag does not do.

I’d suggest you try to generate some points using the script, and then manually input them in InfluxDB, if there is something wrong with the point itself, you will notice it.
Another test could be using the output file to actually ensure if those points are gathered and written.

About your previous post/answer, please encapsulate the code in triple backticks (`) otherwise it’s unreadable

Sample here

The below text …

```toml
#### I'm a comment
[agent]
  interval = "10s"
```

… will be turned rendered as

#### I'm a comment
[agent]
  interval = "10s"