Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Broadcom VMware PowerCLI based ESXi capacity planner html web portal

I had a requirement to provide a solution for monitoring ESXi servers' CPU, memory, and datastore usage to non-IT or developer users without giving them direct access to vCenter or ESXi servers. The solution needed to avoid using any additional applications or tools. Previously, I had set up a system pipeline where the data was delivered in CSV or Excel format. However, I thought it would be more effective to offer an interactive web portal where the information would be visually intuitive and easily accessible.

Here is screenshot of the web application. The application's Infrastructure section allows you to select a vCenter server, Virtual Datacenter, and Clusters, and then displays a list of ESXi hosts with their corresponding usable capacity in percentage.

Broadcom vmware vsphere vcenter esxi vcenter server virtualization vmware powercli automation pipeline configuration capacity planner tool cluster datacenter web based csv.png

I have a PowerCLI script that retrieves ESXi capacity data from connected vCenters and saves it in JSON format on a scheduled basis. Using a PowerShell script, I can automatically copy generated json data to the scripts/database.js file. I can then host the entire web project on an IIS, Apache, or Nginx server to make it accessible to users centrally from their browsers.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#Import-Module VMware.VimAutomation.Core

# $vCenters = [PSCustomObject]@(
#     @{
#         server = '192.168.34.20'
#         user = '[email protected]'
#         password = 'xxxxxxxx'
#     },
#     @{
#         server = '192.168.34.21'
#         user = '[email protected]'
#         password = 'xxxxxxx'
#     }
# )

# Connect-VIServer -Server '192.168.34.20' -User '[email protected]' -Password 'xxxxxxxxxx'
# Connect-VIServer -Server '192.168.34.21' -User '[email protected]' -Password 'xxxxxxxxxx'

#$allEsxiHosts = Get-VMHost
#$allClusters = Get-Cluster
#$allDatacenters = Get-Datacenter
foreach ($esxi in $allEsxiHosts)
{
    #Write-Host $esxi.name
    $clusterName = $esxi | Get-Cluster
    #$clusterName = $allClusters | Where-Object {$_.Id -eq $esxi.ParentId}
    $datacenterName = $esxi | Get-Datacenter
    $datastoreList = $esxi.DatastoreIdList | ForEach-Object {Get-Datastore -Id $_}
    $freeSpaceGB = $datastoreList.FreeSpaceGB | Measure-Object -Sum
    $capacityGB = $datastoreList.CapacityGB | Measure-Object -Sum
    $usageGB = $capacityGB.Sum - $freeSpaceGB.Sum

    [PSCustomObject]@{
        Name = $esxi.name
        Cluster = $clusterName.Name
        Datacenter = $datacenterName.Name
        vCPU = [System.Math]::Ceiling(($esxi.CpuUsageMhz / $esxi.CpuTotalMhz).ToString('P').Replace('%',''))
        Memory = [System.Math]::Ceiling(($esxi.MemoryUsageGB / $esxi.MemoryTotalGB).ToString('P').Replace('%',''))
        Datastore = [System.Math]::Ceiling(($usageGB / $capacityGB.Sum).ToString('P').Replace('%',''))
        vCenter = $esxi.Uid.Split('@')[1].Split(':')[0]
        PowerState = $esxi.PowerState
        State = $esxi.State
     }
}

$csvData =Import-Csv .\esxi_capactity.csv

# Create empty JSON object
$jsonObject = [ordered]@{
    vcenterlist = [ordered]@{}
}

# Iterate through CSV data and build JSON object
foreach ($row in $csvData) {
    $vcenter = $row.VCenter
    $datacenter = $row.Datacenter
    $cluster = $row.Cluster
    $esxiHost = $row.Name

    # Create nested JSON structure
    if (-not $jsonObject.vcenterlist.$vcenter) {
        $jsonObject.vcenterlist.$vcenter = [ordered]@{}
    }
    if (-not $jsonObject.vcenterlist.$vcenter.$datacenter) {
        $jsonObject.vcenterlist.$vcenter.$datacenter = [ordered]@{}
    }
    if (-not $jsonObject.vcenterlist.$vcenter.$datacenter.$cluster) {
        $jsonObject.vcenterlist.$vcenter.$datacenter.$cluster = [ordered]@{}
    }
    # Add ESXi host details
    $jsonObject.vcenterlist.$vcenter.$datacenter.$cluster.$esxiHost = [ordered]@{
        name = $esxiHost
        cpu = $row.vCPU
        memory = $row.Memory
        datastore = $row.Datastore
        state = $row.State
        powerstate = $row.PowerState
    }
}

# Convert JSON object to string
$jsonString = ConvertTo-Json -InputObject $jsonObject -Depth 100

# Output JSON string
$jsonString | Out-File  -Force -FilePath .\capacity_data.json

Useful articles
Add NFS datastores to VMware ESXi hosts using PowerCLI
Using PowerCLI to Increase VMDK (Virtual disk) in VMware Virtual Machine
Add a VMFS datastore using VMware PowerCLI
POWERCLI: VMWARE ESXI CONFIGURE (VSWITCH) VMKERNEL NETWORK PORT FOR ISCSI STORAGE
POWERCLI VMWARE: CONFIGURE SOFTWARE ISCSI STORAGE ADAPTER AND ADD VMFS DATASTORE
POWERCLI: VIRTUAL MACHINE STORAGE MIGRATE/SVMOTION AND DATASTORE PORT BINDING MULTIPATHING
VMWare Powercli: Time Configuration (NTP - Network Time Protocol) on multiple Esxi server
vSphere PowerCLI - Configure syslog on VMware ESXi hosts and Enable security profile firewall
Part 1: VMware Powercli : Gather distributed virtual switch information to JSON file to migrate standard switch
Part 2: Copy or clone distributed virtual switch portgroups to standard switch portgroups - Powercli
Part 3: VMware Powercli: Migrate VMs to another network
Powercli GUI: Determine the EVC Mode that vmware cluster should be configured
vMotion from all VMs on selected Esxi Host to other Esxi host via PowerCLI GUI
Powercli Get vCenter licenses information
Powercli Get vCenter assigned licenses report
PowerCLI one-liner Reporting, Chaning, Assigning and Removing licenses on ESXi
PowerCLI Get-VMhost The operation on computer failed The WinRM client cannot process the request
PowerCLI Add a SCSI Controller to a Virtual Machine
Powershell vCenter server Rest API create and assign tag and tagcategory
PowerCLI create, modify and assign tag and tagcategory
PowerCLI oneliner get the list of users and groups from vCenter SSO
PowerCLI Connect-VIServer Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction

Go Back

Comment

Blog Search

Page Views

12383613

Follow me on Blogarama