Skip to content

Instantly share code, notes, and snippets.

@beezly
Created October 23, 2011 19:08
Show Gist options
  • Save beezly/1307732 to your computer and use it in GitHub Desktop.
Save beezly/1307732 to your computer and use it in GitHub Desktop.
nagios plugin to read free disk space in ruby using snmp
#!/usr/bin/ruby
require 'rubygems'
require 'nagios-probe'
require 'snmp'
require 'optparse'
class NagiosDiskCheck < Nagios::Probe
attr_reader :perfdata
def initialize(opts = {})
@response = {}
@perfdata = ""
@warning_disks = {}
@critical_disks = {}
opts[:warn] = 15.0 unless opts[:warn]
opts[:crit] = 10.0 unless opts[:crit]
opts[:community] = "public" unless opts[:community]
raise 'Host not specified' unless opts[:host]
super(opts)
end
def run
mib = SNMP::MIB.new
mib.load_module "HOST-RESOURCES-TYPES"
type_fixeddisk = mib.oid "hrStorageFixedDisk"
SNMP::Manager.open(:Host => @opts[:host], :Community => @opts[:community], :MibModules => ['HOST-RESOURCES-MIB']) do |manager|
manager.walk ["HOST-RESOURCES-MIB::hrFSMountPoint",
"HOST-RESOURCES-MIB::hrFSType",
"HOST-RESOURCES-MIB::hrFSStorageIndex" ] do | mount_point, type, storage_index |
(unit, size, used, device_type) = \
manager.get_value ["HOST-RESOURCES-MIB::hrStorageAllocationUnits.#{storage_index.value}",
"HOST-RESOURCES-MIB::hrStorageSize.#{storage_index.value}",
"HOST-RESOURCES-MIB::hrStorageUsed.#{storage_index.value}",
"HOST-RESOURCES-MIB::hrStorageType.#{storage_index.value}"]
disk_used = used.to_i * unit.to_i
disk_size = size.to_i * unit.to_i
disk_avail = disk_size - disk_used
disk_pct_used = (disk_used.to_f/disk_size.to_f) * 100
disk_pct_free = 100 - disk_pct_used
# We only report disks that have size, don't start with "/dev" or "/sys" and are fixed (ie. local) disk
unless disk_size == 0 || mount_point.value.to_s =~ /\/(dev|sys)/ || device_type != type_fixeddisk
@response[mount_point.value] = {:size => disk_size, :used => disk_used, :available => disk_avail, :percent_used => disk_pct_used, :percent_free => disk_pct_free}
@perfdata+="#{mount_point.value} (#{disk_used}/#{disk_size}) "
end
end
end
super
end
def check_crit
@response.each do |disk_name, disk_data|
if disk_data[:percent_free] < @opts[:crit] then
@critical_disks[disk_name] = disk_data
end
end
@critical_disks.size == 0 ? false : true
end
def check_warn
@response.each do |disk_name, disk_data|
if disk_data[:percent_free] < @opts[:warn] then
@warning_disks[disk_name] = disk_data
end
end
@warning_disks.size == 0 ? false : true
end
def crit_message
"#{@critical_disks.keys.join ', '} below #{@opts[:crit]}% free"
end
def warn_message
"#{@warning_disks.keys.join ', '} below #{@opts[:warn]}% free"
end
def ok_message
"All disks above #{@opts[:warn]}% free"
end
end
options = {}
optparse = OptionParser.new do |opts|
opts.banner = "Usage: check_disk_snmp [options]"
opts.on("-H", "--host HOST", "Host") do |host|
options[:host] = host
end
opts.on("-W", "--warn WARN", Float, "Minimum Disk Warning Limit") do |warn|
options[:warn] = warn
end
opts.on("-C", "--crit CRIT", Float, "Minimum Disk Critical Limit") do |crit|
options[:crit] = crit
end
opts.on( '-h', '--help', 'Display this screen' ) do
puts opts
exit
end
end
optparse.parse!
raise OptionParser::MissingArgument if options[:host].nil?
begin
check = NagiosDiskCheck.new options
check.run
rescue Exception => e
puts "Unknown: " + e.message + e.backtrace
exit Nagios::UNKNOWN
end
puts "#{check.message} | #{check.perfdata}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment