forked from adamhjk/munin-graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
munin-graphite.rb
executable file
·145 lines (127 loc) · 3.29 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env 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'
use_amqp = false
require 'bunny' if use_amqp
STDOUT.sync = true # ensures no buffering when logging
def log(msg)
puts "[#{Time.new.to_s}] #{msg}"
end
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
class Amqp
def initialize(host='localhost')
@b = Bunny.new(:host=>host)
@b.start
@q = @b.queue('graphite')
end
def send_message(message)
@q.publish(message)
end
def stop
@b.stop
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(".")
log "Doing #{metric_base}"
munin.get_response("list")[0].split(" ").each do |metric|
log "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}" if (value != nil and value.length > 0 and field != nil and field.length > 0)
end
end
end
if use_amqp
amqp = Amqp.new(ARGV[1])
all_metrics.each do |m|
log "Sending #{m}"
amqp.send_message(m)
end
amqp.stop
else
carbon = Carbon.new(ARGV[1])
all_metrics.each do |m|
log "Sending #{m}"
carbon.send(m)
end
end
log "Sleeping for 30 s"
sleep 30
end