Can Telegraf track the OS version from a machine

Hello everyone.

So i’m using Grafana 8.4.4 with flux queries and InfluxDB 2.1.1 to track metrics from a Telegraf i’ve installed in a Windows Server. Basically i searched a lot, but i haven’t found any input plugin or any way to make a pannel that displays the type of OS or the version that has the machine with the Telegraf.

This may not sound very useful, but if you’re tracking info from a lot of clients with different types of machines and OS it would be pretty cool to have more context in a quick look.

If someone knows of an input plugin that does that, or how to send the data from a powershell command to an input, or how to make a custom input that can track the OS version, type, license or whatever, it would be very helpful.

Hello @consultor,
You can include the host with the agent config:

Otherwise you could use the execd processor plugin to add that info:

If you do, you might want to contribute to:

1 Like

Hello,

this script should get you information about the windows operating system and build if you don’t have one already:

$operatingSystem = Get-WmiObject -Class Win32_OperatingSystem | Select-Object -Property Version,BuildNumber, @{ Name = 'OperatingSystem'; Expression = {( $($_.Name.split("|")[-0]))}}
$model = Get-WmiObject win32_computersystem | Select-Object -Property Model

$hostname = (Get-WMIObject Win32_ComputerSystem | Select-Object -ExpandProperty dnshostname).ToUpper()
$name = $operatingSystem.OperatingSystem.Split(" ",5) 
$version = $operatingSystem.Version
$buildNumber = $operatingSystem.BuildNumber




$os = $name[0] + ' ' + $name[1] + ' ' + $name[2] + ' ' + $name[3] -Replace (" ", "\ ")
$edition = $name[-1] -Replace (" ", "\ ")
$model = $model.Model -Replace (" ", "\ ") -replace(",",".")


$body = "win_version,host=$($hostname),name=$($os),edition=$($edition),model=$($model),buildNumber=$($buildNumber),version=$($version) field=1i"


Write-Host $body

Things to consider:

  1. To me the values were all tags, if you want to query the data you need a field hence the “field=1i”
  2. This will run on the individual machine, you’ll need to run this on each machine OR if you’re in active directory you could use PowerShells Active Directory modules to connect to AD and get a list of your servers and go from there
  3. If you’re running telegraf as a service and need to provide credentials to connect to AD then you should use PS built in Microsoft Secure String to store the credentials.

Side note on #3: those credentials can only be read by the user who sets them, so you would need to create an account for the service, log in to the windows machine, define your credentials and tell the service to use the new account. If you run this on each machine separately you shouldn’t need to do that. LOCAL SYSTEM will be sufficient. However if you do use a separate account you need to make sure it has permissions to access windows performance counters otherwise you will lose other windows data performance data.

The script itself: Can’t quite remember fully, it was a long time ago :slight_smile:

The main part to insert the data is the $body line, formatted in Influx Line Protocol

To run the script with telegraf, add a config called exec.conf:

[[inputs.exec]]
commands = ["powershell.exe -ExecutionPolicy Bypass /path_to_your_script/script.ps1"]
interval = "60s"
data_format = "influx"

Script should work on Windows 2012 and later, it does what i need it to but you may need to tweak it a little bit.

1 Like

Hello @Anaisdg,
Thank you for helping me with this, i haven’t changed the original hostname as it is another metric i use in other panels and queries, but i endend using a plugin that is very similar to execd, wich is the one that @philb has helped me with.

1 Like

Hello @philb,
I don´t know what to say, im so thankful that you gave me that much help.
I investigated a lot these days about using inputs.exec, but i knew little more than how to correctly associate the powershell file to the telegraf config file.

With that script and your configuration it does work!
Influx collect the data correctly and display it as tags, but here comes one question wich i think you already try to solve me in number 1 on your list of things to consider;
Since influx display the data as tags, is there a way to display only the tag but not it’s value (since they lack of value) on a panel? Or maybe make the value from each tag to be its own name?

I think i’m making things harder but despite influx collects all the info i want, now i don’t know how to show it on a Grafana panel, because i think you cannot make a query to display just the name of a tag right?
However, did you show the data on Grafana too? and if so how did you?

·Edit: I was confusing the tag that shows the data with another tag called almost exactly the same that doesn´t work. I can show what i wanted, sorry if i confused you.
Thank you so much for everything.

@philb
I encourage you to contribute your script to:

influxdata/telegraf/blob/master/EXTERNAL_PLUGINS.md

Thank you for your help!

Sorry for the delay! I don’t get on as much as I used to.

I’m glad you got it working @consultor

Thanks @Anaisdg if you think it would be a good contribution I’ll take a look the external plugins link you’ve provided.

1 Like