memcached_graphite 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +1 -0
- data/bin/memcached_graphite +90 -0
- data/lib/hash_ext.rb +15 -0
- data/lib/hostname.rb +18 -0
- data/lib/memcached_graphite/version.rb +3 -0
- data/lib/memcached_graphite.rb +7 -0
- data/memcached_graphite.gemspec +26 -0
- metadata +101 -0
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,90 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'pp'
|
4
|
+
require 'logger'
|
5
|
+
require 'optparse'
|
6
|
+
require 'rubygems'
|
7
|
+
require 'dalli'
|
8
|
+
require 'graphite'
|
9
|
+
require 'memcached_graphite'
|
10
|
+
|
11
|
+
#def normalize_key (str)
|
12
|
+
# str.gsub(/\W+/,'_')
|
13
|
+
#end
|
14
|
+
|
15
|
+
def numeric?(object)
|
16
|
+
true if Float(object) rescue false
|
17
|
+
end
|
18
|
+
|
19
|
+
def format_hostname ( hostname, options )
|
20
|
+
hn = Hostname.new(hostname, :base_hostname => options[:base_hostname])
|
21
|
+
|
22
|
+
options[:reverse_hostname] ? hn.reverse : hn.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
#format the stats in a way that graphite expects
|
26
|
+
def format_stats ( allstats, options )
|
27
|
+
carbon_prefix = options[:carbon_prefix]
|
28
|
+
stats = {}
|
29
|
+
allstats.each do |server,server_stats|
|
30
|
+
server_split = server.split(':')
|
31
|
+
server_piece = sprintf("%s.port_%s",
|
32
|
+
format_hostname(server_split[0], options),
|
33
|
+
server_split[1] || 11211 )
|
34
|
+
|
35
|
+
prefix = [carbon_prefix, server_piece].join('.')
|
36
|
+
stats.merge! server_stats.prefix_keys("#{prefix}.")
|
37
|
+
end
|
38
|
+
|
39
|
+
# only return numeric values
|
40
|
+
# & convert to floats
|
41
|
+
Hash[stats.map { |k,v| [k, v.to_f] if numeric? v }]
|
42
|
+
end
|
43
|
+
|
44
|
+
logger = Logger.new($stdout)
|
45
|
+
logger.level = Logger::INFO
|
46
|
+
|
47
|
+
options = { :memcached_servers => 'localhost:11211',
|
48
|
+
:carbon_server => 'localhost:2003',
|
49
|
+
:carbon_prefix => 'memcached',
|
50
|
+
:base_hostname => nil,
|
51
|
+
:reverse_hostname => false }
|
52
|
+
|
53
|
+
opts = OptionParser.new
|
54
|
+
opts.banner = "Usage #{$0} -m memcached:port -g graphite:port [OPTIONS]"
|
55
|
+
opts.on("-m", "--memcached HOSTS", Array,
|
56
|
+
"Comma separated list of memcached servers (server:port,server:port)" ) do |v|
|
57
|
+
options[:memcached_servers] = v
|
58
|
+
end
|
59
|
+
opts.on("-g", "--graphite HOST", String,
|
60
|
+
"Graphite/Carbon host:port") do |v|
|
61
|
+
options[:carbon_server] = v
|
62
|
+
end
|
63
|
+
opts.on("-p", "--prefix PREFIX", String,
|
64
|
+
"Graphite tree prefix (default: #{options[:carbon_prefix]})") do |v|
|
65
|
+
options[:carbon_prefix] = v
|
66
|
+
end
|
67
|
+
opts.on("-b", "--base REGEX", String,
|
68
|
+
"Base hostname regular expression.",
|
69
|
+
"Will be stripped off from the hostname before sending to Graphite.") do |v|
|
70
|
+
options[:base_hostname] = v
|
71
|
+
end
|
72
|
+
opts.on("-r", "--reverse",
|
73
|
+
"Reverse hostname (foo.bar.com becomes com.bar.foo)") do |v|
|
74
|
+
options[:reverse_hostname] = true
|
75
|
+
end
|
76
|
+
opts.on("-d", "--debug", "Turn on debug logging" ) do |v|
|
77
|
+
logger.level = Logger::DEBUG
|
78
|
+
end
|
79
|
+
opts.on("-h", "--help", "help" ) do |v|
|
80
|
+
puts opts
|
81
|
+
exit 1
|
82
|
+
end
|
83
|
+
opts.parse!
|
84
|
+
|
85
|
+
Dalli.logger = logger
|
86
|
+
dc = Dalli::Client.new options[:memcached_servers]
|
87
|
+
g = Graphite::Logger.new(options[:carbon_server], logger)
|
88
|
+
|
89
|
+
stats = format_stats(dc.stats, options)
|
90
|
+
g.log(Time.now.to_i, stats)
|
data/lib/hash_ext.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
class Hash
|
2
|
+
def prefix_keys ( prefix )
|
3
|
+
if block_given?
|
4
|
+
Hash[self.map do |k,v|
|
5
|
+
yield(k,v) ? ["#{prefix}#{k}", v] : [k,v]
|
6
|
+
end]
|
7
|
+
else
|
8
|
+
Hash[self.map { |k,v| [ "#{prefix}#{k}", v] }]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def numeric_values
|
13
|
+
Hash[self.select { |k,v| v.is_a? Numeric }]
|
14
|
+
end
|
15
|
+
end
|
data/lib/hostname.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
class Hostname
|
2
|
+
attr_reader :hostname
|
3
|
+
|
4
|
+
def initialize ( hostname, options = {} )
|
5
|
+
@hostname = hostname
|
6
|
+
if options[:base_hostname]
|
7
|
+
@hostname.gsub!(options[:base_hostname], '')
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def reverse
|
12
|
+
@hostname.split('.').reverse.join('.')
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_s
|
16
|
+
@hostname
|
17
|
+
end
|
18
|
+
end # class Hostname
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "memcached_graphite/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "memcached_graphite"
|
7
|
+
s.version = MemcachedGraphite::VERSION
|
8
|
+
s.authors = ["Aaron Brown"]
|
9
|
+
s.email = ["abrown@ideeli.com"]
|
10
|
+
s.homepage = "https://github.com/ideeli/memcached_graphite"
|
11
|
+
s.summary = %q{Send memcached statistics to graphite/carbon}
|
12
|
+
s.description = %q{Send memcached statistics to graphite/carbon}
|
13
|
+
|
14
|
+
s.rubyforge_project = "memcached_graphite"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
# s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "dalli"
|
24
|
+
s.add_runtime_dependency "graphite"
|
25
|
+
s.rubygems_version = '1.3.5'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: memcached_graphite
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Aaron Brown
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-10-31 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: dalli
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: graphite
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Send memcached statistics to graphite/carbon
|
49
|
+
email:
|
50
|
+
- abrown@ideeli.com
|
51
|
+
executables:
|
52
|
+
- memcached_graphite
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- Rakefile
|
61
|
+
- bin/memcached_graphite
|
62
|
+
- lib/hash_ext.rb
|
63
|
+
- lib/hostname.rb
|
64
|
+
- lib/memcached_graphite.rb
|
65
|
+
- lib/memcached_graphite/version.rb
|
66
|
+
- memcached_graphite.gemspec
|
67
|
+
homepage: https://github.com/ideeli/memcached_graphite
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
none: false
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
hash: 3
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
hash: 3
|
90
|
+
segments:
|
91
|
+
- 0
|
92
|
+
version: "0"
|
93
|
+
requirements: []
|
94
|
+
|
95
|
+
rubyforge_project: memcached_graphite
|
96
|
+
rubygems_version: 1.8.10
|
97
|
+
signing_key:
|
98
|
+
specification_version: 3
|
99
|
+
summary: Send memcached statistics to graphite/carbon
|
100
|
+
test_files: []
|
101
|
+
|