viDB.ps1 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # viDB.ps1 - Virtual Infrastructure Dashboard for VMware
  2. # Date : 2nd December 2017
  3. # Created By : mrkips (Cybergavin)
  4. # Description : A rudimentary script that collects data from vCenters and streams the date to a graphite server for display
  5. # on Grafana dashboards.
  6. # Tested on Windows Server 2019 with Powershell 5.
  7. # Pre-Requisites: (1) Set variable values <vidb-dir>,<carbon-IP>,<carbon-PORT>,<vcentern>. You may add more vCenter IPs or resolvable FQDNs.
  8. # (2) Use a common credential for all vCenters and create a PS credential file $vidbdir\vcenter-creds.xml
  9. # (3) Remove the "18000" adjustment in $epochTime if the remote graphite server is running on a Windows host.
  10. #############################################################################################################################################
  11. Start-Transcript -path viDB.txt
  12. #
  13. # Variables
  14. #
  15. $vidbdir = "<vidb-dir>"
  16. $vidbfile = "$vidbdir\viDB_$(Get-Date -Format `"yyyyMMddHmm`").txt"
  17. $epochTime = [int](Get-Date -UFormat "%s") + 18000 # Adding 18000 to equate epoch time calculation between Windows and Linux (graphite on Linux)
  18. $carbonServer = "<carbon-IP>"
  19. $carbonServerPort = "<carbon-PORT>"
  20. #
  21. # Create Output Directory
  22. #
  23. if (!(Test-Path -Path $vidbdir))
  24. {
  25. New-Item -Path $vidbdir -ItemType directory -Force
  26. }
  27. #
  28. # Initialize PowerCLI and vCenter Connections - Connect to all vCenters
  29. #
  30. Add-PSSnapin VMware.VimAutomation.Core
  31. Set-PowerCLIConfiguration -InvalidCertificateAction ignore -confirm:$false
  32. Set-PowerCLIConfiguration -DefaultVIServerMode Multiple -confirm:$false
  33. $Hosts = @("<vcenter1>","<vcenter2>","<vcenter3>")
  34. $Hosts | %{
  35. $creds = Get-VICredentialStoreItem -File "$vidbdir\vcenter-creds.xml" -Host $_
  36. Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password
  37. }
  38. $creds = $null
  39. #
  40. # ESXi Host Metrics
  41. #
  42. $myvhost = Get-VMHost | where-object {$_.PowerState -eq 'PoweredOn'} | Sort Name | Get-View |
  43. Select Name,
  44. @{N="cpu";E={$_.Hardware.CpuInfo.NumCpuPackages}},
  45. @{N="cores";E={$_.Hardware.CpuInfo.NumCpuCores}},
  46. @{N="memory";E={[math]::round($_.Hardware.MemorySize/1GB,0)}}
  47. ("vi.capacity.vhost.count " +($myvhost |Measure-Object "Name").Count + " " + $epochTime) | Out-File $vidbfile
  48. ("vi.capacity.vhost.cpu.count " +($myvhost | Measure-object "cpu" -Sum).Sum + " " + $epochTime) | Out-File -Append $vidbfile
  49. ("vi.capacity.vhost.cpu.core.count " +($myvhost | Measure-object "cores" -Sum).Sum + " " + $epochTime) | Out-File -Append $vidbfile
  50. ("vi.capacity.vhost.memory.total " +([math]::round(($myvhost | Measure-object "memory" -Sum).Sum/1024,2)) + " " + $epochTime) | Out-File -Append $vidbfile
  51. #
  52. # VM Metrics
  53. #
  54. $myvm = Get-VM
  55. $myvm_on = $myvm | where-object {$_.PowerState -eq "PoweredOn"}
  56. $myvm_os = foreach ($vm in $myvm_on) {(Get-View $vm).summary.Config.GuestFullName}
  57. ("vi.provisioned.vm.count " + ($myvm | Measure-Object).Count + " " + $epochTime) | Out-File -Append $vidbfile
  58. ("vi.provisioned.vm.vpu.total " + ($myvm_on | Measure-Object -Sum NumCPU).Sum + " " + $epochTime) | Out-File -Append $vidbfile
  59. ("vi.provisioned.vm.memory.total " + ([math]::round(($myvm_on | Measure-Object -Sum MemoryGB).Sum/1024,2)) + " " + $epochTime) | Out-File -Append $vidbfile
  60. ("vi.provisioned.storage.total " + ([math]::round(($myvm | Get-HardDisk | measure-Object -Sum CapacityGB).Sum/1024,0)) + " " + $epochTime) | Out-File -Append $vidbfile
  61. ("vi.provisioned.vm.guestos.linux.count " + ($myvm_os -match 'Linux').count + " " + $epochTime) | Out-File -Append $vidbfile
  62. ("vi.provisioned.vm.guestos.windows.count " + ($myvm_os -match 'Windows').count + " " + $epochTime) | Out-File -Append $vidbfile
  63. #
  64. # Storage Metrics
  65. #
  66. $myds = [math]::round(((Get-Datastore | Measure-Object -Sum CapacityGB).Sum)/1024,0)
  67. ("vi.capacity.storage.total " + $myds + " " + $epochTime) | Out-File -Append $vidbfile
  68. #
  69. # Disconnect from vCenters
  70. #
  71. if ($Global:DefaultVIServers)
  72. {
  73. Disconnect-VIServer -Server * -Force -Confirm:$false
  74. }
  75. #
  76. #Stream results to the Carbon server
  77. #
  78. $socket = New-Object System.Net.Sockets.TCPClient
  79. $socket.connect($carbonServer, $carbonServerPort)
  80. $stream = $socket.GetStream()
  81. $writer = New-Object System.IO.StreamWriter($stream)
  82. foreach($line in Get-Content $vidbfile) {
  83. $writer.WriteLine($line)
  84. }
  85. $writer.Flush()
  86. $writer.Close()
  87. $stream.Close()
  88. $socket.Close()
  89. Stop-Transcript
  90. ################################################# THE END ##################################################