Retrieve statistical information for all virtual machines in a object level hierarchy

So the challenge was to obtain stats from a collection of all virtual machines in a particular object hierarchy within vCenter, this being either the datacenter, cluster, resource pool or folder level.

The requirement was to capture all metrics that are for each virtual machine for a 24 hour period, using the closest available statistical interval (default value of ‘5’ minutes) and output to a CSV file.

In order to complete this task, I would require to download and install the powershell interface for vSphere; VMware vSphere PowerCLI  – communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli.

Firstly, I would need to specify a number of variables, in order to connect to vCenter server, the container object  and to specify the output file; these currently are manually entered into the script, however these could be passed as parameters from the scripting interface using arguments or using the read-host cmdlet for manual user input.

# Powershell variables required to be completed to store vCenter Server and output information
$VCenter = “” # Enter the vCenter Server hostname or IP address. For Example, SERVER1.domain.local
$ObjectName = “” #Enter the name of the vCenter Server container object. For Example, Production.
$CSVLocation = “” # Enter the output folder to export the CSV file. For Example, D:\Output\.
$CSVFileName = “” # Enter the filename for the exported CSV file. For Example, ProductionVM

As we are capturing the previous 24 hours, I will also require to to output the date and time sting to the CSV filename in the following format  DDMMYYYY_HHMM

# Powershell variable to add the timestamp as a suffix for the filename.
$Date = (get-date).toString(‘ddMMyyyy_HHmm’)

As the process will be required be automated, I will also need to the add the registered snap-in for PowerCLI to the current session to invoke the required cmdlets;

# Adds registered vSphere PowerCLI snap-ins to the current session
Add-PSsnapin VMware.VimAutomation.Core >$null

Now we will connect to the vCenter server passing the variable specified

# Connect to the vCenter Server
Connect-VIServer $VCenter >$null

Now that we are connected we will need obtain all the virtual machines that exist in the object container in the variable specified:

# Collects all virtual machines in the specified vCenter Server container object
$VM = Get-VM -Location ($ObjectName)

Once the virtual machines in the above object container have been obtained, I will need to loop through each virtual machine to firstly obtain all metrics being captured by running the Get-StatType cmdlet agaisnt the virtual machine and pipeing the output to a variable.

The virtual machine and metrics variable will then be passed to the Get-Stat cmdlet, as stated previously we will be required to obtain all metrics for a 24 hour period so the Start and Finish parameters will use the Date variable, with the substraction method to use the previous day as the start time for the entity.  The returned output is then filtered to only include Entity, Timestamp, MetricID, Value and Unit for each virtual machine , this is then exported to a CSV file.

ForEach ($i in $VM)
{
# Retrieves the statistical infromation for each virtual machine for the previous 24 hours using a 5 minute interval and exports to a CSV file.
$Metrics = Get-VM $i.Name | Get-StatType
$Stats = Get-Stat -Entity $i.Name -Stat $metrics -Start (Date).AddDays(-1) -Finish (Date)
$Stats | Select-Object Entity, Timestamp, MetricID, Value, Unit | Export-Csv ($CSVLocation + $CSVFileName + “_” + $Date + “.csv”) -NoTypeInformation
}

The script (Get-ObjectContainerStats.ps1) in its entirety may be downloaded from:

https://www.box.com/s/b5145a6ba94d5e3afdba

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s