Skip to content

Commit b7b2b7b

Browse files
authored
Cleanup old trace files on system (#372)
# Description Summary of changes: This pull request includes several changes to the `src/SdnDiagnostics` and `src/modules/SdnDiag.Utilities` files, focusing on improving the handling of working directories and trace output files. The most important changes include adding a new function to get the working directory, updating the trace output file handling to include cleanup of stale files, and modifying the data collection process to use the new working directory function. Enhancements to working directory and trace file handling: * [`src/SdnDiagnostics.psd1`](diffhunk://#diff-17aaaa968cc894449c79b449c228b28d8a8990bde4000e59bcf24d8189671ee1R119): Added `Get-SdnWorkingDirectory` to the list of exported functions. * [`src/modules/SdnDiag.Utilities.psm1`](diffhunk://#diff-9e50bcf150b088e2c4df3d0768d8dcad4abf1338de3498749997682448a07bdcR1243-R1252): Added `Get-SdnWorkingDirectory` function to return the working directory used for storing logs and other artifacts. * [`src/modules/SdnDiag.Utilities.psm1`](diffhunk://#diff-9e50bcf150b088e2c4df3d0768d8dcad4abf1338de3498749997682448a07bdcR1785-R1789): Updated `New-TraceOutputFile` function to clean up stale trace files and changed the trace file naming convention to include the process ID. [[1]](diffhunk://#diff-9e50bcf150b088e2c4df3d0768d8dcad4abf1338de3498749997682448a07bdcR1785-R1789) [[2]](diffhunk://#diff-9e50bcf150b088e2c4df3d0768d8dcad4abf1338de3498749997682448a07bdcR1800-R1818) * [`src/modules/SdnDiag.Utilities.psm1`](diffhunk://#diff-9e50bcf150b088e2c4df3d0768d8dcad4abf1338de3498749997682448a07bdcR2161-R2180): Modified `Trace-Output` function to include error handling and ensure the mutex is always released. Improvements to data collection: * [`src/SdnDiagnostics.psm1`](diffhunk://#diff-490865628c61b2e97c50f45b37d7086647c70b2444cbfb9c60cc8c682801356eL1156-R1156): Updated `Start-SdnDataCollection` function to use the new working directory function for copying trace output files. # Change type - [x] Bug fix (non-breaking change) - [ ] Code style update (formatting, local variables) - [ ] New Feature (non-breaking change that adds new functionality without impacting existing) - [ ] Breaking change (fix or feature that may cause functionality impact) - [ ] Other # Checklist: - [x] My code follows the style and contribution guidelines of this project. - [x] I have tested and validated my code changes.
1 parent f7b4419 commit b7b2b7b

File tree

3 files changed

+51
-10
lines changed

3 files changed

+51
-10
lines changed

src/SdnDiagnostics.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@
120120
'Get-SdnVfpPortLayer',
121121
'Get-SdnVfpPortRule',
122122
'Get-SdnVfpPortState',
123+
'Get-SdnWorkingDirectory',
123124
'Import-SdnCertificate',
124125
'Install-SdnDiagnostics',
125126
'Invoke-SdnCommand',

src/SdnDiagnostics.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ function Start-SdnDataCollection {
11501150
foreach ($node in $filteredDataCollectionNodes) {
11511151
[System.IO.FileInfo]$formattedDirectoryName = Join-Path -Path $OutputDirectory.FullName -ChildPath $node.ToLower()
11521152
Copy-FileFromRemoteComputer -Path $tempDirectory.FullName -Destination $formattedDirectoryName.FullName -ComputerName $node -Credential $Credential -Recurse -Force
1153-
Copy-FileFromRemoteComputer -Path (Get-TraceOutputFile) -Destination $formattedDirectoryName.FullName -ComputerName $node -Credential $Credential -Force
1153+
Copy-FileFromRemoteComputer -Path "$(Get-WorkingDirectory)\SdnDiagnostics_TraceOutput*.csv" -Destination $formattedDirectoryName.FullName -ComputerName $node -Credential $Credential -Force
11541154
}
11551155

11561156
$dataCollectionObject.TotalSize = (Get-FolderSize -Path $OutputDirectory.FullName -Total)

src/modules/SdnDiag.Utilities.psm1

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,16 @@ function Get-UserInput {
12641264
return Read-Host
12651265
}
12661266

1267+
function Get-SdnWorkingDirectory {
1268+
<#
1269+
.SYNOPSIS
1270+
Returns the working directory that is used for storing logs and other artifacts
1271+
#>
1272+
1273+
return (Get-WorkingDirectory)
1274+
}
1275+
1276+
12671277
function Get-WorkingDirectory {
12681278

12691279
# check to see if the working directory has been configured into cache
@@ -1796,8 +1806,11 @@ function New-TraceOutputFile {
17961806
$null = New-Item -Path $workingDir -ItemType Directory -Force
17971807
}
17981808

1809+
# cleanup any stale trace files
1810+
Remove-OldTraceOutputFile
1811+
17991812
# build the trace file path and set global variable
1800-
[System.String]$fileName = "SdnDiagnostics_TraceOutput_{0}.csv" -f (Get-Date).ToString('yyyyMMdd')
1813+
[System.String]$fileName = "SdnDiagnostics_TraceOutput_$($PID).csv"
18011814
[System.IO.FileInfo]$filePath = Join-Path -Path $workingDir -ChildPath $fileName
18021815
Set-TraceOutputFile -Path $filePath.FullName
18031816

@@ -1808,6 +1821,25 @@ function New-TraceOutputFile {
18081821
catch {
18091822
$_.Exception | Write-Error
18101823
}
1824+
1825+
return $filePath
1826+
}
1827+
1828+
function Remove-OldTraceOutputFile {
1829+
[CmdletBinding()]
1830+
param()
1831+
1832+
try {
1833+
$workingDir = (Get-WorkingDirectory)
1834+
$files = Get-ChildItem -Path $workingDir | Where-Object { $_.Name -like "SdnDiagnostics_TraceOutput_*.csv" }
1835+
$staleFiles = $files | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-30) }
1836+
if ($staleFiles) {
1837+
$staleFiles | Remove-Item -Force
1838+
}
1839+
}
1840+
catch {
1841+
$_ | Write-Error
1842+
}
18111843
}
18121844

18131845
function New-WorkingDirectory {
@@ -1822,7 +1854,7 @@ function New-WorkingDirectory {
18221854
}
18231855

18241856
# create the trace file
1825-
New-TraceOutputFile
1857+
$null = New-TraceOutputFile
18261858
}
18271859
catch {
18281860
$_.Exception | Write-Error
@@ -2150,16 +2182,24 @@ function Trace-Output {
21502182
}
21512183
}
21522184

2153-
# write the event to trace file to be used for debugging purposes
2154-
$mutexInstance = Wait-OnMutex -MutexId 'SDN_TraceLogging' -ErrorAction Continue
2155-
if ($mutexInstance) {
2156-
$traceEvent | Export-Csv -Append -NoTypeInformation -Path $traceFile
2185+
try {
2186+
# write the event to trace file to be used for debugging purposes
2187+
$mutexInstance = Wait-OnMutex -MutexId 'SDN_TraceLogging' -ErrorAction Continue
2188+
if ($mutexInstance) {
2189+
$traceEvent | Export-Csv -Append -NoTypeInformation -Path $traceFile
2190+
}
2191+
}
2192+
catch {
2193+
$_ | Write-Error
2194+
}
2195+
finally {
2196+
if ($mutexInstance) {
2197+
$mutexInstance.ReleaseMutex()
2198+
}
21572199
}
21582200
}
21592201
end {
2160-
if ($mutexInstance) {
2161-
$mutexInstance.ReleaseMutex()
2162-
}
2202+
# do nothing here
21632203
}
21642204
}
21652205

0 commit comments

Comments
 (0)