nginx_utils 0.0.4 → 0.0.5

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.
data/README.md CHANGED
@@ -8,7 +8,9 @@ Nginx utilities.
8
8
 
9
9
  Add this line to your application's Gemfile:
10
10
 
11
- gem 'nginx_utils'
11
+ ```ruby
12
+ gem 'nginx_utils'
13
+ ```
12
14
 
13
15
  And then execute:
14
16
 
@@ -20,13 +22,34 @@ Or install it yourself as:
20
22
 
21
23
  ## Usage
22
24
 
23
- ```
25
+ From console:
26
+
27
+ $ nginx_logrotate -h
28
+ Usage: nginx_logrotate [options]
29
+ -d, --[no-]debug Debug mode. Run only log output to STDOUT.
30
+ --script_log=VAL Log file for script.
31
+ --log_level=VAL Log level of script log.
32
+ --root_dir=VAL Root directory of Nginx.
33
+ --target_logs=VAL Specify logs of target.
34
+ --retention=VAL Specify in days the retention period of log.
35
+ --pid_file=VAL PID file of Nginx
36
+ $ nginx_status -h
37
+ Usage: nginx_status [options]
38
+ --only-value The output only the value.
39
+
40
+ From ruby:
41
+
42
+ ```ruby
24
43
  require 'nginx_utils'
25
44
  ```
26
45
 
27
- Logrotate:
46
+ ### Logrotate
28
47
 
29
- ```
48
+ Logs of rename target: `Dir.glob "#{root_dir}/**/#{target_logs}"`
49
+ Logs of delete target: `Dir.glob "#{root_dir}/**/#{target_logs}.*"`
50
+
51
+ ```ruby
52
+ # Following parameters are default.
30
53
  params = {
31
54
  debug: false,
32
55
  script_log: "/tmp/nginx_rotate.log",
@@ -39,28 +62,78 @@ params = {
39
62
  }
40
63
 
41
64
  rotate = NginxUtils::Logrotate.new(params)
65
+ # == Configure
66
+ # rotate.config log_level: :worn
67
+
68
+ # == Accessor
69
+ # rotate.logger.formatter = proc { |severity, datetime, progname, msg|
70
+ # "#{datetime}: #{msg}\n"
71
+ # }
72
+ # rotate.rename_logs << add_rename_log
73
+ # rotate.delete_logs << add_delete_log
74
+
42
75
  rotate.execute
43
76
  ```
44
77
 
45
- Status:
78
+ Options that can be specified:
46
79
 
47
- ```
48
- p NginxUtils::Status.get # => {active_connection: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
49
- ```
80
+ * `:debug` => `true` or `false`. If `:debug` is true, it is not execute.
81
+ * `:script_log` => `"/path/to/nginx_rotate.log"`. If `:script` is false, do not output logs.
82
+ * `:log_level` => `:debug` or `:info` or `:warn` or `:error` or `:fatal`.
83
+ * `:root_dir` => `"/path/to/nginx"`. Root directory of Nginx.
84
+ * `:target_logs` => `"*.log"`. Specify logs of target.
85
+ * `:prefix` => `Time.now.strftime("%Y%m%d%H%M%S")`. Prefix use to rename.
86
+ * `:retention` => `90`. Specify in days the retention period of log.
87
+ * `:pid_file` => `"/path/to/nginx.pid"`. Use to restart Nginx.
50
88
 
51
- Logreader:
89
+ ### Status
52
90
 
91
+ Require **HttpStubStatusModule**.
92
+
93
+ ```ruby
94
+ # http://localhost/nginx_status
95
+ NginxUtils::Status.get # => {active_connection: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
96
+
97
+ # Apache like
98
+ # http://example.com/server-status
99
+ NginxUtils::Status.get(host: "example.com", path: "/server-status")
53
100
  ```
54
- reader = NginxUtils::Logreader.new("/path/to/nginx/logs/access.log")
101
+
102
+ ### Logreader
103
+
104
+ LTSV:
105
+
106
+ The default format is `:ltsv`.
107
+
108
+ ```ruby
109
+ log_file = "/path/to/nginx/logs/access.log.ltsv"
110
+ reader = NginxUtils::Logreader.new(log_file)
55
111
  reader.each do |line|
56
112
  p line # => {time: "2013-05-19T08:13:14+00:00", host: "192.168.1.10", ...}
57
113
  end
58
114
  ```
59
115
 
116
+ Combined:
117
+
118
+ ```ruby
119
+ log_file = "/path/to/nginx/logs/access.log.combined"
120
+ reader = NginxUtils::Logreader.new(log_file, format: :combined)
121
+ reader.each {|line| p line} # => {:remote_addr=>"x.x.x.x", :remote_user=>"-", :time_local=>"19/May/2013:23:14:04 +0900", :request=>"GET / HTTP/1.1", :status=>"200", :body_bytes_sent=>"564", :http_referer=>"-", :http_user_agent=>"-"}
122
+ ```
123
+
124
+ Custom:
125
+
126
+ ```ruby
127
+ log_file = "/path/to/nginx/logs/access.log.combined"
128
+ parser = /\[(.*)\]\s"(.*?)"/
129
+ reader = NginxUtils::Logreader.new(log_file, parser: parser)
130
+ reader.each {|line| p line.first} #=> ["19/May/2013:23:13:52 +0900", "GET / HTTP/1.1"]
131
+ ```
132
+
60
133
  Options that can be specified:
61
134
 
62
- * :format => :ltsv or :combined
63
- * :parser => Parse with scan method. Specified in Regexp.
135
+ * `:format` => `:ltsv` or `:combined`. If parser is specified, format is automatically `:custom`.
136
+ * `:parser` => Parse with `String#scan`. Specified in Regexp.
64
137
 
65
138
  ## Contributing
66
139
 
@@ -68,4 +141,4 @@ Options that can be specified:
68
141
  2. Create your feature branch (`git checkout -b my-new-feature`)
69
142
  3. Commit your changes (`git commit -am 'Add some feature'`)
70
143
  4. Push to the branch (`git push origin my-new-feature`)
71
- 5. Create new Pull Request
144
+ 5. Create new Pull Request
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "optparse"
5
+
6
+ begin
7
+ require "nginx_utils"
8
+ rescue LoadError => e
9
+ puts e
10
+ exit 1
11
+ end
12
+
13
+ # Option parse
14
+ options = {}
15
+
16
+ opt = OptionParser.new
17
+ Version = "0.0.1"
18
+ opt.on("-d", "--[no-]debug", "Debug mode. Run only log output to STDOUT.") {|v| options[:debug] = v}
19
+ opt.on("--script_log=VAL", "Log file for script.") {|v| options[:script_log] = v}
20
+ opt.on("--log_level=VAL", "Log level of script log.") {|v| options[:log_level] = v.to_sym}
21
+ opt.on("--root_dir=VAL", "Root directory of Nginx.") {|v| options[:root_dir] = v}
22
+ opt.on("--target_logs=VAL", "Specify logs of target.") {|v| options[:target_logs] = v}
23
+ opt.on("--retention=VAL", "Specify in days the retention period of log.") {|v| options[:retention] = v.to_i}
24
+ opt.on("--pid_file=VAL", "PID file of Nginx") {|v| options[:pid_file] = v}
25
+
26
+ opt.parse! ARGV
27
+
28
+ rotate = NginxUtils::Logrotate.new(options)
29
+ rotate.execute
data/bin/nginx_status ADDED
@@ -0,0 +1,35 @@
1
+ #!/usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "optparse"
5
+
6
+ begin
7
+ require "nginx_utils"
8
+ rescue LoadError => e
9
+ puts e
10
+ exit 1
11
+ end
12
+
13
+ # Option parse
14
+ options = {}
15
+
16
+ opt = OptionParser.new
17
+ Version = "0.0.1"
18
+ opt.on("--only-value", "The output only the value.") {|v| options[:only_value] = v}
19
+
20
+ opt.parse! ARGV
21
+
22
+ if ARGV.size > 0
23
+ host = ARGV.shift
24
+ result = NginxUtils::Status.get host: host
25
+ else
26
+ result = NginxUtils::Status.get
27
+ end
28
+
29
+ if options[:only_value]
30
+ puts result.values.join("\t")
31
+ else
32
+ puts "Active Connections: #{result[:active_connection]}"
33
+ puts "Accepts: #{result[:accepts]} Handled: #{result[:handled]} Requests: #{result[:requests]}"
34
+ puts "Reading: #{result[:reading]} Writing: #{result[:writing]} Waiting: #{result[:waiting]}"
35
+ end
@@ -3,15 +3,18 @@
3
3
  module NginxUtils
4
4
  module Status
5
5
  class << self
6
- def get(options={host: "localhost", port: 80, path: "/nginx_status"})
6
+ def get(options={})
7
+ host = options[:host] || "localhost"
8
+ port = options[:port] || 80
9
+ path = options[:path] || "/nginx_status"
7
10
  begin
8
- req = Net::HTTP::Get.new(options[:path])
9
- res = Net::HTTP.start(options[:host], options[:port]){|http| http.request(req)}
11
+ req = Net::HTTP::Get.new(path)
12
+ res = Net::HTTP.start(host, port){|http| http.request(req)}
10
13
  status = res.body.split("\n")
11
14
  server = status[2].split.map{|i| i.to_i}
12
15
  rww = status[3].split.select{|i| /^[0-9]+$/ =~ i}.map{|i| i.to_i}
13
16
  {
14
- active_connection: status[0].split(":").last.gsub(/\s/, "").to_i,
17
+ active_connections: status[0].split(":").last.gsub(/\s/, "").to_i,
15
18
  accepts: server[0],
16
19
  handled: server[1],
17
20
  requests: server[2],
@@ -1,3 +1,3 @@
1
1
  module NginxUtils
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -7,7 +7,7 @@ describe NginxUtils do
7
7
  describe ".get" do
8
8
  it "should get status" do
9
9
  body = "Active connections: 1 \nserver accepts handled requests\n 4 5 51 \nReading: 1 Writing: 3 Waiting: 2 \n"
10
- status = {active_connection: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
10
+ status = {active_connections: 1, accepts: 4, handled: 5, requests: 51, reading: 1, writing: 3, waiting: 2}
11
11
  response = double("http response mock", body: body)
12
12
  Net::HTTP.should_receive(:start).and_return(response)
13
13
  expect(NginxUtils::Status.get).to eq(status)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nginx_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-20 00:00:00.000000000 Z
12
+ date: 2013-05-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -78,7 +78,9 @@ dependencies:
78
78
  description: The various utilities on nginx
79
79
  email:
80
80
  - i2bskn@gmail.com
81
- executables: []
81
+ executables:
82
+ - nginx_logrotate
83
+ - nginx_status
82
84
  extensions: []
83
85
  extra_rdoc_files: []
84
86
  files:
@@ -88,6 +90,8 @@ files:
88
90
  - LICENSE.txt
89
91
  - README.md
90
92
  - Rakefile
93
+ - bin/nginx_logrotate
94
+ - bin/nginx_status
91
95
  - lib/nginx_utils.rb
92
96
  - lib/nginx_utils/logreader.rb
93
97
  - lib/nginx_utils/logrotate.rb
@@ -113,7 +117,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
117
  version: '0'
114
118
  segments:
115
119
  - 0
116
- hash: -10429621290292328
120
+ hash: -837549919504176739
117
121
  required_rubygems_version: !ruby/object:Gem::Requirement
118
122
  none: false
119
123
  requirements:
@@ -122,7 +126,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
126
  version: '0'
123
127
  segments:
124
128
  - 0
125
- hash: -10429621290292328
129
+ hash: -837549919504176739
126
130
  requirements: []
127
131
  rubyforge_project:
128
132
  rubygems_version: 1.8.25