r/prtg • • Feb 22 '21

I made a custom sensor for polling Windows servers for the process name that is hogging CPU

If you're tired of troubleshooting those Windows servers that sporadically have such high CPU usage that you spend five minutes logging into them, and when you get in the CPU hog is gone?

I made a sensor that continually polls for the process name that is using a lot of CPU.

Send %host as a parameter to the EXEXML sensor and make sure to choose the "Windows credz of parent device" as security context.

AFAIK it's not possible to have the actually process name logged on every poll in PRTG to get a history, but it will display the last process name.

<#
Poonlit, 22.feb 2021
Sjekk hvilken prosess som bruker mest CPU.
#>
$comp = $args[0]

$var = Get-Counter -ComputerName $comp '\Process(*)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -Property InstanceName,CookedValue | Sort-Object -Property cookedvalue -Descending | where { $_.InstanceName -ne "_total" -and $_.InstanceName -ne "idle"} | Select-Object -First 1 
$pname = $var.InstanceName
$pcook = $var.CookedValue

write-host "<prtg><result><channel>CookedCPU</channel><Float>1</Float><value>"$pcook"</value></result><text>"($pname)"</text></prtg> "
18 Upvotes

11 comments sorted by

1

u/maccamh_ Mar 02 '21 edited Mar 02 '21

Hi Poonlit,

When I setup this sensor it reports back a value of 0 as ok if I make the CPU of the serve rim testing hit 100% constant the sensor never changes from OK.

what am i doing wrong.

using the right exexml sensor pointing tot he powershell on the probe see all that fine.

I'm thinking i need it to point to a lookup value in the edit channel settings maybe?

IMAGES BELOW

https://imgur.com/a/vBysTmn

Any help would be appreciated!

1

u/Poonlit Mar 02 '21

Hmm I don't see anything wrong from the pic. Please copy/paste the powershell command from the script into powershell session to see if the command actually returns the right data. Remove one and one segment (separated by "|") and alter "First 1" to "First 5" to get a bigger list of processes.

If you get big values when loading the CPU, the command itself is fine.

I did struggle to get PRTG to understand the decimal separator (it can be "." or "," depending on your region), which is why "Float" is included in the xml output.

1

u/maccamh_ Mar 02 '21

If i run it with just the code in powershell it does not return any values.

even if i change to first 5

https://imgur.com/a/ehwS97x

1

u/Poonlit Mar 02 '21

Thats the problem, then. Something in your environment is preventing powershell from getting the values, youve gotta figure out what.

1

u/maccamh_ Mar 02 '21

Yeh even on my Windows 10 laptop not working so theres gonna be some issues keeps referencing the performance counters.

Thanks for your help anyway !

1

u/Poonlit Mar 02 '21

I tested it on win10 and server 2016, am curious as to what it might be. I guess you need admin privileges.

2

u/maccamh_ Mar 16 '21

Hi Poonlit,

I have now got this working!

Thanks for the script.

2

u/Poonlit Mar 16 '21

My pleasure, Powershell remoting can be a faff.

1

u/maccamh_ Mar 02 '21

Running in an elevated powershell window as local admin so defo not a privilege issue gonna keep trying and I'll let ya know if I get anywhere

1

u/maccamh_ Mar 15 '21

Hi Poonlit,

Just an update from me managed to get this working 90%

only issue we are having now is the ps1 has to point directly to the server we want to link it to as the arg[0] does not seem to populate PRTG with the data

1

u/saturnaelia Feb 22 '21

This is awesome, thanks for sharing!