[Shell Script] This script connects to a site via curl and searches for a string in the web page.

in dev •  7 years ago  (edited)

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'.

Save plugin in $USER1$ dir, in my case (Ubuntu 12.04) it is:
/usr/lib/nagios/plugins
and make it executable.

Command definition:

/etc/nagios3/commands.cfg:
...

Check HTTP

define command{
command_name check_http_curl
command_line $USER1$/check_http_curl.sh $HOSTNAME$ $HOSTNOTES$
}

#!/bin/bash
#

# 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>"
        echo ""
        exit 3
}

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

# 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

# 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 <$2> 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 <$2> 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
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

좋은정보 감사합니다. curl을 이용해서 이렇게 체크도 할 수 있군요...

우얼... 좋은 팁 감사합니다. 파워쉘용도 만들어주세요 ㅋ

넵 유용하게 쓰세요~ 댓글 감사합니다.

Cheer Up!

  • from Clean STEEM activity supporter