forked from adamhjk/munin-graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
munin-graphite.rb
executable file
·106 lines (95 loc) · 2.58 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/ruby
#
# munin-graphite.rb
#
# A Munin-Node to Graphite bridge
#
# Author:: Adam Jacob (<[email protected]>)
# Copyright:: Copyright (c) 2008 HJK Solutions, LLC
# License:: GNU General Public License version 2 or later
#
# This program and entire repository is free software; you can
# redistribute it and/or modify it under the terms of the GNU
# General Public License as published by the Free Software
# Foundation; either version 2 of the License, or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
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(".")[2] + "." + node.split(".")[0]
munin.get_response("list")[0].split(" ").each do |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|
carbon.send(m)
end
sleep 15
end