Wednesday, 9 September 2015

Ping All LIFs and SPs on my Cluster Tool

An update to Ping All LIFs on My Cluster Tool now including Service Processors also (but it will still work against SIMs that don’t have SPs)!

Image: Example run against a test physical 2-node cluster
Copy and paste the below script into a text file and save as say PingAll.ps1. Then run as:

PS C:\ > .\PingAll.ps1 -Cluster {CLUSTERNAME} -Username {USERNAME}

The Script

########################
# Ping All Lifs AndSPs #
########################

Param(
  [Parameter(Mandatory=$true)][String]$Cluster,
  [Parameter(Mandatory=$true)][String]$Username,
  [Int]$w = 80
)

FUNCTION Wr {
  Param([String]$ToDisplay,[String]$ForegroundColor,[String]$BackgroundColor)
  If(!$ToDisplay){ Write-Host; RETURN    }
  If($BackgroundColor){ Write-Host $ToDisplay -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor -NoNewLine; RETURN }
  If($ForegroundColor){ Write-Host $ToDisplay -ForegroundColor $ForegroundColor -NoNewLine; RETURN }
  Write-Host $ToDisplay -ForegroundColor White 
}

## LOAD THE DATA ONTAP POWERSHELL TOOLKIT ##

[Void](Import-Module DataONTAP -ErrorAction SilentlyContinue)
If(!(Get-Module DataONTAP)){ Wr "Unable to load the DataONTAP PowerShell Toolkit - exiting!" Red; Wr; EXIT }
Wr "Loaded the Data ONTAP PowerShell Toolkit!" Green; Wr

## GET CREDENTIALS ##

Wr "Password: " Cyan; $Password = Read-Host -AsSecureString
$SecureString = $Password | ConvertFrom-SecureString
$Credential = New-Object System.Management.Automation.PsCredential($Username,$Password)

## TEST CREDENTIALS AND ACQUIRE LIFS ##

Wr "Checking connection to " Cyan; Wr $Cluster Yellow; Wr " ..." Cyan; Wr
$Connect = Connect-NcController -Name $Cluster -Credential $Credential -Timeout 20000 -ErrorAction SilentlyContinue
If($Connect){ Wr "Successfully connected to " Green; Wr $Cluster Yellow; Wr }
else { Wr "Unable to connect to " Red; Wr $Cluster Yellow; Wr " with provided credentials - exiting!" Red; Wr; EXIT }
      
$Attrs = Get-NcNetInterface -Template
$Attrs.role = ""
$Attrs.address = ""
$Attrs.interfacename = ""
$LIFs = Get-NcNetInterface -Attributes $Attrs

[System.Object]$targets = @{}
[Int]$count = 0
$LIFs | Foreach{
  If ($_.role -ne "cluster"){
    $count++
    [System.Object]$targets.$count = @{}
    [String]$targets.$count.address = $_.address
    [String]$targets.$count.interfacename = $_.interfacename
  }
}     

## >> START OF GET SP SECTION << ##

Function GetNcSpIPs {
  $AttrsNode = Get-NcNode -Template
  $Nodes = (Get-NcNode -Attributes $AttrsNode).Node   
  $AttrsSP = Get-NcServiceProcessorNetwork -Template
  $AttrsSP.IpAddress = "" 
  [System.Array]$NodesAndSpIPs = @()
  $Nodes | Foreach {
    $GetNcSp = Get-NcServiceProcessorNetwork -Node $_ -AddressType ipv4 -Attributes $AttrsSP -ErrorAction SilentlyContinue
    If($GetNcSp.IpAddress){
      $NodesAndSpIPs += $_
      $NodesAndSpIPs += $GetNcSp.IpAddress     
    }
  }
  ,$NodesAndSpIPs
}

[System.Array]$TargetsWithSPs = GetNcSpIPs
For($i = 0; $i -lt $TargetsWithSPs.Count; $i += 2){
  $count++
  [System.Object]$targets.$count = @{}
  [String]$targets.$count.interfacename = ($TargetsWithSPs[$i] + " (SP)")
  [String]$targets.$count.address = $TargetsWithSPs[$i+1]
}

## >> END OF GET SP SECTION << ##

If($count -eq 0){ Wr "No targets to ping - exiting!" Red; Wr; EXIT }

## PING ALL NON-CLUSTER LIFS ##

[System.Array]$status = "" # $status[0] is unused
For($i = 1; $i -le $count; $i++){ $status += "Unitialized" }

Function PrintStatus{
  Param([Int16]$pointer)
  If ($pointer -eq $count){ $pointer = 1}
  else { $pointer ++ }    
  cls
  For($j = 1; $j -le $count; $j++){
    If ($pointer -eq $j){ [String]$Display = " * " } else { [String]$Display = "   " }
    $Display += $Cluster + " : " + $targets.$j.interfacename + " : " + $targets.$j.address
    If ($status[$j] -eq "UP"){ Wr ($Display.PadRight($w).Substring(0,$w)) BLACK GREEN; Wr }
    elseif ($status[$j] -eq "DOWN"){ Wr ($Display.PadRight($w).Substring(0,$w)) WHITE RED; Wr }
    else { Wr ($Display.PadRight($w).Substring(0,$w)) BLACK GRAY ; Wr }
  }
}

Function GetResult{
  For($i = 1; $i -le $count; $i++){
    [Boolean]$Result = Test-Connection -ComputerName $targets.$i.address -Count 1 -Quiet
    If($Result){
      Start-Sleep -Milliseconds 100
      $status[$i] = "UP"
    } else {
      $status[$i] = "DOWN"
    }
    [Void](PrintStatus $i)
  }
}

While ($true){ [Void](GetResult) }

No comments:

Post a Comment