Skip to content

Commit

Permalink
add examples and cli option for config file
Browse files Browse the repository at this point in the history
  • Loading branch information
josqu4red committed Apr 10, 2013
1 parent 9c435b9 commit 7c921f9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 36 deletions.
11 changes: 10 additions & 1 deletion bin/varnishhit
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@ require 'ui'

@config = CmdLine.parse(ARGV)

pipe = VarnishPipe.new(@config)
@regexs = {}
# load regex config file
begin
load @config[:config_file]
rescue Exception => e
puts "Unable to load config.rb (#{e.message})"
exit 1
end

pipe = VarnishPipe.new(@config, @regexs)
ui = UI.new(@config)

done = false
Expand Down
2 changes: 2 additions & 0 deletions config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@regexs = {
}
17 changes: 17 additions & 0 deletions ext/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Example regex file, expressions order matters !
@regexs = {
/^(images)\/.*_(\d+)\.(png)$/ => [1,3,2]
/^\/(\w+)\/.*\.(\w+)$/ => [1, 2],
}

# will match URLs and display them such as:
#
# /images/11/22/logo_400.png => images:png:400
#
# first, then:
#
# /assets/your/own/path/app.js => assets:js
#
# /images/00/11/banner.jpg => images:jpg
#
# validate these expressions using the fine http://rubular.com/
13 changes: 9 additions & 4 deletions lib/cmdline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ def self.parse(args)
@config = {}

opts = OptionParser.new do |opt|
@config[:discard_thresh] = 0
opt.on '-d', '--discard=THRESH', Float, 'Discard keys with request/sec rate below THRESH' do |discard_thresh|
@config[:discard_thresh] = discard_thresh
@config[:config_file] = File.join(File.dirname(File.dirname(__FILE__)), 'config.rb')
opt.on '-c', '--config-file=FILE', String, 'Config file containing regular expressions to categorize traffic' do |config_file|
@config[:config_file] = File.expand_path(config_file)
end

@config[:refresh_rate] = 500
opt.on '-r', '--refresh=MS', Float, 'Refresh the stats display every MS milliseconds' do |refresh_rate|
opt.on '-r', '--refresh=MS', Integer, 'Refresh the stats display every MS milliseconds' do |refresh_rate|
@config[:refresh_rate] = refresh_rate
end

Expand All @@ -32,6 +32,11 @@ def self.parse(args)
@config[:avg_period] = 600 if @config[:avg_period] > 600
@config[:avg_period] = 10 if @config[:avg_period] < 10

unless File.exists?(@config[:config_file])
puts "#{@config[:config_file]}: no such file"
exit 1
end

@config
end
end
57 changes: 26 additions & 31 deletions lib/varnish_pipe.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class VarnishPipe
attr_accessor :stats, :semaphore, :start_time

def initialize(config)
def initialize(config, regexs)
@stats = {
:total_reqs => 0,
:total_bytes => 0,
Expand All @@ -14,14 +14,9 @@ def initialize(config)

@semaphore = Mutex.new
@avg_period = config[:avg_period]
@default_key = "other"

@regexs = {
/^\/(r)\/v2010\/[a-f0-9]{40}\/([a-z]+)\/.*$/ => [1, 2],
/^\/jpg((\/\d{2}){4})\/(\d{3}).*_(PX)P\.(jpg)$/ => [5, 3, 4],
/^\/jpg((\/\d{2}){4})\/(\d{3}).*\.(\w{3})$/ => [4, 3],
/^\/(v2010)\/(\w+)\/.*$/ => [1, 2],
/^\/(\w+)\/.*$/ => [1]
}
@regexs = regexs
end

def start
Expand All @@ -41,32 +36,32 @@ def start
end
end

if key
@semaphore.synchronize do
duration = (Time.now.to_i - @start_ts)
it = duration / @avg_period
idx = duration % @avg_period
key = @default_key unless key

@semaphore.synchronize do
duration = (Time.now.to_i - @start_ts)
it = duration / @avg_period
idx = duration % @avg_period

@stats[:requests][key] ||= {
:hit => Array.new(@avg_period, 0),
:miss => Array.new(@avg_period, 0),
:bytes => Array.new(@avg_period, 0),
:it => Array.new(@avg_period, 0),
:last => ""
}
@stats[:requests][key] ||= {
:hit => Array.new(@avg_period, 0),
:miss => Array.new(@avg_period, 0),
:bytes => Array.new(@avg_period, 0),
:it => Array.new(@avg_period, 0),
:last => ""
}

cur_req = @stats[:requests][key]
if cur_req[:it][idx] < it
cur_req[status.to_sym][idx] = 0
cur_req[:bytes][idx] = 0
end
cur_req[status.to_sym][idx] += 1
cur_req[:bytes][idx] += bytes.to_i
cur_req[:it][idx] = it
cur_req[:last] = url
@stats[:total_reqs] += 1
@stats[:total_bytes] += bytes.to_i
cur_req = @stats[:requests][key]
if cur_req[:it][idx] < it
cur_req[status.to_sym][idx] = 0
cur_req[:bytes][idx] = 0
end
cur_req[status.to_sym][idx] += 1
cur_req[:bytes][idx] += bytes.to_i
cur_req[:it][idx] = it
cur_req[:last] = url
@stats[:total_reqs] += 1
@stats[:total_bytes] += bytes.to_i
end
end

Expand Down

0 comments on commit 7c921f9

Please sign in to comment.