-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRGmetric.rb
157 lines (132 loc) · 4.09 KB
/
RGmetric.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
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env ruby
require 'rubygems'
require 'gmetric'
require 'optparse'
# Get Options
options = {}
option_parser = OptionParser.new do |opts|
opts.banner =
'Help message of RGmetric , v 0.1
heartbeat is not supported now.
mcast in ganglia conf is not supported now.
'
#options[:help] = false
#opts.on('-h','--help','') do
# options[:help] = true
#end
options[:version] = false
opts.on('-V','--version','') do
options[:version] = true
end
options[:heartbeat] = false
opts.on('-b','--heartbeat','') do
options[:heartbeat] = true
end
options[:debug] = false
opts.on('-z','--debug','') do
options[:debug] = true
end
opts.on('-n NAME', '--name=NAME', 'Name of the metric') do |value|
options[:name] = value
end
opts.on('-v VALUE', '--value=VALUE', 'Value of the metric') do |value|
options[:value] = value
end
opts.on('-g GROUP', '--group=GROUP', 'Group of the metric') do |value|
options[:group] = value
end
opts.on('-c CONF', '--conf=CONF', "The configuration file to use for finding send channels (default='/etc/ganglia/gmond.conf')") do |value|
options[:conf] = value || '/etc/ganglia/gmond.conf'
end
opts.on('-t TYPE', '--type=TYPE', 'string|int8|uint8|int16|uint16|int32|uint32|float|double') do |value|
options[:type] = value
end
opts.on('-u UNITS', '--units=UNITS', "Unit of measure for the value e.g. Kilobytes, Celcius (default='')") do |value|
options[:units] = value || ''
end
opts.on('-s SLOPE', '--slope=SLOPE', "Either zero|positive|negative|both (default='both')") do |value|
options[:slope] = value || 'both'
end
opts.on('-x TMAX', '--tmax=TMAX', "The maximum time in seconds between gmetric calls (default='60')") do |value|
options[:tmax] = value || 60
end
opts.on('-d DMAX', '--dmax=DMAX', "The lifetime in seconds of this metric (default='0')") do |value|
options[:dmax] = value || 0
end
opts.on('-S SPOOF', '--spoof=SPOOF', "IP address and name of host/device (colon separated) we are spoofing (default='')") do |value|
options[:spoof] = value || ''
end
end.parse!
# Remove comments in the gmond conf
cfg_path = options[:conf] || '/etc/ganglia/gmond.conf'
cfg_ok = ''
commented = false
IO.read(cfg_path).each do |lines|
case lines
when /^.*'.*(\/\*|\*\/|\/\*.*\*\/|#).*'.*$/ || \
/^.*".*(\/\*|\*\/|\/\*.*\*\/|#).*".*$/
cfg_ok << lines
when /^.*\/\*.*\*\/.*$/
if commented == false
cfg_ok << lines.gsub(/\/\*.*\*\//,'')
end
when /^.*\*\/.*$/
if commented == false
puts "ERROR : commented : #{commented} , matched: */ , lines: #{lines}"
else
cfg_ok << lines.gsub(/^.*\*\//,'')
end
# else commented this lines
commented = false
when /^.*\/\*.*$/
if commented == false
cfg_ok << lines.gsub(/\/\*.*$/,'')
commented = true
end
when /^.*#.*$/
if commented == false
cfg_ok << lines.gsub(/#.*$/,'')
end
else
if commented == false
cfg_ok << lines
end
end
end
# get server's host\port\ttl from conf
host = port = ttl = ''
send_channel = false
cfg_ok.each do |lines|
case lines
when /^.*(udp_send_channel|udp_send_channel.*\{).*$/
send_channel = true
when /^.*host.*=.*$/
if send_channel == true
host = lines.gsub(/^.*host.*=/,'').chomp.strip
end
when /^.*port.*=.*$/
if send_channel == true
port = lines.gsub(/^.*port.*=/,'').chomp.strip
end
when /^.*ttl.*=.*$/
if send_channel == true
ttl = lines.gsub(/^.*ttl.*=/,'').chomp.strip
end
end
end
if options[:debug]
puts "Options:#{options.inspect}"
puts "gmond configuration:#{cfg_ok}"
puts "Gmond/Gmetric Host\\Port\\ttl:"
puts host,port,ttl
end
# Send data
Ganglia::GMetric.send( "#{host}", "#{port}", {
:group => "#{options[:group]}",
:name => "#{options[:name]}",
:units => "#{options[:units]}",
:type => "options[:type]", # unsigned 8-bit int
:value => options[:value], # value of metric
:tmax => options[:tmax], # maximum time in seconds between gmetric calls
:dmax => options[:dmax] # lifetime in seconds of this metric
})