📑

SNS設定

に公開
# SNS 設定
$topicArn = "arn:aws:sns:ap-northeast-1:123456789012:MyServerAlertTopic"
$region = "ap-northeast-1"

# CPU 使用率取得
$cpu = Get-Counter '\Processor(_Total)\% Processor Time' -SampleInterval 1 -MaxSamples 1
$cpuUsage = [math]::Round($cpu.CounterSamples.CookedValue, 2)

# メッセージ生成
$message = "CPU usage on $(hostname): $cpuUsage%"

# 閾値
if ($cpuUsage -gt 80) {
    Write-Host "High CPU: $cpuUsage% - sending alert"

    aws sns publish `
        --topic-arn $topicArn `
        --region $region `
        --message "$message" `
        --subject "High CPU Alert"
} else {
    Write-Host "Normal CPU: $cpuUsage%"
}

Discussion