Skip to content
Snippets Groups Projects
Select Git revision
  • 4cf7dcdf0b62b114b589114ac4def259e5a85652
  • master default protected
  • simple-task/7248-eol-check-add-node-22
  • 6877_check_iml_deployment
4 results

check_reboot_required

Blame
  • check_http 6.25 KiB
    #!/bin/bash
    # ================================================================================
    #
    # CHECK HTTP
    #
    # Check http request to an url with given method.
    # The response header and 
    #
    # -------------------------------------------------------------------------------
    # 2023-09-06  v0.1  <axel.hahn@unibe.ch>
    # ================================================================================
    
    . $( dirname $0 )/inc_pluginfunctions
    
    export self_APPVERSION=1.0
    
    # ----------------------------------------------------------------------
    # FUNCTIONS
    # ----------------------------------------------------------------------
    
    # show help text
    function showHelp(){
        local _self; _self=$(basename $0)
    cat <<EOF
    $( ph.showImlHelpHeader )
    
    Makes an http request with a given method.
    Additionally you can verify the response by
    - http status code
    - content in http response header
    - content in http response body
    - content that is NOT in http response body
    
    SYNTAX:
      $_self [-h]
      $_self [-m METHOD] -u URL \\
          [-b REGEX] [-j FILTER] [-n REGEX] \\ 
          [-r REGEX] \\
          [-s STATUSCODE] \\
          [-l LABEL]
    
    OPTIONS:
    
      -h               this help
    
    PARAMETERS:
    
      Define request:
      -u URL           Set url to fetch; eg. https://www.example.com/
      -m METHOD        Set a method, eg. HEAD; default: GET
    
      What to check:
      -s STATUSCODE    exact Statuscode to check; 3 digits; by default critical
                       is a statuscode greater equal 400
      -r REGEX         Regex must match in http response header
      -j JQ-FILTER     for JSON Response: filter data by a jq
      -b REGEX         Regex must match in response body
      -n REGEX         Regex must NOT match in response body
    
      Output:
      -l LABEL         set a custom label; default: METHOD + URL eg.
                       "GET https://example.com/status"
    
    EXAMPLES:
    
      $_self -u https://www.example.com/
                       Check if GET request to url responds with 200..3xx status. 
    
      $_self -m HEAD -u https://www.example.com/
                       Check if HEAD request to url responds with 200..3xx status
    
      $_self -u [URL] -s 403
                       Check if the GET request to url has a wanted status code.
                       You can verify if a protected url is not accessible.
    
      $_self -u [URL] -b "contact"
                       Check if the GET request to url responds with 200..3xx 
                       status and the response body contains "contact".
    
      $_self -u [URL] -n "error occured"
                       Check if the GET request to url responds with 200..3xx 
                       status and the response body NOT contains "error occured".
    
    EOF
    }
    
    # ----------------------------------------------------------------------
    # MAIN
    # ----------------------------------------------------------------------
    
    ph.hasParamoption "h" "$@"; bOptHelp=$?
    
    if [ $bOptHelp -eq 0 -o $# -eq 0 ]; then
        showHelp
        exit 0
    fi
    
    ph.require "curl"
    
    sUrl=$(          ph.getValueWithParam ''    u "$@")
    sMethod=$(       ph.getValueWithParam 'GET' m "$@" | tr [:lower:] [:upper:])
    
    iStatus=$(       ph.getValueWithParam ''    s "$@")
    sHeader=$(       ph.getValueWithParam ''    r "$@")
    sBody=$(         ph.getValueWithParam ''    b "$@")
    sNotInBody=$(    ph.getValueWithParam ''    n "$@")
    sJq=$(           ph.getValueWithParam ''    j "$@")
    sLabel=$(        ph.getValueWithParam ""    l "$@")
    
    
    curlParams="-si -X $sMethod"
    sProblems=
    sOK=
    
    if [ -z "$sUrl" ]; then
        ph.setStatus unknown
        ph.status "Wrong parameters - no url was given."
        ph.exit
    fi
    
    out=$( curl $curlParams "$sUrl" )
    
    iHeaderEnd=$( echo "$out" | grep -n ^$'\r' | cut -f 1 -d ':' | head -1 )
    
    # echo "$out" | grep -n ^$'\r'; echo "cut header and body on line $iHeaderEnd"
    
    _header=$(echo "$out" | sed -n "1,${iHeaderEnd}p")
    _body=$(  echo "$out" | sed -n "${iHeaderEnd},\$p")
    
    if [ -n "$sJq" ]; then
        _body=$( jq "$sJq" <<< "$_body" 2>/dev/null )
    fi
    
    # echo "HEADER"; echo "$_header"
    # echo "BODY"; echo "$_body"
    
    
    # --- test status
    typeset -i iHttpStatus
    iHttpStatus=$( grep -i "^HTTP/[0-9\.]* " <<< "${_header}" | awk '{ print $2 }')
    
    if [ -n "$iStatus" ]; then
        # if ! grep -i "^HTTP/[0-9\.]* ${iStatus}" <<< "${_header}" >/dev/null; then
        if [ "$iHttpStatus" != "$iStatus" ]; then
            ph.setStatus critical
            sProblems+="- Http status is not [${iStatus}] but [${iHttpStatus}];\n"
        else
            sOK+="- Http status is [${iStatus}];\n"
        fi
    else
        if [ $iHttpStatus -ge 400 ]; then
            ph.setStatus critical
            sProblems+="- Http status is an http error [${iHttpStatus}];\n"
        elif [ $iHttpStatus -ge 300 ]; then
            sOK+="- Http status is a 3xx redirect [${iHttpStatus}];\n"
        else
            sOK+="- Http status is a 2xx OK [${iHttpStatus}];\n"
        fi
    fi
    
    # --- search in http response header
    if [ -n "$sHeader" ]; then
        if ! grep -iE "$sHeader" <<< "${_header}" >/dev/null; then
            ph.setStatus critical
            sProblems+="- Header does not contain [${sHeader}];\n"
        else
            sOK+="- [${sHeader}] was found in header;\n"
        fi    
    fi
    # --- search in http response header
    if [ -n "$sNotInHeader" ]; then
        if grep -iE "$sNotInHeader" <<< "${_header}" >/dev/null; then
            ph.setStatus critical
            sProblems+="- Header does contain unwanted [${sNotInHeader}];\n"
        else
            sOK+="- [${sNotInHeader}] was not found in header;\n"
        fi
    fi
    
    # --- search in http response body
    if [ -n "$sBody" ]; then
        if ! grep -iE "$sBody" <<< "${_body}" >/dev/null; then
            ph.setStatus critical
            sProblems+="- Body does not contain [${sBody}];\n"
        else
            sOK+="- [${sBody}] was found in body;\n"
        fi
        
    fi
    if [ -n "$sNotInBody" ]; then
        if grep -iE "$sNotInBody" <<< "${_body}" >/dev/null; then
            ph.setStatus critical
            sProblems+="- Body contains unwanted [${sNotInBody}];\n"
        else
            sOK+="- [${sNotInBody}] was not found in body;\n"
        fi
        
    fi
    
    # --- output
    test -n "$sProblems" && sProblems="Problems:\n$sProblems\n"
    test -n "$sOK"       && sOK="Found:\n$sOK"
    
    test -n "$sLabel" && ( 
        ph.status "$sLabel" 
        echo "$sMethod $sUrl ($iHttpStatus)"
    )
    test -n "$sLabel" || ph.status "$sMethod $sUrl ($iHttpStatus)"
    
    echo
    echo Command: curl $curlParams "$sUrl"
    echo
    echo -e "${sProblems}${sOK}"
    
    test -n "${sProblems}" && (echo "RESPONSE HEADER:"; echo; echo "$_header")
    
    ph.exit
    
    # ----------------------------------------------------------------------