Virtualisierung

Hyper-V: Offene Snapshots mit CheckMK überwachen (Local Check)

2 min Lesezeit

Für die Überwachung lokaler Snapshots auf einem Hyper-V-Host gibt es derzeit nur ein Plugin von CheckMK. Es gibt die offenen Snapshots über den Piggyback-Mechanismus zurück. Das funktioniert aber nur, wenn der VM-Name mit dem Hostnamen in CheckMK übereinstimmt. Ist das nicht der Fall, werden keine offenen Snapshots angezeigt.

Lösung

Das folgende Skript zeigt alle offenen Snapshots auf einem Host an.

  1. Speichere das Skript auf dem Hyper-V-Host als .ps1-Datei im Ordner C:\ProgramData\checkmk\agent\local, zum Beispiel als hyperv_snapshots.ps1.
  2. Führe in CheckMK eine erneute Serviceerkennung durch. Danach taucht der Check auf und kann folgende Ausgaben liefern:

Ausgabe des Snapshot-Checks in CheckMK

Ausgabe des Snapshot-Checks in CheckMK

Ab wann eine Warnung ausgegeben wird, legst du im Skript mit der Variable $warningSeconds fest. Maßgeblich ist der älteste Snapshot einer VM. Jüngere Snapshots lösen noch keine Warnung aus.

PowerShell
# 22.04.2024
# Sven Stromann - mail@sven-stromann.de
#
# Hyper-V Snapshot Check
#          
# Dieser Check prueft einen Hyper-V Host auf offene Snapshots
# Das Script muss als local Check unter "C:\ProgramData\checkmk\agent\local" abgelegt werden und wird bei der Service-Erkennung automatisch erkannt. 
# 
# Version 0.2
#
# Hyper-V Snapshot Check

$warningSeconds = 86400 #Warnung ab einem Tag
#$warningSeconds = 172800 #Warnung ab zwei Tage

#################################################################################################

# Get all VMs
$vms = Get-VM
$now = Get-Date

# Array to store VM names with open snapshots
$vmNamesWithSnapshots = @()

# Iterate through each VM
foreach ($vm in $vms) {
    # Get open snapshots for the current VM
    $snapshots = Get-VMSnapshot -VMName $vm.Name

    # Check if the VM has open snapshots
    if ($snapshots) {
        # Bei mehreren Snapshots zählt der älteste
        $oldest = ($snapshots | Sort-Object CreationTime | Select-Object -First 1).CreationTime
        $SnapshotAge = [System.Math]::Round(($now.Subtract($oldest).TotalSeconds), 0)

        if($SnapshotAge -gt $warningSeconds){

                # Add the VM name to the array
                $vmNamesWithSnapshots += $vm.Name
        }
    }
}

# Convert the array to a comma-separated string
$vmNamesString = $vmNamesWithSnapshots -join ", "

# Output the results
if ($vmNamesWithSnapshots) {
    $status = "1"
    $statusText = "Warning - " + "There are open Snapshots! [$vmNamesString]"
} else {
    $status = "0"
    $statusText = "OK - " + "No open snapshots found."

}

$stringToPost = $status + " " + "Snapshot" + " " + "-" + " " + $statusText
Write-Host $stringToPost

Tippe, um alle Artikel zu durchsuchen.

Nachricht schreiben

Die Daten nutze ich nur, um dir zu antworten. Mehr dazu im Datenschutz.