I have a problem on prtg probe server on one specific server. In this Server, cpu usage hits 90% and need to restart because of prtg service. How can I restart the prtg service automatically when the cpu usage hit 90%?
I was thinking of creating a script with a condition but couldn't complete it.
-Set-Service [service name] -startuptype automatic
If you're using PowerShell 5.x you can use Get-Counter
to get CPU usage. Try the following on the server to check:
$peridiocallyChecks = {
# Check if logging source is available, if not add it
if (!(Get-EventLog -Source "CPU supervision" -LogNameApplication)){
New-EventLog -LogName Application -Source "CPU supervision"
}
$cpuUsage = [double] (Get-Counter '\Processor(_Total)\% Processor Time' -ErrorAction SilentlyContinue | Select-Object -ExpandProperty CounterSamples | Select-Object -ExpandProperty CookedValue)
if ($cpuUsage -gt 90) {
Write-EventLog -LogName "Application" -Source "CPU supervision" -EntryType Information -EventId 1 -Message "CPU usage is $cpuUsage. Going to stop service"
Stop-Service "yourServiceName"
# Some cooldown time for the service to shutdown
Start-Sleep -Seconds 10
Start-Service "yourServiceName"
Write-EventLog -LogName "Application" -Source "CPU supervision" -EntryType Information -EventId 1 -Message "Restarted service"
}
}
# Trigger every hour
$trigger = New-JobTrigger -Once -At "9/21/2012 0am" -RepetitionInterval (New-TimeSpan -Hour 12) -RepetitionDuration ([TimeSpan]::MaxValue)
Register-ScheduledJob -Name "CPUCheck" -Trigger $trigger -Scriptblock $peridiocallyChecks
Check following links regarding jobs:
Hope that helps