在将网站视为在线时创建具有双重逻辑的脚本的问题

Issue creating a script with dual logic in deeming a website online

我目前正在尝试创建一个脚本,允许我检查多个网站 url 以查看它们是否在线和活动。我的公司有多台服务器,不同的环境处于活动状态(生产、暂存、开发等)。我需要一个脚本来检查所有环境 URL,并告诉我它们是否每天早上都在线,所以我可以领先于解决任何服务器或网站出现故障的问题。

然而,我的问题是我不能完全根据 HTTP 代码的逻辑来判断网站是否在线,从 HTTP 的角度来看,我们的一些网站可能在线,但网站的组件或 Web 部件正在关闭,在页面上显示一条错误消息。

我无法想出一个脚本,它不仅可以检查 HTTP 状态,还可以扫描页面并解析出任何错误消息,然后根据这两种逻辑写入主机,无论站点是否存在是 "Online" 或 "Down"

这是我到目前为止的内容,您会注意到它不包括任何关于关键字解析的内容,因为我不知道如何实现...

#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 仅包含内部 URLs 我需要检查 FYI。

如有任何帮助,我们将不胜感激!谢谢

这里至少可以让您知道该怎么做。需要捕获网站数据以便对其进行解析。然后我们 运行 针对从字符串数组构建的内容进行正则表达式查询。这些字符串是可能在无法正常工作的页面上看到的文本。

# 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 重复的样子。您的日志文件 header 曾经是这样的几行。我把它减少到一个。

@"

---------------------------------------------------------------------------
$(Get-Date)
***Checking Links*** 

"@ | Out-File $log -Append

同时考虑 CodeReview.SE 来批评工作代码。理论上还有其他领域可以改进,但不在这个问题的讨论范围之内。