我已经创建了一个脚本,希望将其作为服务运行。它使用Invoke-RestMethod来监视服务器上的应用程序,当它检测到不需要的条件时,它会重新启动应用程序。当我使用我的用户帐户运行它时,它工作得很好,但当我将它作为服务(作为系统)运行时,什么也没有发生。我将大多数控制台输出输出到一个变量,然后在循环结束时,将变量的内容作为临时日志写入文件。这个文件永远不会被写入。
我使用this guide创建了这个服务,它似乎启动正常并运行……只是什么都没有发生。
下面是我正在使用的脚本,服务应该执行该脚本。
#Configuration Options
$ESHost = ""
$ESPort = 9200
$ESPro = ""
$ESAPI = ""
$ESAPI.Cred = ""
$LogRoot = ""
$LSHost = ""
$LSPort = 9600
$LSPro = ""
$SMTP = @{
server = ""
to = ""
from = ""
port = 25
}
#Look for Logstash service
While (Get-Service Logstash) {
$Date = Get-Date -Format "MMddyyyy_HHmmss"
#Verify service is running
if ((Get-Service Logstash).Status -eq "Running") {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') *****Start Health Check*****`n"
#Collect list of pipelines
$Pipelines = ((Invoke-RestMethod -Uri "$($LSPro)://$($LSHost):$($LSPort)/_node/stats/pipelines").pipelines.PSObject.Properties | Where-Object {$_.MemberType -eq "NoteProperty"}).Name
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') Running pipelines: $($pipelines.count)`n"
foreach ($pipeline in $pipelines) {
$log += " $($pipeline)`n"
}
$down = @()
#Check health of each pipeline
foreach ($pipeline in $pipelines) {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') Starting health check on $($pipeline) pipeline`n"
$pipelineinfo = Invoke-RestMethod -Uri "$($LSPro)://$($LSHost):$($LSPort)/_node/stats/pipelines/$($pipeline)"
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) ephemeral id $($pipelineinfo.ephemeral_id)`n"
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) queue size (bytes) $([string]::Format('{0:N0}',($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes)))`n"
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) queue capacity (bytes) $([string]::Format('{0:N0}',($pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes)))`n"
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) queued events $($pipelineinfo.pipelines.$pipeline.queue.events)`n"
if ($pipelineinfo.pipelines.$pipeline.hash -eq $null) {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) down`n"
$down += $pipeline
}
if ($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes -eq $pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes) {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) queue at max capacity"
Try {
Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Pipeline Queue Full" -Port $smtp.port -Body "Pipeline $($pipeline)'s queue appears to be at full capacity"
}
Catch {
log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
}
}
if (($pipelineinfo.pipelines.$pipeline.queue.queue_size_in_bytes -ne $pipelineinfo.pipelines.$pipeline.queue.max_queue_size_in_bytes) -and ($pipelineinfo.pipelines.$pipeline.hash -ne $null)) {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($pipeline) up`n"
}
}
#Actions on unhealthy pipelines
if ($down.count -gt 0) {
foreach ($pipe in $down) {
Write-Host "$(Get-Date) Pipeline $($pipe) appears to be down. Collecting logs..."
#Compress logs for each failed pipeline
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') Collecting $($pipe) logs...`n"
Try {
Compress-Archive -LiteralPath "$($LogRoot)\pipeline_$($pipe).log" -CompressionLevel Fastest -DestinationPath "$($LogRoot)\pipeline_$($pipe)_$($Date).zip"
}
Catch {
log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
}
}
#Compress logstash log
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') Collecting logstash-plain.log...`n"
Try {
Compress-Archive -LiteralPath "$($LogRoot)\logstash-plain.log" -CompressionLevel Fastest -DestinationPath "$($LogRoot)\logstash-plain_$($Date).zip"
}
Catch {
log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
}
#Send compressed logs in email notification
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') Sending mail notification`n"
Try {
Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Pipeline Down Detected" -Port $smtp.port -Attachments (Get-ChildItem "$LogRoot\*.zip")
}
Catch {
log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') $($error[0].CategoryInfo.Activity)"+": "+"$($error[0].Exception.Message)`n"
}
#Remove compressed logs
Remove-Item (Get-ChildItem "$LogRoot\*.zip") -Force -Confirm:$false
Restart-Service Logstash
Remove-Variable down
Do {
(Get-Service Logstash).Status
Start-Sleep -Seconds 5
}
Until (((Get-Service Logstash).Status) -eq "Running")
Send-MailMessage -SmtpServer $smtp.server -To $smtp.to -From $smtp.from -Subject "Logstash Successfully Restarted" -Port $smtp.port
#Actions on no unhealthy pipelines
} else {
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') All pipelines running`n"
Write-Host "$(Get-Date) All pipelines up"
}
}
$log += "$(Get-Date -Format 'MM-dd-yyyy HH:mm:ss') *****End Health Check*****`n"
$log | Out-File "$($LogRoot)\MonitorService.log" -Append
Remove-Variable log
Start-Sleep -Seconds 300
}发布于 2020-12-31 01:53:20
发现了一些我忽略的问题。
https://stackoverflow.com/questions/65509622
复制相似问题