Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # flip hess mei 2012 [email protected]
- # check string from remote script
- # Variables
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- URL="https://example.com/check-this-or-that.asp"
- RCPTBACKUPFAILED="[email protected]"
- DATE="$(date)"
- STATE_OK="0"
- STATE_WARNING="1"
- STATE_CRITICAL="2"
- STATE_UNKNOWN="3"
- STRING1="foo"
- STRING2="bar"
- STRING3="flippeh"
- # Functions:
- # mailer functie
- function fMailer()
- {
- cat <<EOF | mail -s "Waarschuwing: string check has failed" ${RCPTBACKUPFAILED}
- ########################################################################
- # #
- # BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK #
- # #
- ########################################################################
- ${DATE}
- The string checker @ nagios.example.com has received an error:
- Error was: ${ERROR}
- For questions, please contact:
- SysAdmins @ Example.com
- BOFHRoad 123
- yourtown
- admin@example.com
- EOF
- }
- # The main function.
- function fMain()
- {
- # Check whether sufficient arguments are given:
- [ ${#} = 0 ] || { fShowUsage ; exit "${STATE_UNKNOWN}"; }
- # this script depends on wget
- [ -x /usr/bin/curl ] || { echo "This scripts depends on curl, install it by using \"sudo apt-get install curl\"" ; exit "${STATE_UNKNOWN}"; }
- # get info
- { curl -s "${URL}" > /tmp/check-string ;} || { echo "Failed to curl ${URL}." ; exit "${STATE_UNKNOWN}"; }
- # get status
- OUTPUT="$( cat /tmp/check-string )" || { echo "Failed to get output!" ; exit "${STATE_UNKNOWN}"; }
- # make results
- # STRING1
- if ( echo "${OUTPUT}" | grep -qi "${STRING1}" ) ; then
- # status okay
- echo -e "OKAY - status is ${OUTPUT}"
- exit "${STATE_OK}"
- # STRING2
- elif ( echo "${OUTPUT}" | grep -qi "${STRING2}" ); then
- ERROR="CRITICAL - string detected: \"${STRING2}\" - status is ${OUTPUT}"
- fMailer
- echo "${ERROR}"
- exit "${STATE_CITICAL}"
- # STRING3
- elif ( echo "${OUTPUT}" | grep -qi "${STRING3}" ); then
- echo -e "CRITICAL - string detected: \"${STRING3}\" - status is ${OUTPUT}"
- fMailer
- echo "${ERROR}"
- exit "${STATE_CITICAL}"
- # status unknown
- else
- ERROR="UNKNOWN - unknown string returned: ${OUTPUT}"
- fMailer
- echo "${ERROR}"
- exit "${STATE_UNKNOWN}"
- fi
- }
- # Shows usage.
- function fShowUsage()
- {
- echo "Usage: ${SCRIPT_PATH}"
- return 0
- }
- # Start the program:
- fMain "${@}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment