CPU Monitoring and Alerting via Performance counters, Coral8, and PowerShell

The PowerShell team has a short post on using V2 cmdlets to Monitor performance counters.

Building on that, and the prior work with the PoShAdapter, here’s a sample with Coral8 and PowerShell to alert you via SMS when the rolling 30 seconds average CPU hits a threshold.

create VARIABLE float CPUThreashold  = 70; 
create OUTPUT STREAM fakeout SCHEMA (x string);
CREATE SCHEMA CPUSchema (cpu FLOAT);
create INPUT STREAM inCPU SCHEMA CPUSchema;
create OUTPUT STREAM highCPU SCHEMA CPUSchema;
create local STREAM avgCPU SCHEMA CPUSchema;

ATTACH OUTPUT ADAPTER smsAdapter TYPE PoShAdapter TO STREAM highCPU
PROPERTIES
    BEGINBLOCK  = [[ 
    Add-Type -AssemblyName System.Security   
    Add-Type -Path "C:\Program Files\Coral8\Server\bin\GmailHelper.dll" 
    $pass = ".."
    ]],
PROCESSBlock     = [[
 foreach ($cpuSpike in $input)
 {
  $m = "CPU spike at {0:0.00}" -f $cpuSpike['cpu']
  [RC.Gmail.GmailMessage]::SendFromGmail("user", $pass,"6465555555@tmomail.net", 
   "Via Powershell/Coral8",$m)
 }
]];

ATTACH OUTPUT ADAPTER CPUAdapter TYPE PoShAdapter TO STREAM fakeout
PROPERTIES
    RESULTSSTREAM = "ccl://localhost:6789/Stream/Default/MonitorCPU/inCPU",
    INPUTBLOCK    = [[
Get-Counter '\Processor(*)\% Processor Time'    |
    select -expand CounterSamples               |
        ? { $_.InstanceName -eq "_total" }      | 
            % { ,,($_.CookedValue,0) } # 
]];

INSERT into avgCPU
select avg(cpu)  
from inCPU 
keep 30 SECONDS 
OUTPUT EVERY 30 SECONDS;

INSERT into highCPU 
select cpu 
from avgCPU 
WHERE cpu > CPUThreashold;

Yields:

sms 005

*I lowered the threshold for testing
** Yes, I should check my voice mail

No Comments