This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
95 lines (73 loc) · 2.45 KB
/
main.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
#!/usr/bin/env ruby
__DIR__ = File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'bundler'
Bundler.require
# temporary hack to use rrdcahced unix socket
def enable_rrdcacached
Errand.send(:undef_method, :fetch)
Errand.send(:define_method, :fetch) do |opts = {}|
start = (opts[:start] || Time.now.to_i - 3600).to_s
finish = (opts[:finish] || Time.now.to_i).to_s
function = opts[:function] ? opts[:function].to_s.upcase : "AVERAGE"
args = [@filename, "--start", start, "--end", finish, function, "--daemon", "unix:/tmp/rrdcached.sock"]
data = @backend.fetch(*args)
start = data[0]
finish = data[1]
labels = data[2]
values = data[3]
points = {}
# compose a saner representation of the data
labels.each_with_index do |label, index|
points[label] = []
values.each do |tuple|
value = tuple[index].nan? ? nil : tuple[index]
points[label] << value
end
end
{:start => start, :finish => finish, :data => points}
end
end
require File.expand_path('../lib/graph_series/base', __FILE__)
require File.expand_path('../lib/graph_series/line', __FILE__)
require File.expand_path('../lib/graph_series/bar', __FILE__)
require File.join(__DIR__, 'lib/graph')
require File.join(__DIR__, 'lib/config_loader')
require File.join(__DIR__, 'config/config')
set :public, __DIR__ + '/public'
set :views, __DIR__ + '/views'
template :layout do
File.read(File.join(__DIR__, 'views/layout.haml'))
end
use Rack::Auth::Basic do |username, password|
[username, password] == ['admin', 'admin']
end
get '/' do
@hosts = RRDFaces::view.machines.keys
haml :index
end
get '/*' do
# check if a template exist
template_path = File.join(__DIR__, '/views', "#{params[:splat][0]}.haml")
puts "PATH: #{template_path}"
pass unless File.exist?(template_path)
haml "#{params[:splat][0]}".to_sym
end
get '/:host' do
if RRDFaces::view.machines[params[:host].to_sym]
@hosts = RRDFaces::view.machines.keys
@graphs = RRDFaces::view.default.values
@graphs << RRDFaces::view.machines[params[:host].to_sym].values
@graphs.flatten!
haml :index
end
end
get '/data/:host/:view' do
content_type :json
view = params[:view].to_sym
host = params[:host].to_sym
interval = params[:interval].to_i
index = params[:index].to_i
graph = RRDFaces::view.default[view] || RRDFaces::view.machines[host][view]
Yajl::Encoder.new.encode(graph.to_hash(host, interval, index))
end