Skip to content

Instantly share code, notes, and snippets.

@felixfyi
Forked from denniskong/check_memcache.rb
Created November 9, 2012 17:48
Show Gist options
  • Save felixfyi/4047107 to your computer and use it in GitHub Desktop.
Save felixfyi/4047107 to your computer and use it in GitHub Desktop.
Nagios plugin to check memcached written in ruby
#!/usr/bin/env ruby
# check_memcached.rb ko.de.r.de@googlemail.com 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 <ko.de.r.de@gmail.com>"
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 = "bytes: #{bytes.to_i/1024000} MB limit_maxbytes: #{limit_maxbytes.to_i/1024000} MB"
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