hunter 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hunter.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Search for a pattern in your log files
2
+
3
+ I generally use a tracking gif in the mass emails I send out to get a rough idea of the open rate. It's not a small task to open each log file, filter it and then count each occurance of the gif.
4
+
5
+ Hunter does this for you!
6
+
7
+ **WARNING:** Hunter has <u>**no tests**</u>. This was thrown together, so use with caution. Contributions are very welcome.
8
+
9
+ ## How to Use
10
+
11
+ ### Install
12
+
13
+ gem install hunter
14
+
15
+ ### Use
16
+
17
+ Create a new hunt for the string you want to match:
18
+
19
+ hunt = Hunter::Hunt.new('/images/email/tracking.gif')
20
+
21
+ #### Look in the current log
22
+
23
+ hunt.look_in('/var/log/httpd/access.log')
24
+
25
+ #### Look in an archived log
26
+
27
+ hunt.look_in_archived('/var/log/httpd/log_archive/access.log.1.gz')
28
+
29
+ #### Look through a folder of archived logs
30
+
31
+ hunt.look_through_archive('/var/log/httpd/log_archive')
32
+
33
+ #### Look through a folder of archived logs for a specific log type
34
+
35
+ hunt.look_through_archive('/var/log/httpd/log_archive', 'access.log')
36
+
37
+ ## Known Issues
38
+
39
+ * "Archived" means with the file extension `.gz` – no other compression formats are supported
40
+ * `look_through_archive()` relies on your log appending a digit after the original filename and then compressing to `.gz`
41
+ * e.g. `access.log` becomes `access.log.1.gz`
42
+ * **There are no tests**
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/hunter.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "hunter/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "hunter"
7
+ s.version = Hunter::VERSION
8
+ s.authors = ["Gareth Rees"]
9
+ s.email = ["gareth.h.rees@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Hunter hunts for a pattern in your log files}
12
+ s.description = %q{Hunter was made to look through log archive directories and inside each log count the number of hits on an email tracking gif}
13
+
14
+ s.rubyforge_project = "hunter"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
data/lib/hunter.rb ADDED
@@ -0,0 +1,74 @@
1
+ require "hunter/version"
2
+ require 'zlib'
3
+
4
+ module Hunter
5
+
6
+ class Hunt
7
+
8
+ attr_accessor :string_to_match
9
+
10
+ def initialize(string_to_match)
11
+ @string_to_match = string_to_match
12
+ end
13
+
14
+ # ONE FILE
15
+ def look_in(file)
16
+ counter = 0
17
+ File.open(file, "r") do |infile|
18
+ while line = infile.gets
19
+ counter += 1 if look_for_match(line)
20
+ end
21
+ end
22
+ puts counter
23
+ counter
24
+ end
25
+
26
+ # ONE .GZ FILE
27
+ def look_in_archived(file)
28
+ counter = 0
29
+ File.open(file) do |f|
30
+ gz = Zlib::GzipReader.new(f)
31
+ gz.each_line do |line|
32
+ counter += 1 if look_for_match(line)
33
+ end
34
+ gz.close
35
+ end
36
+ puts counter
37
+ counter
38
+ end
39
+
40
+ # ARCHIVE OF .GZ FILES
41
+ def look_through_archive(directory, file_basename = "*", compression = "gz")
42
+ counter = 0
43
+ files = Dir.glob("/#{directory}/#{file_basename}.*.#{compression}")
44
+ files = sort_archive(files)
45
+
46
+ files.each do |file|
47
+ matches = look_in_archived(file)
48
+ counter += matches
49
+ puts "#{file}: #{matches}"
50
+ end
51
+
52
+ puts "TOTAL: " + counter.to_s
53
+ counter
54
+ end
55
+
56
+
57
+ # HELPERS
58
+ def look_for_match(line)
59
+ true if line.include? @string_to_match
60
+ end
61
+
62
+ def sort_archive(files)
63
+ r = Regexp.new(".([0-9]+).gz")
64
+ sorted_files = files.sort do |f1, f2|
65
+ n1 = r.match(f1)[1].to_i
66
+ n2 = r.match(f2)[1].to_i
67
+ n1 <=> n2
68
+ end
69
+ sorted_files
70
+ end
71
+
72
+ end
73
+
74
+ end
@@ -0,0 +1,3 @@
1
+ module Hunter
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hunter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gareth Rees
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-13 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Hunter was made to look through log archive directories and inside each
15
+ log count the number of hits on an email tracking gif
16
+ email:
17
+ - gareth.h.rees@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - hunter.gemspec
27
+ - lib/hunter.rb
28
+ - lib/hunter/version.rb
29
+ homepage: ''
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: hunter
49
+ rubygems_version: 1.8.11
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Hunter hunts for a pattern in your log files
53
+ test_files: []