How to log the most CPU-Intensive processes in regular intervals

cpu-usagemonitoringwindows-server-2008-r2

I have a server which is idling below 5% cpu load most of the time, except from a few times a day where I can see some minor cpu spikes. I found out about this by using PRTG.

How can I find out what process is causing the cpu spikes? It's easy to monitor WMI CPU load with PRTG, but what I would also really need is a way to log the most CPU-Intensive processes in regular intervals.

I guess this can be done with perfmon, but which performance counters do I need for this job?

Thanks,

Adrian

Best Answer

Data Collector Sets are nice, professional, and they can be triggered by an event such as... high CPU usage.

You could also use Powershell.

$proc = Get-Process | Sort-Object TotalProcessorTime -Descending -EA 0 | Select -First 1
"$(Get-Date) - $($proc.Name) - $($proc.TotalProcessorTime)" | Out-File file.txt -Append

Save that as a *.ps1. Run it as a Scheduled Task every 5 minutes or whatever. The first line finds the top 1 most "CPU intensive" process at the time. The second line logs it to a file with a time stamp.

You could also get as fancy as you can imagine, by having the script only log if the system's total CPU usage is over 99% or something like that. The sky's the limit.