Harden Windows service supervision
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
$ErrorActionPreference = "Continue"
|
||||
|
||||
$ProjectDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$LogDir = Join-Path $ProjectDir "logs"
|
||||
if (!(Test-Path -LiteralPath $LogDir)) {
|
||||
New-Item -ItemType Directory -Path $LogDir | Out-Null
|
||||
}
|
||||
|
||||
$ReportPath = Join-Path $LogDir ("diagnose_" + (Get-Date -Format "yyyyMMdd_HHmmss") + ".txt")
|
||||
$Lines = New-Object System.Collections.Generic.List[string]
|
||||
|
||||
function Add-ReportLine([string]$Text = "") {
|
||||
$Lines.Add($Text)
|
||||
Write-Host $Text
|
||||
}
|
||||
|
||||
Add-ReportLine "DocuTranslate diagnostic report"
|
||||
Add-ReportLine ("Generated: " + (Get-Date))
|
||||
|
||||
$Os = Get-CimInstance Win32_OperatingSystem
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine "===== System uptime ====="
|
||||
Add-ReportLine ("Last boot: " + $Os.LastBootUpTime)
|
||||
Add-ReportLine ("Uptime: " + ((Get-Date) - $Os.LastBootUpTime))
|
||||
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine "===== Power and restart events (last 14 days) ====="
|
||||
$StartTime = (Get-Date).AddDays(-14)
|
||||
$Events = Get-WinEvent -FilterHashtable @{ LogName = "System"; Id = @(41, 1074, 6005, 6006, 6008); StartTime = $StartTime } -ErrorAction SilentlyContinue |
|
||||
Sort-Object TimeCreated -Descending |
|
||||
Select-Object -First 30
|
||||
if ($Events) {
|
||||
foreach ($Event in $Events) {
|
||||
$Message = ($Event.Message -replace "`r?`n", " ")
|
||||
Add-ReportLine ("{0:yyyy-MM-dd HH:mm:ss} ID={1} Provider={2} Message={3}" -f $Event.TimeCreated, $Event.Id, $Event.ProviderName, $Message)
|
||||
}
|
||||
} else {
|
||||
Add-ReportLine "No matching events found."
|
||||
}
|
||||
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine "===== Scheduled tasks ====="
|
||||
foreach ($TaskName in @("DocuTranslate", "DocuTranslateWatchdog")) {
|
||||
$Task = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue
|
||||
if ($null -eq $Task) {
|
||||
Add-ReportLine ("${TaskName}: NOT FOUND")
|
||||
continue
|
||||
}
|
||||
$Info = Get-ScheduledTaskInfo -TaskName $TaskName
|
||||
Add-ReportLine ("{0}: State={1}; LastRun={2}; LastResult={3}; NextRun={4}; Limit={5}" -f $TaskName, $Task.State, $Info.LastRunTime, $Info.LastTaskResult, $Info.NextRunTime, $Task.Settings.ExecutionTimeLimit)
|
||||
}
|
||||
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine "===== Port 8010 ====="
|
||||
$Connections = Get-NetTCPConnection -LocalPort 8010 -State Listen -ErrorAction SilentlyContinue
|
||||
if ($Connections) {
|
||||
foreach ($Connection in $Connections) {
|
||||
$Process = Get-Process -Id $Connection.OwningProcess -ErrorAction SilentlyContinue
|
||||
Add-ReportLine ("Listening: {0}:{1}; PID={2}; Process={3}" -f $Connection.LocalAddress, $Connection.LocalPort, $Connection.OwningProcess, $Process.ProcessName)
|
||||
}
|
||||
} else {
|
||||
Add-ReportLine "Nothing is listening on port 8010."
|
||||
}
|
||||
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine "===== HTTP health ====="
|
||||
try {
|
||||
$Response = Invoke-WebRequest -UseBasicParsing -Uri "http://127.0.0.1:8010/service/meta" -TimeoutSec 8
|
||||
Add-ReportLine ("HTTP status: " + $Response.StatusCode)
|
||||
} catch {
|
||||
Add-ReportLine ("HTTP failed: " + $_.Exception.Message)
|
||||
}
|
||||
|
||||
foreach ($Name in @("autostart.log", "watchdog.log")) {
|
||||
$Path = Join-Path $LogDir $Name
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine ("===== Last 80 lines: " + $Name + " =====")
|
||||
if (Test-Path -LiteralPath $Path) {
|
||||
foreach ($Line in (Get-Content -LiteralPath $Path -Tail 80)) {
|
||||
Add-ReportLine $Line
|
||||
}
|
||||
} else {
|
||||
Add-ReportLine "Log file not found."
|
||||
}
|
||||
}
|
||||
|
||||
$Lines | Out-File -LiteralPath $ReportPath -Encoding utf8
|
||||
Add-ReportLine ""
|
||||
Add-ReportLine ("Report saved to: " + $ReportPath)
|
||||
Reference in New Issue
Block a user