#vCenter settings
$vCenter = "VCENTER-IP"
$user = "USERNAME"
$password = "PASSWORD"
$cluster = "YOUR-CLUSTER"
#Graphite server
$remoteHost = "GRAPHITE-SERVER-IP"
#Socket to send results to Graphite server
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, 2003)
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
Write-Host "Connected"
#Connect to vCenter
Connect-VIServer -Server $vCenter -User $user -Password $password
#Get all ESXi hosts from Cluster
$esxhosts = Get-VMHost -Location $cluster | Sort
#Collect stats foreach ESXi server and bring it in a Graphite compatible format
foreach ($esxName in $esxhosts){
$allstats = Get-Stat -Entity (Get-VMHost $esxName) -MaxSamples 1 -Realtime -Stat cpu.usage.average,disk.usage.average,net.usage.average | Sort
Write-Host $esxName
foreach($stat in $allstats){
#Get Timestamp of stats and convert to UNIX timestamp
$time = $stat.Timestamp
$date = [int][double]::Parse((Get-Date -Date $time -UFormat %s))
#Filter only average stats (Stats for CPU's are available foreach CPU and as average of all CPU's)
$instance = $stat.Instance
if($instance -eq [DBNull]::Value){
#create a base for the graphite tree
$base = "dc.vmware.prod."
#remove the .usage.average
$type = (($stat.MetricId).Replace(".usage.average",""))
#remove the domain from the esxi name
$name = (($esxName.Name).Replace(" ","")).Replace(".your-domain.de","")
$value = $stat.Value
#build the graphite compatible string
$result = "$base$name.$type $value $date"
#Console output just for testing
Write-Host "Sent Result: $result"
#send result to graphite server
$writer.WriteLine($result)
$writer.Flush()
}
}
Write-Host " "
}
## Close the streams
$writer.Close()
$stream.Close()
#disconnect from vcenter
Disconnect-VIServer -Server $vCenter -Confirm:$false -Force
Write-Host "Done"
===
Script works without err... but the data does not shows up in graphite
Anyone has any idea