Вы находитесь на странице: 1из 18

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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
2
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
3
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
4
VmCOM with Windows Scripting Host

• Create a wsf file to bind your script to VmCOM


libraries
<job id="myscript">
<reference object="VmCOM.VmCtl" />
<script language="VBScript"
src="myscript.vbs" />
</job>

• Write script
• VmCOM objects will be available to you

• Run script
cscript //nologo myscript.wsf

5 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
5
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
6
Connecting to VMs with both APIs
CLICK
WIZARD
LINK
1 2 3 4
Create and fill
Create server Connect to
connection Enumerate VMs
object server
object

5 6 7

Create VM Connect to Query or modify


object desired VM VM

7 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
7
Key preliminaries for Perl

BEGIN {
if ($^O eq "MSWin32") {
push(@INC,VmPerl-lib-dir);
}
}

use VMware::VmPerl;
use VMware::VmPerl::ConnectParams;
use VMware::VmPerl::VM;
use VMware::VmPerl::Question;

8 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
8
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
9
Step 4: Enumerate VMs

vmlist
/home/fred/vmware/b/b.vmx
Server /home/susie/vmware/w/w.vmx
object …

• VmCOM (VBS):
Set vmlist = server.RegisteredVmNames

10 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
10
Steps 5 and 6: Creating and connecting
the vm object

VM /home/fred/vmware/b/b.vmx
object

• VmCOM (VBS):
Set vm = CreateObject("VmCOM.VmCtl")
vm.connect cp, configfile

11 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
11
Step 7: Querying the VM
VmCOM (VBS)
Return the power state of the vm.ExecutionState
VM
Return the value of a vm.Config(item)
Example: vm.Config("memSize")
configuration-file item
Return the VM’s heartbeat vm.Heartbeat
count
Detect whether a device is vm.DeviceIsConnected(device)
Example:
connected
vm.DeviceIsConnected("ide1:0")
Get statistics vm.Resource(resource)
Example:
vm.Resource("cpu.waitsec")

12 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
12
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 vm.Stop(vmPowerOpMode_Soft)


off
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
13
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
14
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
15
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
16
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 For ESX Server 2.0.1 2003-10-27


Copyright © 2003 VMware, Inc. All rights reserved.
17
Questions?

ESX Server System Management II


Module 8

Вам также может понравиться