Skip to content

Instantly share code, notes, and snippets.

@nmanzi
Created February 27, 2015 02:30
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nmanzi/42e5059d8b5967f3625f to your computer and use it in GitHub Desktop.
NetApp-SnapshotReport.ps1
#region Help
# ----------
<#
.SYNOPSIS
NetApp-SnapshotReport.ps1 will send an email to a specified address with a list of detected volume snapshots on
any volume belonging to specified controllers.
Highlights:
o Creates a plain but detailed and user-friendly HTML report which is compatible with all modern browsers.
Version History:
[x] Version 1.0 (Release) - 25/02/15
Requirements:
o NetApp DataONTAP Powershell Module
.DESCRIPTION
NetApp-SnapshotReport.ps1 will send an email to a specified address with a list of detected volume snapshots on
any volume belonging to specified controllers.
.PARAMETER Controllers
One or more NetApp Data ONTAP 7-mode controllers specified by IP or hostname.
.PARAMETER Username
Local admin username for the specified controller(s).
.PARAMETER Password
Password for specified username.
.PARAMETER SendMail
Send e-mail option ($true/$false). The default value is "$fale".
.PARAMETER SMTPServer
Mail server address.
.PARAMETER SMTPPort
Mail server port. The default value is "25".
.PARAMETER MailTo
A single mail recipient or an array of mail recipients.
.PARAMETER MailFrom
Mail sender address.
.PARAMETER MailFromPassword
Mail sender password for SMTP authentication.
.PARAMETER SMTPServerTLSorSSL
SMTP TLS/SSL option ($true/$false). The default value is "$fale".
.EXAMPLE
Check controllers controller1 and controller2 for running SIS processes, and email alert.
.\NetApp-ActiveDedupeAlert.ps1 -Controller controller1,controller2 -Username <user> -Password <"pass"> -SMTPServer <server> -MailFrom <Email From> -MailTo <Email To>
.INPUTS
None
.OUTPUTS
None
.NOTES
Author: Nathan Manzi
Website: http://nmanzi.com
Email: nathan@nmanzi.com
Date created: 25/02/15
Last modified: 25/02/15
Version: 1.0
Thanks to http://www.serhatakinci.com for the Hyper-V report the script was based on.
.LINK
http://nmanzi.com
#>
#endregion Help
#region Script Parameters
# -----------------------
[CmdletBinding(SupportsShouldProcess=$True)]
Param (
[parameter(
Mandatory=$true,
HelpMessage='NetApp Controller name/IPs separated by commas (like netapp01,netapp02 or 192.168.1.1,192.168.1.2)')]
[string[]]$Controllers,
[parameter(
Mandatory=$true,
HelpMessage='Username for NetApp Controllers')]
[string]$Username,
[parameter(
Mandatory=$true,
HelpMessage='Password to suit username provided')]
[string]$Password,
[parameter(
Mandatory=$false,
HelpMessage='Enable Send Mail (This is just a switch, value can not be assigned)')]
[bool]$SendMail = $false,
[parameter(
Mandatory=$false,
HelpMessage='SMTP Server Address (Like IP address, hostname or FQDN)')]
[string]$SMTPServer,
[parameter(
Mandatory=$false,
HelpMessage='SMTP Server port number (Default 25)')]
[int]$SMTPPort = "25",
[parameter(
Mandatory=$false,
HelpMessage='Mail To (Recipient e-mail address)')]
[array]$MailTo,
[parameter(
Mandatory=$false,
HelpMessage='Mail From (Sender e-mail address)')]
[string]$MailFrom,
[parameter(
Mandatory=$false,
HelpMessage='For SMTP Authentication (Sender e-mail address password)')]
[string]$MailFromPassword,
[parameter(
Mandatory=$false,
HelpMessage='SMTP TLS/SSL option ($true/$false). The default is "$fale".')]
[bool]$SMTPServerTLSorSSL = $false
)
Import-Module DataONTAP
#endregion
#region Functions
#----------------
Function getCredObject {
$strPass = ConvertTo-SecureString -String $Password -AsPlainText -Force
$objCred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList ($Username, $strPass)
return $objCred
}
# Convert Volume Size to KB/MB/GB/TB
Function sConvert-Size {
param (
# Disk or Volume Space
[Parameter(Mandatory = $true)]
$DiskVolumeSpace,
# Disk or Volume Space Input Unit
[Parameter(Mandatory = $true)]
[string]$DiskVolumeSpaceUnit
)
if ($DiskVolumeSpaceUnit -eq "byte") # byte input
{
if (($DiskVolumeSpace -ge "1024") -and ($DiskVolumeSpace -lt "1048576"))
{
$DiskVolumeSpace = [math]::round(($DiskVolumeSpace/1024))
$DiskVolumeSpaceUnit = "KB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1048576") -and ($DiskVolumeSpace -lt "1073741824"))
{
$DiskVolumeSpace = [math]::round(($DiskVolumeSpace/1024/1024))
$DiskVolumeSpaceUnit = "MB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1073741824") -and ($DiskVolumeSpace -lt "1099511627776"))
{
$DiskVolumeSpace = "{0:N1}" -f ($DiskVolumeSpace/1024/1024/1024)
$DiskVolumeSpaceUnit = "GB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1099511627776") -and ($DiskVolumeSpace -lt "1125899906842624"))
{
$DiskVolumeSpace = "{0:N2}" -f ($DiskVolumeSpace/1024/1024/1024/1024)
$DiskVolumeSpaceUnit = "TB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
Else
{
$DiskVolumeSpace = $DiskVolumeSpace
$DiskVolumeSpaceUnit = "Byte"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
}
elseif ($DiskVolumeSpaceUnit -eq "kb") # kb input
{
if (($DiskVolumeSpace -ge "1") -and ($DiskVolumeSpace -lt "1024"))
{
$DiskVolumeSpace = $DiskVolumeSpace
$DiskVolumeSpaceUnit = "KB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1024") -and ($DiskVolumeSpace -lt "1048576"))
{
$DiskVolumeSpace = ($DiskVolumeSpace/1024)
$DiskVolumeSpaceUnit = "MB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1048576") -and ($DiskVolumeSpace -lt "1073741824"))
{
$DiskVolumeSpace = "{0:N1}" -f ($DiskVolumeSpace/1024/1024)
$DiskVolumeSpaceUnit = "GB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1073741824") -and ($DiskVolumeSpace -lt "1099511627776"))
{
$DiskVolumeSpace = "{0:N2}" -f ($DiskVolumeSpace/1024/1024/1024)
$DiskVolumeSpaceUnit = "TB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
Else
{
$DiskVolumeSpace = $DiskVolumeSpace
$DiskVolumeSpaceUnit = "KB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
}
elseif ($DiskVolumeSpaceUnit -eq "mb") # mb input
{
if (($DiskVolumeSpace -ge "1") -and ($DiskVolumeSpace -lt "1024"))
{
$DiskVolumeSpace = $DiskVolumeSpace
$DiskVolumeSpaceUnit = "MB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1024") -and ($DiskVolumeSpace -lt "1048576"))
{
$DiskVolumeSpace = "{0:N1}" -f ($DiskVolumeSpace/1024)
$DiskVolumeSpaceUnit = "GB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
elseif (($DiskVolumeSpace -ge "1048576") -and ($DiskVolumeSpace -lt "1073741824"))
{
$DiskVolumeSpace = "{0:N2}" -f ($DiskVolumeSpace/1024/1024)
$DiskVolumeSpaceUnit = "TB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
Else
{
$DiskVolumeSpace = $DiskVolumeSpace
$DiskVolumeSpaceUnit = "MB"
return $DiskVolumeSpace, $DiskVolumeSpaceUnit
}
}
else
{
return "Unknown Parameter"
}
}
#endregion
#region Variables
#----------------
$CurrentDate = Get-Date -Format F
$htmlEmailHead = "
<head>
<style>
body{
width:100%;
min-width:1024px;
font-family: Verdana, sans-serif;
font-size:14px;
/*font-weight:300;*/
line-height:1.5;
color:#222222;
background-color:#fcfcfc;
}
p{
color:222222;
}
strong{
font-weight:600;
}
h1{
font-size:30px;
font-weight:300;
}
h2{
font-size:20px;
font-weight:300;
}
#ReportBody{
width:95%;
height:500;
/*border: 1px solid;*/
margin: 0 auto;
}
table{
width:100%;
min-width:1010px;
/*table-layout: fixed;*/
border-collapse: collapse;
border: 1px solid #ccc;
/*margin-bottom:15px;*/
}
/*Row*/
tr{
font-size: 12px;
}
tr:nth-child(odd){
background:#F9F9F9;
}
/*Column*/
td {
padding:10px 8px 10px 8px;
font-size: 12px;
border: 1px solid #ccc;
text-align:center;
vertical-align:middle;
}
/*Table Heading*/
th {
background: #f3f3f3;
border: 1px solid #ccc;
font-size: 14px;
font-weight:normal;
padding:12px;
text-align:center;
vertical-align:middle;
}
</style></head>
<h1>NetApp Volume Snapshot Report</h1>
<hr/>
<p>Volume snapshots on controllers <b>$($Controllers -Join ",")</b> as of <b>$CurrentDate</b></p><hr/>"
$htmlEmailTableHead = "
<table>
<tbody>
<tr>
<th><p>Snapshot Name</p></th>
<th><p>Created on</p></th>
<th><p>Total Size</p></th>
<th><p>Aggregated Size</p></th>
</tr>"
$htmlEmailTableFooter = "</tbody></table>"
$htmlEmailContent = $null
$htmlEmail = $null
$sendEmail = $false
#endregion
#region Program
#--------------
foreach ($Controller in $Controllers) {
$CurrentController = Connect-NaController -Name $Controller -Credential (getCredObject)
$VolList = Get-NaVol -Controller $CurrentController
$htmlEmailContent += "<h3>Controller $Controller</h3>"
if ($VolList) {
foreach ($Vol in $VolList) {
$VolName = $Vol.Name
$VolSnaps = Get-NaSnapshot $Vol -Controller $CurrentController
if ($VolSnaps) {
$sendEmail = $true
$htmlEmailContent += "<h4>Volume $VolName ($($VolSnaps.Count) snapshot(s))</h4>" + $htmlEmailTableHead
foreach ($VolSnap in $VolSnaps) {
$VolSnapName = $VolSnap.Name
$VolSnapCreated = $VolSnap.Created
$VolSnapTotal = sConvert-Size -DiskVolumeSpace $VolSnap.Total -DiskVolumeSpaceUnit "byte"
$VolSnapCumulative = sConvert-Size -DiskVolumeSpace $VolSnap.Cumulative -DiskVolumeSpaceUnit "byte"
$htmlEmailContent += "<tr><td><p>$VolSnapName</p></td><td><p>$VolSnapCreated</p></td><td><p>$VolSnapTotal</p></td><td><p>$VolSnapCumulative</p></td></tr>"
}
$htmlEmailContent += $htmlEmailTableFooter
} else {
Write-Host "No snapshots found on volume $VolName."
}
}
$htmlEmailContent += "<hr/>"
} else {
Write-Error "No volumes found on controller $Controller, something is really wrong."
}
}
if ($sendEmail -and $htmlEmailContent) {
$htmlEmail = $htmlEmailHead + $htmlEmailContent
Write-Host "Report built, sending..."
} else {
Write-Host "Nothing to report on, so I'm exiting"
Break
}
#endregion
#region Send Mail
#---------------
if ($SendMail -or $SMTPServer)
{
if ($SMTPServer -and $MailFrom -and $MailTo -and $htmlEmail)
{
$subject = "NetApp Volume Snapshot Report"
$MailTo = ($MailTo -join ',').ToString()
$mailMessage = New-Object System.Net.Mail.MailMessage
$mailMessage.subject = $subject
$mailMessage.to.add($MailTo)
$mailMessage.from = $MailFrom
$mailMessage.body = $htmlEmail
$mailMessage.IsBodyHtml = $true
$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
if ($MailFromPassword)
{
$smtp.UseDefaultCredentials = $false
$smtp.Credentials = New-Object System.Net.NetworkCredential($MailFrom, $MailFromPassword);
}
if ($SMTPServerTLSorSSL)
{
$smtp.EnableSSL = $true
}
$smtpSendResult = 1
Try
{
$smtp.send($mailMessage)
}
Catch
{
Write-Error -Message "E-Mail could not be sent"
$smtpSendResult = 0
}
if ($smtpSendResult -eq 1)
{
Write-Debug -Message "E-mail has been sent to the address(es): $MailTo"
}
Remove-Variable -Name smtp
Remove-Variable -Name MailFromPassword
}
}
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment