check_proc_pmem 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env bash
  2. # Nagios plugin to check process group memory consumption
  3. # Origin:
  4. # @see http://unix.stackexchange.com/questions/233944/how-to-view-summaric-memory-usage-of-groups-of-commands-instead-of-processes
  5. # @see https://github.com/jonschipp/nagios-plugins/blob/master/check_memory.sh
  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=80
  13. CRIT=90
  14. usage()
  15. {
  16. cat <<EOF
  17. Check memory consumption by process group.
  18. Options:
  19. -c Critical threshold as percentage (0-100) (def: 90)
  20. -w Warning threshold as percentage (0-100) (def: 80)
  21. Usage: $0 -p procname -w 90 -c 95
  22. Example: $0 -p httpd -w 30 -c 40
  23. EOF
  24. }
  25. while getopts "p:c:w:h" ARG;
  26. do
  27. case $ARG in
  28. p) PROCNAME=$OPTARG
  29. ;;
  30. w) WARN=$OPTARG
  31. ;;
  32. c) CRIT=$OPTARG
  33. ;;
  34. h) usage
  35. exit
  36. ;;
  37. esac
  38. done
  39. if [ -z $PROCNAME ]; then
  40. usage
  41. exit
  42. fi
  43. PERCENTAGE=`ps -C $PROCNAME --no-headers -o pmem | xargs | sed -e 's/ /+/g' | bc`
  44. INTPERCENTAGE=${PERCENTAGE/.*/}
  45. RESULT="$PROCNAME uses ${PERCENTAGE}% of memory"
  46. if [ $INTPERCENTAGE -ge $CRIT ]; then
  47. echo "CRITICAL: $RESULT"
  48. exit $CRITICAL;
  49. elif [ $INTPERCENTAGE -ge $WARN ]; then
  50. echo "WARNING: $RESULT"
  51. exit $WARNING;
  52. else
  53. echo "OK: $RESULT"
  54. exit $OK;
  55. fi