Python: JSONPath to extract vCenter information using govc

python-logoIn a previous post, I described how to install and use the ‘govc‘ tool – which is a command line tool for working with VMware vCenter.

When you do a listing on a virtual machine “govc ls -l -json <vmpath>”, you are returned back a very detailed JSON data structure that contains all the information on a vm including: OS, cpu, memory, disk, and network.

However, extracting this information can be challenging with text processing or even basic json tools.  Using JSONPath within a Python script makes drilling down to the relevant information relatively easy.

If you are looking for a more basic example using JSONPath and jsonpath-rw-ext, you can read my article here that uses a simpler JSON example.

Prerequisite, JSON output from govc

The first thing you need is the JSON output from the govc tool.  Read my article here on installing and running govc.

Below is how you would redirect the output for a vm named “esxchild1” to a file named “esxchild1-on.json”.

# use full path to get detailed VM metadata
vmpath=$(govc vm.info esxchild1 | grep "Path:" | awk {'print $2'})
govc ls -l -json $vmpath > esxchild1-on.json

Prerequisite, Python modules

For our Python script to use JSONPath, you need to install those modules.  Here is how you would do that within a virtualenv.

virtualenv jsonpath
source jsonpath/bin/activate
pip install jsonpath-rw jsonpath-rw-ext

Run Python script to extract

Download my ExtractGovcListing.py  python script from github.  Run it like below against your govc json output.

$ python ExtractGovcListing.py esxchild1-on.json

name: esxchild1
State: poweredOn
OS: Ubuntu Linux (64-bit)
Full path: /ha-datacenter/vm
Parent Folder: vm
Default IpAddress: 192.168.122.254
  IPv4 192.168.122.254/24 netmask 255.255.255.0
  IPv6 fe80::20c:29ff:fedc:95bb/64
MemorySizeMB: 1024
CPU: 1
DISK capacity is 16Gb on [datastore1]

I have included two sample files on github: esxchild1-on.json and esxchild1-off.json representing the output of govc when the VM is powered on versus powered off.

JSONPath syntax

Notice the ability to easily dig down several layers deep into the data structure to pull out the VM name.

print("name: {}".format( jp.match1("elements[*].Object.Config.Name",json_data) ))

Or the ability to grab array items from ‘Device’, but only if they contain ‘CapacityInKB’ which indicates a hard drive.

for disk in jp.match("elements[*].Object.Config.Hardware.Device[?CapacityInKB>0]",json_data):
  capacityGB = (disk['CapacityInKB']/1024/1024)
  filestore = disk['Backing']['FileName'].split(' ')
  print("DISK capacity is {}Gb on {}".format( capacityGB,filestore[0] ))

It is processing like this that makes JSONPath so convenient to use.

 

REFERENCES

jsonpath-rw-ext page