Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions check_vsphere/check_vsphere.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The critical threshold

.PARAMETER Metric

The metric to collect and check (CPU or Memory)
The metric to collect and check (CPU, Memory or Datastore)

.PARAMETER Mode

Expand Down Expand Up @@ -75,7 +75,7 @@ param(
[string]$Cluster,
[string]$CredentialFile,
[string]$Critical = 75,
[ValidateSet("CPU", "Memory")][string]$Metric = 'CPU',
[ValidateSet("CPU", "Memory", "Datastore")][string]$Metric = 'CPU',
[ValidateSet("Cluster","Host")][string]$Mode = 'Cluster',
[string]$Password, # Included for compatibility with Powershell on Linux
[Parameter(Mandatory=$true)][string]$Server,
Expand All @@ -93,6 +93,8 @@ $totalCPU
$usedCPU
$totalMemory
$usedMemory
$datastoreTotalGB
$datastoreUsedGB


# Connect using provided credentials, else assume that we're binding with logged in user
Expand Down Expand Up @@ -127,12 +129,16 @@ Switch($Mode) {

'Host' {
$esxHost = Get-VMHost -Name $VMHost
$esxDatastore = Get-VMHost -Name $VMHost | Get-Datastore
$totalCPU = $esxHost.CpuTotalMhz
$usedCPU = $esxHost.CpuUsageMhz
$totalMemory = $esxHost.MemoryTotalGB
$usedMemory = $esxHost.MemoryUsageGB
# $datastoreName = $esxDatastore.Name
$datastoreTotalGB = $esxDatastore.CapacityGB
$datastoreFreeGB = $esxDatastore.FreeSpaceGB
$datastoreUsedGB = $esxDatastore.CapacityGB - $datastoreFreeGB
}

}

$metricRawTotal
Expand All @@ -141,6 +147,8 @@ $metricRawFree
$metricPercentFree
$metricPercentUsed
$metricUnit
$metricDatastoreTotalGB
$metricDatastoreUsedGB

Switch($Metric) {

Expand All @@ -158,13 +166,21 @@ Switch($Metric) {
$metricUnit = "GB"
}

'Datastore' {
$Metric = 'Datastore' # Fix case insensitivity
$metricRawTotal = $datastoreTotalGB
$metricRawUsed = $datastoreUsedGB
$metricUnit = "GB"
}
}

$metricRawFree = $metricRawTotal - $metricRawUsed
$metricPercentUsed = $metricRawUsed / $metricRawTotal * 100
$metricPercentFree = ($metricRawTotal - $metricRawUsed) / $metricRawTotal * 100
$metricRawTotal = [math]::round($metricRawTotal,2)
$metricRawUsed = [math]::round($metricRawUsed,2)
$metricRawFree = [math]::round(($metricRawTotal - $metricRawUsed),2)
$metricPercentUsed = [math]::round(($metricRawUsed / $metricRawTotal * 100),2)
$metricPercentFree = [math]::round((($metricRawTotal - $metricRawUsed) / $metricRawTotal * 100),2)

$outputMessage = 'OK' # Printed to stdout, OK is overridden if needed
$outputMessage = "$Metric OK: $metricRawUsed$metricUnit of $metricRawTotal$metricUnit ($metricPercentUsed%) Used" # Printed to stdout, OK is overridden if needed
$exitCode # Script exit code

#TODO: handle ranges
Expand Down