Skip to content

Instantly share code, notes, and snippets.

@eric
Created April 8, 2010 02:19
Show Gist options
  • Save eric/359705 to your computer and use it in GitHub Desktop.
Save eric/359705 to your computer and use it in GitHub Desktop.
Gather more useful memory statistics for SNMP
#!/usr/bin/env ruby
#
# linux-meminfo
#
# by Eric Lindvall <[email protected]>
#
# Gather more useful memory statistics for SNMP
#
# To see the example output, run:
# linux-meminfo.rb -g .1.3.6.1.4.1.2021.4.6.0
#
# Add some debugging to help diagnose problems
if ARGV[0] == '-d'
debug = true
ARGV.shift
require 'syslog'
Syslog.open File.basename($0)
Syslog.warning "Running with arguments: #{ARGV.inspect}"
end
# The numeric version of "UCD-SNMP-MIB::memAvailReal"
# You can find this by running:
# snmptranslate -On UCD-SNMP-MIB::memAvailReal
OID_BASE = ".1.3.6.1.4.1.2021.4.6"
mem_info = {}
IO.foreach("/proc/meminfo") do |line|
if m = line.chomp.match(/^(\w+):\s+(\S+)/)
mem_info[m[1]] = m[2].to_i
end
end
# The real free memory not counting buffers and caches as used
mem_free = mem_info['MemFree'] + mem_info['Buffers'] + mem_info['Cached']
# We only want to return this info if we're doing an snmpwalk to get this
# or doing an snmpget directly to the OID
case ARGV
when ['-n', OID_BASE], ['-g', "#{OID_BASE}.0"]
puts "#{OID_BASE}.0"
puts "integer"
puts mem_free
Syslog.warning "mem_free: #{mem_free}" if debug
end
# Override UCD-SNMP-MIB::memAvailReal to give useful statistics by using
# the net-snmp "pass" directive.
#
# We want to change the priority to make sure this gets used.
pass -p 5 .1.3.6.1.4.1.2021.4.6 /usr/share/snmp/linux-meminfo.rb
# If you want to print debug info to syslog:
#pass -p 5 .1.3.6.1.4.1.2021.4.6 /usr/share/snmp/linux-meminfo.rb -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment