sync
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -0,0 +1,225 @@
|
||||
# Define the target SMB share and output CSV file
|
||||
$targetMachine = ""
|
||||
$targetShare = "" # Change this to the UNC path of your SMB share
|
||||
$tcpPort = 1433 # Specify the port number to test (e.g., HTTP = 80, HTTPS = 443)
|
||||
$rootDrive = "U:\" #root drive where files will be stored
|
||||
$subFolder = "TPDT-676" #subfolder where all files will be stored on the $rootDrive
|
||||
|
||||
#ensure we have a working drive
|
||||
if(-not(Test-Path -Path $rootDrive)){
|
||||
$rootDrive = "D:\"
|
||||
}
|
||||
|
||||
#create the destination folder if missing
|
||||
$dest = "$rootDrive\$subFolder".Replace("\\","\")
|
||||
if (-not (Test-Path -Path $dest)) {
|
||||
New-Item -ItemType Directory -Path $dest
|
||||
}
|
||||
|
||||
$outputCsvLAT = "$dest\tpdt-676-POS2.csv" #name of the csv file with the results
|
||||
$logFile = "$dest\tpdt-676-POS2.log" #place of log file (for debugging sql job)
|
||||
|
||||
$maxFiles = 3 #how many csv file we keep after doing a rotation
|
||||
$maxFileSizeMB = 10 #What is the max size of a csv file before we do a rotation in MB
|
||||
$testCount = 5 # Number of connection attempts per iteration
|
||||
|
||||
# Function to check the size of the CSV file
|
||||
function Check-CsvFileSize {
|
||||
param ($filePath)
|
||||
|
||||
if (Test-Path $filePath) {
|
||||
$fileSizeMB = (Get-Item $filePath).length / 1MB
|
||||
return $fileSizeMB
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
# Function to rotate files
|
||||
function Rotate-CsvFile {
|
||||
param ($filePath, $rotationPath, $maxFiles)
|
||||
|
||||
#trailing backslash needed for concatenation of the path
|
||||
if (-not $rotationPath.EndsWith("\")) {
|
||||
$rotationPath += "\"
|
||||
}
|
||||
|
||||
# Create rotation directory if it doesn''t exist
|
||||
if (-not (Test-Path $rotationPath)) {
|
||||
New-Item -ItemType Directory -Path $rotationPath
|
||||
}
|
||||
|
||||
# Get the current timestamp to append to the rotated filename
|
||||
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
||||
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
|
||||
$fileExtension = [System.IO.Path]::GetExtension($filePath)
|
||||
|
||||
# Generate new rotated filename
|
||||
$rotatedFileName = "$rotationPath$fileName-$timestamp$fileExtension"
|
||||
|
||||
# Rotate the file
|
||||
Rename-Item -Path $filePath -NewName $rotatedFileName
|
||||
|
||||
# Get all rotated files sorted by creation time
|
||||
$rotatedFiles = Get-ChildItem -Path $rotationPath -Filter "$fileName-*$fileExtension" | Sort-Object LastWriteTime -Descending
|
||||
|
||||
# Keep only the most recent 3 files
|
||||
$filesToDelete = $rotatedFiles | Select-Object -Skip $maxFiles
|
||||
foreach ($file in $filesToDelete) {
|
||||
Remove-Item -Path $file.FullName
|
||||
}
|
||||
}
|
||||
|
||||
function write-log {
|
||||
param(
|
||||
[Parameter(ValueFromPipeline = $true)]
|
||||
[string]$msg,
|
||||
[string]$log = $logFile
|
||||
)
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
|
||||
$line = "$timestamp`t$msg"
|
||||
|
||||
Add-Content -Path $log -Value $line
|
||||
write-host $line
|
||||
}
|
||||
|
||||
#Get the replini-backup URI and hostname
|
||||
function get-target {
|
||||
$shareDS = Invoke-Sqlcmd -ServerInstance "(local)" -Database master -Query @"
|
||||
SELECT SettingValue AS backupSrc, SettingId
|
||||
FROM ActiveSystemServer.cfg.Settings
|
||||
WHERE SettingId LIKE 'Values.Modules.Replication.DbInitializationBackupPath%'
|
||||
AND LEN(SettingValue) > 1;
|
||||
"@
|
||||
$shareValue = $shareDS.backupSrc.Trim()
|
||||
$machineName = ($shareValue -split "\\") | Where-Object { $_ -ne "" } | Select-Object -First 1
|
||||
$result = [PSCustomObject]@{
|
||||
smbShare = "$shareValue"
|
||||
machineName = "$machineName"
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
# Function to perform SMB share reachability test
|
||||
function Test-SmbShareAccessibility {
|
||||
param(
|
||||
[string]$sharePath,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
write-log "to check: $sharePath"
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Measure the time taken to check share accessibility
|
||||
$startTime = [DateTime]::Now
|
||||
|
||||
# Check if the SMB share is accessible using Test-Path
|
||||
if (Test-Path -Path $sharePath) {
|
||||
$successfulConnections++
|
||||
$latencyTimes += (([DateTime]::Now) - $startTime).TotalMilliseconds
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
SMBShare = $sharePath
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
# Function to perform TCP latency and packet loss test
|
||||
function Test-TcpLatencyAndPacketLoss {
|
||||
param(
|
||||
[string]$target,
|
||||
[int]$port,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Perform TCP connection test using Test-NetConnection
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
|
||||
if ($tcpTest) {
|
||||
$successfulConnections++
|
||||
# Simulate latency as the connection time (since Test-NetConnection does not give latency directly)
|
||||
$connectionStart = [DateTime]::Now
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
$connectionEnd = [DateTime]::Now
|
||||
$latency = ($connectionEnd - $connectionStart).TotalMilliseconds
|
||||
$latencyTimes += $latency
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
Machine = $target
|
||||
Port = $port
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (-not (Test-Path $rootDrive)) {
|
||||
write-log "No $rootDrive drive is present"
|
||||
return -1
|
||||
}
|
||||
else {
|
||||
$trg = get-target
|
||||
$targetMachine = $trg.machineName
|
||||
$targetShare = $trg.smbShare
|
||||
|
||||
# Run the TCP latency and packet loss test
|
||||
$result = Test-TcpLatencyAndPacketLoss -target $targetMachine -port $tcpPort -count $testCount
|
||||
|
||||
# Check the CSV file size
|
||||
$csvFileSize = Check-CsvFileSize -filePath $outputCsvLAT
|
||||
|
||||
# If the file size exceeds the threshold, rotate the file. Force casting of the values to [double] to ensure it's not a string comparison
|
||||
if ([double]$csvFileSize -ge [double]$maxFileSizeMB) {
|
||||
Rotate-CsvFile -filePath $outputCsvLAT -rotationPath $dest -maxFiles $maxFiles
|
||||
}
|
||||
|
||||
# Check if the CSV file exists, if not create it
|
||||
if (-not (Test-Path $outputCsvLAT)) {
|
||||
# Create CSV file with headers
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation
|
||||
}
|
||||
else {
|
||||
# Append the result to the CSV file
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation -Append
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
$targetMachine = "GCMPRDCENT" # Define the target SMB share and output CSV file
|
||||
$tcpPort = 1433 # Specify the port number to test (e.g., HTTP = 80, HTTPS = 443)
|
||||
$rootDrive = "U:\" #root drive where files will be stored
|
||||
$subFolder = "TPDT-676" #subfolder where all files will be stored on the $rootDrive
|
||||
|
||||
#ensure we have a working drive
|
||||
if(-not(Test-Path -Path $rootDrive)){
|
||||
$rootDrive = "D:\"
|
||||
}
|
||||
|
||||
#create the destination folder if missing
|
||||
$dest = "$rootDrive\$subFolder".Replace("\\","\")
|
||||
if (-not (Test-Path -Path $dest)) {
|
||||
New-Item -ItemType Directory -Path $dest
|
||||
}
|
||||
|
||||
$outputCsvLAT = "$dest\tpdt-676-hcimon.csv" #name of the csv file with teh results
|
||||
$logFile = "$dest\tpdt-676-hcimon.log" #place of log file (for debugging sql job)
|
||||
|
||||
$maxFiles = 3 #how many csv file we keep after doing a rotation
|
||||
$maxFileSizeMB = 10 #What is the max size of a csv file before we do a rotation in MB
|
||||
$testCount = 5 # Number of connection attempts per iteration
|
||||
|
||||
# Function to check the size of the CSV file
|
||||
function Check-CsvFileSize {
|
||||
param ($filePath)
|
||||
|
||||
if (Test-Path $filePath) {
|
||||
$fileSizeMB = (Get-Item $filePath).length / 1MB
|
||||
return $fileSizeMB
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
# Function to rotate files
|
||||
function Rotate-CsvFile {
|
||||
param ($filePath, $rotationPath, $maxFiles)
|
||||
|
||||
#trailing backslash needed for concatenation of the path
|
||||
if (-not $rotationPath.EndsWith("\")) {
|
||||
$rotationPath += "\"
|
||||
}
|
||||
|
||||
# Create rotation directory if it doesn''t exist
|
||||
if (-not (Test-Path $rotationPath)) {
|
||||
New-Item -ItemType Directory -Path $rotationPath
|
||||
}
|
||||
|
||||
# Get the current timestamp to append to the rotated filename
|
||||
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
||||
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
|
||||
$fileExtension = [System.IO.Path]::GetExtension($filePath)
|
||||
|
||||
# Generate new rotated filename
|
||||
$rotatedFileName = "$rotationPath$fileName-$timestamp$fileExtension"
|
||||
|
||||
# Rotate the file
|
||||
Rename-Item -Path $filePath -NewName $rotatedFileName
|
||||
|
||||
# Get all rotated files sorted by creation time
|
||||
$rotatedFiles = Get-ChildItem -Path $rotationPath -Filter "$fileName-*$fileExtension" | Sort-Object LastWriteTime -Descending
|
||||
|
||||
# Keep only the most recent 3 files
|
||||
$filesToDelete = $rotatedFiles | Select-Object -Skip $maxFiles
|
||||
foreach ($file in $filesToDelete) {
|
||||
Remove-Item -Path $file.FullName
|
||||
}
|
||||
}
|
||||
|
||||
#write to log
|
||||
function write-log {
|
||||
param(
|
||||
[Parameter(ValueFromPipeline = $true)]
|
||||
[string]$msg,
|
||||
[string]$log = $logFile
|
||||
)
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
|
||||
$line = "$timestamp`t$msg"
|
||||
|
||||
Add-Content -Path $log -Value $line
|
||||
write-host $line
|
||||
}
|
||||
|
||||
#Get the replini-backup URI and hostname from ActiveSystemServer settings
|
||||
function get-target {
|
||||
$shareDS = Invoke-Sqlcmd -ServerInstance "(local)" -Database master -Query @"
|
||||
SELECT SettingValue AS backupSrc, SettingId
|
||||
FROM ActiveSystemServer.cfg.Settings
|
||||
WHERE SettingId LIKE ''Values.Modules.Replication.DbInitializationBackupPath%''
|
||||
AND LEN(SettingValue) > 1;
|
||||
"@
|
||||
$shareValue = $shareDS.backupSrc.Trim()
|
||||
$machineName = ($shareValue -split "\\") | Where-Object { $_ -ne "" } | Select-Object -First 1
|
||||
$result = [PSCustomObject]@{
|
||||
smbShare = "$shareValue"
|
||||
machineName = "$machineName"
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
# Function to perform SMB share reachability test
|
||||
function Test-SmbShareAccessibility {
|
||||
param(
|
||||
[string]$sharePath,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
write-log "to check: $sharePath"
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Measure the time taken to check share accessibility
|
||||
$startTime = [DateTime]::Now
|
||||
|
||||
# Check if the SMB share is accessible using Test-Path
|
||||
if (Test-Path -Path $sharePath) {
|
||||
$successfulConnections++
|
||||
$latencyTimes += (([DateTime]::Now) - $startTime).TotalMilliseconds
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
SMBShare = $sharePath
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
# Function to perform TCP latency and packet loss test
|
||||
function Test-TcpLatencyAndPacketLoss {
|
||||
param(
|
||||
[string]$target,
|
||||
[int]$port,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Perform TCP connection test using Test-NetConnection
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
|
||||
if ($tcpTest) {
|
||||
$successfulConnections++
|
||||
# Simulate latency as the connection time (since Test-NetConnection does not give latency directly)
|
||||
$connectionStart = [DateTime]::Now
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
$connectionEnd = [DateTime]::Now
|
||||
$latency = ($connectionEnd - $connectionStart).TotalMilliseconds
|
||||
$latencyTimes += $latency
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
Machine = $target
|
||||
Port = $port
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path $rootDrive)) {
|
||||
write-log "No $rootDrive drive is present, skipping checks."
|
||||
return -1
|
||||
}
|
||||
else {
|
||||
|
||||
# Run the TCP latency and packet loss test
|
||||
$result = Test-TcpLatencyAndPacketLoss -target $targetMachine -port $tcpPort -count $testCount
|
||||
|
||||
# Check the CSV file size
|
||||
$csvFileSize = Check-CsvFileSize -filePath $outputCsvLAT
|
||||
|
||||
# If the file size exceeds the threshold, rotate the file. Force casting of the values to [double] to ensure it's not a string comparison
|
||||
if ([double]$csvFileSize -ge [double]$maxFileSizeMB) {
|
||||
Rotate-CsvFile -filePath $outputCsvLAT -rotationPath $dest -maxFiles $maxFiles
|
||||
}
|
||||
|
||||
# Check if the CSV file exists, if not create it
|
||||
if (-not (Test-Path $outputCsvLAT)) {
|
||||
# Create CSV file with headers
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation
|
||||
}
|
||||
else {
|
||||
# Append the result to the CSV file
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation -Append
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
$tcpPort = 1433 # Specify the port number to test (e.g., HTTP = 80, HTTPS = 443)
|
||||
$rootDrive = "U:\" #root drive where files will be stored
|
||||
$subFolder = "TPDT-676" #subfolder where all files will be stored on the $rootDrive
|
||||
|
||||
#ensure we have a working drive
|
||||
if(-not(Test-Path -Path $rootDrive)){
|
||||
$rootDrive = "D:\"
|
||||
}
|
||||
|
||||
#create the destination folder if missing
|
||||
$dest = "$rootDrive\$subFolder".Replace("\\","\")
|
||||
if (-not (Test-Path -Path $dest)) {
|
||||
New-Item -ItemType Directory -Path $dest
|
||||
}
|
||||
|
||||
$outputCsvLAT = "$dest\tpdt-676-POS.csv" #name of the csv file with teh results
|
||||
$logFile = "$dest\tpdt-676-POS.log" #place of log file (for debugging sql job)
|
||||
|
||||
$maxFiles = 3 #how many csv file we keep after doing a rotation
|
||||
$maxFileSizeMB = 10 #What is the max size of a csv file before we do a rotation in MB
|
||||
$testCount = 5 # Number of connection attempts per iteration
|
||||
|
||||
#list of POS to check
|
||||
$psList = @(
|
||||
"CAMA34945659T05",
|
||||
"CAMA04140508T10",
|
||||
"CSUN89545123T13",
|
||||
"CAMA32144422T04",
|
||||
"WSU186A07",
|
||||
"WAM083A04",
|
||||
"CAMA00180002T07",
|
||||
"CAMA26338993T08",
|
||||
"CAMA25680308T12",
|
||||
"CAMA10680305T05",
|
||||
"WCV853A04",
|
||||
"CAMA31345688T05",
|
||||
"CAMA22080072T13",
|
||||
"CCVI81639628T03",
|
||||
"WAM060B03",
|
||||
"CSUN17180460T04",
|
||||
"WCV806A10",
|
||||
"CAMA01740468T08",
|
||||
"CCVI83342696T11",
|
||||
"CCVI84339629T04",
|
||||
"WAM216A06",
|
||||
"CSUN14334905T03",
|
||||
"WAM269A03",
|
||||
"WAM080A03",
|
||||
"WCV832A04",
|
||||
"WCV848A08",
|
||||
"CAMA28336544T05",
|
||||
"CAMA60180481T10",
|
||||
"WSU721A08",
|
||||
"CAMA02980066T05",
|
||||
"CAMA89843155T12",
|
||||
"CAMA29632238T05",
|
||||
"CAMA09038344T03",
|
||||
"WCV836B04",
|
||||
"WAM601A03",
|
||||
"CAMA23334459T10",
|
||||
"WAM065B05",
|
||||
"WCV812B09",
|
||||
"WAM626A10",
|
||||
"WCV267A05",
|
||||
"WSU141B09",
|
||||
"WCV50705",
|
||||
"CAMA24946011T04",
|
||||
"CSUN12145469T05",
|
||||
"CAMA08136638T10",
|
||||
"CAMA51080448T04",
|
||||
"WCV844A04",
|
||||
"WSU841A02",
|
||||
"CSUN64580053T02",
|
||||
"CCVI24335530T06"
|
||||
)
|
||||
|
||||
# Function to check the size of the CSV file
|
||||
function Check-CsvFileSize {
|
||||
param ($filePath)
|
||||
|
||||
if (Test-Path $filePath) {
|
||||
$fileSizeMB = (Get-Item $filePath).length / 1MB
|
||||
return $fileSizeMB
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
# Function to rotate files
|
||||
function Rotate-CsvFile {
|
||||
param ($filePath, $rotationPath, $maxFiles)
|
||||
|
||||
#trailing backslash needed for concatenation of the path
|
||||
if (-not $rotationPath.EndsWith("\")) {
|
||||
$rotationPath += "\"
|
||||
}
|
||||
|
||||
# Create rotation directory if it doesn''t exist
|
||||
if (-not (Test-Path $rotationPath)) {
|
||||
New-Item -ItemType Directory -Path $rotationPath
|
||||
}
|
||||
|
||||
# Get the current timestamp to append to the rotated filename
|
||||
$timestamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
||||
$fileName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
|
||||
$fileExtension = [System.IO.Path]::GetExtension($filePath)
|
||||
|
||||
# Generate new rotated filename
|
||||
$rotatedFileName = "$rotationPath$fileName-$timestamp$fileExtension"
|
||||
|
||||
# Rotate the file
|
||||
Rename-Item -Path $filePath -NewName $rotatedFileName
|
||||
|
||||
# Get all rotated files sorted by creation time
|
||||
$rotatedFiles = Get-ChildItem -Path $rotationPath -Filter "$fileName-*$fileExtension" | Sort-Object LastWriteTime -Descending
|
||||
|
||||
# Keep only the most recent 3 files
|
||||
$filesToDelete = $rotatedFiles | Select-Object -Skip $maxFiles
|
||||
foreach ($file in $filesToDelete) {
|
||||
Remove-Item -Path $file.FullName
|
||||
}
|
||||
}
|
||||
|
||||
#write to log
|
||||
function write-log {
|
||||
param(
|
||||
[Parameter(ValueFromPipeline = $true)]
|
||||
[string]$msg,
|
||||
[string]$log = $logFile
|
||||
)
|
||||
|
||||
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
|
||||
$line = "$timestamp`t$msg"
|
||||
|
||||
Add-Content -Path $log -Value $line
|
||||
write-host $line
|
||||
}
|
||||
|
||||
#Get the replini-backup URI and hostname from ActiveSystemServer settings
|
||||
function get-target {
|
||||
$shareDS = Invoke-Sqlcmd -ServerInstance "(local)" -Database master -Query @"
|
||||
SELECT SettingValue AS backupSrc, SettingId
|
||||
FROM ActiveSystemServer.cfg.Settings
|
||||
WHERE SettingId LIKE ''Values.Modules.Replication.DbInitializationBackupPath%''
|
||||
AND LEN(SettingValue) > 1;
|
||||
"@
|
||||
$shareValue = $shareDS.backupSrc.Trim()
|
||||
$machineName = ($shareValue -split "\\") | Where-Object { $_ -ne "" } | Select-Object -First 1
|
||||
$result = [PSCustomObject]@{
|
||||
smbShare = "$shareValue"
|
||||
machineName = "$machineName"
|
||||
}
|
||||
|
||||
return $result
|
||||
}
|
||||
|
||||
# Function to perform SMB share reachability test
|
||||
function Test-SmbShareAccessibility {
|
||||
param(
|
||||
[string]$sharePath,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
write-log "to check: $sharePath"
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Measure the time taken to check share accessibility
|
||||
$startTime = [DateTime]::Now
|
||||
|
||||
# Check if the SMB share is accessible using Test-Path
|
||||
if (Test-Path -Path $sharePath) {
|
||||
$successfulConnections++
|
||||
$latencyTimes += (([DateTime]::Now) - $startTime).TotalMilliseconds
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
SMBShare = $sharePath
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
# Function to perform TCP latency and packet loss test
|
||||
function Test-TcpLatencyAndPacketLoss {
|
||||
param(
|
||||
[string]$target,
|
||||
[int]$port,
|
||||
[int]$count
|
||||
)
|
||||
|
||||
$successfulConnections = 0
|
||||
$latencyTimes = @()
|
||||
|
||||
for ($i = 1; $i -le $count; $i++) {
|
||||
# Perform TCP connection test using Test-NetConnection
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
|
||||
if ($tcpTest) {
|
||||
$successfulConnections++
|
||||
# Simulate latency as the connection time (since Test-NetConnection does not give latency directly)
|
||||
$connectionStart = [DateTime]::Now
|
||||
$tcpTest = Test-NetConnection -ComputerName $target -Port $port -InformationLevel Quiet
|
||||
$connectionEnd = [DateTime]::Now
|
||||
$latency = ($connectionEnd - $connectionStart).TotalMilliseconds
|
||||
$latencyTimes += $latency
|
||||
}
|
||||
else {
|
||||
# Log failed connection as N/A latency
|
||||
$latencyTimes += "N/A"
|
||||
}
|
||||
|
||||
Start-Sleep -Milliseconds 500 # Slight delay between attempts
|
||||
}
|
||||
|
||||
# Calculate packet loss percentage
|
||||
$packetLoss = (($count - $successfulConnections) / $count) * 100
|
||||
|
||||
# Calculate average latency excluding N/A entries
|
||||
$avgLatency = ($latencyTimes | Where-Object { $_ -ne "N/A" } | Measure-Object -Average).Average
|
||||
|
||||
return [pscustomobject]@{
|
||||
Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||
Machine = $target
|
||||
Port = $port
|
||||
PacketLoss = "$packetLoss"
|
||||
AvgLatency = if ($null -ne $avgLatency) { $avgLatency } else { "N/A" }
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path $rootDrive)) {
|
||||
write-log "No $rootDrive drive is present, skipping checks."
|
||||
return -1
|
||||
}
|
||||
else {
|
||||
|
||||
$psList | ForEach-Object {
|
||||
$pos_name = $_
|
||||
Write-Output "pos name: $pos_name"
|
||||
|
||||
# Run the TCP latency and packet loss test
|
||||
$result = Test-TcpLatencyAndPacketLoss -target $pos_name -port $tcpPort -count $testCount
|
||||
|
||||
# Check the CSV file size
|
||||
$csvFileSize = Check-CsvFileSize -filePath $outputCsvLAT
|
||||
|
||||
# If the file size exceeds the threshold, rotate the file. Force casting of the values to [double] to ensure it's not a string comparison
|
||||
if ([double]$csvFileSize -ge [double]$maxFileSizeMB) {
|
||||
Rotate-CsvFile -filePath $outputCsvLAT -rotationPath $dest -maxFiles $maxFiles
|
||||
}
|
||||
|
||||
# Check if the CSV file exists, if not create it
|
||||
if (-not (Test-Path $outputCsvLAT)) {
|
||||
# Create CSV file with headers
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation
|
||||
}
|
||||
else {
|
||||
# Append the result to the CSV file
|
||||
$result | Export-Csv -Path $outputCsvLAT -NoTypeInformation -Append
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user