CPU monitor and trigger to collect Logs
# ============================================================================
# Trend Micro Diagnostic Auto Collection on High CPU
# ============================================================================
# Configuration
$CpuThreshold = 90 # CPU %
$ConsecutiveBreaches = 5 # Number of consecutive checks
$CheckIntervalSeconds = 60 # Check interval
$CooldownMinutes = 30 # Wait before next collection
# Paths
$TrendPath = "C:\Program Files\Trend Micro\Deep Security Agent"
$DiagCommand = "dsa_control.cmd"
$LogFolder = "C:\temp"
# Create log folder if needed
if (!(Test-Path $LogFolder)) {
New-Item -Path $LogFolder -ItemType Directory -Force | Out-Null
}
$LogFile = Join-Path $LogFolder "TrendMicro_CPU_Monitor.log"
# Log Function
function Write-Log {
param([string]$Message)
$TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
"$TimeStamp - $Message" | Out-File -FilePath $LogFile -Append -Encoding UTF8
}
Write-Log "============================================================"
Write-Log "Trend Micro CPU Monitoring Started"
Write-Log "CPU Threshold: $CpuThreshold%"
Write-Log "Consecutive Breaches Required: $ConsecutiveBreaches"
Write-Log "Check Interval: $CheckIntervalSeconds Seconds"
Write-Log "============================================================"
$BreachCount = 0
while ($true)
{
try
{
# Get current CPU
$CpuUsage = (Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples[0].CookedValue
$CpuUsage = [math]::Round($CpuUsage,2)
Write-Log "Current CPU Usage: $CpuUsage%"
if ($CpuUsage -ge $CpuThreshold)
{
$BreachCount++
Write-Log "Threshold exceeded. Count=$BreachCount/$ConsecutiveBreaches"
}
else
{
if ($BreachCount -gt 0)
{
Write-Log "CPU returned to normal. Counter reset."
}
$BreachCount = 0
}
if ($BreachCount -ge $ConsecutiveBreaches)
{
Write-Log "High CPU condition confirmed."
if (Test-Path $TrendPath)
{
try
{
Write-Log "Running Trend Micro diagnostic collection."
Push-Location $TrendPath
$Process = Start-Process `
-FilePath "cmd.exe" `
-ArgumentList "/c $DiagCommand -d" `
-PassThru `
-Wait `
-WindowStyle Hidden
Pop-Location
Write-Log "Trend Micro diagnostic completed."
Write-Log "Diagnostic files location:"
Write-Log "C:\ProgramData\Trend Micro\Deep Security Agent\diag"
Write-Log "Exit Code: $($Process.ExitCode)"
}
catch
{
Write-Log "ERROR running Trend Micro diagnostic: $($_.Exception.Message)"
}
}
else
{
Write-Log "Trend Micro path not found: $TrendPath"
}
$BreachCount = 0
Write-Log "Entering cooldown period ($CooldownMinutes minutes)."
Start-Sleep -Seconds ($CooldownMinutes * 60)
}
}
catch
{
Write-Log "Monitoring Error: $($_.Exception.Message)"
}
Start-Sleep -Seconds $CheckIntervalSeconds
}
Comments
Post a Comment