trishoar

check_mem.sh

Feb 17th, 2015
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.64 KB | None | 0 0
  1. #!/bin/bash
  2. #TH v0.2 30/08/2013
  3. #Quick and Dirty Memory SNMP Poller
  4.  
  5. if [ $# -le 3 ] && [ $# -gt 5 ]; then
  6.  echo "Usage: $0 <hostname> <SNMP-comunity> <warning> <critical> <optional - Port>"
  7.  echo "Warning and Critical must be in percent"
  8.  echo "e.g 10.10.10.1 public 80 90"
  9.  exit 127
  10. fi
  11.  
  12. HOST=$1
  13. COMUNITY=$2
  14. WARN=$3
  15. CRITICAL=$4
  16.  
  17. if [ -z $5 ]; then
  18.  PORT=161
  19.  else
  20.   PORT=$5
  21. fi
  22.  
  23. #make sure warning is less than critical
  24. if [ "$WARN" -ge "$CRITICAL" ]; then
  25.   echo "WArning must be less than critical"
  26.   exit 127
  27. fi
  28.  
  29. DiskUse() {
  30. snmpdf -r 3 -t 1 -v 2c -c "$COMUNITY" "$HOST":$PORT 2> /dev/null |head -6|tail -5|awk '{print $3" "$4" "$5'}
  31. }
  32. DISKUSE=(`DiskUse`)
  33.  
  34. #check at this point that the probe worked and quit if it failed
  35. #this is kinda crap, but the alterntive is not using bash
  36. if [ "${#DISKUSE[@]}" -ne 15 ]; then
  37.  echo "SNMP problem. No data received from host."
  38.  exit 3
  39. fi
  40.  
  41. TOTAL=${DISKUSE[0]}
  42. USED=${DISKUSE[1]}
  43. BUFFERS=${DISKUSE[7]}
  44. CACHE=${DISKUSE[10]}
  45. REALUSED=$(($USED - $CACHE - $BUFFERS))
  46. #bash does not like floating point numbers
  47. printf -v PERCENTF "%.0f" `echo "scale=50; 100/$TOTAL*$REALUSED"|bc`
  48.  
  49. if [ "$PERCENTF" -ge "$WARN" ] && [ "$PERCENTF" -le "$CRITICAL" ]; then
  50.  echo "MEMORY WARNING - ${PERCENTF}% Used|Total ${TOTAL}KB, Buffers ${BUFFERS}KB, Cached ${CACHE}KB, Used ${REALUSED}KB"
  51.  exit 1
  52. elif [ "$PERCENTF" -ge "$CRITICAL" ]; then
  53.  echo "MEMORY CRITICAL - ${PERCENTF}% Used|Total ${TOTAL}KB, Buffers ${BUFFERS}KB, Cached ${CACHE}KB, Used ${REALUSED}KB"
  54.  exit 2
  55. else
  56.  echo "MEMORY OK - ${PERCENTF}% Used|Total ${TOTAL}KB, Buffers ${BUFFERS}KB, Cached ${CACHE}KB, Used ${REALUSED}KB"
  57.  exit 0
  58. fi
Advertisement
Add Comment
Please, Sign In to add comment