capslock avatar

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

capslock

Published: 04 Sept 2017 › Updated: 04 Sept 2017[Shell Script] This script connects to a site via curl and searches for a string in the web page.

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

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

alt

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  "
        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  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  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

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

Written by

Read more #dev posts


Best Posts From capslock

We have not curated any of capslock's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From capslock