forked from mmangino/er-scout-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memcached_monitor.rb
68 lines (57 loc) · 1.69 KB
/
memcached_monitor.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'timeout'
class MissingLibrary < StandardError; end
class TestFailed < StandardError; end
class MemcachedMonitor < Scout::Plugin
attr_accessor :connection
def setup_memcache
begin
require 'memcache'
rescue LoadError
begin
require "rubygems"
require 'memcache'
rescue LoadError
raise MissingLibrary
end
end
self.connection=MemCache.new("#{option(:host)}:#{option(:port)}")
end
def build_report
begin
setup_memcache
test_setting_value
test_getting_value
report(option(:host)=>"OK")
rescue Timeout::Error => e
alert("Memcached failed to respond","Memcached on #{option(:host)} failed to respond within #{timeout_value} seconds")
rescue MemCache::MemCacheError => e
alert("Memcache connection failed","unable to connect to memcache on #{option(:host)}")
rescue TestFailed=>e
#do nothing, we already alerted, so no report
rescue MissingLibrary=>e
error("Could not load all required libraries",
"I failed to load the starling library. Please make sure it is installed.")
rescue Exception=>e
error("Got unexpected error: #{e} #{e.class}")
end
end
def test_setting_value
@test_value=rand.to_s
timeout(timeout_value) do
connection.set(option(:key),@test_value)
end
end
def timeout_value
(option(:timeout)||1).to_f
end
def test_getting_value
value=""
timeout(timeout_value) do
value=connection.get(option(:key))
end
if value != @test_value
alert("Unable to retrieve key from #{option(:host)}","Expected #{@test_value} but got #{value}")
raise TestFailed
end
end
end