Topics: Scripting, Virtualization
Using govc
The vSphere web GUI is a nice visual tool, but if you need to retrieve vCenter information in bulk or perform mass operations across VMs, then a command line tool such as govc in invaluable. You can find the repo for govc at https://github.com/vmware/govmomi/tree/master/govc, along with installation instructions. govc is written in Go, which means it has support on Linux as well as most other platforms.
To perform a quick install on Linux, run this command:
Next, you'll want to set up basic connectivity to the vCenter, and for this purpose, you can use a set of environment variables, so the CLI knows how to connect to the vCenter.$ sudo curl -L -o - \ "https://github.com/vmware/govmomi/releases/latest/download/govc_$(uname -s)_$(uname \ -m).tar.gz" | sudo tar -C /usr/local/bin -xvzf - govc
Next, you can try out a few basic commands:# vCenter host export GOVC_URL=myvcenter.name.com # vCenter credentials export GOVC_USERNAME=myuser export GOVC_PASSWORD=MyP4ss # disable cert validation export GOVC_INSECURE=true
Next, set a variable $dc, so that we can use it later:$ govc about Name: VMware ESXi Vendor: VMware, Inc. Version: 6.7.0 Build: 8169922 OS type: vmnix-x86 API type: HostAgent API version: 6.7 Product ID: embeddedEsx UUID $ govc datacenter.info Name: mydc Path: /mydc Hosts: 1 Clusters: 0 Virtual Machines: 3 Networks: 1 Datastores: 1 $ govc ls /mydc/vm /mydc/network /mydc/host /mydc/datastore
Now you can request various information from the vCenter. For example:$ dc=$govc ls /)
Network:
$ govc ls -l=true $dc/network
ESXi Cluster:
# cluster name govc ls $dc/host # details on cluster, all members and their cpu/mem utilization govc host.info [clusterPath] # all members listed (type: HostSystem, ResourcePool) govc ls -l=true [clusterPath] # for each cluster member of type HostSystem, individual stats govc host.info [memberPath]
Datastores:
# top level datastores (type: Datastore and StoragePod) govc ls -l=true $dc/datastore # for atomic Datastore type, get capacity govc datastore.info [datastorePath] # get StoragePod overall utilization govc datastore.cluster.info [storagePodPath] # get list of storage pod members govc ls [storagePodPath] # then get capacity of each member govc datastore.info [storagePodMemberPath]
VM information:
# show basic info on any VM names that start with 'myvm' govc vm.info myvm* # show basic info on single VM govc vm.info myvm-001 # use full path to get detailed VM metadata vmpath=$(govc vm.info myvm-001 | grep "Path:" | awk {'print $2'}) govc ls -l -json $vmpath
Shtudown VM, power up VM:
# gracefully shutdown guest OS using tools govc vm.power -s=true myvm-001 # force immediate powerdown govc vm.power -off=true myvm-001 # power VM back on govc vm.power -on=true myvm-001