# # PowerCLI script for monitoring old VM snaphots in a datacenter. # Author: Haukur Kristinsson # # Example commandline in nsc.ini (for NRPE++): # command[check_REYDatacenterSnaphots]=C:\Users\haukurk>powershell -PSConsoleFile "c:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& 'c:\ReportsCLI\REYSnapshotNagios.ps1'" # Where check_REYDatacenterSnaphots is the command, vim.psc1 the console file, and REYSnapshotNagios.ps1 this script. # # This is nothing fancy, but I decided to share it with you.. # $server = "localhost" #vCenter server.. $user = "nagios" # user $pass = "xxxx" # pass $datacenter = "**Samskip_IS**" # datacenter name $criticalSnaphostDays = -5 # how many days can a snapshot last? Connect-VIServer -Server $server -Protocol https -User $user -Password $pass #Start with 0 snapshots :-) $oldSnapshots = 0 # Fetch snapshots from the datacenter. Get-View -ViewType VirtualMachine -Property Name,Snapshot -SearchRoot(Get-Datacenter $datacenter | get-view).MoRef ` -Filter @{"Snapshot"="VMware.Vim.VirtualMachineSnapshotInfo"} | foreach-object { $vmname = $_.Name (Get-View -Id $_.MoRef -Property Snapshot).Snapshot.RootSnapshotList | foreach-object { If ($_.CreateTime -lt (get-date).adddays($criticalSnaphostDays)) { # Lets increment the integer. $oldSnapshots++ } } } # Return Values for NRPE: # - OK (0) $exitOK = 0 # - WARNING (1) $exitWarn = 1 # - Critial (2) $exitCritical = 2 # - UNKNOWN (3) $exitUnknown = 3 # Exit with approriate exit code. if ($oldSnapshots -eq 0) { Write-Host "OK - $oldSnapshots snapshots older than $criticalSnaphostDays" exit $exitOK } if ($oldSnapshots -eq 1) { Write-Host "WARNING - $oldSnapshots snapshots older than $criticalSnaphostDays" exit $exitCritical } # Something went wrong... Write-Host "UNKNOWN script state" exit $returnStateUnknown