nginx_log_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 376cd4f52ef8a1ac33e8509e9586bdd0626d27fa
4
+ data.tar.gz: 7df42d3f6e7a9c48eb98b48b8a0cd0fbf2b63db0
5
+ SHA512:
6
+ metadata.gz: 4fdc13af500c7c2ba93342a4a0b4f6535ec13229bee7d4607facc53f9639ccbee582e806fb8cc307adb4dc72b559ab84611b4ce92782969284d8c4959a43cda1
7
+ data.tar.gz: eddeebffa2b86dce689f1ff9b0a5f51382029cf860083782c074e270fabb37781443742a194c4acb20682a7332c951b59cabdf38ccd8305a9a702f84f5d64ca8
@@ -0,0 +1,100 @@
1
+ require 'nginx_log_parser/version'
2
+
3
+ class NginxLogParser
4
+ attr_accessor :format
5
+ attr_accessor :file
6
+ attr_accessor :live
7
+
8
+ DEFAULT_FORMAT = "$remote_addr - $remote_user [$time_local] \"$request\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\""
9
+
10
+ def initialize(file = nil, live: false, format: nil)
11
+ self.file = file
12
+ self.live = live
13
+ self.format = format || DEFAULT_FORMAT
14
+
15
+ if file =~ /\.gz$/ and live
16
+ raise StandardError.new "Cannot live stream a .gz file"
17
+ end
18
+ end
19
+
20
+ def each_entry
21
+ begin
22
+ f = File.open(file)
23
+
24
+ if live
25
+ f.seek(0, IO::SEEK_END)
26
+ while true do
27
+ sleep 0.1 while f.eof?
28
+ yield parse_line(f.gets)
29
+ end
30
+ else
31
+ f.each_line do |line|
32
+ yield parse_line(line)
33
+ end
34
+ end
35
+
36
+ ensure
37
+ f.close if f
38
+ end
39
+ end
40
+
41
+ def parse_line(line)
42
+ re = format_regex
43
+
44
+ pieces = line.match(/#{re}/i)
45
+ matches = Hash[pieces.names.map(&:to_sym).zip(pieces.captures)]
46
+ matches[:status] = matches[:status].to_i if matches[:status]
47
+ matches[:body_bytes_sent] = matches[:body_bytes_sent].to_i if matches[:body_bytes_sent]
48
+
49
+ matches
50
+ end
51
+
52
+ def format=(new_format)
53
+ @format = new_format
54
+
55
+ # Force regex generation
56
+ @fmt = nil
57
+ end
58
+
59
+ private
60
+ PIECES = {
61
+ remote_addr: "\\d+\\.\\d+\\.\\d+\\.\\d+",
62
+ remote_user: "[a-z_-]+",
63
+ time_local: "\\d{2}/[a-zA-Z]{3}/\\d{4}:\\d{2}:\\d{2}:\\d{2} [+-]\\d{4}",
64
+ request: ".+",
65
+ status: "\\d+",
66
+ body_bytes_sent: "\\d+",
67
+ http_referer: ".+",
68
+ http_user_agent: ".+",
69
+ connection_requests: "\\d+",
70
+ connection: "\\d+",
71
+ msec: "\\d+(?:\\.\\d+)?",
72
+ pipe: "[\\.p]",
73
+ request_length: "\\d+",
74
+ request_time: "\\d+(?:\\.\\d+)?",
75
+ time_iso8601: "\\d{4}-?\\d{2}-?\\d{2}T\\d{2}:?\\d{2}:?\\d{2}(?:[+-]\\d{2}:\\d{2}|[A-Z]{1,5})"
76
+ }
77
+
78
+ def format_regex
79
+ @fmt ||= begin
80
+ fmt = format.dup
81
+
82
+ # Escape reserved regex symbols
83
+ # Notice that we do not escape the $ symbol
84
+ escapes = %w( ( ) + ? * . \\ . | ^ ) + [ '[', ']' ]
85
+ escapes.each do |char|
86
+ fmt.gsub!(char, "\\\\#{char}")
87
+ end
88
+
89
+ puts fmt
90
+
91
+ # Replace $vars with regexes
92
+ PIECES.each do |find, replace|
93
+ fmt.gsub!("$#{find}", "(?<#{find}>#{replace})")
94
+ end
95
+
96
+ fmt.gsub!(/ /, "\\s+")
97
+ fmt
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,4 @@
1
+ class NginxLogParser
2
+ VERSION = '0.0.1'
3
+ end
4
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nginx_log_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Francesco Boffa
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple hello world gem
14
+ email: fra.boffa@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/nginx_log_parser.rb
20
+ - lib/nginx_log_parser/version.rb
21
+ homepage: https://github.com/aomega08/nginx_log_parser
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.5
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Hola!
45
+ test_files: []