diff --git a/ansible/deploy_file_1c_windows_telemetry.yml b/ansible/deploy_file_1c_windows_telemetry.yml index b9df33a..0f3d6c5 100644 --- a/ansible/deploy_file_1c_windows_telemetry.yml +++ b/ansible/deploy_file_1c_windows_telemetry.yml @@ -105,8 +105,13 @@ $taskName = "{{ aw_windows_file_1c_auto_upload_task_name }}" $powerShellExe = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' $taskCommand = "`"$powerShellExe`" -NoProfile -ExecutionPolicy Bypass -File `"{{ aw_windows_state_root }}\export-upload-file-1c-telemetry.ps1`" -ConfigPath `"{{ aw_windows_state_root }}\deployment-config.json`"" - & cmd.exe /c "schtasks /Delete /TN `"$taskName`" /F >nul 2>&1" | Out-Null - & schtasks.exe /Create /TN $taskName /TR $taskCommand /SC HOURLY /MO {{ aw_windows_file_1c_auto_upload_interval_hours | int }} /ST 00:00 /RU SYSTEM /RL HIGHEST /F | Out-Null - if ($LASTEXITCODE -ne 0) { - throw "Не удалось создать scheduled task $taskName" + $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue + if ($existingTask) { + $action = New-ScheduledTaskAction -Execute $powerShellExe -Argument "-NoProfile -ExecutionPolicy Bypass -File `"{{ aw_windows_state_root }}\export-upload-file-1c-telemetry.ps1`" -ConfigPath `"{{ aw_windows_state_root }}\deployment-config.json`"" + Set-ScheduledTask -TaskName $taskName -Action $action | Out-Null + } else { + & schtasks.exe /Create /TN $taskName /TR $taskCommand /SC HOURLY /MO {{ aw_windows_file_1c_auto_upload_interval_hours | int }} /ST 00:00 /RU SYSTEM /RL HIGHEST /F | Out-Null + if ($LASTEXITCODE -ne 0) { + throw "Не удалось создать scheduled task $taskName" + } } diff --git a/windows/ActivityWatch.Windows.Common.psm1 b/windows/ActivityWatch.Windows.Common.psm1 index a35f810..a12a1db 100755 --- a/windows/ActivityWatch.Windows.Common.psm1 +++ b/windows/ActivityWatch.Windows.Common.psm1 @@ -1652,6 +1652,10 @@ function Register-ActivityWatchFile1CAutoUploadTask { $powerShellExe = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' $taskCommand = "`"$powerShellExe`" -NoProfile -ExecutionPolicy Bypass -File `"$uploadScript`" -ConfigPath `"$ConfigPath`"" + if (Set-ActivityWatchScheduledTaskAction -TaskName $taskName -Execute $powerShellExe -Arguments "-NoProfile -ExecutionPolicy Bypass -File `"$uploadScript`" -ConfigPath `"$ConfigPath`"") { + return + } + Remove-ActivityWatchScheduledTask -TaskName $taskName & schtasks.exe /Create /TN $taskName /TR $taskCommand /SC HOURLY /MO $intervalHours /ST 00:00 /RU SYSTEM /RL HIGHEST /F | Out-Null if ($LASTEXITCODE -ne 0) { diff --git a/windows/export-upload-file-1c-telemetry.ps1 b/windows/export-upload-file-1c-telemetry.ps1 index 477a347..3907491 100644 --- a/windows/export-upload-file-1c-telemetry.ps1 +++ b/windows/export-upload-file-1c-telemetry.ps1 @@ -10,6 +10,27 @@ param( Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +$LogDir = 'C:\ProgramData\AWatch-rus\logs' +$LogPath = Join-Path $LogDir 'file1c-telemetry.log' +$ScpExe = Join-Path $env:WINDIR 'System32\OpenSSH\scp.exe' + +New-Item -ItemType Directory -Path $LogDir -Force | Out-Null + +function Write-RunLog { + param( + [Parameter(Mandatory = $true)] + [string]$Message + ) + + $line = '{0} {1}' -f ([DateTime]::UtcNow.ToString('yyyy-MM-ddTHH:mm:ssZ')), $Message + Add-Content -LiteralPath $LogPath -Value $line -Encoding UTF8 +} + +trap { + Write-RunLog ("ERROR: " + ($_ | Out-String).Trim()) + exit 1 +} + function New-TemporarySshKeyCopy { param( [Parameter(Mandatory = $true)] @@ -119,17 +140,26 @@ function Invoke-SshUploadWithRetry { ) for ($attempt = 1; $attempt -le $Attempts; $attempt++) { - & scp.exe -q -i $KeyPath -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=NUL $SourcePath $Destination + Write-RunLog "scp attempt=$attempt source=$SourcePath destination=$Destination" + & $ScpExe -q -i $KeyPath -o LogLevel=ERROR -o StrictHostKeyChecking=no -o UserKnownHostsFile=NUL $SourcePath $Destination if ($LASTEXITCODE -eq 0) { + Write-RunLog "scp success source=$SourcePath" return } if ($attempt -ge $Attempts) { throw "scp upload failed after $Attempts attempts for $SourcePath with rc=$LASTEXITCODE" } + Write-RunLog "scp retry source=$SourcePath rc=$LASTEXITCODE delay=${DelaySeconds}s" Start-Sleep -Seconds $DelaySeconds } } +Write-RunLog 'file1c exporter start' + +if (-not (Test-Path -LiteralPath $ScpExe)) { + throw "scp client not found: $ScpExe" +} + if (-not (Test-Path -LiteralPath $RemoteKeyPath)) { throw "SSH private key not found: $RemoteKeyPath" } @@ -275,15 +305,19 @@ Write-JsonLines -Path ([string]$files['host']) -Rows $hostRowsNormalized $effectiveKeyPath = New-TemporarySshKeyCopy -SourceKeyPath $RemoteKeyPath try { + Write-RunLog "prepared datasets documents=$($documentRows.Count) reglog=$($reglogRows.Count) audit=$($auditRows.Count) host=$($hostRowsNormalized.Count)" foreach ($dataset in 'documents', 'reglog', 'audit', 'host') { Invoke-SshUploadWithRetry -KeyPath $effectiveKeyPath -SourcePath ([string]$files[$dataset]) -Destination "$AnalyticsUser@$AnalyticsHost`:$RemoteRoot/$dataset/" } + Write-RunLog "upload complete analyticsHost=$AnalyticsHost remoteRoot=$RemoteRoot" } finally { Remove-Item -LiteralPath $effectiveKeyPath -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath $outRoot -Recurse -Force -ErrorAction SilentlyContinue } +Write-RunLog 'file1c exporter done' + [ordered]@{ analyticsHost = $AnalyticsHost analyticsUser = $AnalyticsUser