Fixed logfile path
This commit is contained in:
@@ -6,8 +6,9 @@ $addressDefinitions = @(
|
|||||||
"8.8.8.8-9" # Using range notation for cleaner definition
|
"8.8.8.8-9" # Using range notation for cleaner definition
|
||||||
)
|
)
|
||||||
|
|
||||||
# Set up logging configuration
|
# Set up logging configuration - Use absolute path to avoid system32 issues
|
||||||
$logFolder = ".\Logs"
|
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||||
|
$logFolder = Join-Path $scriptPath "LogsPing"
|
||||||
$logFile = Join-Path $logFolder "network_monitor.log"
|
$logFile = Join-Path $logFolder "network_monitor.log"
|
||||||
$statLogFile = Join-Path $logFolder "statistics.log"
|
$statLogFile = Join-Path $logFolder "statistics.log"
|
||||||
$csvLogFile = Join-Path $logFolder "ping_data.csv"
|
$csvLogFile = Join-Path $logFolder "ping_data.csv"
|
||||||
@@ -15,15 +16,32 @@ $errorLogFile = Join-Path $logFolder "errors.log"
|
|||||||
|
|
||||||
# Create log directory if it doesn't exist
|
# Create log directory if it doesn't exist
|
||||||
if (-not (Test-Path $logFolder)) {
|
if (-not (Test-Path $logFolder)) {
|
||||||
New-Item -Path $logFolder -ItemType Directory | Out-Null
|
try {
|
||||||
|
New-Item -Path $logFolder -ItemType Directory -Force | Out-Null
|
||||||
|
Write-Host "Created log directory: $logFolder" -ForegroundColor Green
|
||||||
|
} catch {
|
||||||
|
Write-Host "Error creating log directory: $_" -ForegroundColor Red
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Initialize log files with headers - Use streamwriter for lower memory footprint
|
# Initialize log files with headers - Use streamwriter for lower memory footprint
|
||||||
$null = if (-not (Test-Path $csvLogFile)) {
|
try {
|
||||||
$csvStream = [System.IO.StreamWriter]::new($csvLogFile)
|
if (-not (Test-Path $csvLogFile)) {
|
||||||
$csvStream.WriteLine("Timestamp,IPAddress,Status,ResponseTime,SuccessRate")
|
# Ensure directory exists
|
||||||
$csvStream.Close()
|
$csvDir = Split-Path -Parent $csvLogFile
|
||||||
$csvStream.Dispose()
|
if (-not (Test-Path $csvDir)) {
|
||||||
|
New-Item -Path $csvDir -ItemType Directory -Force | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
$csvStream = [System.IO.StreamWriter]::new($csvLogFile)
|
||||||
|
$csvStream.WriteLine("Timestamp,IPAddress,Status,ResponseTime,SuccessRate")
|
||||||
|
$csvStream.Close()
|
||||||
|
$csvStream.Dispose()
|
||||||
|
Write-Host "Created CSV log file: $csvLogFile" -ForegroundColor Green
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Write-Host "Error creating CSV log file: $_" -ForegroundColor Red
|
||||||
}
|
}
|
||||||
|
|
||||||
# Prepare log streamwriters - more memory efficient than Out-File
|
# Prepare log streamwriters - more memory efficient than Out-File
|
||||||
@@ -44,11 +62,22 @@ function Write-Log {
|
|||||||
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
||||||
$logLine = "$timestamp - $Message"
|
$logLine = "$timestamp - $Message"
|
||||||
|
|
||||||
# Use streamwriter instead of Out-File
|
# Use streamwriter instead of Out-File with error handling
|
||||||
$stream = [System.IO.StreamWriter]::new($LogPath, $true)
|
try {
|
||||||
$stream.WriteLine($logLine)
|
# Ensure directory exists
|
||||||
$stream.Close()
|
$logDir = Split-Path -Parent $LogPath
|
||||||
$stream.Dispose()
|
if (-not (Test-Path $logDir)) {
|
||||||
|
New-Item -Path $logDir -ItemType Directory -Force | Out-Null
|
||||||
|
}
|
||||||
|
|
||||||
|
$stream = [System.IO.StreamWriter]::new($LogPath, $true)
|
||||||
|
$stream.WriteLine($logLine)
|
||||||
|
$stream.Close()
|
||||||
|
$stream.Dispose()
|
||||||
|
} catch {
|
||||||
|
# Fall back to console output if file writing fails
|
||||||
|
Write-Host "Failed to write to log file $LogPath. Error: $_" -ForegroundColor Red
|
||||||
|
}
|
||||||
|
|
||||||
if (-not $NoConsole) {
|
if (-not $NoConsole) {
|
||||||
Write-Host $logLine
|
Write-Host $logLine
|
||||||
@@ -174,10 +203,14 @@ function Write-PingDataLog {
|
|||||||
$statusText = if ($Status) { "UP" } else { "DOWN" }
|
$statusText = if ($Status) { "UP" } else { "DOWN" }
|
||||||
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
$timestamp = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
|
||||||
|
|
||||||
$stream = [System.IO.StreamWriter]::new($csvLogFile, $true)
|
try {
|
||||||
$stream.WriteLine("$timestamp,$Address,$statusText,$ResponseTime,$SuccessRate")
|
$stream = [System.IO.StreamWriter]::new($csvLogFile, $true)
|
||||||
$stream.Close()
|
$stream.WriteLine("$timestamp,$Address,$statusText,$ResponseTime,$SuccessRate")
|
||||||
$stream.Dispose()
|
$stream.Close()
|
||||||
|
$stream.Dispose()
|
||||||
|
} catch {
|
||||||
|
Write-Host "Error writing to CSV log file: $_" -ForegroundColor Red
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# Function to log statistics periodically - streamwriter based
|
# Function to log statistics periodically - streamwriter based
|
||||||
@@ -237,6 +270,14 @@ function Invoke-MemoryCleanup {
|
|||||||
[System.GC]::Collect()
|
[System.GC]::Collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Display script information
|
||||||
|
Write-Host "Network Monitoring Tool" -ForegroundColor Cyan
|
||||||
|
Write-Host "======================" -ForegroundColor Cyan
|
||||||
|
Write-Host "Log Directory: $logFolder" -ForegroundColor Yellow
|
||||||
|
Write-Host "Monitoring these IPs: $($addresses -join ', ')" -ForegroundColor Yellow
|
||||||
|
Write-Host "Press Enter to continue or Ctrl+C to exit..." -ForegroundColor Green
|
||||||
|
$null = Read-Host
|
||||||
|
|
||||||
# Main monitoring loop
|
# Main monitoring loop
|
||||||
try {
|
try {
|
||||||
$pingInterval = 1 # seconds between pings
|
$pingInterval = 1 # seconds between pings
|
||||||
|
|||||||
Reference in New Issue
Block a user