Runbook results email notification

We are setting up a runbook to process a series of common monitoring tasks that are currently performed manually. For example, we want to retrieve the current disk space usage and disk free space for each server and then email that to the relevant parties.

The results are mostly neatly displayed in the WebUI (see screenshot below) but it appears to be impossible to get the same information (not worried about style for now) into an email.

Am I fighting a losing battle?

Hey @William_Hurst,

Thanks for reaching out.

I think your path forward here will be to use output variables.

Essentially what you will do is you will store the data inside of an output variable, then later reference it in the email step to send within the email.

Here is some documentation on Output Variables: Output variables - Octopus Deploy

Can you please give that a look and let me know if you think it will work for your use case or if we need to dig in a bit more?

Thanks,
Jeremy

Hey Jeremy

Output variables seem like a very viable option.

I have 2 questions:

  1. Is there a way to output an array? In this case there are multiple drives on multiple servers that need a return message.
  2. Are there any resources for understanding how I can format the output so that it goes into the notification with styling, line breaks, etc? I have played around with it for about 30 minutes but I couldn’t quite get it right.

Hey William,

I took a stab at making it work this morning and I was able to get a basic form of it going. It’s not doing every data point, I just did 1 data point as a test.

Here is what I did:
First, I edited the Step Template and modified the following code:

Set-OctopusVariable -name "NumDrives" -value "$($drivelist.Count)"
# ================= RUN CHECKS =================
$i = 0
foreach($d in $driveList) {
$driveDescr = "$($d.DeviceID) [$($d.VolumeName)]"
$pDrivespaceGBFree = [Math]::Round(($d.FreeSpace / [Math]::Pow(1024,3)), 1)
$pDrivespaceGBTotal = [Math]::Round(($d.Size / [Math]::Pow(1024,3)), 1)
$pDrivespacePercentFree = [Math]::Round($pDrivespaceGBFree / $pDrivespaceGBTotal,1) * 100
Write-Host "Drive $driveDescr : Free $pDrivespaceGBFree GB ($pDrivespacePercentFree%), Total $pDrivespaceGBTotal GB"
Set-OctopusVariable -name "Drive$($i)" -value "Drive $driveDescr : Free $pDrivespaceGBFree GB ($pDrivespacePercentFree%), Total $pDrivespaceGBTotal GB"

if ($checkSpaceAbsolute) {
    if ($pDrivespaceGBFree -lt $pSpaceGB) { 
        Write-Error "Drive $driveDescr has less than the required space ($pSpaceGB GB)"
    }
}
if ($checkSpacePercent) {
    if ($pDrivespacePercentFree -lt $pSpacePercent) { 
        Write-Error "Drive $driveDescr has less than the required space ($pSpacePercent %)"
    }
}
$i++
}
  • First I created an output variable for numdrives and did a count on the drivelist array to see how many drives there were. We use this in the next step of the process

  • Then I created $i to create a dynamic amount of output variables. $i =0 before the loop starts, and $i++ at the end of the loop.

  • The big add here is this line:
    Set-OctopusVariable -name "Drive$($i)" -value "Drive $driveDescr : Free $pDrivespaceGBFree GB ($pDrivespacePercentFree%), Total $pDrivespaceGBTotal GB"

This will create the dynamic output variable to later be iterated through.

Our next step is to collate the data into one output variable to mail. I did a run a script(on the server or worker) with the following logic:

$machines = @()

#{each machine in Octopus.Action[Run A Script].Output}
 $machines += "#{machine}"
#{/each}
$dataforemail
$machines = $machines | Sort-Object

$machines | ForEach-Object {
  Write-Highlight $_
  
	$drivecount = $OctopusParameters["Octopus.Action[Run A Script].Output[$_].NumDrives"]
    
    for ($i=0;$i -lt $drivecount;$i++){
    $drivedata = $OctopusParameters["Octopus.Action[Run A Script].Output[$_].Drive$($i)"]
    Write-Highlight $drivedata
    $dataforemail += $_
    $dataforemail += "<br>"
    $dataforemail += $drivedata
    $dataforemail += "<br>"
    
    
    }
  }

Set-OctopusVariable -name "EmailData" -value "$dataforemail"
  • This first creates an array of all the machines to later iterate through.
  • Then we create a variable that we will add all of the data to ($dataforemail)
  • Then we go through machine, set the number of drives for the for loop, iterate through the drives and add the data to $dataforemail.
  • Once all the data has been added, we simply put this in an output variable.

Finally, we have the email step:

All of this results in the following email:

image

There is a double title for the name of the tentacle for extra drives, but you could put an if statement for if $i -eq 0 so that you only get one machine name to resolve that.

For instance:

        if ($i -eq 0){
        $dataforemail += $_
        }
        $dataforemail += "<br>"
        $dataforemail += $drivedata
        $dataforemail += "<br>"

I hope I explained that properly.

Please let me know if you get it going on your end or if you have any questions.

Thanks,
Jeremy

That helped a lot. I tinkered a little bit more and I managed to get close to what I was looking for. Thank you for your assistance.

Hey @William_Hurst,

You’re very welcome. Thank you for letting me know you’re in a good state.

I hope you have a great rest of your week.

Thanks,
Jeremy

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.