我目前正在尝试创建一个脚本,允许我检查多个网址,以查看它们是否在线且处于活动状态。我的公司有多个服务器,其中有不同的环境活动(生产,登台,开发等)我需要一个脚本,可以检查所有环境URL并告诉我他们是否每天早上都在线,所以我可以领先于游戏解决任何服务器或网站崩溃问题。
我的问题是,我不能单独将逻辑严格地建立在HTTP代码上以使网站在线或不在线,我们的一些网站可能从HTTP角度在线,但网站的组件或网站部件已关闭在页面上显示错误消息。
我在编写一个脚本时遇到问题,该脚本不仅可以检查HTTP状态,还可以扫描页面并解析出任何错误消息,然后根据这两个逻辑写入主机,无论该站点是否为“在线“或”向下“
以下是我到目前为止的情况,您会注意到它不包含任何关于关键词解析的内容,因为我不知道如何实现......
#Lower Environments Checklist Automated Script
Write-Host Report generated at (Get-date)
write-host("Lower Environments Status Check");
$msg = ""
$array = get-content C:\LowerEnvChecklist\appurls.txt
$log = "C:\LowerEnvChecklist\lowerenvironmentslog.txt"
write-host("Checking appurls.txt...One moment please.");
("`n--------------------------------------------------------------------------- ") | out-file $log -Append
Get-Date | Out-File $log -Append
("`n***Checking Links***") | out-file $log -Append
("`n") | out-file $log -Append
for ($i=0; $i -lt $array.length; $i++) {
$HTTP_Status = -1
$HTTP_Request = [System.Net.WebRequest]::Create($array[$i])
$HTTP_Request.Timeout =60000
$HTTP_Response = $HTTP_Request.GetResponse()
$HTTP_Status = [int]$HTTP_Response.StatusCode
If ($HTTP_Status -eq 200) {
$msg = $array[$i] + " is ONLINE!"
}
Else {
$msg = $array[$i] + " may be DOWN, please check!"
}
$HTTP_Response.Close()
$msg | Out-File $log -Append -width 120
write-host $msg
}
("`n") | out-file $log -Append
("`n***Lower Environments Checklist Completed***") | out-file $log -Append
write-host("Lower Environments Checklist Completed");
appurls.txt 只包含我需要检查的内部网址。
任何帮助将不胜感激!感谢。
答案 0 :(得分:0)
这至少可以让你知道该怎么做。需要捕获网站数据才能解析它。然后我们运行一个正则表达式查询,该查询是从一个字符串数组构建的。这些字符串是可能在页面上看不到的文本。
# build a regex query of error strings to match against.
$errorTexts = "error has occurred","Oops","Unable to display widget data","unexpected error occurred","temporarily unavailable"
$regex = ($errorTexts | ForEach-Object{[regex]::Escape($_)}) -join "|"
# Other preproccessing would go here
# Loop through each element of the array
ForEach($target in $array){
# Erase results for the next pass in case of error.
$result, $response, $stream, $page = $null
# Navigate to the website.
$result = [System.Net.WebRequest]::Create($target)
$response = $result.GetResponse()
$stream = [System.IO.StreamReader]$response.GetResponseStream()
$page = $stream.ReadToEnd()
# Determine if the page is truly up based on the information above.
If($response.StatusCode -eq 200){
# While the page might have rendered need to determine there are no errors present
if($page -notmatch $regex){
$msg = "$target is online!"
} else {
$msg = "$target may be DOWN, please check!"
}
} else {
$msg = "$target may be DOWN, please check!"
}
# Log Result
$msg | Out-File $log -Append -width 120
# Close the connection
$response.Close()
}
# Other postproccessing would go here
我想展示一下here-string看起来像是要替换你的out-file
重复内容。你的日志文件头曾经是这几行。我把它减少到了一个。
@"
---------------------------------------------------------------------------
$(Get-Date)
***Checking Links***
"@ | Out-File $log -Append
还要考虑CodeReview.SE来批评工作代码。理论上还有其他方面可以改进,但这个问题超出了这个范围。