Skip to content

Instantly share code, notes, and snippets.

@furu-nob
Created August 1, 2016 02:45
Show Gist options
  • Select an option

  • Save furu-nob/dfd8b07f6dc9d54aa82b8d72195aada3 to your computer and use it in GitHub Desktop.

Select an option

Save furu-nob/dfd8b07f6dc9d54aa82b8d72195aada3 to your computer and use it in GitHub Desktop.

Revisions

  1. furu-nob created this gist Aug 1, 2016.
    60 changes: 60 additions & 0 deletions check_http_curl_w_digest_auth
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/bin/bash
    #
    # Origin https://exchange.nagios.org/directory/Plugins/Network-Protocols/HTTP/check_http_curl-2Esh/details
    #
    # This script connects to a site via curl and searches for a string in the web page.
    # If the string is not found it throws out an error and Nagios exit is set to 'critical'.
    ##
    # Ver. 0.1a
    # Last modified by Roberto Carraro on 20150528
    #
    # Ver. 0.11
    # modified to digest authentication by Nobuyuki Furuyama on 2016/08/01

    # Declare constants
    # The following ones will be used as exit codes by Nagios
    readonly EXIT_OK=0
    # Warning is not used in this script so far
    readonly EXIT_WARNING=1
    readonly EXIT_CRITICAL=2
    readonly EXIT_UNKNOWN=3

    print_usage() {
    echo ""
    echo "This plugin checks the content of a web page."
    echo "If a certaing text string is not found Nagios exit is set to 'critical'."
    echo ""
    echo "Usage: $0 <URL> <text string to search> <authuser:password>"
    echo ""
    exit 3
    }

    if [ $# -lt 3 ] ; then
    print_usage
    fi

    URL=$1
    TEXT_STRING=$2
    DIGEST_AUTH_PAIR=$3

    # Connect to URL and search for text string (don't output anything)
    # We are only interested in the exit state of the grep command, not the output of curl
    #curl $1 | grep -i $2 > /dev/null
    curl --digest -u ${DIGEST_AUTH_PAIR} ${URL}|grep "${TEXT_STRING}"

    # The exit state of the last row is evaluated '0' if True or '1' if False and is intercepted by '$?'
    if [[ $? -eq 1 ]] ; then
    # No string found or site down or site non reachable
    echo "There is a problem with the site or string <${TEXT_STRING}> not found."
    # Throws out the exit code which will be handled by Nagios as Critical
    exit $EXIT_CRITICAL
    else
    # String found and site up and reachable
    echo "Site up and string <${TEXT_STRING}> found."
    # Throws out the exit code which will be handled by Nagios as Ok
    exit $EXIT_OK
    fi

    # Something's wrong; catch-all
    echo "UNKNOWN"
    exit $EXIT_UNKNOWN