Automate your vCenter interactions from the Linux commandline with govmomi and govc

This is the first time in a LONG time that I found a tool I’m really happy with: govc allows you to interact with VMware vCenter without the need for the dreaded vClient, Windows machines or to write your own scripts to access the horrible VMware API. This way you can easily automate many tasks on VMware directly from the command line or your bash scripts.

Disclaimer: I don’t like VMware. I hated every single moment when I was forced to start a Windows VM just to interface with ESX/vCenter, so this tool is exactly what I desired.

First thing first, you’ll need the tool. You can compile it if you want, but there’s handy binaries already available on the project github page: https://github.com/vmware/govmomi/releases

I downloaded it and created a small wrapper for it:

 # curl -LO https://github.com/vmware/govmomi/releases/download/v0.9.0/govc_linux_amd64.gz
# gunzip govc_linux_amd64.gz
# cat >govc <<EOF
#!/bin/bash

export GOVC_URL='https://username:password@vsphere-ip-or-hostname/sdk'
export GOVC_DATACENTER=VSPHERE_DC
export GOVC_INSECURE=true

/usr/bin/govc_linux_amd64 \$@
EOF
# chmod +x govc*
# cp -i govc* /usr/bin/

Note that you only need GOVC_INSECURE=true if you are using self-signed certificates and you don’t have the CA added to your local trusted certs.

At this point by calling /usr/bin/govc you should be able to use the tool without specifying too many options on the command line. For a start, you can get some basic info about your environment:

 # govc about
Name:         VMware vCenter Server
Vendor:       VMware, Inc.
Version:      5.5.0
Build:        4180647
OS type:      linux-x64
API type:     VirtualCenter
API version:  5.5
Product ID:   vpx
UUID:         AAAA-BBBB-CCCC-DDDD-EEEE
# govc datacenter.info
Name:              VSPHERE_DC
Hosts:             10
Clusters:          0
Virtual Machines:  0
Networks:          6
Datastores:        14

What else can we do? Quite a lot of stuff, actually. To get an idea, just run govc without any argument. To see what parameters a command supports, run govc command –help.

For example you can easily get the datastore usage info for the whole datacenter with one simple command:

# govc datastore.info
Name:        ds1_SAS
  Path:      /VSPHERE_DC/datastore/ds1_SAS
  Type:      VMFS
  URL:       ds:///vmfs/volumes/AAAA1-BBBB-CCCC-DDDD/
  Capacity:  833.0 GB
  Free:      424.7 GB
Name:        ds2_SAS
  Path:      /VSPHERE_DC/datastore/ds2_SAS
  Type:      VMFS
  URL:       ds:///vmfs/volumes/AAAA2-BBBB-CCCC-DDDD/
  Capacity:  833.0 GB
  Free:      388.7 GB
[...]

Amazed by this, I thought I could as easily get all the info about the ESX hosts with host.info, but nope:

 # govc host.info
/usr/bin/govc_linux_amd64: default host resolves to multiple instances, please specify
# govc host.info -host.ip=192.168.100.1
Name:              192.168.100.1
  Path:            /VSPHERE_DC/host/192.168.100.1/192.168.100.1
  Manufacturer:    HP
  Logical CPUs:    24 CPUs @ 2294MHz
  Processor type:  Intel(R) Xeon(R) CPU E5-2630 0 @ 2.30GHz
  CPU usage:       2061 MHz (3.7%)
  Memory:          131037MB
  Memory usage:    51020 MB (1.4%)
  Boot time:       2016-08-17 18:54:24.980915 +0000 UTC

It turns out, it doesn’t work the same way. As you see, I had to use the -host.ip parameter to specify the IP address of the ESX host.

At the time I thought the -host.ip parameter was the only way to specify the target, but it’s not accepted by the vm.info command:

 # govc vm.info -host.ip=192.168.100.100
/usr/bin/govc_linux_amd64: flag provided but not defined: -host.ip
# govc vm.info -vm.ip=192.168.100.100
Name:           Control
  Path:         /VSPHERE_DC/vm/services/Control
  UUID:         AAAA-BBBB-CCCC-DDDD-FFFF
  Guest name:   Red Hat Enterprise Linux 6 (64-bit)
  Memory:       4096MB
  CPU:          3 vCPU(s)
  Power state:  poweredOn
  Boot time:    2016-04-27 11:49:10.410135 +0000 UTC
  IP address:   192.168.100.100
  Host:         192.168.100.1

At the time I was quite puzzled because I couldn’t find a good way to iterate over the hosts or VMs. Until I noticed the ls command:

 # govc ls
/VSPHERE_DC/vm
/VSPHERE_DC/network
/VSPHERE_DC/host
/VSPHERE_DC/datastore
# govc ls /VSPHERE_DC/host
/VSPHERE_DC/host/192.168.100.1
/VSPHERE_DC/host/192.168.100.2
[...]
# govc ls /VSPHERE_DC/vm
/VSPHERE_DC/vm/database
/VSPHERE_DC/vm/template
[...]
# govc ls /VSPHERE_DC/vm/database/*
/VSPHERE_DC/vm/database/opencart/DB-OPENCART01
/VSPHERE_DC/vm/database/social/DB-SOCIAL-MASTER03
/VSPHERE_DC/vm/database/social/DB_SOCIAL_SLAVE03
[...]

And after a few more minutes fiddling and reading the –help options I realized you could invoke govc with the result from the ls command as a parameter, for example:

 # govc vm.info /VSPHERE_DC/vm/database/opencart/DB-OPENCART01
Name:           DB-OPENCART01
  Path:         /VSPHERE_DC/vm/database/opencart/DB-OPENCART01
[...]
# govc ls /VSPHERE_DC/vm/database/* | xargs govc vm.info | grep ^Name
Name:           DB-OPENCART01
Name:           DB_SOCIAL_SLAVE03
Name:           DB-SOCIAL-MASTER03
[...]

Iterating over hosts, VMs and datastores is easy!

govc also allows you to run esxcli on the specified host, for example in one of my many experiments I ran:

 # govc host.esxcli --host.ip=192.168.100.1 vm process list | grep DisplayName | awk {'print $2'} | sort
TOMCAT01
TOMCAT-TEST
WS01
WS01-TEST
WS02
WS-TEST

A very important thing to notice is that every command can be run with the parameter -json=true. This outputs a machine-parsable format that includes many more details than the normal text output.

 # govc vm.info -json /VSPHERE_DC/vm/database/opencart/DB-OPENCART01 | python -m json.tool
{
    "VirtualMachines": [
        {
            "AlarmActionsEnabled": true,
            "AvailableField": null,
            "Capability": {
                "BootOptionsSupported": true,
                "BootRetryOptionsSupported": true,
                "ChangeTrackingSupported": true,
                "ConsolePreferencesSupported": false,
[...]

This, of course, could be very handy for your scripts!

More info on the project github page: https://github.com/vmware/govmomi/tree/master/govc

A small introduction to this tool that helped me get started: http://www.virtuallyghetto.com/2014/09/govmomi-vsphere-sdk-for-go-govc-cli-kubernetes-on-vsphere-part-1.html

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s