WilliamLam.com

  • About
    • About
    • Privacy
  • VMware Cloud
  • Tanzu
    • Application Modernization
    • Tanzu services
    • Tanzu Community Edition
    • Tanzu Kubernetes Grid
    • vSphere with Tanzu
  • Home Lab
  • Nested Virtualization
  • Apple
You are here: Home / Automation / Retrieving detailed per-VM space utilization on VSAN

Retrieving detailed per-VM space utilization on VSAN

06.27.2018 by William Lam // 6 Comments

I was recently helping out my friend Paudie O'Riordan with a request from a customer who was looking for a way to collect detailed space utilization for their VMs (VM Home, VMDK & swap) running on VSAN. Today, this level of granularity is not available in the vSphere UI and the customer was interested in both the used and reserved capacity on a per-VM basis. Luckily, this information can be retrieved using the VSAN Management API.

To do so, we just need to use the VsanQueryObjectIdentities() API method, which I have used in the past to retrieve things like "thick" provisioned VM and translating VSAN Object IDs to their friendly VM display name. To retrieve space utilization information, we just need to set the includeSpaceSummary property to be true. While developing the PowerCLI sample script, I found that this specific property is currently not supported when querying vCenter Server and to retrieve this information, you must go directly to each ESXi host within the VSAN Cluster. I have already filed a feature request and it looks like this will be fixed in a future vSphere release.

In the meantime, we still have a solution in which we can use vCenter Server to provide us with a list of ESXi hosts within the VSAN Cluster and given ESXi host credentials, connect to each host using the VSAN API and aggregating the results as if it was done at the VSAN Cluster level. With that, I have created the VSANVMDetailedUsage.ps1 which includes a simple function called Get-VSANVMDetailedUsage which accepts the name of VSAN Cluster as well as an optional VM name for additional filtering. As mentioned earlier, you will need to provide valid credentials to your ESXi hosts (read-only is sufficient) and I have a check in place to ensure the following two variables have been set within the script before it proceeds:

$ESXiHostUsername= ""
$ESXiHostPassword = ""

Here is an example of running the script with just the VSAN Cluster name and no VM filtering, it will retrieve ALL VMs within the VSAN Cluster:

Get-VSANVMDetailedUsage -Cluster "VSAN-Cluster"


Here is an example of running the script with VSAN Cluster name and filtering on a specific VM:

Get-VSANVMDetailedUsage -Cluster "VSAN-Cluster" -VM "Ubuntu-SourceVM"

More from my site

  • How to move vSAN Datastore into a Folder?
  • How to tell if an ESXi host is a VSAN Witness Virtual Appliance programmatically?
  • VSAN Flash/MD capacity reporting
  • VSAN vCheck Plugins
  • Automating Customer Experience Improvement Program (CEIP) configuration using vSphere API and PowerCLI

Categories // Automation, PowerCLI, VSAN Tags // PowerCLI, VSAN, vSphere API

Comments

  1. Dave Perella says

    10/10/2018 at 4:20 am

    William, 1st love your posts always seem to find solutions to the oddball issues we are having. So thanks we appreciate you sharing. So to the nitty-gritty; we have script that collects vmdk sizes per VM for "costing"...etc...the line of code we use to capture this information from our SAN connected disks is:

    - @{Name="Storage_GB_VMPremium"; Expression={[int]($($_.LayoutEx.File|?{$_.name -like "*-flat.vmdk"}|?{($_.name -like '*-VMP-*')}|%{$_.size}|measure-object -Sum).sum/1024/1024/1024)}}, `

    The datastores have -VMP- in the name the result is the size of the disk in an appropriately named column. However, when we try that same line of code with our VSAN datastores:

    - @{Name="Storage_GB_vSAN"; Expression={[int]($($_.LayoutEx.File|?{$_.name -like "*-flat.vmdk"}|?{($_.name -like '*-VSAN*')}|%{$_.size}|measure-object -Sum).sum/1024/1024/1024)}}, `

    The result is zeros across the board. I have a couple ideas but can't find any information on what I need to do to get the result I need.

    Any help you can provide would again be greatly appreciated.

    Dave

    Reply
    • thementor666 says

      01/14/2019 at 1:41 am

      Hi Dave i think the reason you cant do select on the result is because in William's function the results are being piped to Format-Table
      if you remove the pipe to Format-Table and just return result i think what you're trying to do will work.

      -DM

      Reply
  2. Daniel Burtescu says

    01/31/2019 at 3:36 pm

    Hi William, will your script take in consideration the size of vm files after or before the vsan policy is applied? If a vmdk has a used size of 100 GB and we apply a raid6 policy will the script report 100 GB or 150 GB?

    Reply
    • William Lam says

      02/01/2019 at 4:31 am

      The property I'm keying off of is called physicalUsedGB, so it should be whats actually being consumed. Should be simple enough test for you to try out 🙂

      Reply
  3. Louise says

    09/17/2019 at 11:42 pm

    Hi William, we ran into an error while running the code

    Exception calling "VsanQueryObjectIdentities" with "6" argument(s): "'NoneType' object has no attribute '_moId'"
    At C:\Users\Administrator\Documents\VSANVMDetailedUsage02.ps1:54 char:9
    + $identities = $vos.VsanQueryObjectIdentities($null,$null,$nul ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FaultException

    ConvertFrom-Json : Cannot bind argument to parameter 'InputObject' because it is null.
    At C:\Users\Administrator\Documents\VSANVMDetailedUsage02.ps1:56 char:37
    + $json = $identities.RawData|ConvertFrom-Json
    + ~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [ConvertFrom-Json], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

    Any help you can provide would again be greatly appreciated.

    Reply
  4. Roshan says

    09/23/2019 at 12:25 pm

    Hi William,

    My customer needs to get the detailed per VM VSAN usage and your script perfectly work. But based on customers security policy they cant have same password for all ESXi host. Therefore we need to create a separate user for connecting to ESXi host with the same password. So what are the permissions should be given to this user to get the same result. We have tried with a user with Read-Only role and it failed.

    Reply

Thanks for the comment! Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search

Author

William Lam is a Senior Staff Solution Architect working in the VMware Cloud team within the Cloud Infrastructure Business Group (CIBG) at VMware. He focuses on Cloud Native technologies, Automation, Integration and Operation for the VMware Cloud based Software Defined Datacenters (SDDC)

Connect

  • Email
  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Vimeo

Recent

  • Changing the default HTTP(s) Reverse Proxy Ports on ESXi 8.0 03/22/2023
  • Quick Tip - How to download ESXi ISO image for all releases including patch updates? 03/15/2023
  • SSD with multiple NVMe namespaces for VMware Homelab 03/14/2023
  • Is my vSphere Cluster managed by vSphere Lifecycle Manager (vLCM) as a Desired Image or Baseline? 03/10/2023
  • Interesting VMware Homelab Kits for 2023 03/08/2023

Advertisment

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright WilliamLam.com © 2023

 

Loading Comments...