nginxtop 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06769fe40c9c272f91056f96f40b0da3b2ea6cb6
4
+ data.tar.gz: 74f4f583dbee58710692a2bc2ec3717e0c227980
5
+ SHA512:
6
+ metadata.gz: e221640f46a3da378f682236fa57c1af593649a3e642f781e203fce3719bf02103a96bda6830dd46fb70797d9739046ecf4172d9841afd422d15c78c7e07e83c
7
+ data.tar.gz: df3b5beb90954cbe734030170bf6d8182912e6e1ec47b0f39f55c61ef5a907dcda0fb8e764073684e8fe25fa4886d7194644f7a362da4bce0e7013d22f4f8c23
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in nginxtop.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Ashish Bista
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Nginxtop
2
+
3
+ NGINX live log parsing and monitoring tool.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'nginxtop'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install nginxtop
20
+
21
+ ## Usage
22
+
23
+ Run `nginxtop`.
24
+
25
+ For more options:
26
+
27
+ `nginxtop --help`
28
+
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ashishbista/nginxtop.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/nginxtop ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "nginxtop"
5
+ require 'optparse'
6
+
7
+ options = {file: '/var/log/nginx/access.log', no_watch: false}
8
+
9
+ parser = OptionParser.new do|opts|
10
+ opts.banner = "Usage: nginxtop [options]"
11
+ opts.on('-f', '--file file', 'NGINX log file') do |name|
12
+ options[:file] = file
13
+ end
14
+
15
+ opts.on('-n', '--no-watch', "Don't watch log forever") do
16
+ options[:no_watch] = true
17
+ end
18
+
19
+ opts.on('-h', '--help', 'Displays help') do
20
+ puts opts
21
+ exit
22
+ end
23
+ end
24
+
25
+ parser.parse!
26
+
27
+ NginxTop::Listener.new(options).listen
28
+
data/dev/null ADDED
File without changes
data/lib/nginxtop.rb ADDED
@@ -0,0 +1,26 @@
1
+ require "nginxtop/version"
2
+ require "nginxtop/listener"
3
+ require "nginxtop/output"
4
+ require "nginxtop/parser"
5
+ require "nginxtop/request"
6
+ require "nginxtop/printer"
7
+ require 'io/console'
8
+
9
+ module NginxTop
10
+ REQUESTS = []
11
+
12
+ def self.count(status)
13
+ REQUESTS.select{ |r| r.status_verb == status }.count
14
+ end
15
+
16
+ def self.bar(status)
17
+ n = (IO.console.winsize.last * 0.25 * (( 100 * count(status)/REQUESTS.count))/100).to_i rescue 0
18
+ "|" * n
19
+ end
20
+
21
+ def self.uptime
22
+ time_diff = Time.now - Listener::START_TIME
23
+ Time.at(time_diff.to_i.abs).utc.strftime "%H:%M:%S"
24
+ end
25
+
26
+ end
@@ -0,0 +1,51 @@
1
+ module NginxTop
2
+ class Listener
3
+
4
+ START_TIME = Time.now
5
+
6
+ def initialize(options)
7
+ @log_file = options[:file]
8
+ @no_watch = options[:no_watch]
9
+ install_signal_handlers
10
+ end
11
+
12
+ def listen
13
+
14
+ File.readlines(@log_file).each do |line|
15
+ Parser.new.parse(line.chomp)
16
+ end
17
+
18
+ Output.create
19
+
20
+ file = File.open(@log_file)
21
+ file.seek(0,IO::SEEK_END)
22
+ thread1 = Thread.new do
23
+ loop do
24
+ select([file])
25
+ line = file.gets
26
+ Parser.new.parse(line.chomp) if line
27
+ end
28
+ end
29
+
30
+ thread2 = Thread.new do
31
+ loop do
32
+ sleep 1
33
+ Output.create
34
+ end
35
+ end
36
+ unless @no_watch
37
+ thread1.join
38
+ thread2.join
39
+ end
40
+
41
+ end
42
+
43
+ def install_signal_handlers
44
+ Signal.trap('INT') do
45
+ puts 'Caught interrupt! Stopping parsing...'
46
+ exit 130
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,25 @@
1
+ require 'pry'
2
+ module NginxTop
3
+ class Output
4
+ def self.create
5
+
6
+ output = Printer.stream
7
+
8
+ output << sprintf("%31s \n", Printer.bold(Printer.white("NGINX TOP")))
9
+
10
+ output << sprintf(" %5s %s %-50s %s %d \t %20s %s\n", Printer.cyan("2XX"), Printer.white("["), Printer.green(NginxTop.bar(:success)), Printer.white("]"), NginxTop.count(:success), Printer.cyan("Running for:"), Printer.white(NginxTop.uptime))
11
+ output << sprintf(" %5s %s %-50s %s %d \t %20s %d\n", Printer.cyan("3XX"), Printer.white("["), Printer.gray(NginxTop.bar(:redirection)), Printer.white("]"), NginxTop.count(:redirection), Printer.cyan("Total requests:"), NginxTop::REQUESTS.count)
12
+ output << sprintf(" %5s %s %-50s %s %d\n", Printer.cyan("4XX"), Printer.white("["), Printer.yellow(NginxTop.bar(:client_error)), Printer.white("]"), NginxTop.count(:client_error))
13
+ output << sprintf(" %5s %s %-50s %s %d\n", Printer.cyan("5XX"), Printer.white("["), Printer.red(NginxTop.bar(:server_error)), Printer.white("]"), NginxTop.count(:server_error))
14
+
15
+ output << sprintf("\n%31s \n", Printer.bold(Printer.white("TOP PAGES")))
16
+
17
+ NginxTop::REQUESTS.group_by(&:path).sort_by(&:count)[0..15].each do |r|
18
+ output << sprintf(" %-58s %s\n", Printer.light_gray(r.first), Printer.white(r.last.count))
19
+ end
20
+
21
+ puts output
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module NginxTop
2
+ class Parser
3
+ def parse(log)
4
+ NginxTop::REQUESTS << Request.new(log)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ module NginxTop
2
+ class Printer
3
+ COLORS = {
4
+ red: "\e[31m",
5
+ yellow: "\e[33m",
6
+ blue: "\e[34m",
7
+ green: "\e[32m",
8
+ white: "\e[97m",
9
+ bold: "\e[1m",
10
+ dim: "\e[2m",
11
+ gray: "\e[90m",
12
+ cyan: "\e[36m",
13
+ light_green: "\e[92m",
14
+ light_gray: "\e[37m"
15
+ }
16
+
17
+ class << self
18
+ def stream
19
+ sprintf "\033c"
20
+ end
21
+
22
+ COLORS.each_pair do |k, v|
23
+ define_method k do |str|
24
+ v + str.to_s + "\e[0m"
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ module NginxTop
2
+ class Request
3
+ REGEX = /((?-mix:(?-mix:(?:(?:[a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*(?:[A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9]))|(?-mix:(?-mix:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?-mix:(?-mix:(?:[0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4})|(?-mix:(?:(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::(?:(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?))|(?-mix:(?:(?:[0-9A-Fa-f]{1,4}:){6})(?-mix:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))|(?-mix:(?:(?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)::(?:(?:[0-9A-Fa-f]{1,4}:)*)(?-mix:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))))))\ ([\w-]+)\ (\/\S*|-)\ \[((?-mix:(?-mix:\d{2}\/(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}:\d{2}:\d{2}:\d{2}\ (?:[+-]\d{4}|[A-Z]{3,4}))|(?-mix:\d{2}\/(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\/\d{4}\ \d{2}:\d{2}:\d{2})))?\]\ "([A-Z]+) (\S+) HTTP\/(\d+(?:\.\d+)*)"\ (\d{3})\ (\d+|-)/
4
+
5
+ DIRECTIVES = [:remote_host, :remote_logname, :user, :timestamp, :http_method, :path, :http_version, :http_status, :bytes_sent]
6
+ attr_accessor *DIRECTIVES
7
+ def initialize(log)
8
+ @log = log
9
+ log.scan(REGEX).flatten.each_with_index do |value, index|
10
+ instance_variable_set "@#{DIRECTIVES[index]}".to_sym, value
11
+ end
12
+ end
13
+
14
+ def status_verb
15
+ status = http_status.to_i
16
+ if status.between?(200, 299)
17
+ :success
18
+ elsif status.between?(300, 399)
19
+ :redirection
20
+ elsif status.between?(400, 499)
21
+ :client_error
22
+ elsif status.between?(500, 599)
23
+ :server_error
24
+ else
25
+ :unknown
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module NginxTop
2
+ VERSION = "0.1.1"
3
+ end
data/nginxtop.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'nginxtop/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nginxtop"
8
+ spec.version = NginxTop::VERSION
9
+ spec.authors = ["Ashish Bista"]
10
+ spec.email = ["b@ashishbista.com"]
11
+
12
+ spec.summary = %q{NGIN Top: NGINX Access Log Parser and Monitoring Tool.}
13
+ spec.description = %q{Parse and monitor NGINX incoming requests based on its access log.}
14
+ spec.homepage = "https://github.com/ashishbista/nginxtop"
15
+ spec.license = "MIT"
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "pry", "~> 0.10.4"
25
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nginxtop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ashish Bista
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.10.4
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.10.4
69
+ description: Parse and monitor NGINX incoming requests based on its access log.
70
+ email:
71
+ - b@ashishbista.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/nginxtop
84
+ - dev/null
85
+ - lib/nginxtop.rb
86
+ - lib/nginxtop/listener.rb
87
+ - lib/nginxtop/output.rb
88
+ - lib/nginxtop/parser.rb
89
+ - lib/nginxtop/printer.rb
90
+ - lib/nginxtop/request.rb
91
+ - lib/nginxtop/version.rb
92
+ - nginxtop.gemspec
93
+ homepage: https://github.com/ashishbista/nginxtop
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.5.1
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: 'NGIN Top: NGINX Access Log Parser and Monitoring Tool.'
117
+ test_files: []