Advertisement
d1cor

check_mem.sh (nagios3 plugin)

Sep 5th, 2013
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.42 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # Script to check memory usage on Linux. Ignores memory used by disk cache.
  4. #
  5. # Requires the bc command
  6. #
  7. print_help() {
  8.     echo "Usage:"
  9.     echo "[-w] Warning level as a percentage"
  10.     echo "[-c] Critical level as a percentage"
  11.     exit 0
  12. }
  13.  
  14. while test -n "$1"; do
  15.     case "$1" in
  16.         --help|-h)
  17.             print_help
  18.             exit 0
  19.             ;;
  20.         -w)
  21.             warn_level=$2
  22.             shift
  23.             ;;
  24.         -c)
  25.             critical_level=$2
  26.             shift
  27.             ;;
  28.         *)
  29.             echo "Unknown Argument: $1"
  30.             print_help
  31.             exit 3
  32.             ;;
  33.     esac
  34.     shift
  35. done
  36.  
  37. if [ "$warn_level" == "" ]; then
  38.     echo "No Warning Level Specified"
  39.     print_help
  40.     exit 3;
  41. fi
  42.  
  43. if [ "$critical_level" == "" ]; then
  44.     echo "No Critical Level Specified"
  45.     print_help
  46.     exit 3;
  47. fi
  48.  
  49. free=`free -m | grep "buffers/cache" | awk '{print $4}'`
  50. used=` free -m | grep "buffers/cache" | awk '{print $3}'`
  51.  
  52. total=$(($free+$used))
  53.  
  54. result=$(echo "$used / $total * 100" |bc -l|cut -c -2)
  55.  
  56. if [ "$result" -lt "$warn_level" ]; then
  57.     echo "Memory OK. $result% used."
  58.     exit 0;
  59. elif [ "$result" -ge "$warn_level" ] && [ "$result" -le "$critical_level" ]; then
  60.     echo "Memory WARNING. $result% used."
  61.     exit 1;
  62. elif [ "$result" -gt "$critical_level" ]; then
  63.     echo "Memory CRITICAL. $result% used."
  64.     exit 2;
  65. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement