252 lines
8.2 KiB
PowerShell
252 lines
8.2 KiB
PowerShell
# Define the IP addresses to monitor
|
|
$addresses = @(
|
|
"1.1.1.1",
|
|
"1.0.0.1"
|
|
)
|
|
|
|
# Initialize runspace pool
|
|
$RunspacePool = [runspacefactory]::CreateRunspacePool(1, 20)
|
|
$RunspacePool.Open()
|
|
|
|
# Initialize statistics
|
|
$stats = @{}
|
|
foreach ($addr in $addresses) {
|
|
$stats[$addr] = @{
|
|
MinTime = [int]::MaxValue
|
|
MaxTime = 0
|
|
TotalTime = 0
|
|
SuccessCount = 0
|
|
FailCount = 0
|
|
HighTrafficFailCount = 0
|
|
Samples = @()
|
|
LastStatus = $null
|
|
LastResponseTime = 0
|
|
}
|
|
}
|
|
|
|
# Initialize network traffic statistics
|
|
$trafficStats = @{
|
|
HighUploadInstances = 0
|
|
HighDownloadInstances = 0
|
|
LastBytesReceived = 0
|
|
LastBytesSent = 0
|
|
LastMeasurement = Get-Date
|
|
HighTrafficStartTime = $null
|
|
IsHighTraffic = $false
|
|
}
|
|
|
|
$TestPort80ScriptBlock = {
|
|
param($ComputerName)
|
|
|
|
$tcpClient = New-Object System.Net.Sockets.TcpClient
|
|
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
|
|
|
try {
|
|
$result = $tcpClient.BeginConnect($ComputerName, 80, $null, $null)
|
|
$success = $result.AsyncWaitHandle.WaitOne(1000)
|
|
$stopwatch.Stop()
|
|
|
|
if ($success) {
|
|
$tcpClient.EndConnect($result)
|
|
return @{
|
|
Success = $true
|
|
ResponseTime = $stopwatch.ElapsedMilliseconds
|
|
Address = $ComputerName
|
|
}
|
|
}
|
|
return @{
|
|
Success = $false
|
|
ResponseTime = 0
|
|
Address = $ComputerName
|
|
}
|
|
}
|
|
catch {
|
|
$stopwatch.Stop()
|
|
return @{
|
|
Success = $false
|
|
ResponseTime = 0
|
|
Address = $ComputerName
|
|
}
|
|
}
|
|
finally {
|
|
$tcpClient.Close()
|
|
}
|
|
}
|
|
|
|
function Get-NetworkTraffic {
|
|
$stats = Get-WmiObject Win32_PerfFormattedData_Tcpip_NetworkInterface |
|
|
Where-Object {$_.Name -eq "Intel[R] I210 Gigabit Network Connection _2"}
|
|
|
|
if ($stats) {
|
|
return @{
|
|
BytesReceivedPerSec = [double]$stats.BytesReceivedPerSec
|
|
BytesSentPerSec = [double]$stats.BytesSentPerSec
|
|
}
|
|
}
|
|
return $null
|
|
}
|
|
|
|
function Get-Mean {
|
|
param([array]$Numbers)
|
|
if ($Numbers.Count -eq 0) { return 0 }
|
|
return ($Numbers | Measure-Object -Average).Average
|
|
}
|
|
|
|
function Reset-Display {
|
|
Clear-Host
|
|
$host.UI.RawUI.CursorPosition = @{X=0; Y=0}
|
|
}
|
|
|
|
function Format-Number {
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[double]$Number,
|
|
[int]$Width = 6,
|
|
[int]$Decimals = 1
|
|
)
|
|
return $Number.ToString("F$Decimals").PadLeft($Width)
|
|
}
|
|
|
|
# Create log file
|
|
$logFile = "network_traffic.log"
|
|
"Timestamp,BytesReceived,BytesSent,HighUploadAlert,HighDownloadAlert" | Out-File $logFile
|
|
|
|
while ($true) {
|
|
Reset-Display
|
|
$currentTime = Get-Date
|
|
$currentTimeString = $currentTime.ToString("HH:mm:ss")
|
|
|
|
# Start parallel ping jobs
|
|
$Jobs = @()
|
|
|
|
foreach ($addr in $addresses) {
|
|
$PowerShell = [powershell]::Create().AddScript($TestPort80ScriptBlock).AddArgument($addr)
|
|
$PowerShell.RunspacePool = $RunspacePool
|
|
|
|
$Jobs += @{
|
|
PowerShell = $PowerShell
|
|
Handle = $PowerShell.BeginInvoke()
|
|
Address = $addr
|
|
}
|
|
}
|
|
|
|
# Monitor network traffic while waiting for pings
|
|
$traffic = Get-NetworkTraffic
|
|
if ($traffic) {
|
|
$downloadMbps = ($traffic.BytesReceivedPerSec * 8) / (1024 * 1024)
|
|
$uploadMbps = ($traffic.BytesSentPerSec * 8) / (1024 * 1024)
|
|
|
|
$uploadAlert = ""
|
|
$downloadAlert = ""
|
|
$previousHighTrafficState = $trafficStats.IsHighTraffic
|
|
|
|
# Check for high traffic condition
|
|
$isHighTrafficNow = ($uploadMbps -gt 80) -or ($downloadMbps -gt 80)
|
|
|
|
if ($isHighTrafficNow -and -not $previousHighTrafficState) {
|
|
$trafficStats.HighTrafficStartTime = $currentTime
|
|
$trafficStats.IsHighTraffic = $true
|
|
}
|
|
elseif (-not $isHighTrafficNow -and $previousHighTrafficState) {
|
|
$trafficStats.IsHighTraffic = $false
|
|
}
|
|
|
|
if ($uploadMbps -gt 80) {
|
|
$trafficStats.HighUploadInstances++
|
|
$uploadAlert = "HIGH UPLOAD ALERT: $([math]::Round($uploadMbps, 2)) Mbps"
|
|
}
|
|
|
|
if ($downloadMbps -gt 80) {
|
|
$trafficStats.HighDownloadInstances++
|
|
$downloadAlert = "HIGH DOWNLOAD ALERT: $([math]::Round($downloadMbps, 2)) Mbps"
|
|
}
|
|
|
|
"$currentTimeString,$($traffic.BytesReceivedPerSec),$($traffic.BytesSentPerSec),$($downloadMbps -gt 80),$($uploadMbps -gt 80)" |
|
|
Out-File $logFile -Append
|
|
}
|
|
|
|
# Display header
|
|
Write-Host "Port 80 Monitor - Last Updated: $currentTimeString" -ForegroundColor Cyan
|
|
Write-Host "Network Traffic:" -ForegroundColor Yellow
|
|
Write-Host " Download: $([math]::Round($downloadMbps, 2)) Mbps" -ForegroundColor Yellow
|
|
Write-Host " Upload: $([math]::Round($uploadMbps, 2)) Mbps" -ForegroundColor Yellow
|
|
Write-Host "High Traffic Instances:" -ForegroundColor Yellow
|
|
Write-Host " Download: $($trafficStats.HighDownloadInstances)" -ForegroundColor Yellow
|
|
Write-Host " Upload: $($trafficStats.HighUploadInstances)" -ForegroundColor Yellow
|
|
|
|
if ($downloadAlert) { Write-Host $downloadAlert -ForegroundColor Red }
|
|
if ($uploadAlert) { Write-Host $uploadAlert -ForegroundColor Red }
|
|
|
|
Write-Host "`nIP Address Status Min Max Mean Success% Fails HTFails" -ForegroundColor Yellow
|
|
Write-Host "--------------- -------- ----- ----- ----- -------- ----- --------" -ForegroundColor Yellow
|
|
|
|
# Collect results from parallel jobs
|
|
foreach ($job in $Jobs) {
|
|
$result = $job.PowerShell.EndInvoke($job.Handle)
|
|
$addr = $job.Address
|
|
|
|
# Clean up runspace
|
|
$job.PowerShell.Dispose()
|
|
|
|
$isInHighTrafficWindow = $false
|
|
if ($trafficStats.IsHighTraffic -or
|
|
($trafficStats.HighTrafficStartTime -and
|
|
$currentTime -le $trafficStats.HighTrafficStartTime.AddSeconds(1))) {
|
|
$isInHighTrafficWindow = $true
|
|
}
|
|
|
|
if ($result.Success) {
|
|
$responseTime = $result.ResponseTime
|
|
$stats[$addr].TotalTime += $responseTime
|
|
$stats[$addr].SuccessCount++
|
|
$stats[$addr].Samples += $responseTime
|
|
$stats[$addr].LastStatus = "UP"
|
|
$stats[$addr].LastResponseTime = $responseTime
|
|
|
|
if ($stats[$addr].Samples.Count -gt 60) {
|
|
$stats[$addr].Samples = $stats[$addr].Samples[-60..-1]
|
|
}
|
|
|
|
if ($responseTime -lt $stats[$addr].MinTime) {
|
|
$stats[$addr].MinTime = $responseTime
|
|
}
|
|
if ($responseTime -gt $stats[$addr].MaxTime) {
|
|
$stats[$addr].MaxTime = $responseTime
|
|
}
|
|
|
|
$status = "UP"
|
|
$color = "Green"
|
|
} else {
|
|
$stats[$addr].FailCount++
|
|
if ($isInHighTrafficWindow) {
|
|
$stats[$addr].HighTrafficFailCount++
|
|
}
|
|
$stats[$addr].LastStatus = "DOWN"
|
|
$status = "DOWN"
|
|
$color = "Red"
|
|
}
|
|
|
|
$totalRequests = $stats[$addr].SuccessCount + $stats[$addr].FailCount
|
|
$successRate = [math]::Round(($stats[$addr].SuccessCount / $totalRequests) * 100, 1)
|
|
$meanTime = [math]::Round((Get-Mean $stats[$addr].Samples), 1)
|
|
|
|
$ipField = $addr.PadRight(15)
|
|
$statusField = $status.PadRight(8)
|
|
$minField = Format-Number -Number $stats[$addr].MinTime -Width 5 -Decimals 0
|
|
$maxField = Format-Number -Number $stats[$addr].MaxTime -Width 5 -Decimals 0
|
|
$meanField = Format-Number -Number $meanTime -Width 5 -Decimals 1
|
|
$successField = Format-Number -Number $successRate -Width 8 -Decimals 1
|
|
$failsField = $stats[$addr].FailCount.ToString().PadLeft(5)
|
|
$htFailsField = $stats[$addr].HighTrafficFailCount.ToString().PadLeft(8)
|
|
|
|
Write-Host $ipField -NoNewline
|
|
Write-Host $statusField -NoNewline -ForegroundColor $color
|
|
Write-Host "$minField $maxField $meanField $successField $failsField $htFailsField"
|
|
}
|
|
|
|
Start-Sleep -Milliseconds 500
|
|
}
|
|
|
|
# Cleanup
|
|
$RunspacePool.Close()
|
|
$RunspacePool.Dispose() |