check_mem is a simple Nagios plugin to check memory utilisation on Linux servers. I have written both a bash and a Ruby version of this script.
bash version:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
#!/bin/bash AWK="/usr/bin/awk" BASENAME="/bin/basename" ECHO="/bin/echo" EGREP="/bin/egrep" FREE="/usr/bin/free" GREP="/bin/grep" THISPROG=$( ${BASENAME} $0 ) VERBOSE=0 E_OK=0 E_WARNING=1 E_CRITICAL=2 E_UNKNOWN=3 function print_error { ${ECHO} "Error: $@" >&2 } function printv { (( VERBOSE )) && { ${ECHO} "--> $@" } } function print_usage { { ${ECHO} "Usage: ${THISPROG} [-hv] -w <warning_mb> -c <critical_mb>" ${ECHO} " -c Critical memory (MB)" ${ECHO} " -w Warning memory (MB)" ${ECHO} " -h Display this usage message" ${ECHO} " -v Verbose mode" } >&2 } function check_arguments { ERROR_COUNT=0 if [ -z "${WARNING}" ]; then print_error "-w option mandatory" (( ERROR_COUNT = ERROR_COUNT + 1 )) else ${ECHO} "${WARNING}" | ${EGREP} -qs '^[0-9]+$' if [ "$?" -ne "0" ]; then print_error "-w must be an integer" (( ERROR_COUNT = ERROR_COUNT + 1 )) fi fi if [ -z "${CRITICAL}" ]; then print_error "-c option mandatory" (( ERROR_COUNT = ERROR_COUNT + 1 )) else ${ECHO} "${CRITICAL}" | ${EGREP} -qs '^[0-9]+$' if [ "$?" -ne "0" ]; then print_error "-c must be an integer" (( ERROR_COUNT = ERROR_COUNT + 1 )) fi fi if [ "${ERROR_COUNT}" -gt "0" ]; then exit ${E_UNKNOWN} else if [ "${WARNING}" -le "${CRITICAL}" ]; then print_error "-w must be > -c" exit ${E_UNKNOWN} fi fi } function check_memory { FREE_MEMORY=$( ${FREE} -m | ${GREP} 'buffers/cache' | ${AWK} '{print $4}' ) if [ "${FREE_MEMORY}" -lt "${CRITICAL}" ]; then ${ECHO} "CRITICAL: Free memory ${FREE_MEMORY}MB" exit ${E_CRITICAL} elif [ "${FREE_MEMORY}" -lt "${WARNING}" ]; then ${ECHO} "WARNING: Free memory ${FREE_MEMORY}MB" exit ${E_WARNING} else ${ECHO} "OK: Free memory ${FREE_MEMORY}MB" exit ${E_OK} fi } # # main() # while getopts ":c:hvw:" OPTION; do case ${OPTION} in "c") CRITICAL="${OPTARG}" ;; "h") print_usage && exit 0 ;; "v") VERBOSE=1 ;; "w") WARNING="${OPTARG}" ;; * ) print_usage && exit 1 ;; esac done shift $(( ${OPTIND} - 1 )) if [ "$#" -ne "0" ]; then print_usage && exit 1 fi check_arguments check_memory exit 0 |
Ruby version:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#!/usr/bin/ruby require 'optparse' this_script = "check_mem" FREE="/usr/bin/free -m" E_OK = 0 E_WARNING = 1 E_CRITICAL = 2 E_UNKNOWN = 3 options = {} options[:verbose] = false o = OptionParser.new do |opts| opts.banner = "Usage: #{this_script} -w <warn> -c <crit>" opts.on("-w", "--warning WARNING", "Warning threshold") { |warning| options[:warning] = warning } opts.on("-c", "--critical CRITICAL", "Critical threshold") { |critical| options[:critical] = critical } opts.on("-h", "Print help" ) { puts opts; exit(0) } opts.on("-v", "--verbose", "Be verbose") do options[:verbose] = true end end begin o.parse! ARGV rescue OptionParser::InvalidOption => e puts e puts o exit(E_UNKNOWN) end def print_error(msg) $stderr.puts "#{msg}" end def check_arguments(options) error_count = 0 if options[:warning] == nil print_error "-w option mandatory" error_count += 1 end if options[:critical] == nil print_error "-c option mandatory" error_count += 1 end if options[:warning].to_i == 0 print_error "-w must be an integer > 0" error_count += 1 end if options[:critical].to_i == 0 print_error "-c must be an integer > 0" error_count += 1 end if options[:warning].to_i <= options[:critical].to_i print_error "-w must be > -c" error_count += 1 end if error_count > 0 exit(E_UNKNOWN) end end def check_memory(options) memory_output = %x[ #{FREE} ] free_memory = memory_output.grep(/buffers\/cache/).to_s.split[3] if free_memory.to_i < options[:critical].to_i puts "CRITICAL: Free memory #{free_memory}MB" exit(E_CRITICAL) elsif free_memory.to_i < options[:warning].to_i puts "WARNING: Free memory #{free_memory}MB" exit(E_WARNING) else puts "OK: Free memory #{free_memory}MB" exit(E_OK) end end check_arguments(options) check_memory(options) |