Skip to content

Instantly share code, notes, and snippets.

@denniskong
Created November 1, 2009 12:32
Show Gist options
  • Save denniskong/223513 to your computer and use it in GitHub Desktop.
Save denniskong/223513 to your computer and use it in GitHub Desktop.
Nagios plugin to check memcached written in ruby
#!/usr/bin/env ruby
# check_memcached.rb [email protected] 20091101 v0.1
# Copyright 2009, Dennis Kong
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require "rubygems"
require "memcache"
ver = 0.1
if ARGV[3].nil?
puts "#{$0} v#{ver} by Dennis Kong <[email protected]>"
puts "Nagios plugin to check memcache and return status\r\n\r\n"
puts "Usage: #{$0} <server> <port> <warning> <critical>"
puts "e.g. #{$0} ./ localhost 11211 70 85"
exit 3
end
warn = ARGV[2].to_i
crit = ARGV[3].to_i
unless (warn < crit)
puts "<warning> must be less than <critical>"
exit 3
end
server = "#{ARGV[0]}:#{ARGV[1]}"
stats = MemCache.new(server).stats[server]
limit_maxbytes = stats["limit_maxbytes"].to_i
bytes = stats["bytes"].to_f
used = ((bytes / limit_maxbytes) * 100)
if (used > crit)
retval = 2
elsif (used > warn)
retval= 1
else
retval = 0
end
return_str = ""
stats.each do |k,v|
return_str += "#{k}: #{v} "
end
stat_string = "usage: #{used.round}% #{return_str}"
if ( retval == 0 )
puts "OK - #{stat_string} \n"
exit retval
elsif ( retval == 1 )
print "WARNING - #{stat_string} \n"
exit retval
elsif ( retval == 2 )
print "CRITICAL - #{stat_string} \n"
exit retval
end
exit retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment