Analyzing esxtop data and generating a detailed report using PowerShell

Analyzing esxtop data and generating a detailed report using PowerShell can be achieved by capturing the esxtop output and processing it to extract relevant metrics. In this example, we’ll use PowerShell to execute esxtop in batch mode, capture the output, parse the data, and generate a report in a document format (e.g., CSV or HTML). The report will focus on storage-related metrics, including DAVG (Device Average Response Time). Let’s proceed with the PowerShell script:

# Function to run esxtop and capture the output
function RunEsxtop {
    # Set the ESXi host IP address or hostname
    $esxiHost = "ESXI_HOST_IP_OR_HOSTNAME"

    # Set the credentials to connect to the ESXi host (if required)
    $username = "USERNAME"
    $password = "PASSWORD"

    # Define the esxtop command to run
    $esxtopCommand = "esxtop -b -d 1 -n 10 -a 'CMDS/s,DAVG'"

    # Run esxtop command and capture the output
    $esxtopOutput = Invoke-SSHCommand -ComputerName $esxiHost -Command $esxtopCommand -Username $username -Password $password

    # Return the esxtop output
    return $esxtopOutput
}

# Function to parse esxtop output and generate a report
function GenerateEsxtopReport {
    param (
        [Parameter(Mandatory=$true)]
        [string]$esxtopOutputPath
    )

    # Read esxtop output from the specified file
    $esxtopOutput = Get-Content -Path $esxtopOutputPath

    # Initialize an empty array to store the parsed data
    $esxtopData = @()

    # Process each line of the esxtop output
    foreach ($line in $esxtopOutput) {
        # Skip blank lines and lines that do not contain relevant data
        if ($line -match "^[0-9]+\s+[0-9]+\.[0-9]+") {
            # Extract the relevant data using regular expressions
            $match = $line | Select-String -Pattern "([0-9]+)\s+([0-9]+\.[0-9]+)"
            $cmdsPerSec = $match.Matches.Groups[1].Value
            $davg = $match.Matches.Groups[2].Value

            # Create a custom object to represent the data
            $esxtopEntry = [PSCustomObject]@{
                "CMDS/s" = $cmdsPerSec
                "DAVG (ms)" = $davg
            }

            # Add the custom object to the array
            $esxtopData += $esxtopEntry
        }
    }

    # Generate a CSV report
    $csvReportPath = "C:\Reports\esxtop_report.csv"
    $esxtopData | Export-Csv -Path $csvReportPath -NoTypeInformation

    # Generate an HTML report (optional)
    $htmlReportPath = "C:\Reports\esxtop_report.html"
    $esxtopData | ConvertTo-Html | Out-File -FilePath $htmlReportPath
}

# Run esxtop and save the output to a file
$esxtopOutputPath = "C:\Temp\esxtop_output.txt"
RunEsxtop | Out-File -FilePath $esxtopOutputPath

# Generate the report
GenerateEsxtopReport -esxtopOutputPath $esxtopOutputPath

Write-Host "Esxtop report generated successfully."

Note: The script uses the Invoke-SSHCommand cmdlet to execute esxtop remotely on the ESXi host. Ensure you have the appropriate SSH module or module for the method you use to connect to the ESXi host remotely.

The script runs esxtop with the specified options to capture the relevant storage-related metrics, including CMDS/s (command rate) and DAVG (Device Average Response Time). The output is then processed and stored in an array as custom objects. The script generates a CSV report with these metrics and optionally an HTML report for a more visually appealing view of the data.

Please make sure to adjust the script according to your specific environment, including the ESXi host credentials, output file paths, and additional metrics you want to capture from esxtop. Test the script in a non-production environment first and ensure that you have the necessary permissions to access the ESXi host remotely.

Leave a comment