XenServer PowerShell: Capture Metrics


Sometimes, I need to capture CPU, RAM and disk metrics on my XenServer hosts.  I’ve been able to automate this process with PowerShell.

Start-XSMetrics

This script starts the metric gathering process on a XenServer host.

The first block are the parameters.

function Start-XSMetrics {
    param(
        [string] $testname, #This will be the name of the file
        [string] $hostserver, #the XenServer host to capture metrics 
        [string] $SSHModulePath, #3rd part SSH module
        [string] $XenServerUsername, #Username for XenServer admin
        [string] $xenserverPW #Password for XenServer admin
        )
    
    # Add the SSH PowerShell Module
    import-module $SSHModulePath -force

To make this work, I need to create a SSH session to the XenServer host. I had to use a PowerShell module to make this happen (SSH from PowerShell source).

On XenServer, I use the command “rrd2csv” to to gather system-level metrics and export to a comma separate file. In addition, I set the following parameter

-n     uses  the performance metric label instead of a GUID

-s     interval, in seconds, between metric capture.

I want to run this command and output to a CSV file.

    #Write log
    Write-host (date -Format hh:mm:ss)   -   Starting metric gathering on XenServer host $hostserver -ForegroundColor Yellow
 
    #start-process powershell.exe -Argument ".\CaptureXSMetrics.ps1 $testname $hostserver" -WindowStyle Minimized
    $command = "rrd2csv -n -s 5 >" + $testname + ".csv"

The final part is to make the SSH connection and run the command, which is done as follows:

    New-SshSession -Computername $hostserver -Username $XenServerUsername -password $xenserverPW
    Write-host (date -Format hh:mm:ss)   -   Capturing metrics. Leave window open -ForegroundColor Red
    invoke-sshcommand -computername $hostserver -Command $command
    Remove-SshSession -ComputerName $hostserver  
} 

The PowerShell window will remain active until the rrd2csv process is terminated.

A word of caution, if you break out of the PowerShell command, rrd2csv remains running. This is why I have a stop metrics function

Stop-XSMetrics

function Stop-XSMetrics {
    param(
        [string] $hostserver,
        [string] $XenServerUsername,
        [string] $xenserverPW
        )

    #Stop XenServer metrics gathering       
    Write-host (date -Format hh:mm:ss)   -   Stop gathering XenServer metrics on $hostserver server -ForegroundColor Yellow
    import-module SSH-sessions 
    New-SshSession -Computername $hostserver -Username $XenServerUsername -password $xenserverPW
    invoke-sshcommand -computername $hostserver -Command 'pkill rrd2csv'
    Remove-SshSession -ComputerName $hostserver
}

 

Daniel (Follow on Twitter @djfeller)
Citrix Workspace Poster
XenApp/XenDesktop On-Prem Poster
XenApp/XenDesktop Cloud Service Poster

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: