Version 1.1
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
[CmdletBinding()]
|
||||
param(
|
||||
[datetime]$StartDate = (Get-Date).Date.AddHours(9),
|
||||
[string]$SSHUser = "stressuser",
|
||||
[switch]$ResetKillSwitch
|
||||
)
|
||||
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = "Stop"
|
||||
|
||||
$BasePath = $PSScriptRoot
|
||||
$KillFile = Join-Path $BasePath "KILL"
|
||||
$LogPath = Join-Path $BasePath "Logs"
|
||||
$RunStamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
||||
$LogFile = Join-Path $LogPath "stress-$RunStamp.log"
|
||||
|
||||
$VMs = @(
|
||||
113..120 | ForEach-Object { "10.52.70.$_" }
|
||||
113..120 | ForEach-Object { "10.52.68.$_" }
|
||||
)
|
||||
|
||||
$Schedule = @(
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(0); Load = 30 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(1); Load = 40 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(2); Load = 50 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(3); Load = 60 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(4); Load = 70 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(5); Load = 80 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(7); Load = 70 }
|
||||
[pscustomobject]@{ Offset = [timespan]::FromHours(8); Load = 60 }
|
||||
)
|
||||
$EndTime = $StartDate.AddDays(1)
|
||||
|
||||
New-Item -ItemType Directory -Path $LogPath -Force | Out-Null
|
||||
|
||||
function Write-Log {
|
||||
param([string]$Message)
|
||||
$line = "{0} {1}" -f (Get-Date -Format "yyyy-MM-dd HH:mm:ss"), $Message
|
||||
Write-Host $line
|
||||
Add-Content -LiteralPath $LogFile -Value $line
|
||||
}
|
||||
|
||||
function Invoke-VMCommand {
|
||||
param(
|
||||
[string]$VM,
|
||||
[string]$Command,
|
||||
[switch]$Quiet
|
||||
)
|
||||
$output = & ssh -o BatchMode=yes -o ConnectTimeout=8 "$SSHUser@$VM" $Command 2>&1
|
||||
$exitCode = $LASTEXITCODE
|
||||
if (-not $Quiet -and $output) {
|
||||
$output | ForEach-Object { Write-Log "[$VM] $_" }
|
||||
}
|
||||
return $exitCode
|
||||
}
|
||||
|
||||
function Stop-AllStress {
|
||||
param([string]$Reason)
|
||||
Write-Log "STOP: $Reason"
|
||||
foreach ($vm in $VMs) {
|
||||
$rc = Invoke-VMCommand -VM $vm -Command "sudo -n /usr/bin/pkill -x stress-ng || true" -Quiet
|
||||
if ($rc -eq 0) { Write-Log "[$vm] stress-ng beendet bzw. war nicht aktiv." }
|
||||
else { Write-Log "WARNUNG: [$vm] konnte nicht gestoppt werden (SSH-Code $rc)." }
|
||||
}
|
||||
}
|
||||
|
||||
function Assert-NoKillSwitch {
|
||||
if (Test-Path -LiteralPath $KillFile) {
|
||||
throw "NOT-AUS aktiv: $KillFile"
|
||||
}
|
||||
}
|
||||
|
||||
function Wait-Until {
|
||||
param([datetime]$Time)
|
||||
while ((Get-Date) -lt $Time) {
|
||||
Assert-NoKillSwitch
|
||||
$seconds = [math]::Min(10, [math]::Max(1, ($Time - (Get-Date)).TotalSeconds))
|
||||
Start-Sleep -Seconds $seconds
|
||||
}
|
||||
}
|
||||
|
||||
function Set-StressLoad {
|
||||
param([int]$Load)
|
||||
Assert-NoKillSwitch
|
||||
Write-Log "Setze CPU-Ziellast auf allen VMs auf $Load %."
|
||||
foreach ($vm in $VMs) {
|
||||
$command = "sudo -n /usr/bin/pkill -x stress-ng 2>/dev/null || true; nohup sudo -n /usr/bin/stress-ng --cpu 0 --cpu-load $Load --timeout 26h >/tmp/stress-ng.log 2>&1 </dev/null &"
|
||||
$rc = Invoke-VMCommand -VM $vm -Command $command -Quiet
|
||||
if ($rc -eq 0) { Write-Log "[$vm] $Load % gestartet." }
|
||||
else { Write-Log "FEHLER: [$vm] Start fehlgeschlagen (SSH-Code $rc)." }
|
||||
}
|
||||
}
|
||||
|
||||
$completedNormally = $false
|
||||
try {
|
||||
if ($ResetKillSwitch -and (Test-Path -LiteralPath $KillFile)) {
|
||||
Remove-Item -LiteralPath $KillFile -Force
|
||||
Write-Log "Vorhandenen NOT-AUS auf ausdrücklichen Wunsch zurückgesetzt."
|
||||
}
|
||||
Assert-NoKillSwitch
|
||||
|
||||
if (-not (Get-Command ssh -ErrorAction SilentlyContinue)) {
|
||||
throw "ssh wurde auf diesem Mac nicht gefunden."
|
||||
}
|
||||
if ((Get-Date) -ge $EndTime) {
|
||||
throw "Der gewählte Testzeitraum ist bereits beendet. Nutze -StartDate mit dem gewünschten 09:00-Start."
|
||||
}
|
||||
|
||||
Write-Log "Precheck für $($VMs.Count) Debian-VMs; Start $StartDate, Ende $EndTime."
|
||||
$failed = @()
|
||||
foreach ($vm in $VMs) {
|
||||
$cmd = "command -v /usr/bin/stress-ng >/dev/null && sudo -n /usr/bin/stress-ng --version >/dev/null && sudo -n /usr/bin/pkill --version >/dev/null"
|
||||
if ((Invoke-VMCommand -VM $vm -Command $cmd -Quiet) -ne 0) { $failed += $vm }
|
||||
else { Write-Log "[$vm] Precheck OK." }
|
||||
}
|
||||
if ($failed.Count -gt 0) {
|
||||
throw "Precheck fehlgeschlagen: $($failed -join ', '). Kein Stress-Test wurde gestartet."
|
||||
}
|
||||
|
||||
if ((Get-Date) -lt $StartDate) {
|
||||
Write-Log "Warte bis zum Startzeitpunkt $StartDate."
|
||||
Wait-Until -Time $StartDate
|
||||
}
|
||||
|
||||
for ($stageIndex = 0; $stageIndex -lt $Schedule.Count; $stageIndex++) {
|
||||
$stage = $Schedule[$stageIndex]
|
||||
$stageTime = $StartDate.Add($stage.Offset)
|
||||
$nextTime = if ($stageIndex -eq ($Schedule.Count - 1)) { $EndTime } else {
|
||||
$StartDate.Add($Schedule[$stageIndex + 1].Offset)
|
||||
}
|
||||
if ((Get-Date) -ge $nextTime) {
|
||||
Write-Log "Überspringe vergangenen Schritt $stageTime ($($stage.Load) %)."
|
||||
continue
|
||||
}
|
||||
if ((Get-Date) -lt $stageTime) { Wait-Until -Time $stageTime }
|
||||
Set-StressLoad -Load $stage.Load
|
||||
}
|
||||
|
||||
Wait-Until -Time $EndTime
|
||||
Stop-AllStress -Reason "Planmäßiges Ende erreicht."
|
||||
$completedNormally = $true
|
||||
}
|
||||
catch {
|
||||
Write-Log "ABBRUCH: $($_.Exception.Message)"
|
||||
Stop-AllStress -Reason "Sicherheitsstopp nach Abbruch oder NOT-AUS."
|
||||
throw
|
||||
}
|
||||
finally {
|
||||
if (-not $completedNormally) {
|
||||
Write-Log "Controller beendet; Sicherheitsstopp wurde ausgeführt."
|
||||
}
|
||||
}
|
||||
|
||||
Write-Log "Test erfolgreich abgeschlossen."
|
||||
Reference in New Issue
Block a user