Skip to content

Commit 41a4ebe

Browse files
author
jschoch
committed
first pass at merb-gen flat
works ok, much to do
1 parent 2b1e16f commit 41a4ebe

15 files changed

+2775
-3
lines changed

README

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ need to figure out what to do with min/max/avg etc
88

99

1010
wondering if gchart is slowing things down, adding gridlines and line widths would be nice
11+
adding min/max x axis labels would be good
12+
add candle chart for min/max/avg

README.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Merb Flat
2+
==============
3+
4+
An example of a small Merb application. Uses a single views directory and one
5+
controller file.
6+
7+
Start with
8+
9+
merb
10+

Rakefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require 'rubygems'
2+
require 'rake/rdoctask'
3+
4+
require 'merb-core'
5+
require 'merb-core/tasks/merb'
6+
7+
include FileUtils
8+
9+
# Load the basic runtime dependencies; this will include
10+
# any plugins and therefore plugin rake tasks.
11+
init_env = ENV['MERB_ENV'] || 'rake'
12+
Merb.load_dependencies(:environment => init_env)
13+
14+
# Get Merb plugins and dependencies
15+
Merb::Plugins.rakefiles.each { |r| require r }
16+
17+
# Load any app level custom rakefile extensions from lib/tasks
18+
tasks_path = File.join(File.dirname(__FILE__), "lib", "tasks")
19+
rake_files = Dir["#{tasks_path}/*.rake"]
20+
rake_files.each{|rake_file| load rake_file }
21+
22+
desc "Start runner environment"
23+
task :merb_env do
24+
Merb.start_environment(:environment => init_env, :adapter => 'runner')
25+
end
26+
27+
require 'spec/rake/spectask'
28+
require 'merb-core/test/tasks/spectasks'
29+
desc 'Default: run spec examples'
30+
task :default => 'spec'
31+
32+
##############################################################################
33+
# ADD YOUR CUSTOM TASKS IN /lib/tasks
34+
# NAME YOUR RAKE FILES file_name.rake
35+
##############################################################################

application.rb

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Rrdcrunchr < Merb::Controller
2+
3+
def _template_location(action, type = nil, controller = controller_name)
4+
controller == "layout" ? "layout.#{action}.#{type}" : "#{action}.#{type}"
5+
end
6+
7+
def index
8+
@hosts = get_hosts.sort
9+
@rrdtypes = get_rrdtypes.sort
10+
#@hosts = get_hosts
11+
#@rrdtypes = get_rrdtypes
12+
@hosts.inspect
13+
@rrdtypes.inspect
14+
@rrdgroups = ["All"]
15+
16+
render
17+
18+
end
19+
20+
def foo
21+
render
22+
end
23+
24+
end

config/framework.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Merb::Config[:framework] = {
2+
:application => Merb.root / "application.rb",
3+
:config => [Merb.root / "config", nil],
4+
:public => [Merb.root / "public", nil],
5+
:view => Merb.root / "views"
6+
}
7+

config/init.rb

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Go to http://wiki.merbivore.com/pages/init-rb
2+
3+
# use_orm :none
4+
use_test :rspec
5+
use_template_engine :erb
6+
7+
# Specify a specific version of a dependency
8+
# dependency "RedCloth", "> 3.0"
9+
dependency "merb-assets", ">1.0"
10+
dependency "merb-more", ">1.0"
11+
require 'rubygems'
12+
# require 'hpricot'
13+
require 'erb'
14+
require 'yaml'
15+
require 'gchart'
16+
17+
CONF = YAML.load_file("./rcr.yaml")
18+
Merb::BootLoader.before_app_loads do
19+
# This will get executed after dependencies have been loaded but before your app's classes have loaded.
20+
end
21+
22+
Merb::BootLoader.after_app_loads do
23+
# This will get executed after your app's classes have been loaded.
24+
require 'lib/rrdcrunchr.rb'
25+
end
26+
27+
# Move this to application.rb if you want it to be reloadable in dev mode.
28+
Merb::Router.prepare do
29+
match('/').to(:controller => "rrdcrunchr", :action =>'index')
30+
match('/foo').to(:controller => "rrdcrunchr", :action =>'foo')
31+
default_routes
32+
end
33+
34+
Merb::Config.use { |c|
35+
c[:environment] = 'production',
36+
c[:framework] = {},
37+
c[:log_level] = :debug,
38+
c[:log_stream] = STDOUT,
39+
# or use file for logging:
40+
# c[:log_file] = Merb.root / "log" / "merb.log",
41+
c[:use_mutex] = false,
42+
c[:session_store] = 'cookie',
43+
c[:session_id_key] = '_rrdcrunchr_session_id',
44+
c[:session_secret_key] = 'ff7bd582e6f458507313c2392d6e1dae1f9dc184',
45+
c[:exception_details] = true,
46+
c[:reload_classes] = true,
47+
c[:reload_templates] = true,
48+
c[:reload_time] = 0.5
49+
c[:reload_files] = true
50+
}

lib/rrdcrunchr.rb

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
require 'rubygems'
2+
require 'gchart'
3+
4+
def get_hosts
5+
# probly want to do this at init
6+
7+
paths = Dir[CONF['rrd_data_home']+"/*"].sort
8+
#hosts = {}
9+
hosts = ["All"]
10+
#count = 0
11+
for path in paths
12+
p = path.split("/")
13+
#hosts[p[-1]] = count
14+
#count = count +1
15+
hosts.push(p[-1])
16+
puts "hosts found: #{p[-1]}"
17+
end
18+
19+
hosts
20+
end
21+
def get_rrdtypes
22+
puts "Searching for RRD Types"
23+
paths = Dir[CONF['rrd_data_home']+"/**/*.rrd"].sort
24+
rrdtypes = ["All"]
25+
dir_exclude = CONF['dir_exclude'].split(" ")
26+
for path in paths
27+
p = path.split("/")
28+
if (rrdtypes.include?(p[-1]))
29+
print "."
30+
else
31+
rrdtypes.push(p[-1]) unless dir_exclude.include?(p[-2])
32+
print "*"
33+
end
34+
end
35+
rrdtypes
36+
end
37+
38+
def median (array, already_sorted=false)
39+
return nil if array.empty?
40+
array = array.sort unless already_sorted
41+
m_pos = array.size / 2
42+
return array.size % 2 == 1 ? array[m_pos] : mean (array[m_pos-1..m_pos])
43+
end
44+
def mean(array)
45+
array.inject(0) { |sum, x| sum +=x } /array.size.to_f
46+
end
47+
48+
def get_rrd(interval,rrd_file,rrd_type,resolution)
49+
time = Time.now
50+
start_time = ((time.tv_sec/resolution.to_i)*resolution.to_i)
51+
puts "#{CONF['rrdtool_path']}/rrdtool fetch #{rrd_file} #{rrd_type} -e #{start_time} -r #{resolution} -s e-#{interval}m |#{CONF['awk_path']}/awk '{printf\"%s,\", $2}'"
52+
rawdata = `#{CONF['rrdtool_path']}/rrdtool fetch #{rrd_file} #{rrd_type} -e #{start_time} -r #{resolution} -s e-#{interval}m |#{CONF['awk_path']}/awk '{printf"%s,", $2}'`
53+
datas = rawdata.split(",")
54+
mdata = []
55+
ymax =0
56+
ymin = 100000000
57+
for d in datas
58+
#STDOUT.print "#{d} " unless d.downcase =~ /nan/
59+
stat = d.to_f
60+
if (stat)
61+
mdata.push(stat) unless d.downcase =~ /nan/
62+
end
63+
if (ymax < stat)
64+
ymax = d.to_f
65+
end
66+
if (ymin > stat)
67+
ymin = d.to_f
68+
end
69+
end
70+
is_zero = false
71+
stat_median = median(mdata)
72+
if (stat_median == 0 and ymin.to_f == 0 && ymax.to_f == 0)
73+
is_zero = true
74+
end
75+
[mdata,is_zero,ymin,ymax,stat_median]
76+
end
77+
78+
79+
def get_uri(rrd_file,interval,resolution,host)
80+
81+
# check exclude list
82+
#
83+
84+
names = rrd_file.split("\/")
85+
source = "http://chart.apis.google.com/chart"
86+
type = "ls"
87+
colour = "8198A2"
88+
threshold_colour = "ff0000"
89+
90+
is_zero = false
91+
min_data,is_zero,ymin,ymax,stat_median = get_rrd(interval,rrd_file,"MIN",resolution)
92+
max_data,is_zero,ymin,ymax,stat_median = get_rrd(interval,rrd_file,"MAX",resolution)
93+
avg_data,is_zero,ymin,ymax,stat_median = get_rrd(interval,rrd_file,"AVERAGE",resolution)
94+
95+
96+
# 80th percentile stuff, not needed right now
97+
98+
99+
all_data = []
100+
101+
if (min_data == avg_data && max_data == avg_data)
102+
all_data = avg_data
103+
else
104+
all_data = [min_data,max_data,avg_data]
105+
end
106+
# :line_colors => 'FF0000,00FF00,666666',
107+
uri = Gchart.line( :size => '1000x165',
108+
:title => "#{host} #{names[-1]}",
109+
:bg => 'FFCCFF',
110+
:line_colors => 'FF0000,00FF00,666666',
111+
:axis_with_labels => ['x','y','r'],
112+
:encoding => 'simple',
113+
:data => all_data)
114+
115+
names = rrd_file.split("\/")
116+
[uri,names[-1],ymin,ymax,stat_median,is_zero]
117+
end
118+
119+
def draw_table(img_src,counter_name,ymin,ymax,stat_median,dir_name)
120+
template = ERB.new <<-EOF
121+
<center>
122+
<table id=result border=0><tr>
123+
<td id=counter><%= counter_name %></td>
124+
<td rowspan=2 id=sparkline>
125+
<img src=<%=img_src%>></a></td>
126+
<td rowspan=2>
127+
<div id=max><%= ymax.to_f %></div>
128+
<div id=med><%= stat_median %></div>
129+
<div id=min><%= ymin.to_f %></div></td><td rowspan=2>put my description here</td>
130+
</tr>
131+
<tr>
132+
<td id=filter><%= dir_name %></td>
133+
</tr>
134+
</table></center>
135+
136+
EOF
137+
template.result(binding)
138+
139+
140+
end

log/merb.8080.pid

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
15979

0 commit comments

Comments
 (0)