Skip to content

Instantly share code, notes, and snippets.

@jaisingh
Created May 3, 2011 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaisingh/954340 to your computer and use it in GitHub Desktop.
Save jaisingh/954340 to your computer and use it in GitHub Desktop.
nagios check class
class NagiosCheck
OK,WARN,CRIT,UNKNOWN = 0,1,2,3
OK_STR = "OK"
WARN_STR = "WARNING"
CRIT_STR = "CRITICAL"
UNKNOWN_STR = "UNKNOWN"
attr_reader :exit_code
attr_writer :exit_str
attr_accessor :perfdata, :outputdata
def initialize
@exit_code = OK
@exit_str = ""
@perfdata = ""
@outputdata = ""
end
def exit_str
str = case @exit_code
when OK then OK_STR
when WARN then WARN_STR
when CRIT then CRIT_STR
else UNKNOWN_STR
end
str += " - " + @outputdata if @outputdata != ""
str += " | " + @perfdata if @perfdata != ""
return str
end
# set the exit code based on the warn_range and crit_range
def set_exit_code ( value, warn_range, crit_range )
result = OK
result = WARN if range_match(value.to_i, warn_range)
result = CRIT if range_match(value.to_i, crit_range)
self.exit_code = result
return result
end
# only sets the exit code if it is worse than the current exit code
def exit_code=(exit_code)
if exit_code > @exit_code
if exit_code == UNKNOWN && @exit_code != OK
# WARN and CRIT are higher priority than UNKNOWN
return
end
@exit_code = exit_code
end
end
protected
# returns true if value is within range
#10 < 0 or > 10, (outside the range of {0 .. 10})
#10: < 10, (outside {10 .. ∞})
#~:10 > 10, (outside the range of {-∞ .. 10})
# 10:20 < 10 or > 20, (outside the range of {10 .. 20})
# @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20})
def range_match ( value, range )
return (value < 0 || value > range) if range.class != String
result = false
negate = (range =~ /^@(.*)/)
range = $1 if negate
if range =~ /^(\d+)$/
result = (value < 0 || value > $1.to_i)
# untested from here to the end of the function
elsif range =~ /^(\d+):[~]?$/
result = (value < $1.to_i)
elsif range =~ /^~:(\d+)$/
result = (value > $1.to_i)
elsif range =~ /^(\d+):(\d+)$/
result = (value > $1.to_i && value < $2.to_i)
end
return negate ? !result : result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment