check_memory.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #!/usr/bin/env bash
  2. # Origninal from http://blog.vergiss-blackjack.de/wp-uploads/2010/04/check_memory.txt
  3. # Modified by Jon Schipp
  4. # Nagios plugin to check memory consumption
  5. # Excludes Swap and Caches so only the real memory consumption is considered
  6. # Nagios Exit Codes
  7. OK=0
  8. WARNING=1
  9. CRITICAL=2
  10. UNKNOWN=3
  11. # set default values for the thresholds
  12. WARN=90
  13. CRIT=95
  14. usage()
  15. {
  16. cat <<EOF
  17. Check memory consumption excluding swap and cache to determine real usage.
  18. Options:
  19. -c Critical threshold as percentage (0-100) (def: 95)
  20. -w Warning threshold as percentage (0-100) (def: 90)
  21. Usage: $0 -w 90 -c 95
  22. EOF
  23. }
  24. while getopts "c:w:h" ARG;
  25. do
  26. case $ARG in
  27. w) WARN=$OPTARG
  28. ;;
  29. c) CRIT=$OPTARG
  30. ;;
  31. h) usage
  32. exit
  33. ;;
  34. esac
  35. done
  36. MEM_TOTAL=`free | fgrep "Mem:" | awk '{ print $2 }'`;
  37. MEM_USED=`free | fgrep "/+ buffers/cache" | awk '{ print $3 }'`;
  38. PERCENTAGE=$(($MEM_USED*100/$MEM_TOTAL))
  39. RESULT=$(echo "${PERCENTAGE}% ($((($MEM_USED)/1024)) of $((MEM_TOTAL/1024)) MB) used")
  40. if [ $PERCENTAGE -gt $CRIT ]; then
  41. echo "CRITICAL: $RESULT"
  42. exit $CRITICAL;
  43. elif [ $PERCENTAGE -gt $WARN ]; then
  44. echo "WARNING: $RESULT"
  45. exit $WARNING;
  46. else
  47. echo "OK: $RESULT"
  48. exit $OK;
  49. fi