I was reading VCDX56’s post Nutanix AHV VM Reporting Via REST API authored by Magnus Andersson @magander3 where, as the title suggests, he discusses a script he wrote to gather information about VMs running on a Nutanix AHV cluster using the REST APIs. At the end of the post, he mentioned that he would like to change the script from Bash to Python.
I have recently been doing quite a bit REST API scripting with Python, so I took a crack at it last night.
Before we get to the script a couple of things. I wrote the script for Python 3. I tested it on Python 3.5.2 on Linux (Ubuntu & CentOS), and with Python 3.6.3 on Windows 10. On the Nutanix side this script was tested against AOS 5.1.2 and Nutanix Community Edition (CE) 2017.05.22.
This script utilizes the “requests” module, which is a standard module that should already be installed on Ubuntu 16.04. For Windows or other situations where the requests module isn’t loaded, you will need to install it via “pip install requests”. If pip is not installed, reinstall/upgrade your Python installation and include pip as you install it.
Mac users should use Homebrew to install Python 3 by running “brew install python3” from Terminal. That will include pip3 so you can then run “pip3 install requests”. Then you can run the script by running “python3 ahv-vm-reporting.py”. Thanks to Peter Chang @virtualbacon for the Mac instructions/testing.
Comparing to Magnus’ work, you will see that I added a couple of fields and I did not include the Self Service Portal information. For that, I’m waiting for the Nutanix Calm integration in Prism Central.
As you can probably tell from the variables, this script requires a user with “View Only” rights to the Nutanix cluster. While you could use a more privileged account, there is no need. The CIP variable is the Cluster Virtual IP Address or resolvable DNS name of the cluster.
# Make sure to change these variables to match your environment!
# The rest of these variables shouldn't need to changed under normal circumstances.
finalcsv = CIP + "-VM_Report-" + datetime.datetime.today().strftime('%Y%m%d%H%M') + ".csv"
# Define URIs here. Keep them as variables as the API could change.
buri2 = "https://" + CIP + ":9440/PrismGateway/services/rest/v2.0/"
uri = buri2 + urivm + "?include_vm_disk_config=true&include_vm_nic_config=true"
vmip['No NIC present'] = "No NIC present"
vmhost = "VM powered off"
Again, credit is very much due to Magnus @magander3 for the idea and creation of the Bash version of this script.