# ---------------------------------------------------------------------- # File: check_all_csv_frespace.ps1 # Description: NRPE Nagios Check for Windows Server 2008/2012 Hyper-V Cluster for Checking CSV Space (Cluster Shared Volumes). # # Checks: Hyper-V Cluster Shared Volume Space # # Required: Windows Server 2008/2012 with the following Cmdlets # Get-ClusterSharedVolume # # Notes: - Support all exit codes # - This script can easily be broken out into separate checks # # Variables: - Set $warningperc to customize High Warning # - Set $criticalperc to customize Critical Warning # # ---------------------------------------------------------------------- # Author: Whutest # Edited: Thiago Lorenzzoni # Date: August 19, 2019 # Modified: August 19, 2019 # Version: 2.1 # ---------------------------------------------------------------------- $returnStateOK = 0 $returnStateWarning = 1 $returnStateCritical = 2 $returnStateUnknown = 3 Set-ExecutionPolicy RemoteSigned Import-Module FailoverClusters $warningperc = $args[0] $criticalperc = $args[1] $csvolsall = $env:temp+ "\csvolsall.txt" $csvolsperf = $env:temp+ "\csvolsperf.txt" $csvolswarn = $env:temp+ "\csvolswarn.txt" $csvolscrit = $env:temp+ "\csvolscrit.txt" $csvolsOK = $env:temp+ "\csvolsOK.txt" if (!$warningperc) { $warningperc = 5} if (!$criticalperc) { $criticalperc = 2} if ($warningperc -lt $criticalperc) { Write-Host "UNKNOWN: Warning must be more than Critical!" exit $returnStateUnknown } If ((Test-Path -path $csvolsall) -eq $True) { Clear-Content $csvolsall } If ((Test-Path -path $csvolsperf) -eq $True) { Clear-Content $csvolsperf } If ((Test-Path -path $csvolscrit) -eq $True) { Clear-Content $csvolscrit } If ((Test-Path -path $csvolswarn) -eq $True) { Clear-Content $csvolswarn } If ((Test-Path -path $csvolsOK) -eq $True) { Clear-Content $csvolsOK } $CSVs = Get-ClusterSharedVolume | Where-Object { $_.State -eq "Online" } | select -ExpandProperty Name foreach ( $CSV in $CSVs ) { $print_string=""; $csvinfo= Get-ClusterSharedVolume "$CSV" | select -Property Name -ExpandProperty SharedVolumeInfo $print_string= "Volume " +$CSV+ ": Total " + $([System.Math]::Round($csvinfo.Partition.Size/1024/1024/1024, 2)) + " GB Used " + $([System.Math]::Round(($csvinfo.Partition.UsedSpace/1024/1024/1024), 2)) + " GB Free " + $([System.Math]::Round(($csvinfo.Partition.FreeSpace/1024/1024/1024), 2)) + " GB (" + $([System.Math]::Round($csvinfo.Partition.PercentFree, 2)) + "%)" ac $csvolsall $print_string $print_string2= "'used_" +$CSV+ "'=" + $csvinfo.Partition.UsedSpace ac $csvolsperf $print_string2 if ($csvinfo | select -ExpandProperty Partition | Where-Object { $_.PercentFree -le $criticalperc }) { $print_string= "Volume " +$CSV+ ": Total " + $([System.Math]::Round($csvinfo.Partition.Size/1024/1024/1024, 2)) + " GB Used " + $([System.Math]::Round(($csvinfo.Partition.UsedSpace/1024/1024/1024), 2)) + " GB Free " + $([System.Math]::Round(($csvinfo.Partition.FreeSpace/1024/1024/1024), 2)) + " GB (" + $([System.Math]::Round($csvinfo.Partition.PercentFree, 2)) + "%)" ac $csvolscrit $print_string } ElseIf ($csvinfo | select -ExpandProperty Partition | Where-Object { ($_.PercentFree -gt $criticalperc) -and ( $_.PercentFree -lt $warningperc) }) { $print_string= "Volume " +$CSV+ ": Total " + $([System.Math]::Round($csvinfo.Partition.Size/1024/1024/1024, 2)) + " GB Used " + $([System.Math]::Round(($csvinfo.Partition.UsedSpace/1024/1024/1024), 2)) + " GB Free " + $([System.Math]::Round(($csvinfo.Partition.FreeSpace/1024/1024/1024), 2)) + " GB (" + $([System.Math]::Round($csvinfo.Partition.PercentFree, 2)) + "%)" ac $csvolswarn $print_string } ElseIf ($csvinfo | select -ExpandProperty Partition | Where-Object { $_.PercentFree -ge $warningperc }) { $print_string= "Volume " +$CSV+ ": Total " + $([System.Math]::Round($csvinfo.Partition.Size/1024/1024/1024, 2)) + " GB Used " + $([System.Math]::Round(($csvinfo.Partition.UsedSpace/1024/1024/1024), 2)) + " GB Free " + $([System.Math]::Round(($csvinfo.Partition.FreeSpace/1024/1024/1024), 2)) + " GB (" + $([System.Math]::Round($csvinfo.Partition.PercentFree, 2)) + "%)" ac $csvolsOK $print_string } } If ( ((Test-Path -path $csvolscrit) -eq $True) -and ((Test-Path -path $csvolswarn) -eq $True) -and ((Get-Content $csvolswarn | Measure-Object -Line | Select-Object -ExpandProperty Lines) -gt "0") -and ((Get-Content $csvolscrit | Measure-Object -Line | Select-Object -ExpandProperty Lines) -gt "0")) { $crits=Get-Content $csvolscrit $warns=Get-Content $csvolswarn $all=Get-Content $csvolsall $perf=Get-Content $csvolsperf $print_string="CRITICAL: " +$crits $print_string2="WARNING: " +$warns Write-Host $print_string " $print_string2" Write-Output $all Write-Output "| $perf" exit $returnStateCritical } ElseIf ( ((Test-Path -path $csvolscrit) -eq $True) -and (( Get-Content $csvolscrit | Measure-Object -Line | Select-Object -ExpandProperty Lines) -gt "0")) { $crits=Get-Content $csvolscrit $all=Get-Content $csvolsall $perf=Get-Content $csvolsperf $print_string="CRITICAL: " +$crits Write-Host $print_string Write-Output $all Write-Output "| $perf" exit $returnStateCritical } ElseIf ( ((Test-Path -path $csvolswarn) -eq $True) -and (( Get-Content $csvolswarn | Measure-Object -Line | Select-Object -ExpandProperty Lines) -gt "0")) { $warns=Get-Content $csvolswarn $all=Get-Content $csvolsall $perf=Get-Content $csvolsperf $print_string="WARNING: " +$warns Write-Host $print_string Write-Output $all Write-Output "| $perf" exit $returnStateWarning } ElseIf ( ((Test-Path -path $csvolscrit) -eq $False) -or (( Get-Content $csvolscrit | Measure-Object -Line | Select-Object -ExpandProperty Lines) -eq "0") -and ((Test-Path -path $csvolswarn) -eq $False) -or ((Get-Content $csvolswarn | Measure-Object -Line | Select-Object -ExpandProperty Lines) -eq "0")) { $OKs=Get-Content $csvolsOK $all=Get-Content $csvolsall $perf=Get-Content $csvolsperf $print_string="OK: All volumes are ok" Write-Host $print_string Write-Output $all Write-Output "| $perf" exit $returnStateOK }