munchies 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/munchies.rb +71 -0
  2. data/lib/munchies/version.rb +3 -0
  3. metadata +48 -0
@@ -0,0 +1,71 @@
1
+ require 'stringio'
2
+
3
+ module Munchies
4
+ class Logfile
5
+
6
+ include Enumerable
7
+
8
+ # Matches timestamps in logfile
9
+ TIME_REGEXP = /^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}\b/
10
+
11
+ def initialize path, starting_at = Time.now
12
+ raise ArgumentError unless File.exists?( path )
13
+
14
+ @log = File.open( path )
15
+ @ending_at = starting_at
16
+ @starting_at = (@ending_at - 300)
17
+ end
18
+
19
+ def backwards
20
+
21
+ filesize = @log.stat.size
22
+ buffer_size = 128000
23
+ offset = buffer_size
24
+
25
+ # Seek to the end of the file
26
+ @log.seek(0, File::SEEK_END)
27
+
28
+ timestamp = @starting_at.strftime("%FT%T")
29
+
30
+ while @log.tell > 0
31
+ @log.seek(-offset, File::SEEK_END)
32
+
33
+ buffer = @log.read( buffer_size )
34
+
35
+ # Look at the first two lines of the buffer and check if we are close
36
+ # to the desired timestamp. First line may be incomplete.
37
+ #
38
+ buffer_line = buffer.split("\n")[1]
39
+ if (t = buffer_line.match(TIME_REGEXP)) && (t[0] < timestamp)
40
+
41
+ (1..300).each do |i|
42
+
43
+ if index = buffer.index( timestamp )
44
+ # Calculate byte offest and seek to this position
45
+ position = (@log.tell - buffer_size + index)
46
+ @log.seek( position, File::SEEK_SET )
47
+ return
48
+ end
49
+
50
+ # If the first timestamp doesn't match count up seconds until it does
51
+ timestamp = (@starting_at + i).strftime("%FT%T")
52
+ end
53
+ end
54
+
55
+ offset += buffer_size
56
+
57
+ return if offset > filesize
58
+ end
59
+
60
+ end
61
+
62
+ def each &block
63
+ backwards
64
+
65
+ @log.each_line do |line|
66
+ yield line
67
+ end
68
+ end
69
+
70
+ end
71
+ end
@@ -0,0 +1,3 @@
1
+ module Munchies
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: munchies
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - hukl
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-02-17 00:00:00.000000000 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Emits the last 5 minutes of lines of a log file
16
+ email: contact@smyck.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/munchies.rb
22
+ - lib/munchies/version.rb
23
+ has_rdoc: true
24
+ homepage: http://github.com/hukl/munchies
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project: ! '[none]'
44
+ rubygems_version: 1.5.2
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Prepares log files for munin modules
48
+ test_files: []