Skip to content

Instantly share code, notes, and snippets.

@vMarkusK
Created April 11, 2019 13:11
Show Gist options
  • Save vMarkusK/996b6cfce7578f66ad5036287121247b to your computer and use it in GitHub Desktop.
Save vMarkusK/996b6cfce7578f66ad5036287121247b to your computer and use it in GitHub Desktop.
vCenter VAMI Health for Influx API
# External References
## https://github.com/rumart/vSpherePerfData/blob/master/vcsa/appliance_health.ps1
# Config
$vCenter = "<vCenter FQDN>"
$Metrics = "applmgmt","database-storage","load","mem","software-packages","storage","swap","system"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Create Credentials
#$Credentials = Get-Credential -Message "Vami Crendetials"
#$Credentials | Export-CliXml -Path "$($env:USERPROFILE)\VamiCred.xml"
# Load Credentials
$Credentials = Import-CliXml -Path "$($env:USERPROFILE)\VamiCred.xml"
$UserName = $Credentials.UserName
$Password = $Credentials.GetNetworkCredential().Password
# Function for generating correct timestamp for influx input
function Get-DBTimestamp($timestamp = (get-date)){
if($timestamp -is [system.string]){
$timestamp = [datetime]::ParseExact($timestamp,'dd.MM.yyyy HH:mm:ss',$null)
}
return $([long][double]::Parse((get-date $($timestamp).ToUniversalTime() -UFormat %s)) * 1000 * 1000 * 1000)
}
# RestAPI Calls
$BaseUri = "https://$vcenter/rest/"
$SessionUri = $BaseUri + "com/vmware/cis/session"
## Connect
$auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($UserName + ':' + $Password))
Remove-Variable UserName, Password, Credentials
$header = @{
'Authorization' = "Basic $auth"
}
try {
$Token = Invoke-RestMethod -Method Post -Headers $header -Uri $SessionUri -ErrorAction Stop
}
catch {
Write-Host "StatusCode:" $_.Exception.Response.StatusCode.value__
Exit
}
$SessionHeader = @{'vmware-api-session-id' = $($Token.Value)}
## Get Health
$LastCheckUri = $BaseUri + "appliance/health/system/lastcheck"
$LastCheckResponse = Invoke-RestMethod -Method Get -Headers $SessionHeader -Uri $LastCheckUri
$Result = @()
foreach($Check in $Metrics){
$uri = $BaseUri + "appliance/health/$Check"
$response = Invoke-RestMethod -Method Get -Headers $sessionheader -Uri $uri
$value = $response.value
switch($value){
"green" {$val = 0}
"orange" {$val = 1}
"red" {$val = 2}
"gray" {$val = 9}
"unknown" {$val = 9}
default {$val = 9}
}
$timestamp = Get-DBTimestamp (get-date $LastCheckResponse.value)
$measurement = "health_$Check"
$Result += "$measurement,server=$vcenter text=""$value"",value=$val $timestamp"
}
$Result
# Post data to Influx API
#$postUri = "http://$influxServer" + ":$influxPort/write?db=$database"
#Invoke-RestMethod -Method Post -Uri $postUri -Body ($tbl -join "`n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment