How to use govc to manage your Vmware environment ?
Govc - a Vsphere CLI build on top of GovMomi
Govc is a vSphere CLI written in Go Language as an alternative to the use the Vcenter/Vsphere Gui and it’s really well-suited for automation purpose and faster if you need to get information on all your Esxi hosts.
Govc is part of GovMomi which is used by a lot of amazing tools like Docker, Kubernetes, Terraform, Packer and so on.
Govc is an easy command line interface using the GovMomi SDK Library, portable as it’s compiled for Linux, FreBDS, OSx and windows.
The Govmomi project is an open source project lead by VMware, it’s a goLang Library for interacting with Vmware Vsphere APIs, Esxi and Vcenter.
Why using Govc and not PowerCli instead ?
The answer is simple, Govc is written in go and is a lot lighter and faster than Powershell. It’s portable and you can use bash to automate any workflow.
Even If I’m love Powershell and PowerCli but sometimes it will go faster for me to use Govc instead and I will get the result of the query a lot faster. This is the big plus of Govc, it’s really fast.
Installation of GO and govc
First of all, you need to have GoLang installed.
You can download the latest version of go on this url :
And follow this to install it :
Once installed, verify your version of go :
root@mt-ub-terform:~# go version
go version go1.12.7 linux/amd64
Then, just install Govc
go get -u github.com/vmware/govmomi/govc
Check if govc is correctly installed :
root@mt-ub-terform:~# govc version
govc 0.21.0
You can output to json format and use the command-line json processor ‘jq’ to print it nicely.
Json format is more user friendly and can be useful for dev or scripting purpose.
jq is like sed,awk,grep for JSON data.
Install jq package :
apt-get install jq
Output with Json format
root@mt-ub-terform:/usr/local/bin# govc about -json | jq '.'
{
"About": {
"Name": "VMware vCenter Server",
"FullName": "VMware vCenter Server 6.7.0 build-15976728",
"Vendor": "VMware, Inc.",
"Version": "6.7.0",
"Build": "15976728",
"LocaleVersion": "INTL",
"LocaleBuild": "000",
"OsType": "linux-x64",
"ProductLineId": "vpx",
"ApiType": "VirtualCenter",
"ApiVersion": "6.7.3",
"InstanceUuid": "0711EE34-9114-43D3-8961-0715147BACF1",
"LicenseProductName": "VMware VirtualCenter Server",
"LicenseProductVersion": "6.0"
}
}
And that’s it ! We are ready to use Govc to manage our Vmware Environment.
Connection to your Vsphere Environment :
First, check that you can reach your Vcenter environment from the server where you install govc :
root@mt-ub-terform:~# curl --verbose -k -X POST https://mt-sles-vclab.mtx.vodafonemalta.com/sdk
* Trying 192.168.120.190...
* Connected to mt-sles-vclab.mtx.vodafonemalta.com (192.168.120.190) port 443 (#0)
If you have issue, check that proxy is not blocking and firewall is open for port https ( 443 ).
Let’s connect to our Vcenter an start grabbing few information using Environment variable :
export GOVC_USERNAME='MTX\superjulsimadmin' GOVC_PASSWORD='Ve3fse405xdefegvrev0' GOVC_URL='mt-sles-vclab.mtx.vodafonemalta.com' GOVC_INSECURE=true
I will recommend you to start your command with a space if you don’t want to let your credentials information save on your history. It’s mandatory if you are using root account or an account shared with other users.
Simple, right ? If you have more than one datacenter, you need to specify the name :
export GOVC_DATACENTER=NameDatacenter
⚠️ Be aware than these variables are set temporary and lost when you will close your terminal session.
If you have a secure account, you can add all these variable on your .bash_profile :
vim ~/.profile
export GOVC_USERNAME='MTX\superjulsimadmin'
export GOVC_PASSWORD='Ve3fse405xdefegvrev0'
export GOVC_URL='mt-sles-vclab.mtx.vodafonemalta.com'
export GOVC_INSECURE=true
and then, read/load the file :
source ~/.profile
Check if you are connected :
root@mt-ub-terform:~# govc about
Name: VMware vCenter Server
Vendor: VMware, Inc.
Version: 6.7.0
Build: 15976728
OS type: linux-x64
API type: VirtualCenter
API version: 6.7.3
Product ID: vpx
UUID: bacaefe9-7151-4e4c-833e-360c566f0d20
Basic command with Govc
Help command
go --help
go command --help
root@mt-ub-terform:~# govc vm.ip --help
Usage: govc vm.ip [OPTIONS] VM...
List IPs for VM.
Grab information about the datacenter :
root@mt-ub-terform:~# govc datacenter.info
Name: Lab
Path: /Lab
Hosts: 7
Clusters: 3
Virtual Machines: 13
Networks: 17
Datastores: 16
This command is taking microsecond to display, this is the power of Govc 😄
Grab information about datastore :
root@mt-ub-terform:~# govc datastore.info
Name: nstnd3-Local Datastore 1
Path: /Lab/datastore/Nested VI Cluster/nstnd3-Local Datastore 1
Type: VMFS
URL: ds:///vmfs/volumes/5d9b4427-6d0167ee-a620-1c98ec183574/
Capacity: 271.8 GB
Free: 270.8 GB
Name: nstnd3-Local Datastore 2
Path: /Lab/datastore/Nested VI Cluster/nstnd3-Local Datastore 2
Type: VMFS
URL: ds:///vmfs/volumes/5d9f3cf5-216169a3-fc13-1c98ec183574/
Capacity: 1117.8 GB
Free: 1116.8 GB
Name: nstnd1-Local Datastore 1
Path: /Lab/datastore/Nested VI Cluster/nstnd1-Local Datastore 1
Type: VMFS
URL: ds:///vmfs/volumes/5d9b3e89-65cef531-0abb-1c98ec187560/
Capacity: 271.8 GB
Free: 270.8 GB
List all vms and folder :
root@mt-ub-terform:~# govc find vm
vm
vm/Z - Clone
vm/B - VIBS
vm/A - Virtual Infrastructure
vm/D - Test Applications
vm/T - Templates
vm/N - Nested VI
vm/E - EADR Testing
vm/E - EADR Testing/eadr-iso-test
vm/N - Nested VI/mt-wi-vbstrmsrv
vm/N - Nested VI/nst-wi-sprdns
vm/N - Nested VI/nst-esx-sprnd3
vm/N - Nested VI/nst-esx-sprnd1
vm/A - Virtual Infrastructure/mt-phy-vclab
vm/B - VIBS/mt-hpe-ovgd
vm/B - VIBS/mt-ce-ov4vc
vm/B - VIBS/mt-ub-testtera
You can use the find command to get a lot of information and you specify the type you want as below :
a VirtualApp
c ClusterComputeResource
d Datacenter
f Folder
g DistributedVirtualPortgroup
h HostSystem
m VirtualMachine
n Network
o OpaqueNetwork
p ResourcePool
r ComputeResource
s Datastore
w DistributedVirtualSwitch
For Datastore by example :
root@mt-ub-terform:~# govc find . -type s
/Lab/datastore/Other/2-ISOVI-3P1FC-L2
/Lab/datastore/Other/20-LABVI-MTX1-TMPL
/Lab/datastore/Lab VI Cluster/40-LABVI-MTX2-LUN40
/Lab/datastore/Lab VI Cluster/83-LABVI-3P1-L83
/Lab/datastore/Lab VI Cluster/30-LABVI-MTX1-LUN30
/Lab/datastore/Lab VI Cluster/84-LABVI-3P2-L84
You can combine as well command and filter :
If I want to know all Vms which are on specific datastore :
root@mt-ub-terform:~# govc find . -type m -datastore $(govc find -i datastore -name 40-LABVI-MTX2-LUN40)
/Lab/vm/D - Test Applications/mt-ce-hpeov02
/Lab/vm/B - VIBS/mt-ce-ov4vc
If you want to have information of one of few virtual machines using regexp :
root@mt-ub-terform:~# govc vm.info *wi*
Name: tmpl-win-2016
Path: /Lab/vm/T - Templates/tmpl-win-2016
UUID: 423075c3-f90f-18b8-a8f1-84513e22d390
Guest name: Microsoft Windows Server 2016 or later (64-bit)
Memory: 8192MB
CPU: 4 vCPU(s)
Power state: poweredOff
Boot time: <nil>
IP address:
Host: mt-esx-labnd1.mtx.vodafonemalta.com
Name: mt-wi-vbstrmsrv
Path: /Lab/vm/N - Nested VI/mt-wi-vbstrmsrv
UUID: 42305c05-92e8-16be-10ec-340a87b413fb
Guest name: Microsoft Windows Server 2016 or later (64-bit)
Memory: 8192MB
CPU: 4 vCPU(s)
Power state: poweredOff
Boot time: <nil>
IP address:
Host: mt-esx-labnd1.mtx.vodafonemalta.com
Name: nst-wi-sprdns
Path: /Lab/vm/N - Nested VI/nst-wi-sprdns
UUID: 42303e31-38d1-94af-01d6-3453de7362a2
Guest name: Microsoft Windows Server 2016 or later (64-bit)
Memory: 4096MB
CPU: 2 vCPU(s)
Power state: poweredOff
Boot time: <nil>
IP address:
Host: mt-esx-labnd1.mtx.vodafonemalta.com
Find a virtual machine by IP :
root@mt-ub-terform:~# govc vm.info -vm.ip=192.168.120.70
Name: mt-ce-hpeov
Path: /Lab/vm/B - VIBS/mt-ce-hpeov
UUID: 42309c34-9663-45e0-2ba8-ccafb4acfbeb
Guest name: Red Hat Enterprise Linux 6 (64-bit)
Memory: 24576MB
CPU: 8 vCPU(s)
Power state: poweredOn
Boot time: 2020-10-29 08:38:57.343068 +0000 UTC
IP address: 192.168.120.70
Host: mt-esx-labnd1.mtx.vodafonemalta.com
You can as get a lot of information on the host :
root@mt-ub-terform:~# govc host.info mt-esx-labnd1.mtx.vodafonemalta.com
Name: mt-esx-labnd1.mtx.vodafonemalta.com
Path: /Lab/host/Lab/mt-esx-labnd1.mtx.vodafonemalta.com
Manufacturer: HP
Logical CPUs: 12 CPUs @ 2400MHz
Processor type: Intel(R) Xeon(R) CPU E7450 @ 2.40GHz
CPU usage: 5303 MHz (18.4%)
Memory: 131068MB
Memory usage: 28639 MB (21.9%)
Boot time: 2020-03-05 08:49:37.207 +0000 UTC
State: connected
If you want by example to get this information for more than one host, small bash and magic is happening :
root@mt-ub-terform:~# for i in $(govc find . -type h | cut -d '/' -f5 | grep labnd) ; do govc host.info $i; done
Name: mt-esx-labnd1.mtx.vodafonemalta.com
Path: /Lab/host/Lab/mt-esx-labnd1.mtx.vodafonemalta.com
Manufacturer: HP
Logical CPUs: 12 CPUs @ 2400MHz
Processor type: Intel(R) Xeon(R) CPU E7450 @ 2.40GHz
CPU usage: 4788 MHz (16.6%)
Memory: 131068MB
Memory usage: 28632 MB (21.8%)
Boot time: 2020-03-05 08:49:37.207 +0000 UTC
State: connected
Name: mt-esx-labnd2.mtx.vodafonemalta.com
Path: /Lab/host/Lab/mt-esx-labnd2.mtx.vodafonemalta.com
Manufacturer: HP
Logical CPUs: 12 CPUs @ 2400MHz
Processor type: Intel(R) Xeon(R) CPU E7450 @ 2.40GHz
CPU usage: 3912 MHz (13.6%)
Memory: 131068MB
Memory usage: 39246 MB (29.9%)
Boot time: 2019-09-16 10:22:57.210999 +0000 UTC
State: connected
Automation with govc and bash scripting
One of the best use of Govc is that we can use esxcli on vsphere host and launch cli command on Virtual Machine ( required Vmtools or openvm-tools installed on the vm ).
By example, if I need to get information of all my Esxi, I can use a bash script, make a loop and launch EsxCli on all my hosts. Simple, fast and powerful 😄
Let’s make a real scenario.
We need to update our infrastructure in term of Storage or Virtual Connect by example and we always need to get the information of the drivers version for our HBA Cards.
If we have to do this work manually, it’s taking easily half day and maybe more.
Then, I made a Bash script using govc to get all this information :
for i in 1 2 3 4
do
echo mt-esx-superserver$i.mtx.vodafonemalta.com
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com storage core adapter list
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com network nic get -n vmnic0 | grep FirmwareVersion
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com network nic get -n vmnic9 | grep FirmwareVersion
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com network nic get -n vmnic2 | grep FirmwareVersion
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com software vib list | grep fc
govc host.esxcli --host.dns=mt-esx-superserver$i.mtx.vodafonemalta.com software vib list | grep elxnet
done
The result will be as below and I will have the information for all my hosts :
mt-esx-superpower1.mtx.vodafonemalta.co
HBA Name Driver Link State UID Capabilities Description
-------- ------ ---------- --- ------------ -----------
vmhba64 qfle3f link-n/a fcoe.10009cb654939e69:20009cb654939e69 Second Level Lun ID () Broadcom Corporation FCoE Adapter
vmhba65 qfle3f link-up fcoe.10009cb654939e6d:20009cb654939e6d Second Level Lun ID () Broadcom Corporation FCoE Adapter
vmhba66 qfle3f link-up fcoe.10009cb654844271:20009cb654844271 Second Level Lun ID () Broadcom Corporation FCoE Adapter
vmhba67 qfle3f link-n/a fcoe.10009cb654844275:20009cb654844275 Second Level Lun ID () Broadcom Corporation FCoE Adapter
vmhba32 vmkusb link-n/a usb.vmhba32 () USB
FirmwareVersion: Storm: 7.13.11.0 MFW: 7.15.78
FirmwareVersion: Storm: 7.13.11.0 MFW: 7.15.78
FirmwareVersion: Storm: 7.13.11.0 MFW: 7.15.78
hpe-esxi-fc-enablement 650.2.6.10-4240417 HPE PartnerSupported 2020-02-04
scsi-bnx2fc 1.713.20.v60.4-1OEM.600.0.0.2494585 QLogic VMwareCertified 2020-02-04
brcmfcoe 11.4.1078.25-14vmw.650.3.96.13932383 VMW VMwareCertified 2020-05-04
lpfc 11.4.33.25-14vmw.650.3.96.13932383 VMW VMwareCertified
Another scenario, by example, you want to get the filesystem storage space information for a number of Virtual machine.
Let’s assume you have a list of vm or you want to make a test and you will take 3 VMs :
govc find . -type m | cut -d '/' -f5 | grep mt* | head -n 3 > listvmtest.txt
Once you have your file as below :
mt-ub-postfix
mt-ub-teleg1
mt-ub-repo1
Let’s make the script to get the filesystem info with the df command:
#!/bin/bash
for i in $(cat /usr/local/bin/listvmtest.txt)
do
echo $i
govc guest.df -vm $i
done
And here is the result :
root@mt-ub-terform:/usr/local/bin# ./getDiskStorageVm.sh
mt-ub-postfix
Filesystem Size Used Avail Use%
/ 20.1GB 7.0GB 13.1GB 35%
/boot 471.6MB 182.2MB 289.4MB 39%
mt-ub-teleg1
Filesystem Size Used Avail Use%
/ 15.2GB 7.3GB 7.9GB 48%
/boot 471.6MB 285.5MB 186.1MB 61%
mt-ub-repo1
Filesystem Size Used Avail Use%
/ 15.2GB 6.8GB 8.4GB 45%
/boot 471.6MB 208.2MB 263.4MB 44%
/ubuntu_repo 196.7GB 157.0GB 39.7GB 80%
I can hear you thinking, wow it’s very cool but I have windows VM as well in my infrastructure.
No problem, only solution ! It works as well for windows VMs ! It will only required that vmtools is installed on the windows vm.
root@mt-ub-terform:/usr/local/bin# ./getDiskStorageVm.sh
mt-wi-trmsrv01
Filesystem Size Used Avail Use%
C:\ 99.9GB 66.8GB 33.1GB 67%
For conclude, GOVC is very powerful and very fast and can be used for a lot of different purpose.
- create network, resource pool
- create/remove/clone vm, network, resource pool, cluster, datastore, …
- poweroff/on vm
- vmotion or svmotion/ vm
- Upload file on Vms or upload ISO on Datastore
- Launch command on Vms
- And Much more !
If you want to know more about Govc, have a look on the official documentation :
And on my GitHub repository for Govc : GitHub Govc