6.0
Table Of Contents
- vSphere SDK for Perl Programming Guide
- Contents
- Getting Started with vSphere SDK for Perl
- Writing vSphere SDK for Perl Scripts
- Refining vSphere SDK for Perl Scripts
- Creating and Using Filters
- Filtering Views Selectively Using Properties
- Retrieving the ServiceInstance Object on a vSphere Host
- Saving and Using Sessions
- Using Multiple Sessions
- Learning About Object Structure Using Data::Dumper
- Specifying Untyped Arguments in Scheduled Tasks and Callbacks
- Using Advanced Subroutines
- vSphere SDK for Perl Subroutine Reference
- Web Services for Management Perl Library
- Credential Store Perl Library
VMware, Inc. 27
Chapter 2 Writing vSphere SDK for Perl Scripts
Managedentitiesofferspecificoperationsthatvarydependingontheentitytype.Forexample,a
VirtualMachine managedentityprovidesoperationsforcreating,monitoring,andcontrollingvirtual
machines.Youcanpoweravirtualmachineonoroff(PowerOnVM,PowerOffVM)andyoucancapturestate
(Snapshot).AHostSystem entityprovidesoperations
forenteringandexitingmaintenancemode
(EnterMaintenanceMode_Task,ExitMaintenanceMode_Task)andforrebootingtheserver
(RebootHost_Task).
TheManagedEntitybaseclassincludesseveralpropertiesthatareinheritedbyeachsubclass,suchasaname
property,whosedatatypeisastring.ManagedEntityalsoincludesafewoperationsthatareinheritedby
each
subclass(Destroy_Task,andReload,forexample).VirtualMachineandHostSystemextendthe
ManagedEntityclass,soeachsubclasshasanamepropertyinheritedfromManagedEntity.
Accessing Server-Side Inventory Objects
ThevSphereSDKforPerlprovidessubroutinesforaccessingserver‐sideinventoryobjectsandothermanaged
objectsthatprovidefunctionalitytotheserverasawhole.
Example 2‐1obtainsallentitiesofaspecifictypefromtheinventory.Theentitytypeispassedasaparameter
totheVim::find_entity_views()subroutine,which
returnsanarrayofreferencestoviewobjectsthatmap
tothecorrespondingserver‐sideentities.
Example 2‐2startsattheleveloftheentireserviceandusestheVim::get_service_content()subroutine
toobtainaninstanceoftheServiceContentobject:
my $content = Vim::get_service_content();
YoucanusetheServiceContentobjecttoretrievealocalviewoftheservicesprovidedbytheserver,asin
thisexample:
my $diagMgr = Vim::get_view(mo_ref => $content->diagnosticManager);
Example 2‐2showshowthesetwocallsformthebasisofascriptthatfollowschangesinthelogfile,whichcan
beaccessedasthelogfilepropertyofthediagnosticManager.
Example 2-2. Following Changes in a Log File
#!/usr/bin/perl
#
# Copyright 2007 VMware, Inc. All rights reserved.
#
# This script creates a Perl object reference to the ServiceContent data
# object, and then creates a reference to the diagnosticManager. The script
# follows ('tails') the log as it changes.
use strict;
use warnings;
use VMware::VIRuntime;
# read/validate options and connect to the server
Opts::parse();
Opts::validate();
Util::connect();
# get ServiceContent
my $content = Vim::get_service_content();
my $diagMgr = Vim::get_view(mo_ref => $content->diagnosticManager);
# Obtain the last line of the logfile by setting an arbitrarily large
# line number as the starting point
my $log = $diagMgr->BrowseDiagnosticLog(
key => "hostd",
start => "999999999");
my $lineEnd = $log->lineEnd;
# Get the last 5 lines of the log first, and then check every 2 seconds
# to see if the log size has increased.
my $start = $lineEnd - 5;