nginx_utils 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,187 @@
1
+ # coding: utf-8
2
+
3
+ module NginxUtils
4
+ class Logrotate
5
+ attr_accessor :logger, :rename_logs, :delete_logs
6
+
7
+ def initialize(options={})
8
+ # Debug
9
+ # debug: false => Not execute. Only output of logs to STDOUT.
10
+ # debug: true => Execute rotate logs Nginx. (default)
11
+ if options[:debug]
12
+ debug
13
+ else
14
+ @execute = true
15
+
16
+ # Script log file
17
+ # not specified => /tmp/nginx_rotate.log (default)
18
+ # script_log: false => Not output of logs.
19
+ # script_log: "/path/to/nginx_rotate.log" => /path/to/nginx_rotate.log
20
+ # script_log: STDOUT => STDOUT
21
+ set_logger options[:script_log]
22
+
23
+ # Script log level
24
+ # not specified => Logger::DEBUG (default)
25
+ # log_level: [:fatal|:error|:info|:warn]
26
+ set_log_level options[:log_level]
27
+ end
28
+
29
+ # Target logs
30
+ # Log of rename target is "#{root_dir}/**/#{target_logs}"
31
+ # Log of delete target is "#{root_dir}/**/#{target_logs}.*"
32
+ # Default parameters are as follows:
33
+ # - root_dir => /usr/local/nginx
34
+ # - target_logs => *.log
35
+ @root_dir = options[:root_dir] || "/usr/local/nginx"
36
+ @target_logs = options[:target_logs] || "*.log"
37
+ @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
38
+ @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
39
+
40
+ # Rename prefix
41
+ # Log of rename target to add the prefix.
42
+ # File name of the renamed after: "#{filename}.#{prefix}"
43
+ # Current time default. (YYYYmmddHHMMSS)
44
+ @prefix = options[:prefix] || Time.now.strftime("%Y%m%d%H%M%S")
45
+
46
+ # Retention period
47
+ # Delete log last modification time has passed the retention period.
48
+ # Specified unit of day.
49
+ dates = options[:retention] || 90
50
+ @retention = Time.now - (dates.to_i * 3600 * 24)
51
+
52
+ # PID file of Nginx
53
+ # The default is "#{root_dir}/logs/nginx.pid".
54
+ @pid_file = options[:pid_file] || File.join(@root_dir, "logs", "nginx.pid")
55
+ end
56
+
57
+ def config(options={})
58
+ # Debug
59
+ unless options[:debug].nil?
60
+ if options[:debug]
61
+ debug
62
+ else
63
+ debug false
64
+ end
65
+ end
66
+
67
+ # Script log file
68
+ unless options[:script_log].nil?
69
+ set_logger options[:script_log]
70
+ end
71
+
72
+ # Script log level
73
+ unless options[:log_level].nil?
74
+ set_log_level options[:log_level]
75
+ end
76
+
77
+ # Target logs
78
+ reglog = false
79
+ unless options[:root_dir].nil?
80
+ @root_dir = options[:root_dir]
81
+ reglob = true
82
+ end
83
+
84
+ unless options[:target_logs].nil?
85
+ @target_logs = options[:target_logs]
86
+ reglob = true
87
+ end
88
+
89
+ if reglob
90
+ @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
91
+ @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
92
+ end
93
+
94
+ # Rename prefix
95
+ unless options[:prefix].nil?
96
+ @prefix = options[:prefix]
97
+ end
98
+
99
+ # Retention period
100
+ unless options[:retention].nil?
101
+ @retention = Time.now - (options[:retention].to_i * 3600 * 24)
102
+ end
103
+
104
+ # PID file of Nginx
105
+ unless options[:pid_file].nil?
106
+ @pid_file = options[:pid_file]
107
+ end
108
+ end
109
+
110
+ def rename
111
+ @rename_logs.each do |log|
112
+ after = "#{log}.#{@prefix}"
113
+ if File.exists? after
114
+ @logger.warn "File already exists: #{after}" if @logger
115
+ else
116
+ File.rename(log, after) if @execute
117
+ @logger.debug "Rename log file: #{log} to #{after}" if @logger
118
+ end
119
+ end
120
+ end
121
+
122
+ def delete
123
+ @delete_logs.each do |log|
124
+ if File.stat(log).mtime < @retention
125
+ File.unlink(log) if @execute
126
+ @logger.debug "Delete log file: #{log}" if @logger
127
+ end
128
+ end
129
+ end
130
+
131
+ def restart
132
+ if File.exists? @pid_file
133
+ cmd = "kill -USR1 `cat #{@pid_file}`"
134
+ @logger.debug "Nginx restart command: #{cmd}" if @logger
135
+ if @execute
136
+ if system(cmd)
137
+ @logger.info "Nginx restart is successfully!" if @logger
138
+ else
139
+ @logger.error "Nginx restart failed!" if @logger
140
+ raise "Nginx restart failed!" if @logger == false
141
+ end
142
+ end
143
+ else
144
+ @logger.warn "Pid file is not found. not restart nginx. (#{@pid_file})" if @logger
145
+ end
146
+ end
147
+
148
+ def execute
149
+ @logger.info "Nginx logrotate is started!" if @logger
150
+ rename
151
+ delete
152
+ @logger.info "Nginx logrotate is successfully!" if @logger
153
+ restart
154
+ end
155
+
156
+ private
157
+ def debug(set=true)
158
+ if set
159
+ @execute = false
160
+ @logger = Logger.new(STDOUT)
161
+ else
162
+ @execute = true
163
+ end
164
+ end
165
+
166
+ def set_logger(log)
167
+ case log
168
+ when nil then @logger = Logger.new("/tmp/nginx_rotate.log")
169
+ when false then @logger = false
170
+ else @logger = Logger.new(log)
171
+ end
172
+ end
173
+
174
+ def set_log_level(level)
175
+ if @logger
176
+ case level
177
+ when :fatal then @logger.level = Logger::FATAL
178
+ when :error then @logger.level = Logger::ERROR
179
+ when :warn then @logger.level = Logger::WARN
180
+ when :info then @logger.level = Logger::INFO
181
+ when :debug then @logger.level = Logger::DEBUG
182
+ else @logger.level = Logger::DEBUG
183
+ end
184
+ end
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+
3
+ module NginxUtils
4
+ module Status
5
+ class << self
6
+ def get(options={host: "localhost", port: 80, path: "/nginx_status"})
7
+ begin
8
+ req = Net::HTTP::Get.new(options[:path])
9
+ res = Net::HTTP.start(options[:host], options[:port]){|http| http.request(req)}
10
+ status = res.body.split("\n")
11
+ server = status[2].split.map{|i| i.to_i}
12
+ rww = status[3].split.select{|i| /^[0-9]+$/ =~ i}.map{|i| i.to_i}
13
+ {
14
+ active_connection: status[0].split(":").last.gsub(/\s/, "").to_i,
15
+ accepts: server[0],
16
+ handled: server[1],
17
+ requests: server[2],
18
+ reading: rww[0],
19
+ writing: rww[1],
20
+ waiting: rww[2]
21
+ }
22
+ rescue
23
+ raise "Nginx status get failed"
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module NginxUtils
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/nginx_utils.rb CHANGED
@@ -1,190 +1,8 @@
1
1
  # coding: utf-8
2
2
 
3
- require "nginx_utils/version"
4
3
  require "logger"
4
+ require "net/http"
5
5
 
6
- module NginxUtils
7
- class Logrotate
8
- attr_accessor :logger, :rename_logs, :delete_logs
9
-
10
- def initialize(options={})
11
- # Debug
12
- # debug: false => Not execute. Only output of logs to STDOUT.
13
- # debug: true => Execute rotate logs Nginx. (default)
14
- if options[:debug]
15
- debug
16
- else
17
- @execute = true
18
-
19
- # Script log file
20
- # not specified => /tmp/nginx_rotate.log (default)
21
- # script_log: false => Not output of logs.
22
- # script_log: "/path/to/nginx_rotate.log" => /path/to/nginx_rotate.log
23
- # script_log: STDOUT => STDOUT
24
- set_logger options[:script_log]
25
-
26
- # Script log level
27
- # not specified => Logger::DEBUG (default)
28
- # log_level: [:fatal|:error|:info|:warn]
29
- set_log_level options[:log_level]
30
- end
31
-
32
- # Target logs
33
- # Log of rename target is "#{root_dir}/**/#{target_logs}"
34
- # Log of delete target is "#{root_dir}/**/#{target_logs}.*"
35
- # Default parameters are as follows:
36
- # - root_dir => /usr/local/nginx
37
- # - target_logs => *.log
38
- @root_dir = options[:root_dir] || "/usr/local/nginx"
39
- @target_logs = options[:target_logs] || "*.log"
40
- @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
41
- @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
42
-
43
- # Rename prefix
44
- # Log of rename target to add the prefix.
45
- # File name of the renamed after: "#{filename}.#{prefix}"
46
- # Current time default. (YYYYmmddHHMMSS)
47
- @prefix = options[:prefix] || Time.now.strftime("%Y%m%d%H%M%S")
48
-
49
- # Retention period
50
- # Delete log last modification time has passed the retention period.
51
- # Specified unit of day.
52
- dates = options[:retention] || 90
53
- @retention = Time.now - (dates.to_i * 3600 * 24)
54
-
55
- # PID file of Nginx
56
- # The default is "#{root_dir}/logs/nginx.pid".
57
- @pid_file = options[:pid_file] || File.join(@root_dir, "logs", "nginx.pid")
58
- end
59
-
60
- def config(options={})
61
- # Debug
62
- unless options[:debug].nil?
63
- if options[:debug]
64
- debug
65
- else
66
- debug false
67
- end
68
- end
69
-
70
- # Script log file
71
- unless options[:script_log].nil?
72
- set_logger options[:script_log]
73
- end
74
-
75
- # Script log level
76
- unless options[:log_level].nil?
77
- set_log_level options[:log_level]
78
- end
79
-
80
- # Target logs
81
- reglog = false
82
- unless options[:root_dir].nil?
83
- @root_dir = options[:root_dir]
84
- reglob = true
85
- end
86
-
87
- unless options[:target_logs].nil?
88
- @target_logs = options[:target_logs]
89
- reglob = true
90
- end
91
-
92
- if reglob
93
- @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
94
- @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
95
- end
96
-
97
- # Rename prefix
98
- unless options[:prefix].nil?
99
- @prefix = options[:prefix]
100
- end
101
-
102
- # Retention period
103
- unless options[:retention].nil?
104
- @retention = Time.now - (options[:retention].to_i * 3600 * 24)
105
- end
106
-
107
- # PID file of Nginx
108
- unless options[:pid_file].nil?
109
- @pid_file = options[:pid_file]
110
- end
111
- end
112
-
113
- def rename
114
- @rename_logs.each do |log|
115
- after = "#{log}.#{@prefix}"
116
- if File.exists? after
117
- @logger.warn "File already exists: #{after}" if @logger
118
- else
119
- File.rename(log, after) if @execute
120
- @logger.debug "Rename log file: #{log} to #{after}" if @logger
121
- end
122
- end
123
- end
124
-
125
- def delete
126
- @delete_logs.each do |log|
127
- if File.stat(log).mtime < @retention
128
- File.unlink(log) if @execute
129
- @logger.debug "Delete log file: #{log}" if @logger
130
- end
131
- end
132
- end
133
-
134
- def restart
135
- if File.exists? @pid_file
136
- cmd = "kill -USR1 `cat #{@pid_file}`"
137
- @logger.debug "Nginx restart command: #{cmd}" if @logger
138
- if @execute
139
- if system(cmd)
140
- @logger.info "Nginx restart is successfully!" if @logger
141
- else
142
- @logger.error "Nginx restart failed!" if @logger
143
- raise "Nginx restart failed!" if @logger == false
144
- end
145
- end
146
- else
147
- @logger.warn "Pid file is not found. not restart nginx. (#{@pid_file})" if @logger
148
- end
149
- end
150
-
151
- def execute
152
- @logger.info "Nginx logrotate is started!" if @logger
153
- rename
154
- delete
155
- @logger.info "Nginx logrotate is successfully!" if @logger
156
- restart
157
- end
158
-
159
- private
160
- def debug(set=true)
161
- if set
162
- @execute = false
163
- @logger = Logger.new(STDOUT)
164
- else
165
- @execute = true
166
- end
167
- end
168
-
169
- def set_logger(log)
170
- case log
171
- when nil then @logger = Logger.new("/tmp/nginx_rotate.log")
172
- when false then @logger = false
173
- else @logger = Logger.new(log)
174
- end
175
- end
176
-
177
- def set_log_level(level)
178
- if @logger
179
- case level
180
- when :fatal then @logger.level = Logger::FATAL
181
- when :error then @logger.level = Logger::ERROR
182
- when :warn then @logger.level = Logger::WARN
183
- when :info then @logger.level = Logger::INFO
184
- when :debug then @logger.level = Logger::DEBUG
185
- else @logger.level = Logger::DEBUG
186
- end
187
- end
188
- end
189
- end
190
- end
6
+ require "nginx_utils/version"
7
+ require "nginx_utils/logrotate"
8
+ require "nginx_utils/status"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ require "spec_helper"
4
+
5
+ describe NginxUtils do
6
+ describe "Status" do
7
+ describe ".get" do
8
+ it "should get status" do
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}
11
+ response = OpenStruct.new(body: body)
12
+ Net::HTTP.should_receive(:start).and_return(response)
13
+ expect(NginxUtils::Status.get).to eq(status)
14
+ end
15
+
16
+ it "should generate an exception if status get fails" do
17
+ res = OpenStruct.new(body: "invalid content")
18
+ Net::HTTP.should_receive(:start).and_return(res)
19
+ expect(proc{NginxUtils::Status.get}).to raise_error("Nginx status get failed")
20
+ end
21
+ end
22
+ end
23
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  require "fakefs/spec_helpers"
2
+ require "ostruct"
2
3
  require "nginx_utils"
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.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -89,9 +89,12 @@ files:
89
89
  - README.md
90
90
  - Rakefile
91
91
  - lib/nginx_utils.rb
92
+ - lib/nginx_utils/logrotate.rb
93
+ - lib/nginx_utils/status.rb
92
94
  - lib/nginx_utils/version.rb
93
95
  - nginx_utils.gemspec
94
- - spec/lib/nginx_utils_spec.rb
96
+ - spec/nginx_utils/logrotate_spec.rb
97
+ - spec/nginx_utils/status_spec.rb
95
98
  - spec/spec_helper.rb
96
99
  homepage: https://github.com/i2bskn/nginx_utils
97
100
  licenses:
@@ -108,7 +111,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
111
  version: '0'
109
112
  segments:
110
113
  - 0
111
- hash: -2674447921800970947
114
+ hash: 17147575815869233
112
115
  required_rubygems_version: !ruby/object:Gem::Requirement
113
116
  none: false
114
117
  requirements:
@@ -117,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
120
  version: '0'
118
121
  segments:
119
122
  - 0
120
- hash: -2674447921800970947
123
+ hash: 17147575815869233
121
124
  requirements: []
122
125
  rubyforge_project:
123
126
  rubygems_version: 1.8.23
@@ -125,5 +128,6 @@ signing_key:
125
128
  specification_version: 3
126
129
  summary: Nginx utilities
127
130
  test_files:
128
- - spec/lib/nginx_utils_spec.rb
131
+ - spec/nginx_utils/logrotate_spec.rb
132
+ - spec/nginx_utils/status_spec.rb
129
133
  - spec/spec_helper.rb