Quick and Dirty Scripting

A blog that focuses on automating system administration tasks for Linux, Windows, and VMware ESX

Monday, November 26, 2007

 

VMware's Very Cool Get-Stat Command

 

Today I want to briefly show  you the power of the VMware's Get-State command.  With it you can grab statistics on any VI entity - ESX Host, Virtual Machine, or Resource Pool.  To use it, all you need to do is supply the entity you want to monitor and which statistics do you want to grab (common, CPU, memory, disk, or network). So let's get some stats on an ESX host.

 $vc = Get-VIServer "vc1.example.com"

$esx = Get-VIHost "esx1.example.com"

Get-Stat $esx -Memory -maxsamples 3 -realtime

MetricId             Timestamp                                     Value Unit
---------            ----------                                    ----- ----
mem.vmmemctl.average 11/26/2007 1:52:2...                              0 KB
mem.vmmemctl.average 11/26/2007 1:52:0...                              0 KB
mem.vmmemctl.average 11/26/2007 1:51:4...                              0 KB

Value       : 21.49
Timestamp   : 11/26/2007 1:52:20 PM
MetricId    : mem.usage.average
Unit        : %
Description : Memory usage as percentage of total configured or available memory
Entity      : VMware.VimAutomation.Client20.VMHostImpl

Value       : 21.49
Timestamp   : 11/26/2007 1:52:00 PM
MetricId    : mem.usage.average
Unit        : %
Description : Memory usage as percentage of total configured or available memory
Entity      : VMware.VimAutomation.Client20.VMHostImpl

Value       : 21.49
Timestamp   : 11/26/2007 1:51:40 PM
MetricId    : mem.usage.average
Unit        : %
Description : Memory usage as percentage of total configured or available memory
Entity      : VMware.VimAutomation.Client20.VMHostImpl

mem.active.average   11/26/2007 1:52:2...                          78988 KB
mem.active.average   11/26/2007 1:52:0...                          78988 KB
mem.active.average   11/26/2007 1:51:4...                          78988 KB
mem.granted.average  11/26/2007 1:52:2...                        2097504 KB
mem.granted.average  11/26/2007 1:52:0...                        2097504 KB
mem.granted.average  11/26/2007 1:51:4...                        2097504 KB

Get-Stat $esx -common -maxsamples 1 -realtime

MetricId             Timestamp                                     Value Unit
---------            ----------                                    ----- ----
cpu.usage.average    11/26/2007 2:05:4...                           4.86 %

Value       : 297
Timestamp   : 11/26/2007 2:05:40 PM
MetricId    : cpu.usagemhz.average
Unit        : MHz
Description : CPU usage in MHz over the collected interval. For hosts this can be represented on a per Virtual Machine
              basis as a stacked graph
Entity      : VMware.VimAutomation.Client20.VMHostImpl

mem.usage.average    11/26/2007 2:05:4...                          21.48 %

Value       : 105
Timestamp   : 11/26/2007 2:05:40 PM
MetricId    : disk.usage.average
Unit        : KBps
Description : Aggregated storage performance statistics. For hosts this can be represented on a per Virtual Machine bas
              is as a stacked graph
Entity      : VMware.VimAutomation.Client20.VMHostImpl

And finally,

$stats = Get-Stat $esx -common -maxsamples 1 -realtime

$stats. GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

Since the object returned to you is an array,this allows you to store and manipulate the return results - like

$stats[2]

MetricId             Timestamp                                     Value Unit
---------            ----------                                    ----- ----
mem.usage.average    11/26/2007 2:07:0...                          21.48 %

or $stats[2].Value

21.48

These values can then be piped into a table or graph to display the results.

Next week, I want to go back to something basic in Powershell - XML configuration. Powershell makes parsing XML documents very easy and I want to show how you can use this to create XML configuration files.

Labels: , ,


Monday, November 5, 2007

 

PowerShell and VMware Managment

I know that I just blogged yesterday about sending emails with PowerShell, BASH, and VBScript, but I got a chance today to install VMware's PSSnapin for Powershell. From the looks of this beta, its going to be a very powerful product that almost anyone can write scripts for. What an example.

I wrote a script in Perl using VMware's Perl SDK to display any VM Snapshot that a supplied VirtualCenter knows about. The code to do this in Perl is as follows:

use strict;
use warnings;
use Getopt::Long;
use VMware::VIRuntime;
use Term::ReadKey;

my %opts = (server => undef, username => undef,);

GetOptions (\%opts,"server=s", "username=s");

if( !defined ($opts{server} && $opts{username}) ) {
help();
exit (1);
}

ReadMode('noecho');
print "Enter $opts{username}\'s password: ";
my $password = ReadLine(0);
chomp $password;
ReadMode('normal');

# login
my $url = "https://" . $opts{server}. "/sdk/vimService";
Vim::login(service_url => $url, user_name => $opts{username}, password => $password);

# get VirtualMachine views for all powered on VM's
my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine');

# snapshot each VM
my $i;
foreach (@$vm_views) {
if( defined $_->snapshot ) {
print "-------------------------------------------------------------\n";
print $_->name . " has at least one snapshot defined . . . \n";
foreach $i (0 .. $#{$_->snapshot->rootSnapshotList} ) {
print "\t" . $_->snapshot->rootSnapshotList->[$i]->name . " @ " . $_->snapshot->rootSnapshotList->[$i]->createTime . "\n";
}
print "-------------------------------------------------------------\n";
}
}

# logout
Vim::logout();

sub help {
my $help_text = <<'END'; USAGE: query_snapshot.pl --server --user

Example:
query_snapshot.pl --server vc1 --user administrator

END
print $help_text;
}

Now using Powershell and the VMware Snapin:

Add-PSSnapin VMware.VimAutomation.Core
Get-VIServer vc1.example.com
Get-VM | % { $_.Name; Get-Snapshots $_ }

That's it. Everything is provided for you like Authentication, handling XML returned by VirutalCenter's Web Services, etc . . . The Perl SDK is very powerful as well, but you have to do a lot more coding to get things done. With the PowerShell snapin, the script is all of three lines. Now the snapin is only in beta testing right now so things can change in the future.

Labels: ,


Archives

October 2007   November 2007   December 2007   January 2008   February 2008   April 2008   July 2008  

View Brian Denicola's profile on LinkedIn

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]