Scripting VmCOM and VmPerl Scripting techniques
ESX Server System Management II Module 8
Why use scripting? • Automate tasks • For scheduled execution • To avoid manual errors
• Gather information about servers’ and VMs’ state • For interactive display • For remote logging
• Build simple single-purpose interfaces
2 2
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
VMware Scripting APIs (version 2.1) • Use these tools to script • GSX Server 2.0 and later • ESX Server 2.0
• Key advantages of new APIs • Interoperable control of VMs under GSX Server and ESX Server • Convenient scripting from Windows applications using COM • Convenient scripting from Linux and Windows Perl scripts
• Download documentation from http://www.vmware.com/support/developer/ 3 3
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Getting ready to use VmCOM • Download installer binary from VMware Web site • Go to Download section of http://www.vmware.com • Log in using registered user account
• Run installer VMware-VMCOM-xxxx.exe • Add to your development environment • Visual Basic: Choose Project References • Click to enable VMware VmCOM version Type Library
• Or ensure that Windows Scripting Host is installed 4 4
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
VmCOM with Windows Scripting Host • Create a wsf file to bind your script to VmCOM libraries <job id="myscript"> <script language="VBScript" src="myscript.vbs" />
• Write script • VmCOM objects will be available to you
• Run script cscript //nologo myscript.wsf 5 5
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Getting ready to use VmPerl • Make sure that Perl is installed • Linux: Use Perl 5.00503 or later • Windows: Use the build of Perl shipped in VmPerl package
• Install VmPerl package • Copy installer binary from VMware Web site or server media • Linux: tar xzvf VMware-VMPERL-v.v.v-xxxx.tar.gz cd vmware-api-distrib ./vmware-install.pl • Windows: VMware-VMPERL-xxxx.exe
6 6
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Connecting to VMs with both APIs CLICK WIZARD LINK 1
2
Create and fill connection object
Create server object
5
3
6
Create VM object
Connect to server
Enumerate VMs
7
Connect to desired VM
7 7
4
Query or modify VM
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Key preliminaries for Perl BEGIN { if ($^O eq "MSWin32") { push(@INC,VmPerl-lib-dir); } } use use use use
VMware::VmPerl; VMware::VmPerl::ConnectParams; VMware::VmPerl::VM; VMware::VmPerl::Question; 8
8
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Steps 1, 2, and 3: Creating and connecting the server object Server object
Params object
• VmCOM (VBS): Set cp = CreateObject("VmCOM.VmConnectParams") Set server = CreateObject("VmCOM.VmServerCtl") cp.Hostname = "hostname" cp.Username = "username" cp.Password = "password" server.Connect cp
9 9
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Step 4: Enumerate VMs vmlist /home/fred/vmware/b/b.vmx /home/susie/vmware/w/w.vmx …
Server object • VmCOM (VBS):
Set vmlist = server.RegisteredVmNames
10 10
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Steps 5 and 6: Creating and connecting the vm object VM object
/home/fred/vmware/b/b.vmx
• VmCOM (VBS): Set vm = CreateObject("VmCOM.VmCtl") vm.connect cp, configfile
11 11
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Step 7: Querying the VM VmCOM (VBS) Return the power state of the VM
vm.ExecutionState
Return the value of a configuration-file item
vm.Config(item) Example: vm.Config("memSize")
Return the VM’s heartbeat count
vm.Heartbeat
Detect whether a device is connected
vm.DeviceIsConnected(device) Example: vm.DeviceIsConnected("ide1:0")
Get statistics
vm.Resource(resource) Example: vm.Resource("cpu.waitsec")
12 12
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Step 7: Managing the VM VmCOM (VBS) Return the state of the VM
vm.ExecutionState
Cleanly power on or resume
vm.Start(vmPowerOpMode_Soft)
Cleanly shut down and power off
vm.Stop(vmPowerOpMode_Soft)
Run scripts then suspend
vm.Suspend(vmPowerOpMode_Soft)
Set VM resources
vm.Resource(resource) = value Example: vm.Resource("cpu.shares") = 500
• Soft power modes run scripts before or after the operation; require VMware Tools 13 13
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Answering state-change questions (VmCOM/VBS)
if vm.ExecutionState = vmExecutionState_Stuck then Set q = vm.PendingQuestion Set choices = q.Choices ' display text and choice list ' get response (numeric index into list) vm.AnswerQuestion q, 1 end if
14 14
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Passing information out of a VM • Uses: a VM can report its SID, its IP address, an application’s state, etc. • Create arbitrarily named keys with values Example: myIP = 192.168.130.2
• To set a key (Windows): VMwareService.exe --cmd "info-set guestinfo.myIP 192.168.130.2"
• To read a key’s value (VmCOM/VBS): vm.GuestInfo("myIP")
15 15
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
The vmware-cmd utility • Easy command-line interactions with servers, VMs • Embed in batch files or shell scripts • For remote use, supply –H hostname –U username –P password
• To list VMs: vmware-cmd –l
• To register a VM: vmware-cmd –s register /home/fred/…/b.vmx
• To start a VM: vmware-cmd /home/fred/…/b.vmx start
• To check for heartbeat: vmware-cmd /home/fred/…/b.vmx getheartbeat
wait a few seconds, then repeat
16 16
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Scheduling program execution • Use Control Panel Scheduled Tasks on Windows • Use cron under Console OS and remote Linux hosts export EDITOR=vi crontab –e min hr mday mon wday command … ….
17 17
For ESX Server 2.0.1 2003-10-27 Copyright © 2003 VMware, Inc. All rights reserved.
Questions?
ESX Server System Management II Module 8