Class: NginxUtils::Logrotate

Inherits:
Object
  • Object
show all
Defined in:
lib/nginx_utils.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logrotate

Returns a new instance of Logrotate.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nginx_utils.rb', line 10

def initialize(options={})
  # Debug
  #   debug: false => Not execute. Only output of logs to STDOUT.
  #   debug: true => Execute rotate logs Nginx. (default)
  if options[:debug]
    debug
  else
    @execute = true

    # Script log file
    #   not specified => /tmp/nginx_rotate.log (default)
    #   script_log: false => Not output of logs.
    #   script_log: "/path/to/nginx_rotate.log" => /path/to/nginx_rotate.log
    #   script_log: STDOUT => STDOUT
    set_logger options[:script_log]

    # Script log level
    #   not specified => Logger::DEBUG (default)
    #   log_level: [:fatal|:error|:info|:warn]
    set_log_level options[:log_level]
  end

  # Target logs
  # Log of rename target is "#{root_dir}/**/#{target_logs}"
  # Log of delete target is "#{root_dir}/**/#{target_logs}.*"
  # Default parameters are as follows:
  #   - root_dir => /usr/local/nginx
  #   - target_logs => *.log
  @root_dir = options[:root_dir] || "/usr/local/nginx"
  @target_logs = options[:target_logs] || "*.log"
  @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
  @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))

  # Rename prefix
  # Log of rename target to add the prefix.
  # File name of the renamed after: "#{filename}.#{prefix}"
  # Current time default. (YYYYmmddHHMMSS)
  @prefix = options[:prefix] || Time.now.strftime("%Y%m%d%H%M%S")

  # Retention period
  # Delete log last modification time has passed the retention period.
  # Specified unit of day.
  dates = options[:retention] || 90
  @retention = Time.now - (dates.to_i * 3600 * 24)

  # PID file of Nginx
  # The default is "#{root_dir}/logs/nginx.pid".
  @pid_file = options[:pid_file] || File.join(@root_dir, "logs", "nginx.pid")
end

Instance Attribute Details

#delete_logsObject

Returns the value of attribute delete_logs.



8
9
10
# File 'lib/nginx_utils.rb', line 8

def delete_logs
  @delete_logs
end

#loggerObject

Returns the value of attribute logger.



8
9
10
# File 'lib/nginx_utils.rb', line 8

def logger
  @logger
end

#rename_logsObject

Returns the value of attribute rename_logs.



8
9
10
# File 'lib/nginx_utils.rb', line 8

def rename_logs
  @rename_logs
end

Instance Method Details

#config(options = {}) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/nginx_utils.rb', line 60

def config(options={})
  # Debug
  unless options[:debug].nil?
    if options[:debug]
      debug
    else
      debug false
    end
  end

  # Script log file
  unless options[:script_log].nil?
    set_logger options[:script_log]
  end

  # Script log level
  unless options[:log_level].nil?
    set_log_level options[:log_level]
  end

  # Target logs
  reglog = false
  unless options[:root_dir].nil?
    @root_dir = options[:root_dir]
    reglob = true
  end

  unless options[:target_logs].nil?
    @target_logs = options[:target_logs]
    reglob = true
  end

  if reglob
    @rename_logs = Dir.glob(File.join(@root_dir, "**", @target_logs))
    @delete_logs = Dir.glob(File.join(@root_dir, "**", "#{@target_logs}.*"))
  end

  # Rename prefix
  unless options[:prefix].nil?
    @prefix = options[:prefix]
  end

  # Retention period
  unless options[:retention].nil?
    @retention = Time.now - (options[:retention].to_i * 3600 * 24)
  end

  # PID file of Nginx
  unless options[:pid_file].nil?
    @pid_file = options[:pid_file]
  end
end

#deleteObject



125
126
127
128
129
130
131
132
# File 'lib/nginx_utils.rb', line 125

def delete
  @delete_logs.each do |log|
    if File.stat(log).mtime < @retention
      File.unlink(log) if @execute
      @logger.debug "Delete log file: #{log}" if @logger
    end
  end
end

#executeObject



151
152
153
154
155
156
157
# File 'lib/nginx_utils.rb', line 151

def execute
  @logger.info "Nginx logrotate is started!" if @logger
  rename
  delete
  @logger.info "Nginx logrotate is successfully!" if @logger
  restart
end

#renameObject



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nginx_utils.rb', line 113

def rename
  @rename_logs.each do |log|
    after = "#{log}.#{@prefix}"
    if File.exists? after
      @logger.warn "File already exists: #{after}" if @logger
    else
      File.rename(log, after) if @execute
      @logger.debug "Rename log file: #{log} to #{after}" if @logger
    end
  end
end

#restartObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/nginx_utils.rb', line 134

def restart
  if File.exists? @pid_file
    cmd = "kill -USR1 `cat #{@pid_file}`"
    @logger.debug "Nginx restart command: #{cmd}" if @logger
    if @execute
      if system(cmd)
        @logger.info "Nginx restart is successfully!" if @logger
      else
        @logger.error "Nginx restart failed!" if @logger
        raise "Nginx restart failed!" if @logger == false
      end
    end
  else
    @logger.warn "Pid file is not found. not restart nginx. (#{@pid_file})" if @logger
  end
end