Skip to content

Commit 2ea829e

Browse files
committed
Network health script.
1 parent 7045e21 commit 2ea829e

6 files changed

Lines changed: 1410 additions & 0 deletions

File tree

scripts/networkhealth/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,9 @@ If the `-OutputMode` is set to `Event` or `All`, the script will register a new
3535
Example:
3636
kubectl logs -l name=networkhealth --all-containers=true >> networkhealth.txt
3737
Provide the generated networkhealth.txt
38+
```
39+
40+
## Cmonnad to run vfpDropCounterMetrics
41+
```
42+
.\vfpDropCounterMetrics.ps1 -TimeIntervalInSeconds 30 -PrintMatchedRules $true -PodNamePrefixes tcp-server,tcp-client
3843
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
param (
2+
[Parameter(Mandatory=$true)][string]$ServiceIPS = "",
3+
[Parameter(Mandatory=$false)][string]$RunFromOutside = $false,
4+
[Parameter(Mandatory=$false)][string]$CopyScriptToNode = $false,
5+
[Parameter(Mandatory=$false)][string]$HpcPodPrefix = "hpc",
6+
[Parameter(Mandatory=$false)][string]$HpcNamepsace = "demo",
7+
[Parameter(Mandatory=$false)][string]$Layer = "LB_DSR",
8+
[Parameter(Mandatory=$false)][string]$Group = "LB_DSR_IPv4_OUT"
9+
10+
)
11+
12+
# Eg: .\checkLbDsrMissing.ps1 -ServiceIPS "20.51.25.211,10.0.10.189,10.0.92.4"
13+
14+
$Logfile = "health.log"
15+
16+
function LogError {
17+
param (
18+
[parameter(Mandatory=$true)][string] $message
19+
)
20+
Write-Host $message -ForegroundColor Red
21+
Add-content $Logfile -value "[FAILED] $message"
22+
}
23+
24+
function LogSuccess {
25+
param (
26+
[parameter(Mandatory=$true)][string] $message
27+
)
28+
Write-Host $message -ForegroundColor Green
29+
Add-content $Logfile -value "[SUCCESS] $message"
30+
}
31+
32+
function CheckLbDsrRuleMissing {
33+
34+
Write-Host "Checking $Layer Rule missing"
35+
$lbDsrRuleMissing = $false
36+
37+
$serviceIPList = $ServiceIPS.Split(",")
38+
39+
$portNameList = vfpctrl /list-vmswitch-port | sls "Port name"
40+
41+
foreach($portName in $portNameList) {
42+
$portId = $portName.ToString().Split(":")[1].Trim()
43+
$entries = vfpctrl /port $portId /layer $Layer /list-group
44+
if($entries.Count -le 8) {
45+
continue
46+
}
47+
foreach($serviceIP in $serviceIPList) {
48+
$entries = vfpctrl /port $portId /layer $Layer /group $Group /list-rule | sls $serviceIP
49+
if($entries.Count -le 1) {
50+
LogError "VFP $Layer Rule missing for Service IP : $serviceIP in VFP Port : $portName"
51+
$lbDsrRuleMissing = $true
52+
} else {
53+
LogSuccess "VFP $Layer Rule present for Service IP : $serviceIP in VFP Port : $portName"
54+
}
55+
}
56+
}
57+
58+
if($lbDsrRuleMissing){
59+
LogError "Mitigation : Restart-Service -f kubeproxy "
60+
return
61+
}
62+
63+
LogSuccess "No issues identified with $Layer Rule Missing for Service IPS : $ServiceIPS."
64+
}
65+
66+
function ExecFromOutside {
67+
$hpcPods = kubectl get pods -n $HpcNamepsace | sls $HpcPodPrefix
68+
if($CopyScriptToNode -eq $true) {
69+
foreach($hpcPod in $hpcPods) {
70+
$podId = $hpcPod.ToString().Split(" ")[0].Trim()
71+
Write-Host "Copying script to $podId"
72+
kubectl cp .\checkLbDsrMissing.ps1 "$podId`:`checkLbDsrMissing.ps1" -n $HpcNamepsace
73+
}
74+
}
75+
foreach($hpcPod in $hpcPods) {
76+
$podId = $hpcPod.ToString().Split(" ")[0].Trim()
77+
kubectl exec -it $podId -n $HpcNamepsace -- powershell ".\checkLbDsrMissing.ps1 -ServiceIPS '$ServiceIPS' -Layer $Layer -Group $Group"
78+
}
79+
}
80+
81+
if($RunFromOutside -eq $true) {
82+
ExecFromOutside
83+
}
84+
85+
CheckLbDsrRuleMissing
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
$Logfile = ".\health.log"
2+
$minThreshold = 50
3+
4+
function LogMessage {
5+
param (
6+
[parameter(Mandatory=$true)][string] $message
7+
)
8+
Add-content $Logfile -value "$message"
9+
}
10+
11+
function CountAvailableEphemeralPorts([string]$protocol = "TCP") {
12+
13+
[uint32]$portRangeSize = 64
14+
# First, remove all the text bells and whistle (plain text, table headers, dashes, empty lines, ...) from netsh output
15+
$tcpRanges = (netsh int ipv4 sh excludedportrange $protocol) -replace "[^0-9,\ ]", '' | ? { $_.trim() -ne "" }
16+
17+
# Then, remove any extra space characters. Only capture the numbers representing the beginning and end of range
18+
$tcpRangesArray = $tcpRanges -replace "\s+(\d+)\s+(\d+)\s+", '$1,$2' | ConvertFrom-String -Delimiter ","
19+
#Convert from PSCustomObject to Object[] type
20+
$tcpRangesArray = @($tcpRangesArray)
21+
22+
# Extract the ephemeral ports ranges
23+
$EphemeralPortRange = (netsh int ipv4 sh dynamicportrange $protocol) -replace "[^0-9]", '' | ? { $_.trim() -ne "" }
24+
$EphemeralPortStart = [Convert]::ToUInt32($EphemeralPortRange[0])
25+
$EphemeralPortEnd = $EphemeralPortStart + [Convert]::ToUInt32($EphemeralPortRange[1]) - 1
26+
27+
# Find the external interface
28+
$externalInterfaceIdx = (Get-NetRoute -DestinationPrefix "0.0.0.0/0")[0].InterfaceIndex
29+
$hostIP = (Get-NetIPConfiguration -ifIndex $externalInterfaceIdx).IPv4Address.IPAddress
30+
31+
# Extract the used TCP ports from the external interface
32+
$usedTcpPorts = (Get-NetTCPConnection -LocalAddress $hostIP -ErrorAction Ignore).LocalPort
33+
$usedTcpPorts | % { $tcpRangesArray += [pscustomobject]@{P1 = $_; P2 = $_ } }
34+
35+
# Extract the used TCP ports from the 0.0.0.0 interface
36+
$usedTcpGlobalPorts = (Get-NetTCPConnection -LocalAddress "0.0.0.0" -ErrorAction Ignore).LocalPort
37+
$usedTcpGlobalPorts | % { $tcpRangesArray += [pscustomobject]@{P1 = $_; P2 = $_ } }
38+
# Sort the list and remove duplicates
39+
$tcpRangesArray = ($tcpRangesArray | Sort-Object { $_.P1 } -Unique)
40+
41+
$tcpRangesList = New-Object System.Collections.ArrayList($null)
42+
$tcpRangesList.AddRange($tcpRangesArray)
43+
44+
# Remove overlapping ranges
45+
for ($i = $tcpRangesList.P1.Length - 2; $i -gt 0 ; $i--) {
46+
if ($tcpRangesList[$i].P2 -gt $tcpRangesList[$i + 1].P1 ) {
47+
$tcpRangesList.Remove($tcpRangesList[$i + 1])
48+
$i++
49+
}
50+
}
51+
52+
# Remove the non-ephemeral port reservations from the list
53+
$filteredTcpRangeArray = $tcpRangesList | ? { $_.P1 -ge $EphemeralPortStart }
54+
$filteredTcpRangeArray = $filteredTcpRangeArray | ? { $_.P2 -le $EphemeralPortEnd }
55+
56+
if ($null -eq $filteredTcpRangeArray) {
57+
$freeRanges = @($EphemeralPortRange[1])
58+
}
59+
else {
60+
$freeRanges = @()
61+
# The first free range goes from $EphemeralPortStart to the beginning of the first reserved range
62+
$freeRanges += ([Convert]::ToUInt32($filteredTcpRangeArray[0].P1) - $EphemeralPortStart)
63+
64+
for ($i = 1; $i -lt $filteredTcpRangeArray.length; $i++) {
65+
# Subsequent free ranges go from the end of the previous reserved range to the beginning of the current reserved range
66+
$freeRanges += ([Convert]::ToUInt32($filteredTcpRangeArray[$i].P1) - [Convert]::ToUInt32($filteredTcpRangeArray[$i - 1].P2) - 1)
67+
}
68+
69+
# The last free range goes from the end of the last reserved range to $EphemeralPortEnd
70+
$freeRanges += ($EphemeralPortEnd - [Convert]::ToUInt32($filteredTcpRangeArray[$filteredTcpRangeArray.length - 1].P2))
71+
}
72+
73+
# Count the number of available free ranges
74+
[uint32]$freeRangesCount = 0
75+
($freeRanges | % { $freeRangesCount += [Math]::Floor($_ / $portRangeSize) } )
76+
77+
return $freeRangesCount
78+
}
79+
80+
function CheckPortExhaustion {
81+
Write-Host "Checking Port Exhaustion"
82+
$avTcpPorts = CountAvailableEphemeralPorts -protocol TCP
83+
if($avTcpPorts -lt $minThreshold) {
84+
$message = "Available TCP ports are $avTcpPorts. Port exhaustion suspected."
85+
Write-Host "$message" -ForegroundColor Red
86+
LogMessage -message $message
87+
return $true
88+
}
89+
$avUdpPorts = CountAvailableEphemeralPorts -protocol UDP
90+
if($avTcpPorts -lt $minThreshold) {
91+
$message = "Available UDP ports are $avUdpPorts. Port exhaustion suspected."
92+
Write-Host "$message" -ForegroundColor Red
93+
LogMessage -message $message
94+
return $true
95+
}
96+
Write-Host "Available TCP Ports : $avTcpPorts , UDP Ports : $avUdpPorts . No port exhaustion suspected." -ForegroundColor Green
97+
return $false
98+
}
99+
100+
$i = 0
101+
Remove-Item $Logfile -ErrorAction Ignore
102+
103+
While($true) {
104+
$i++
105+
Write-Host "#============== Iteration : $i"
106+
if(CheckPortExhaustion) {
107+
Write-Host "DNS Issue Found." -ForegroundColor Red
108+
}
109+
Start-Sleep -Seconds 10
110+
}

0 commit comments

Comments
 (0)