I am looking for ESX/ESXi specific commands/samples pyvmomi APIs to determine system memory information on the hypervisor - free/total/used.
3 Answers
Run ESXi host command
vsish -e get /memory/comprehensive
Raw output
Comprehensive {
Physical memory estimate:12454784 KB
Given to VMKernel:12454784 KB
Reliable memory:0 KB
Discarded by VMKernel:1580 KB
Mmap critical space:0 KB
Mmap buddy overhead:3084 KB
Kernel code region:18432 KB
Kernel data and heap:14336 KB
Other kernel:1421360 KB
Non-kernel:120036 KB
Reserved memory at low addresses:59900 KB
Free:10875956 KB
}
Format
vsish -e get /memory/comprehensive | sed 's/:/ /' | awk '
/Phys/ { phys = $(NF-1); units = $NF; width = length(phys) }
/Free/ { free = $(NF-1) }
END { print width, units, phys, phys-free, free }' |
while read width units phys used free; do
printf "Phys %*d %s\n" $width $phys $units
printf "Used %*d %s\n" $width $used $units
printf "Free %*d %s\n" $width $free $units
done
Output
Phys 12454784 KB
Used 1580564 KB
Free 10874220 KB

- 61
- 1
- 2
As there is also CLI mentioned in the question title, I'll add how one can retrieve memory usage via ESXi's command line. I used ESXi 6.7, but this should work since version ESX 4.0 as only the performance gathering is run on the ESXi host.
In ESXi SSH session run below to gather performance data:
# Source: https://kb.vmware.com/s/article/1004953 esxtop -b -n 1 > /tmp/perf.csv
SCP the performance data to a Linux machine.
Run the script below in a Linux terminal in a folder that contains the perf.csv:
printf "Total Memory: "; \ line_overall_mem="$(head -1 perf.csv | tr "," "\12" | grep -in "Machine MBytes" | cut -d ":" -f1)"; \ tail -1 perf.csv | tr "," "\12" | sed -n "${line_overall_mem}p" | sed 's/"//g'; \ printf "Free Memory: "; \ line_free_mem="$(head -1 perf.csv | tr "," "\12" | grep -in 'Memory\\Free MBytes' | cut -d ":" -f1)"; \ tail -1 perf.csv | tr "," "\12" | sed -n "${line_free_mem}p" | sed 's/"//g'
Output should be something like:
Total Memory: 24566 Free Memory: 7519
I've got couple free ESXis running with 6.7 U2. VMware's hardware compatibility list actually states that my hardware is only compatible with ESXi 4.1, but they're still running fine.
I've got the above and more information automatically written to a NFS share once a day. From the NFS share the information gets published via Linux VM web server.

- 85
- 10
I found the answer at:
https://gist.github.com/deviantony/5eff8d5c216c954973e2
Specifically, these lines:
memoryCapacity = hardware.memorySize
memoryCapacityInMB = hardware.memorySize/MBFACTOR
memoryUsage = stats.overallMemoryUsage
freeMemoryPercentage = 100 - (
(float(memoryUsage) / memoryCapacityInMB) * 100
)

- 9,282
- 3
- 19
- 36