Skip to content

Instantly share code, notes, and snippets.

@vMarkusK
Last active April 4, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vMarkusK/a1cff3440b4cf7867837720d7d433b34 to your computer and use it in GitHub Desktop.
Save vMarkusK/a1cff3440b4cf7867837720d7d433b34 to your computer and use it in GitHub Desktop.
Advanced Sensor will Report vCenter Statistics about Resource usage.
<#
.SYNOPSIS
PRTG vCenter Advanced Sensor
.DESCRIPTION
Advanced Sensor will Report vCenter Statistics about Resource usage.
.EXAMPLE
PRTG-vCenter.ps1 -VIServer vCenter.lan.local -Check DS
.PARAMETER -VIServer vCenter.lan.local
Specify the vCenter Host FQDN
(vCenter.lan.local).
.PARAMETER -Check DS
Specify the vCenter Check
(DS, Cluster).
.Notes
NAME: PRTG-vCenter.ps1
LASTEDIT: 04/04/2016
VERSION: 1.0
KEYWORDS: PRTG-vCenter
.Link
https://mycloudrevolution.wordpress.com/
#Requires PS -Version 2.0
#>
[cmdletbinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[string] $VIServer,
[Parameter(Position=1, Mandatory=$false)]
[string] $Check
)
# Start Load VMware Snapin (if not already loaded)
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
if (!(Add-PSSnapin -PassThru VMware.VimAutomation.Core)) {
# Error out if loading fails
Write-Error "ERROR: Cannot load the VMware Snapin. Is the PowerCLI installed?"
Exit
}
}
# End Load VMware Snapin (if not already loaded)
# Start Global Definitions
#Datastore to Ignore in DS Check
$DatastoreIgnore = "esx"
#Datastore to Ignore in Cluster Check
$ClusterIgnore = "STANDBY"
#Max CPU usage for non HA cluster
$limitResourceCPUClusNonHA = 0.6
#Max MEM usage for non HA cluster
$limitResourceMEMClusNonHA = 0.6
# End Global Definitions
# Start vCenter Connection
Write-Host "Starting to Process vCenter Connection to " $VIServer " ..."-ForegroundColor Magenta
$OpenConnection = $global:DefaultVIServers | where { $_.Name -eq $VIServer }
if($OpenConnection.IsConnected) {
Write-Host "vCenter is Already Connected..." -ForegroundColor Yellow
$VIConnection = $OpenConnection
} else {
Write-Host "Connecting vCenter..."
$VIConnection = Connect-VIServer -Server $VIServer
}
if (-not $VIConnection.IsConnected) {
Write-Error "Error: vCenter Connection Failed"
Exit
}
# End vCenter Connection
If ($Check -eq "DS") {
New-VIProperty -Name PercentFree -ObjectType Datastore -Value {
param($ds)
[math]::Round(((100 * ($ds.FreeSpaceMB)) / ($ds.CapacityMB)),0)
} -Force | Out-Null
$Datastores = Get-Datastore | where {$_.State -eq "Available" -and $_.Accessible -eq "True"}
$OutputDatastores = @($Datastores | Where-Object {$_.Name -notmatch $DatastoreIgnore} | Select-Object Name, PercentFree| Sort-Object Name)
Write-Host "<prtg>"
foreach ($OutputDatastore in $OutputDatastores){
$Name = "DS - " + $OutputDatastore.Name
$Free = $OutputDatastore.PercentFree
Write-Host "<result>"
"<channel>$Name</channel>"
"<value>$Free</value>"
"<unit>Percent</unit>"
"<showChart>1</showChart>"
"<showTable>1</showTable>"
"<LimitMinWarning>20</LimitMinWarning>"
"<LimitMinError>10</LimitMinError>"
"<LimitMode>1</LimitMode>"
"</result>"
}
Write-Host "</prtg>"
# $OutputDatastores | ft -AutoSize
}
If ($Check -eq "Cluster") {
$VM = Get-VM
$clusviews = Get-View -ViewType ClusterComputeResource | Where-Object {$_.Name -notmatch $ClusterIgnore}
$capacityinfo = @()
foreach ($cluv in ($clusviews | Where {$_.Summary.NumHosts -gt 0 } | Sort Name)) {
if ( $cluv.Configuration.DasConfig.Enabled -eq $true ) {
$DasRealCpuCapacity = $cluv.Summary.EffectiveCpu - (($cluv.Summary.EffectiveCpu * $cluv.Configuration.DasConfig.FailoverLevel)/$cluv.Summary.NumHosts)
$DasRealMemCapacity = $cluv.Summary.EffectiveMemory - (($cluv.Summary.EffectiveMemory * $cluv.Configuration.DasConfig.FailoverLevel)/$cluv.Summary.NumHosts)
} else {
$DasRealCpuCapacity = $cluv.Summary.EffectiveCpu * $limitResourceCPUClusNonHA
$DasRealMemCapacity = $cluv.Summary.EffectiveMemory * $limitResourceMEMClusNonHA
}
$cluvmlist = $VM | where { $cluv.Host -contains $_.VMHost.Id }
#CPU
$CluCpuUsage = (get-view $cluv.ResourcePool).Summary.runtime.cpu.OverallUsage
$CluCpuUsageAvg = $CluCpuUsage
if ($cluvmlist -and $cluv.host -and $CluCpuUsageAvg -gt 0){
$VmCpuAverage = $CluCpuUsageAvg/($cluvmlist.count)
$CpuVmLeft = [math]::round(($DasRealCpuCapacity-$CluCpuUsageAvg)/$VmCpuAverage,0)
}
elseif ($CluCpuUsageAvg -eq 0) {$CpuVmLeft = "N/A"}
else {$CpuVmLeft = 0}
#MEM
$CluMemUsage = (get-view $cluv.ResourcePool).Summary.runtime.memory.OverallUsage
$CluMemUsageAvg = $CluMemUsage/1MB
if ($cluvmlist -and $cluv.host -and $CluMemUsageAvg -gt 100){
$VmMemAverage = $CluMemUsageAvg/(Get-Cluster $cluv.name|Get-VM).count
$MemVmLeft = [math]::round(($DasRealMemCapacity-$CluMemUsageAvg)/$VmMemAverage,0)
}
elseif ($CluMemUsageAvg -lt 100) {$CluMemUsageAvg = "N/A"}
else{$MemVmLeft = 0}
# vCPU to pCPU ratio
if ($cluvmlist){
$vCPUpCPUratio = [math]::round(($cluvmlist|Measure-Object -Sum -Property NumCpu).sum / $cluv.summary.NumCpuThreads,0)
}
else { $vCPUpCPUratio = "0 (vCPU < pCPU)"}
$clucapacity = [PSCustomObject] @{
ClusterName = $cluv.name
"Estimated Num VM Left (CPU)" = $CpuVmLeft
"Estimated Num VM Left (MEM)" = $MemVmLeft
"vCPU/pCPU ratio" = $vCPUpCPUratio
}
$capacityinfo += $clucapacity
}
Write-Host "<prtg>"
foreach ($capacity in $capacityinfo){
$Name = $capacity.ClusterName + " - VM Left (CPU)"
$Value = $capacity."Estimated Num VM Left (CPU)"
Write-Host "<result>"
"<channel>$Name</channel>"
"<value>$Value</value>"
"<showChart>1</showChart>"
"<showTable>1</showTable>"
"<LimitMinWarning>10</LimitMinWarning>"
"<LimitMinError>5</LimitMinError>"
"<LimitMode>1</LimitMode>"
"</result>"
$Name = $capacity.ClusterName + " - VM Left (MEM)"
$Value = $capacity."Estimated Num VM Left (MEM)"
Write-Host "<result>"
"<channel>$Name</channel>"
"<value>$Value</value>"
"<showChart>1</showChart>"
"<showTable>1</showTable>"
"<LimitMinWarning>10</LimitMinWarning>"
"<LimitMinError>5</LimitMinError>"
"<LimitMode>1</LimitMode>"
"</result>"
$Name = $capacity.ClusterName + " - vCPU/pCPU"
$Value = $capacity."vCPU/pCPU ratio"
Write-Host "<result>"
"<channel>$Name</channel>"
"<value>$Value</value>"
"<showChart>1</showChart>"
"<showTable>1</showTable>"
"<LimitMaxWarning>3</LimitMaxWarning>"
"<LimitMaxError>4</LimitMaxError>"
"<LimitMode>1</LimitMode>"
"</result>"
}
Write-Host "</prtg>"
# $capacityinfo | Sort ClusterName | ft -AutoSize
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment