-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmunin-graphite.rb
92 lines (81 loc) · 1.9 KB
/
munin-graphite.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env ruby
#
# Written by Adam Jacob <[email protected]>
#
# The latest version is available at:
#
# http://github.com/adamhjk/munin-graphite/tree/master/munin-graphite.rb
require 'socket'
class Munin
def initialize(host='localhost', port=4949)
@munin = TCPSocket.new(host, port)
@munin.gets
end
def get_response(cmd)
@munin.puts(cmd)
stop = false
response = Array.new
while stop == false
line = @munin.gets
line.chomp!
if line == '.'
stop = true
else
response << line
stop = true if cmd == "list"
end
end
response
end
def close
@munin.close
end
end
class Carbon
def initialize(host='localhost', port=2003)
@carbon = TCPSocket.new(host, port)
end
def send(msg)
@carbon.puts(msg)
end
def close
@carbon.close
end
end
while true
metric_base = "servers."
all_metrics = Array.new
munin = Munin.new(ARGV[0])
munin.get_response("nodes").each do |node|
metric_base << node.split(".").reverse.join(".")
puts "Doing #{metric_base}"
munin.get_response("list")[0].split(" ").each do |metric|
puts "Grabbing #{metric}"
mname = "#{metric_base}"
has_category = false
base = false
munin.get_response("config #{metric}").each do |configline|
if configline =~ /graph_category (.+)/
mname << ".#{$1}"
has_category = true
end
if configline =~ /graph_args.+--base (\d+)/
base = $1
end
end
mname << ".other" unless has_category
munin.get_response("fetch #{metric}").each do |line|
line =~ /^(.+)\.value\s+(.+)$/
field = $1
value = $2
all_metrics << "#{mname}.#{metric}.#{field} #{value} #{Time.now.to_i}"
end
end
end
carbon = Carbon.new(ARGV[1])
all_metrics.each do |m|
puts "Sending #{m}"
carbon.send(m)
end
sleep 60
end