We have a Windows 2003 server where 3 services are running constantly. Sometimes these services consume over 90% CPU. Restarting these services returns normalcy. I need a script/program that will constantly monitor CPU usage and if usage is high then restart those services.
After a bit of research I found this script to monitor CPU usage from Technet.
Script to monitor CPU usage:
(get-counter -Counter "\Processor(_Total)\% Processor Time"
-SampleInterval 1 -MaxSamples 10 |
select -ExpandProperty countersamples | select -ExpandProperty
cookedvalue | Measure-Object -Average).average
This monitors the CPU usage for 10 seconds and then displays the averaged output.
Also from Stack Overflow and Server Fault I found the scripts to restart Windows services. (Which one is better?)
Now all I need is for the CPU usage script to call the service restart scripts when the condition that usage is >90% is met. Any help would be appreciated.
Answer
Just for the record..here's the answer:
$cpuutil=(get-counter -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 |
select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
If ($cpuutil -ge 90)
{Restart-Service MyService1, "My Service2", MyService3}
Else
{Exit}
Powershell truly makes life simpler!
No comments:
Post a Comment