From fafcc5e6a050926db2efd521ccaad8e73c37c56d Mon Sep 17 00:00:00 2001 From: Madzone Date: Fri, 14 Aug 2026 22:00:07 +0200 Subject: [PATCH] Version 1.1 --- Stress/KILL-STRESS.ps1 | 29 ++++++++ Stress/README.md | 72 +++++++++++++++++++ Stress/SETUP-VMS.ps1 | 100 ++++++++++++++++++++++++++ Stress/START-STRESS.ps1 | 154 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 355 insertions(+) create mode 100644 Stress/KILL-STRESS.ps1 create mode 100644 Stress/README.md create mode 100644 Stress/SETUP-VMS.ps1 create mode 100644 Stress/START-STRESS.ps1 diff --git a/Stress/KILL-STRESS.ps1 b/Stress/KILL-STRESS.ps1 new file mode 100644 index 0000000..70fc4f1 --- /dev/null +++ b/Stress/KILL-STRESS.ps1 @@ -0,0 +1,29 @@ +[CmdletBinding()] +param([string]$SSHUser = "stressuser") + +Set-StrictMode -Version Latest +$BasePath = $PSScriptRoot +$KillFile = Join-Path $BasePath "KILL" +$LogPath = Join-Path $BasePath "Logs" +$LogFile = Join-Path $LogPath ("kill-{0}.log" -f (Get-Date -Format "yyyyMMdd-HHmmss")) +$VMs = @( + 113..120 | ForEach-Object { "10.52.70.$_" } + 113..120 | ForEach-Object { "10.52.68.$_" } +) + +New-Item -ItemType Directory -Path $LogPath -Force | Out-Null +New-Item -ItemType File -Path $KillFile -Force | Out-Null + +function Write-Log([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 +} + +Write-Log "NOT-AUS aktiviert. Kill-Datei: $KillFile" +foreach ($vm in $VMs) { + & ssh -o BatchMode=yes -o ConnectTimeout=5 "$SSHUser@$vm" "sudo -n /usr/bin/pkill -x stress-ng || true" 2>&1 | ForEach-Object { Write-Log "[$vm] $_" } + if ($LASTEXITCODE -eq 0) { Write-Log "[$vm] Stoppbefehl ausgeführt." } + else { Write-Log "WARNUNG: [$vm] nicht erreichbar oder Stopp fehlgeschlagen." } +} +Write-Log "NOT-AUS abgeschlossen. Vor einem Neustart Ursache prüfen und START-STRESS.ps1 mit -ResetKillSwitch starten." diff --git a/Stress/README.md b/Stress/README.md new file mode 100644 index 0000000..0ed67db --- /dev/null +++ b/Stress/README.md @@ -0,0 +1,72 @@ +# STRESS – Debian-VM-Stresstest + +Die Steuerung läuft auf dem Mac mit PowerShell 7 (`pwsh`) und spricht ausschließlich die 16 Debian-VMs per SSH an: + +- LINKS: `10.52.70.113` bis `10.52.70.120` +- RECHTS: `10.52.68.113` bis `10.52.68.120` + +## Voraussetzungen + +Auf jeder VM müssen `/usr/bin/stress-ng` und `/usr/bin/pkill` vorhanden sein. Der voreingestellte Benutzer ist `stressuser`; der SSH-Zugang per Schlüssel muss ohne Passwort und ohne interaktive Rückfrage funktionieren. + +Mit `sudo visudo` auf jeder VM freigeben: + +```text +stressuser ALL=(root) NOPASSWD: /usr/bin/stress-ng, /usr/bin/pkill +``` + +## Automatische Vorbereitung aller VMs + +Wenn auf allen VMs bereits derselbe Admin-Benutzer per SSH erreichbar ist und `sudo` verwenden darf, übernimmt `SETUP-VMS.ps1` die oben genannten Schritte. Beispiel mit dem Admin-Benutzer `admin`: + +```powershell +pwsh ./SETUP-VMS.ps1 -AdminUser admin +``` + +Falls der öffentliche Schlüssel an einem anderen Ort liegt: + +```powershell +pwsh ./SETUP-VMS.ps1 -AdminUser admin -PublicKeyFile ~/.ssh/mein_schluessel.pub +``` + +Das Skript verändert keine vCenter- oder ESXi-Systeme. Abhängig von der vorhandenen sudo-Konfiguration kann auf jeder VM einmal das Admin-Passwort abgefragt werden. Fehlgeschlagene VMs werden am Ende aufgelistet; ein erneuter Aufruf ist möglich. + +Vor dem echten Lauf einmal testen: + +```powershell +ssh stressuser@10.52.70.113 +``` + +## Start + +Im Ordner `STRESS`: + +```powershell +pwsh ./START-STRESS.ps1 +``` + +Der Standardlauf ist an den heutigen Zeitplan ab 09:00 gebunden. Bereits vergangene Stufen werden übersprungen und die aktuell passende Last wird gesetzt. Ist das planmäßige Ende am Folgetag um 09:00 schon vorbei, verlangt das Skript ein neues Startdatum: + +```powershell +pwsh ./START-STRESS.ps1 -StartDate "2026-08-15 09:00" +``` + +Zeitplan: 09:00 30 %, 10:00 40 %, 11:00 50 %, 12:00 60 %, 13:00 70 %, 14:00 80 %, 16:00 70 %, 17:00 60 %, Ende am Folgetag um 09:00. + +## NOT-AUS + +In einem zweiten Terminal: + +```powershell +pwsh ./KILL-STRESS.ps1 +``` + +Das legt zuerst die Datei `KILL` an und sendet anschließend den Stoppbefehl an alle VMs. Der Controller prüft die Datei spätestens alle zehn Sekunden. Wegen nicht erreichbarer VMs können einzelne Stopps fehlschlagen; deshalb Temperaturen und Prozesse anschließend kontrollieren. + +Nach Prüfung der Ursache kann ein neuer Lauf den NOT-AUS ausdrücklich zurücksetzen: + +```powershell +pwsh ./START-STRESS.ps1 -ResetKillSwitch -StartDate "2026-08-15 09:00" +``` + +Protokolle liegen im Unterordner `Logs`. diff --git a/Stress/SETUP-VMS.ps1 b/Stress/SETUP-VMS.ps1 new file mode 100644 index 0000000..3dfaad9 --- /dev/null +++ b/Stress/SETUP-VMS.ps1 @@ -0,0 +1,100 @@ +[CmdletBinding()] +param( + [Parameter(Mandatory)] + [string]$AdminUser, + + [string]$StressUser = "stressuser", + [string]$PublicKeyFile = (Join-Path $HOME ".ssh/id_ed25519.pub") +) + +Set-StrictMode -Version Latest +$ErrorActionPreference = "Stop" + +$VMs = @( + 113..120 | ForEach-Object { "10.52.70.$_" } + 113..120 | ForEach-Object { "10.52.68.$_" } +) + +if (-not (Get-Command ssh -ErrorAction SilentlyContinue)) { + throw "ssh wurde auf diesem Mac nicht gefunden." +} +if (-not (Test-Path -LiteralPath $PublicKeyFile)) { + throw "Öffentlicher SSH-Schlüssel fehlt: $PublicKeyFile`nErzeuge ihn zuerst mit: ssh-keygen -t ed25519" +} + +$publicKey = (Get-Content -LiteralPath $PublicKeyFile -Raw).Trim() +if ($publicKey -notmatch '^ssh-(ed25519|rsa|ecdsa-)') { + throw "Die Datei sieht nicht wie ein gültiger öffentlicher SSH-Schlüssel aus: $PublicKeyFile" +} +if ($StressUser -notmatch '^[a-z_][a-z0-9_-]*$') { + throw "Ungültiger Linux-Benutzername: $StressUser" +} + +$keyParts = $publicKey -split '\s+' +$publicKey = "$($keyParts[0]) $($keyParts[1])" +$quotedUser = "'$StressUser'" +$quotedKey = "'$publicKey'" + +$setupScript = @" +set -eu +export DEBIAN_FRONTEND=noninteractive + +apt-get update +apt-get install -y stress-ng openssh-server procps sudo +systemctl enable --now ssh + +if ! id $quotedUser >/dev/null 2>&1; then + useradd --create-home --shell /bin/bash $quotedUser +fi + +home_dir=`$(getent passwd $quotedUser | cut -d: -f6) +install -d -m 700 -o $quotedUser -g $quotedUser "`$home_dir/.ssh" +touch "`$home_dir/.ssh/authorized_keys" +if ! grep -Fqx -- $quotedKey "`$home_dir/.ssh/authorized_keys"; then + printf '%s\n' $quotedKey >> "`$home_dir/.ssh/authorized_keys" +fi +chown $quotedUser`:$quotedUser "`$home_dir/.ssh/authorized_keys" +chmod 600 "`$home_dir/.ssh/authorized_keys" + +printf '%s ALL=(root) NOPASSWD: /usr/bin/stress-ng, /usr/bin/pkill\n' $quotedUser > /etc/sudoers.d/stress-test +chmod 440 /etc/sudoers.d/stress-test +visudo -cf /etc/sudoers.d/stress-test + +test -x /usr/bin/stress-ng +test -x /usr/bin/pkill +echo SETUP_OK +"@ + +$payload = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($setupScript)) +$failed = [System.Collections.Generic.List[string]]::new() + +Write-Host "Bereite $($VMs.Count) Debian-VMs vor." +Write-Host "Je nach sudo-Konfiguration kann pro VM ein Admin-Passwort abgefragt werden." + +foreach ($vm in $VMs) { + Write-Host "" + Write-Host "[$vm] Einrichtung startet ..." -ForegroundColor Cyan + $remoteCommand = "printf '%s' '$payload' | base64 -d | sudo bash" + & ssh -tt -o ConnectTimeout=10 "$AdminUser@$vm" $remoteCommand + if ($LASTEXITCODE -ne 0) { + Write-Warning "[$vm] Einrichtung fehlgeschlagen." + $failed.Add($vm) + continue + } + + & ssh -o BatchMode=yes -o ConnectTimeout=8 "$StressUser@$vm" "sudo -n /usr/bin/stress-ng --version >/dev/null && sudo -n /usr/bin/pkill --version >/dev/null && echo PRECHECK_OK" + if ($LASTEXITCODE -ne 0) { + Write-Warning "[$vm] Einrichtung lief, aber der abschließende Zugangstest schlug fehl." + $failed.Add($vm) + } else { + Write-Host "[$vm] Fertig und geprüft." -ForegroundColor Green + } +} + +Write-Host "" +if ($failed.Count -gt 0) { + Write-Host "Fehlgeschlagene VMs: $($failed -join ', ')" -ForegroundColor Red + exit 1 +} + +Write-Host "Alle 16 VMs wurden erfolgreich vorbereitet." -ForegroundColor Green diff --git a/Stress/START-STRESS.ps1 b/Stress/START-STRESS.ps1 new file mode 100644 index 0000000..5285a1a --- /dev/null +++ b/Stress/START-STRESS.ps1 @@ -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