The vSphere SDK for Perl provides subroutines for accessing server-side inventory objects and other managed objects that provide functionality to the server as a whole.
Sample Script (Commented Version) obtains all entities of a specific type from the inventory. The entity type is passed as a parameter to the Vim::find_entity_views() subroutine, which returns an array of references to view objects that map to the corresponding server-side entities.
Following Changes in a Log File starts at the level of the entire service and uses the Vim::get_service_content() subroutine to obtain an instance of the ServiceContent
object.
my $content = Vim::get_service_content();
You can use the ServiceContent
object to retrieve a local view of the services provided by the server, as in this example.
my $diagMgr = Vim::get_view(mo_ref => $content->diagnosticManager);
Following Changes in a Log File shows how these two calls form the basis of a script that follows changes in the log file, which can be accessed as the logfile
property of the diagnosticManager
.
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; # Disconnect on receipt of an interrupt signal while in the infinite loop below. $SIG{INT} = sub { Util::disconnect(); exit; }; while (1) { $log = $diagMgr->BrowseDiagnosticLog( key => "hostd", start => $start); if ($log->lineStart != 0) { foreach my $line (@{$log->lineText}) { # next if ($line =~ /verbose\]/); print "$line\n"; } } $start = $log->lineEnd + 1; sleep 2; }