Saturday, 31 October 2015

Find Dead NFS Clients

After a 7 to C migration project, I wanted an automated way to find out how many of the NFS clients we’d brought across from 7-Mode are actually now dead (NFS clients that have been demised and should have been been tidied up on the 7-Mode system.) So, I created this tool which gets the list of unique NFS clients on the cDOT clusters, then goes through the list pinging them from an NFS data LIF on the cDOT SVM to test connectivity. It will try up to 3 data LIFs before returning the NFS client as being dead.

Image: Screen output from a simple example
The Script

Save as say TestNfsClients.ps1 and run in PowerShell as .\TestNfsClients.ps1.

FUNCTION Wr {
  Param([String]$ToDisplay,[String]$Color = "WHITE")
  If($ToDisplay){ Write-Host $ToDisplay -ForegroundColor $Color -NoNewLine } else { Write-Host }
}

## INPUTS ##
Wr; Wr "<<< FIND DEAD NFS CLIENTS >>>" MAGENTA; Wr; Wr
Wr "File Out  : " CYAN; $FILEOUT  = Read-Host
Wr "Cluster   : " CYAN; $CLUSTER  = Read-Host
Wr "Vserver   : " CYAN; $VSERVER  = Read-Host
Wr "User name : " CYAN; $USERNAME = Read-Host
Wr "Password  : " CYAN; $PASSWORD = Read-Host -AsSecureString; Wr
$Credential = New-Object System.Management.Automation.PsCredential($USERNAME,$PASSWORD)
$Connect = Connect-NcController -Name $CLUSTER -Credential $Credential -ErrorAction SilentlyContinue
If(!$Connect){ Wr "Unable to connect to $CLUSTER!" RED; Wr; Wr; EXIT }
$Attrs = Get-NcVserver -Template
$TestVserver = Get-NcVserver -Vserver $VSERVER -Attributes $Attrs
If(!$TestVserver){ Wr "Cannot get Vserver $VSERVER!" RED; Wr; Wr; EXIT }

## FIND THE UNIQUE NFS CLIENTS IN THE EXPORT RULES ##
$Attrs = Get-NcExportRule -Template
$Attrs.ClientMatch = ""
$GetExpRule = Get-NcExportRule -Attributes $Attrs -VserverContext $VSERVER
[System.Array]$Clients = @()
$GetExpRule | Foreach {
  $Client = $_.ClientMatch
  If( $Client -Match "/" ){
  } elseif ( $Clients -contains $Client ){
  } else { $Clients += $Client }
}
If($Clients.Count -eq 0){ Wr "No NFS Clients!" RED; Wr; Wr; EXIT }
Wr "CLIENTS: " CYAN; Wr $Clients; Wr; Wr
$UniqueClients = $Clients.Count
Wr "TOTAL (UNIQUE) CLIENTS = " CYAN; Wr $UniqueClients; Wr; Wr

## GET DATA LIFS SERVING NFS ##
$Attrs = Get-NcNetInterface -Template
$Attrs.InterfaceName = ""
$Attrs.DataProtocols = ""
$Query = Get-NcNetInterface -Template
$Query.Role = "data"
$Query.OpStatus = "up"
$GetInts = Get-NcNetInterface -Attributes $Attrs -Query $Query -VserverContext $VSERVER
[System.Array]$NfsLifs = @()
$GetInts | Foreach { If( $_.DataProtocols -Contains "nfs" ){ $NfsLifs += $_.InterfaceName} }
If($NfsLifs.Count -eq 0){ Wr "No UP NFS Data LIFs!" RED; Wr; Wr; EXIT }
Wr "NFS DATA LIFs: " CYAN; Wr $NfsLifs; Wr; Wr

## GET THE DEAD CLIENTS ##
Wr "<<< TESTING CLIENTS >>>" MAGENTA; Wr; Wr
[System.Array]$DeadClients = @()
FUNCTION Test-Client {
  [Int]$test = 1
  Foreach ($LIF in $NfsLifs){
    Wr "Test " CYAN; Wr $test; Wr
    $PingClient = Ping-NcHost -Host $Client -InterfaceOwner $VSERVER -Interface $LIF
    If($PingClient.Success -eq "True"){ RETURN $TRUE }
    $test ++
    If($test -gt 3){ RETURN $FALSE }
  }
  RETURN $FALSE
}
Foreach ($Client in $Clients){
  $Alive = Test-Client
  If($Alive){ Wr "$Client is alive!" GREEN; Wr }
  else { Wr "$Client is dead!" RED; Wr; $DeadClients += $Client }
}
Wr; Wr "DEAD CLIENTS:" CYAN; Wr; Wr
$DeadClients | Foreach { Wr $_; Wr }; Wr
$DeadCount = $DeadClients.Count
Wr "DEAD CLIENTS / UNIQUE CLIENTS = "; Wr ("$DeadCount / $UniqueClients"); Wr; Wr
$DeadClients | Out-File $FILEOUT -Encoding Default

No comments:

Post a Comment