Skip to content

Commit

Permalink
Clean up and simplify the DSL
Browse files Browse the repository at this point in the history
Use instance_eval instead of eval'd code blocks
to implement the filter and execution blocks

Kudos to https://github.com/edjames for the
suggestion!
  • Loading branch information
pvdb committed Aug 6, 2011
1 parent 10b2574 commit b0f4052
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,44 +60,44 @@ Advanced Examples

* read from STDIN and print out the length of each line _(to illustrate -e option)_

$ ntail -e '{ |line| puts line.size }'
$ ntail -e 'puts size'

* read from STDIN but only print out non-empty lines _(to illustrate -f option)_

$ ntail -f '{ |line| line.size $ 0 }'
$ ntail -f 'size != 0'

* the following invocations behave exactly the same _(to illustrate -e and -f options)_

$ ntail
$ ntail -f '{ |line| true }' -e '{ |line| puts line }'
$ ntail -f 'true' -e 'puts self'

* print out all HTTP requests that are coming from a given IP address

$ ntail -f '{ |line| line.remote_address == "208.67.222.222" }' /var/log/nginx/access.log
$ ntail -f 'remote_address == "208.67.222.222"' /var/log/nginx/access.log

* find all HTTP requests that resulted in a '5xx' HTTP error/status code _(e.g. Rails 500 errors)_

$ gunzip -S .gz -c access.log-20101216.gz | ntail -f '{ |line| line.server_error_status? }'
$ gunzip -S .gz -c access.log-20101216.gz | ntail -f 'server_error_status?'

* generate a summary report of HTTP status codes, for all non-200 HTTP requests

$ ntail -f '{ |line| line.status != "200" }' -e '{ |line| puts line.status }' access.log | sort | uniq -c
$ ntail -f 'status != "200"' -e 'puts status' access.log | sort | uniq -c
76 301
16 302
2 304
1 406

* print out GeoIP country and city information for each HTTP request _(depends on the optional `geoip` gem)_

$ ntail -e '{ |line| puts [line.to_country_s, line.to_city_s].join("\t") }' /var/log/nginx/access.log
$ ntail -e 'puts [to_country_s, to_city_s].join("\t")' /var/log/nginx/access.log
United States Los Angeles
United States Houston
Germany Berlin
United Kingdom London

* print out the IP address and the corresponding host name for each HTTP request _(slows things down considerably, due to `nslookup` call)_

$ ntail -e '{ |line| puts [line.remote_address, line.to_host_name].join("\t") }' /var/log/nginx/access.log
$ ntail -e 'puts [remote_address, to_host_s].join("\t")' /var/log/nginx/access.log
66.249.72.196 crawl-66-249-72-196.googlebot.com
67.192.120.134 s402.pingdom.com
75.31.109.144 adsl-75-31-109-144.dsl.irvnca.sbcglobal.net
Expand Down
4 changes: 2 additions & 2 deletions lib/ntail/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def run!
if log_line.parsable
parsable_lines += 1
unless @options.parse_only
if !@options.filter || @options.filter.call(log_line)
if !@options.filter || log_line.instance_eval(@options.filter)
lines_processed += 1
if @options.code
@options.code.call(log_line)
log_line.instance_eval(@options.code)
else
puts log_line.to_s(:color => true)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ntail/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def parse_options(argv, defaults = {})
end

opts.on '--filter', '-f CODE', "Ruby code block for filtering (parsed) lines - needs to return true or false." do |value|
options.filter = eval "Proc.new #{value}"
options.filter = value
end

opts.on '--execute', '-e CODE', "Ruby code block for processing each (parsed) line." do |value|
options.code = eval "Proc.new #{value}"
options.code = value
end

opts.on '--line-number', '-l LINE_NUMBER', "Only process the line with the given line number" do |value|
Expand Down

0 comments on commit b0f4052

Please sign in to comment.