flipje

nagios_check_string_remote_script

May 24th, 2012
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.73 KB | None | 0 0
  1. #!/bin/bash
  2. # flip hess mei 2012 [email protected]
  3. # check string from remote script
  4.  
  5. # Variables
  6.  
  7.   PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  8.   SCRIPT_PATH="${0}"
  9.  
  10.   URL="https://example.com/check-this-or-that.asp"
  11.   RCPTBACKUPFAILED="[email protected]"
  12.   DATE="$(date)"
  13.  
  14.   STATE_OK="0"
  15.   STATE_WARNING="1"
  16.   STATE_CRITICAL="2"
  17.   STATE_UNKNOWN="3"
  18.  
  19.   STRING1="foo"
  20.   STRING2="bar"
  21.   STRING3="flippeh"
  22.  
  23. # Functions:
  24.  
  25. # mailer functie
  26. function fMailer()
  27. {
  28.   cat <<EOF | mail -s "Waarschuwing: string check has failed" ${RCPTBACKUPFAILED}
  29.  
  30.     ########################################################################
  31.     #                                                                      #
  32.     #   BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK BARK   #
  33.     #                                                                      #
  34.     ########################################################################
  35.  
  36.      ${DATE}
  37.  
  38.      The string checker @ nagios.example.com has received an error:
  39.  
  40.      Error was: ${ERROR}
  41.  
  42.      For questions, please contact:
  43.  
  44.      SysAdmins @ Example.com
  45.      BOFHRoad 123
  46.      yourtown
  47.      admin@example.com
  48.  
  49. EOF
  50. }
  51.  
  52.  
  53. # The main function.
  54. function fMain()
  55. {
  56.   # Check whether sufficient arguments are given:
  57.   [ ${#} = 0 ] || { fShowUsage ; exit "${STATE_UNKNOWN}"; }
  58.  
  59.   # this script depends on wget
  60.   [  -x /usr/bin/curl ] || { echo "This scripts depends on curl, install it by using \"sudo apt-get install curl\"" ; exit "${STATE_UNKNOWN}"; }
  61.  
  62.   # get info
  63.   { curl -s "${URL}" > /tmp/check-string ;} || { echo "Failed to curl ${URL}." ; exit "${STATE_UNKNOWN}"; }
  64.  
  65.   # get status
  66.   OUTPUT="$( cat /tmp/check-string )" || { echo "Failed to get output!" ; exit "${STATE_UNKNOWN}"; }
  67.  
  68.   # make results
  69.  
  70.   # STRING1
  71.   if ( echo "${OUTPUT}" | grep -qi "${STRING1}" ) ; then
  72.      # status okay
  73.      echo -e "OKAY - status is ${OUTPUT}"
  74.      exit "${STATE_OK}"
  75.  
  76.   # STRING2
  77.   elif ( echo "${OUTPUT}" | grep -qi "${STRING2}" ); then
  78.      ERROR="CRITICAL - string detected: \"${STRING2}\" - status is ${OUTPUT}"
  79.      fMailer
  80.      echo "${ERROR}"
  81.      exit "${STATE_CITICAL}"
  82.  
  83.   # STRING3
  84.   elif ( echo "${OUTPUT}" | grep -qi "${STRING3}" ); then
  85.      echo -e "CRITICAL - string detected: \"${STRING3}\" - status is ${OUTPUT}"
  86.      fMailer
  87.      echo "${ERROR}"
  88.      exit "${STATE_CITICAL}"
  89.  
  90.   # status unknown
  91.   else
  92.      ERROR="UNKNOWN - unknown string returned: ${OUTPUT}"
  93.      fMailer
  94.      echo "${ERROR}"
  95.      exit "${STATE_UNKNOWN}"
  96.   fi
  97.  
  98. }
  99.  
  100. # Shows usage.
  101. function fShowUsage()
  102. {
  103.   echo "Usage: ${SCRIPT_PATH}"
  104.   return 0
  105. }
  106.  
  107.  
  108. # Start the program:
  109. fMain "${@}"
  110.  
  111. # Exit with previous return code:
  112. exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment