Repository navigation
Expand file tree
/
Copy pathgraphserver.rb
More file actions
152 lines (122 loc) · 3.9 KB
/
Copy pathgraphserver.rb
File metadata and controls
152 lines (122 loc) · 3.9 KB
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
#!/usr/bin/ruby
require 'rubygems'
require 'json'
require 'sinatra'
require 'sparklines'
require 'ftools';
@@rrd_path = "/var/lib/ganglia/rrds/"
### due to the new ganglia setup (see FB 5), the server is now known
### by it's FQDN, and not localhost, so we have to change the path
### on the file system to match --jos
@@liftium_path = "#{@@rrd_path}/Liftium/liftium-s1/"
# 2x2 pixel gif
@@fallback_image = File.expand_path( File.dirname( $0 ) + '/white.gif' );
def send_rrd_graph(rrd_opts)
temp_file = Tempfile.new('rrd-image')
rrd_opts.unshift temp_file.path
# on success, send the graph
if system "rrdtool graph #{rrd_opts.join(' ')}"
send_file temp_file.path,
:type => 'image/png',
:disposition => nil
temp_file.close
# otherwise, send a fallback image
else
# XXX don't know how to capture STDERR from a syscall :(
send_fallback_image "System call failed: " + $?.to_s
end
end
def send_fallback_image( reason )
### record why we're sending this image
warn "[ERROR] Sending fallback image because: #{reason}"
### record why we're sending this error
@response[ "X-GraphError" ] = "#{reason}"
send_file @@fallback_image,
:type => 'image/gif',
:disposition => nil
end
def get_rrd_array(range,net,tag)
results = []
rrd_opts = []
rrd_opts << "--start=-#{params[:range]}"
rrd_opts << "-m 75"
['loads','attempts'].each do |stat|
rrd_opts << "DEF:#{stat}=#{@@liftium_path}/liftium_#{net}_#{tag}_#{stat}.rrd:sum:AVERAGE"
end
rrd_opts << "CDEF:fill=loads,attempts,/,100,*"
rrd_opts << "XPORT:fill"
xml = `rrdtool xport #{rrd_opts.join(' ')}`
data = xml.scan(/<v>(.*)<\/v>/)
data.each do |item|
if item[0].to_s == "NaN"
results << 0
else
# Convert rrds sci notation to decimal
value = "%4.2f" % item[0]
results << value.to_i
end
end
return results
end
get '/chart' do
rrd_opts = []
colors = [ "00FF00C0", "FF0000C0", "0000FFC0" ]
net = params[:net]
tag = params[:tag]
rrd_opts << "--start=-#{params[:range]}"
rrd_opts << "--title=\"#{params[:title]}\""
rrd_opts << "--vertical-label=requests"
rrd_opts << "--height=#{params[:height]}"
rrd_opts << "--width=#{params[:width]}"
rrd_opts << "--slope-mode"
rrd_opts << "--lower-limit=0"
['attempts','rejects','loads'].each do |stat|
rrd_opts << "DEF:#{stat}=#{@@liftium_path}/liftium_#{net}_#{tag}_#{stat}.rrd:sum:AVERAGE"
rrd_opts << "AREA:#{stat}\##{colors.shift}:#{stat}"
end
rrd_opts << "VDEF:attempts_max=attempts,MAXIMUM"
rrd_opts << "CDEF:fill_rate=loads,attempts,/,attempts_max,*"
rrd_opts << "LINE:fill_rate#000000:fill_rate"
send_rrd_graph(rrd_opts)
end
get '/misc' do
rrd_opts = []
stat = params[:stat]
rrd_opts << "--start=\"-#{params[:range]}\""
rrd_opts << "--title=\"#{params[:title]}\""
rrd_opts << "--height=#{params[:height]}"
rrd_opts << "--width=#{params[:width]}"
rrd_opts << "--slope-mode"
rrd_opts << "DEF:#{stat}=#{@@liftium_path}/liftium_#{stat}.rrd:sum:AVERAGE"
rrd_opts << "AREA:#{stat}\#000000CC:#{stat}"
send_rrd_graph(rrd_opts)
end
get '/sparkline' do
send_fallback_image "Manually disabled for perf"
range = params[:range]
net = params[:net]
tag = params[:tag]
results = get_rrd_array(range,net,tag)
begin
image = Sparklines.plot( results,
:type => 'area',
:min => 0,
:max => 100,
:step => 3,
:upper => 0,
:above_color => "#0098E0",
:height => params[:height] )
content_type 'image/png'
image
rescue Exception => e
send_fallback_image e.to_s
end
return image
end
get '/json_spark' do
range = params[:range]
net = params[:net]
tag = params[:tag]
results = get_rrd_array(range,net,tag)
return results.to_json
end