«

»

List number of VMs per Nutanix AHV host

Got a question a few days ago about how to list number of virtual machines (VMs) per Nutanix AHV host. From the top of my head I can think of two ways:

PRISM

It’s really simple to either view VMs per AHV Hosts direct in the UI and even export the information to a CSV or JSON file.

  1. Log in to PRISM
  2. Click Select the VM view
  3. Click Table
  4. Click Host and you’ll sort the output based on the AHV Hosts and you need to count the VMs per AHV Host.

The PRISM exports option requires you to follow thew above first three steps but instead of clicking the HOST link you’ll select the drop down option Export CSV or Export JSON.

When you got your file, just make a summary in your preferred CSV and or JSON editor.

Script

However, if you need this information every X seconds, minutes, hours and so on based on the work you’re currently doing you can run a script via REST API or acli. I used the below script during a live migration test the other days to keep track of the number of VMs per AHV Host every 30 seconds.

#!/bin/bash
# Script to list number of VMs per AHV hosts
# Author: Magnus Andersson - Sr Staff Solution Architect @Nutanix
# Version 1.0 created 2017-04-26
#
hosts=`acli host.list | awk '{if (NR!=1) {print $1}}'`
for i in $hosts ;
    do
echo "Number of VMs on AHV host $i is:"
acli host.list_vms $i | awk '{if (NR!=1) {print $1}}' |wc -l
done

If needed you can redirect the output to a log file if needed but for me it was ok to have the output printed to the terminal. I used the below command (just copy and paste into the SSH session) to run the script as long as I needed. Just press “ctrl + c” to stop the script.

while true
	do
	echo `date`
	/home/nutanix/test.sh
	sleep 30
done

The below figure is a script output from my test cluster.

This script was tested and verified on AOS 5.1.0.1 and AHV 20160925.44