Save Money Using Citrix Octoblu Automation for Power Management

Save Money Using Citrix Octoblu Automation for Power Management

Save Money Using Citrix Octoblu Automation for Power Management

This post has already been read 6489 times!

Running a home lab is cool but can become expensive.  As your lab grows so will your noise levels at night and your electricity bill, something I am sure many of you will agree annoys your wife/partner etc.  I was looking at how I can use Octoblu to provide a manageable way to automate the start up and shut-down sequences of my lab whilst offering me the flexibility to manually kick off the routines if I need to get to my lab out of my “defined” Octoblu core hours.  It will also save me some cash as I will not be running the lab overnight and whilst I am not using it.

So, this post will walk you through setting up Octoblu to connect and manage your home lab (this example uses XenServer but you should be able to find Powershell commands for the other available hypervisors)

Lets get going.

Octoblu Connector

First you will need a shell connector to allow Octoblu to execute commands from within your perimeter network.  Head over to https://app.octoblu.com and log in with your credentials.  Then navigate to Things and All Things

01-things

Scroll down until you find the Utilities Section and click to ass (I did mean add but made a typo and it’s funny, so it stays. Thanks @andyjmorgan for pointing out) a Shell Thing

02-shell

Octoblu will prompt you to select a version that you want to use.  The latest version will be fine.

03-use-this-version

Finally select to download the version for the operating system you want to run the connector on.

04-download-your-version

Once you have the installer ZIP file downloaded copy it to the machine that you want to use for the connector.

NOTE: This machine will need to stay on all the time to facilitate the access for Octoblu.

Once you have the ZIP file copy it into the root or a directory that you want to use to run your MeshBlu connectors from.

This is important EXTRACT THE INSTALLER PRESERVING THE NAMING CONVENTION.  If you attempt to rename the directory the installer will not work.

05-extract-to-local-server

Once extracted open up the directory and run “MeshbluConnectorInstaller.exe”

06-run-installer

Click Begin Install and let the installer run through keeping all the defaults.  Once done switch back to the Octoblu web page and click on begin configuring.

Configure your connector as shown below.  Note the command line, file extension and argument.  You will need all these in place to make it work as expected.

07-configure-thing

Click on Save and you will see your new connector in Octoblu and it will show as on-line.

08-thing-live

XenServer SDK

The next step will be to get your local Powershell environment ready to issue the commands to power manage your machines and hosts.

You can get the XenServer 7.0 SDK from here:

http://downloadns.citrix.com.edgesuite.net/11625/XenServer-7.0.0-SDK.zip

Extract this ZIP File and copy the XenServerPSModule directory to the Modules directory in the same path that you defined for powershell.exe in the shell thing previously.

09-xenserver-powershell-commandlets

Powershell Script

Next, the script.  Now this is a simple script and by now means robust enough for a production environment but it does server my purpose well.  Feel free to enhance this and drop me your new code – happily update this post with better script examples!

UPDATE: This is why I love the Citrix Community.  After posting this Eric from XenApp Blog dropped me a tweet with a one liner that will shut down all the VM’s on the XenServer Pool.  I went back to him saying that I needed to keep a couple of machines running (Octoblu Connector and Xen Orchestrata for Backups) and he said he would come back with something.  So, below is a new script courtesy of Eric @ XenApp Blog – Thank you !

The Profile.ps1 File

This file allows you to define functions that will be available within powershell without having to load any specific modules.  When the powershell exe file is launched all contents from this file are read into memory at launch and allows you to use the functions straight away!

To get this set up:

First Open C:\Windows\System32\WindowsPowerShell\v1.0

If not there create a file called profile.ps1, if it is there, open it for editing

Below is the updated script.  When Adding it to the profile.ps1 file be sure to put the commands inside a function like:

function RunMe {

COMMANDS

}

Then from powershell to execute these commands you would only need to type in RunMe

NOTE: make sure to update the e-mail code at the bottom of the script

NOTE 2: Don’t forget to change the host name and password

NOTE 3: Create a text file called vmlist.txt in c:\scripts containing all the machines you want to shut down

function Lab-Shutdown {
  $ErrorActionPreference = 'Continue'
  import-module XenServerPSModule
  connect-xenserver -url http://xs.bretty.local -username USERNAME -password PASSWORD
  $file = "c:\Scripts\vmlist.txt"
  $VMNames = Get-Content $file
  foreach($VMName in $VMNames)
    {
      $VM = Get-XenVM -Name $VMName
      Write-Host "Scheduling Power off of VM '$VMName'"
      Invoke-XenVM -VM $VM -XenAction CleanShutdown -Async
    }
  Disconnect-XenServer
  $From = "xs@bretty.me.uk"
  $To = "dave@bretty.me.uk"
  $SMTPServer = "xsmail.bretty.me.uk"
  $SMTPPort = "25"
  $Username = "xs@bretty.me.uk"
  $Password = "PASSWORD"
  $subject = "bretty.local Lab Shutdown"
  $body = "bretty.local - lab has been shutdown"
  $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
  $smtp.EnableSSL = $false
  $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
  $smtp.Send($From, $To, $subject, $body);
}

Reverse the process and add a script to start-up your lab.

Here is the script for starting up the lab

function Lab-Startup {
  $ErrorActionPreference = 'Continue'
  import-module XenServerPSModule
  connect-xenserver -url http://xs.bretty.local -username USERNAME -password PASSWORD
  $file = "c:\Scripts\vmlist.txt"
  $VMNames = Get-Content $file
  foreach($VMName in $VMNames)
    {
      $VM = Get-XenVM -Name $VMName
      Write-Host "Scheduling Power on of VM '$VMName'"
      Invoke-XenVM -VM $VM -XenAction Start -Async
    }
  Disconnect-XenServer
  $From = "xs@bretty.me.uk"
  $To = "dave@bretty.me.uk"
  $SMTPServer = "xsmail.bretty.me.uk"
  $SMTPPort = "25"
  $Username = "xs@bretty.me.uk"
  $Password = "PASSWORD"
  $subject = "bretty.local Lab Startup"
  $body = "bretty.local - lab has been started"
  $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
  $smtp.EnableSSL = $false
  $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);
  $smtp.Send($From, $To, $subject, $body);
}

Don’t forget NOT to shutdown the machine you use for the Meshblu Connector

The Octoblu Flow

Head back over to Octoblu and create a new flow that looks like the below.  I will give more detail about the components in the following images.

10-flow-details

The Trigger: This is here so that you can manually trigger this flow if required from your iPhone App or Octoblu itslef (or your Apple Watch if you want to look like a right geek!)

The Text Message: I have put this in here so that I get notified when the Flow is started.  This lets me know that something is happening and the script will e-mail me when its all finished.  This is also good to time how long your lab is taking to start up and shut down.

The Scheduler: This is used to trigger the script.  This uses the Crontab time format.  A great tool to figure out the code you need can be found here: http://crontab.guru/

11-timer-thing

The Shell Thing.  All I need to put in here is the function name for the function in profile.ps1 that shuts down my lab.  As this ps1 file is loaded for all users when launching powershell these functions will be available within powershell when executed from Octoblu

12-script-thing

Both Flows

Create a flow for Start-Up and Shutdown and specify the times you want this to happen

13-startup-and-shutdown

On Demand

If you don’t want to wait for Octoblu to trigger your flow you wan always run them manually from the App

14-app-access

That’s it, how to provide simple power management capability to your home lab using Citrix Octoblu, XenServer SDK and Powershell.

Hopefully this will help some of you out and will save you some money!

Laters,

b@m

2 thoughts on “Save Money Using Citrix Octoblu Automation for Power Management

  1. Pingback: Power On Servers Through IPMI with Citrix Octoblu - xenappblog

  2. Pingback: Save Money Using Citrix Octoblu Automation for Power Management

Leave a Reply

Your email address will not be published. Required fields are marked *