Метка: PowerShell

  • PowerShell контроль доступности сайта с перезапуском демона

    однажды с бэк-эндом одного далёкого сервера случался кабздец, и он перестал отзываться, при этом статику сервер отдавал, а то, что обрабывает JAVA код, то видимо ломалось.
    как временная мера такому безобразию был накидан примерно вот такой скрипт.
    коль баш мне роднее, то пришлось немного погуглить, и нагуглилась вот эта страничка с функцией проверки сайта

    Using PowerShell to Query Web Site Information


    на всякий случай привожу код ниже полностью.

     function Get-WebSite {
    <#  
    .SYNOPSIS  
        Retrieves information about a website.
    .DESCRIPTION
        Retrieves information about a website.
    .PARAMETER Url
        URL of the website to test access to.
    .PARAMETER UseDefaultCredentials
        Use the currently authenticated user's credentials  
    .PARAMETER Proxy
        Used to connect via a proxy
    .PARAMETER TimeOut
        Timeout to connect to site, in milliseconds
    .PARAMETER Credential
        Provide alternate credentials              
    .NOTES  
        Name: Get-WebSite
        Author: Boe Prox
        DateCreated: 08Feb2011        
    .EXAMPLE  
        Get-WebSite -url "http://www.bing.com"
         
    Description
    ------------
    Returns information about Bing.Com to include StatusCode and type of web server being used to host the site.
     
    #>
    [cmdletbinding(
        DefaultParameterSetName = 'url',
        ConfirmImpact = 'low'
    )]
        Param(
            [Parameter(
                Mandatory = $True,
                Position = 0,
                ParameterSetName = '',
                ValueFromPipeline = $True)]
                [string][ValidatePattern("^(http|https)\://*")]$Url,
            [Parameter(
                Position = 1,
                Mandatory = $False,
                ParameterSetName = 'defaultcred')]
                [switch]$UseDefaultCredentials,
            [Parameter(
                Mandatory = $False,
                ParameterSetName = '')]
                [string]$Proxy,
            [Parameter(
                Mandatory = $False,
                ParameterSetName = '')]
                [Int]$Timeout,
            [Parameter(
                Mandatory = $False,
                ParameterSetName = 'altcred')]
                [switch]$Credential            
                             
            )
    Begin {     
        $psBoundParameters.GetEnumerator() | % { 
            Write-Verbose "Parameter: $_" 
            }
        
        #Create the initial WebRequest object using the given url
        Write-Verbose "Creating the web request object"        
        $webRequest = [net.WebRequest]::Create($url)
         
        #Use Proxy address if specified
        If ($PSBoundParameters.ContainsKey('Proxy')) {
            #Create Proxy Address for Web Request
            Write-Verbose "Creating proxy address and adding into Web Request"
            $webRequest.Proxy = New-Object -TypeName Net.WebProxy($proxy,$True)
            }
             
        #Set timeout
        If ($PSBoundParameters.ContainsKey('TimeOut')) {
            #Setting the timeout on web request
            Write-Verbose "Setting the timeout on web request"
            $webRequest.Timeout = $timeout
            }        
         
        #Determine if using Default Credentials
        If ($PSBoundParameters.ContainsKey('UseDefaultCredentials')) {
            #Set to True, otherwise remains False
            Write-Verbose "Using Default Credentials"
            $webrequest.UseDefaultCredentials = $True
            }
        #Determine if using Alternate Credentials
        If ($PSBoundParameters.ContainsKey('Credentials')) {
            #Prompt for alternate credentals
            Write-Verbose "Prompt for alternate credentials"
            $wc.Credential = (Get-Credential).GetNetworkCredential()
            }            
             
        #Set TimeStamp prior to attempting connection    
        $then = get-date
        }
    Process {    
        Try {
            
            #Make connection to gather response from site
            $response = $webRequest.GetResponse()
            #If successful, get the date for comparison
            $now = get-date         
                     
            #Generate report
            Write-Verbose "Generating report from website connection and response"  
            $report = @{
                URL = $url
                StatusCode = $response.Statuscode -as [int]
                StatusDescription = $response.StatusDescription
                ResponseTime = "$(($now - $then).totalseconds)"
                WebServer = $response.Server
                Size = $response.contentlength
                Date = $now
                } 
                
                $report | Out-File -FilePath c:\Tools\checkServer_log.txt -Append                    
            }
        Catch {
            #Get timestamp of failed attempt
            $now = get-date
            #Put the current error into a variable for later use
            $errorstring = "$($error[0])"
            
            Restart-Service ApacheTomCat9
            
            #Generate report
            $report = @{
                URL = $url
                StatusCode = ([regex]::Match($errorstring,"\b\d{3}\b")).value
                StatusDescription = (($errorstring.split('\)')[2]).split('.\')[0]).Trim()
                ResponseTime = "$(($now - $then).totalseconds)"
                WebServer = $response.Server
                Size = $response.contentlength
                Date = $now            
                } 
            $report | Out-File -FilePath c:\Tools\checkServer_error_log.txt -Append;  
    
            }                        
        }
    End {        
        #Display Report    
        New-Object PSObject -property $report 
        }    
    }
    
     for(;;) {
     try {
        # invoke the worker script
        Get-WebSite -url "https://site.ru/Form.php" -TimeOut "15000" -Verbose
     }
     catch {
      # do something with $_, log it, more likely
     }
    
     # wait for a minute
     Start-Sleep 60
    }
    

    чуть выше в коде есть комментарий про соль нашу, это как раз если что-то пошло не так, то рестарт демона
    дальше берём вызов этой функции в бесконечный цикл, с паузой 60 секунд, и проверяем…

    for(;;) {
    try {
    # invoke the worker script
    Get-WebSite -url "https://site.ru/Form.php" -TimeOut "15000" -Verbose
    }
    catch {
    # do something with $_, log it, more likely
    }
    
    # wait for a minute
    Start-Sleep 60
    }
    

    это просто черновик чтоб не забыть. дальше возможно дополню его