Module: Locd::Newsyslog
- Includes:
- NRSER::Log::Mixin
- Defined in:
- lib/locd/newsyslog.rb
Overview
Use newsyslog to rotate Agent log files.
Defined Under Namespace
Classes: Entry
Class Method Summary collapse
- .log_dir ⇒ Object
- .log_to_file(&block) ⇒ Object
-
.run(agent, tmp_conf_dir: self.tmp_conf_dir, keep_conf_files: false) ⇒ Cmds::Result?
Run
newsyslogfor an agent to rotate it's log files (if present and needed). -
.run_all(tmp_conf_dir: self.tmp_conf_dir, trim_logs: true) ⇒ Hash<Locd::Agent, Cmds::Result?>
Call Newsyslog.run for each agent.
- .tmp_conf_dir ⇒ Object
- .trim_logs(keep_days: 7) ⇒ Object
Class Method Details
.log_dir ⇒ Object
327 328 329 |
# File 'lib/locd/newsyslog.rb', line 327 def self.log_dir @log_dir ||= Locd.config.log_dir.join( Locd::Agent::RotateLogs.label ) end |
.log_to_file(&block) ⇒ Object
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'lib/locd/newsyslog.rb', line 337 def self.log_to_file &block time = Time.now.iso8601 date = time.split( 'T', 2 )[0] # Like # # ~/.locd/log/com.nrser.locd.rotate-logs/2018-02-14/2018-02-14T03:46:57+08:00.log # path = self.log_dir / date / "#{ time }.log" FileUtils.mkdir_p( path.dirname ) unless path.dirname.exist? appender = SemanticLogger.add_appender \ file_name: path.to_s begin result = block.call ensure SemanticLogger.remove_appender appender end end |
.run(agent, tmp_conf_dir: self.tmp_conf_dir, keep_conf_files: false) ⇒ Cmds::Result?
Run newsyslog for an agent to rotate it's log files (if present and
needed).
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/locd/newsyslog.rb', line 207 def self.run agent, tmp_conf_dir: self.tmp_conf_dir, keep_conf_files: false logger.debug "Calling {.run_for} agent #{ agent.label }...", agent: agent, tmp_conf_dir: tmp_conf_dir # Make sure `tmp_conf_dir` is a {Pathname} tmp_conf_dir = tmp_conf_dir.to_pn # Collect the unique log paths log_paths = agent.log_paths if log_paths.empty? logger.info "Agent #{ agent.label } has no log files." return nil end logger.info "Setting up to run `newsyslog` for agent `#{ agent.label }`", log_paths: log_paths.map( &:to_s ) # NOTE Total race condition since agent may be started after this and # before we rotate... f-it for now. pid_path = nil if pid = agent.pid( refresh: true ) logger.debug "Agent is running", pid: pid pid_path = tmp_conf_dir / 'pids' / "#{ agent.label }.pid" FileUtils.mkdir_p( pid_path.dirname ) unless pid_path.dirname.exist? pid_path.write pid logger.debug "Wrote PID #{ pid } to file", pid_path: pid_path end entries = log_paths.map { |log_path| Entry.new log_path: log_path, pid_path: pid_path } conf_contents = entries.map( &:render ).join( "\n" ) + "\n" logger.debug "Generated conf entries", entries.map { |entry| [ entry.log_path.to_s, entry.instance_variables.map { |name| entry.instance_variable_get name } ] }.to_h conf_path = tmp_conf_dir / 'confs' / "#{ agent.label }.conf" FileUtils.mkdir_p( conf_path.dirname ) unless conf_path.dirname.exist? conf_path.write conf_contents logger.debug "Wrote entries to conf file", conf_path: conf_path.to_s, conf_contents: conf_contents cmd = Cmds.new "newsyslog <%= opts %>", kwds: { opts: { # Turn on verbose output v: true, # Point to the conf file f: conf_path, # Don't run as root r: true, } } logger.info "Executing `#{ cmd.prepare }`" result = cmd.capture if result.ok? logger.info \ "`newsyslog` command succeeded for agent `#{ agent.label }`" + ( result.out.empty? ? nil : ", output:\n" + result.out.indent(1, indent_string: '> ') ) FileUtils.rm( pid_path ) if pid_path FileUtils.rm( conf_path ) unless keep_conf_files logger.debug "Files cleaned up." else logger.error "`newsyslog` command failed for agent #{ agent.label }", result: result.to_h end logger.debug "Returning", result: result.to_h result end |
.run_all(tmp_conf_dir: self.tmp_conf_dir, trim_logs: true) ⇒ Hash<Locd::Agent, Cmds::Result?>
Call run for each agent.
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/locd/newsyslog.rb', line 310 def self.run_all tmp_conf_dir: self.tmp_conf_dir, trim_logs: true log_to_file do Locd::Agent.all.values. reject { |agent| agent.label == Locd::Agent::RotateLogs.label }. map { |agent| [agent, run( agent, tmp_conf_dir: tmp_conf_dir )] }. to_h. tap { |_| self.trim_logs if trim_logs } end end |
.tmp_conf_dir ⇒ Object
332 333 334 |
# File 'lib/locd/newsyslog.rb', line 332 def self.tmp_conf_dir @tmp_conf_dir ||= Locd.config.tmp_dir.join( Locd::Agent::RotateLogs.label ) end |
.trim_logs(keep_days: 7) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/locd/newsyslog.rb', line 360 def self.trim_logs keep_days: 7 logger.info "Removing old self run log directories...", log_dir: self.log_dir.to_s, keep_days: keep_days unless self.log_dir.directory? logger.warn "{Locd::Newsyslog.log_dir} does not exist!", log_dir: self.log_dir return nil end day_dirs = self.log_dir.entries.select { |dir_name| dir_name.to_s =~ /\d{4}\-\d{2}\-\d{2}/ && (self.log_dir / dir_name).directory? } to_remove = day_dirs.sort[0...(-1 * keep_days)] if to_remove.empty? logger.info "No old self run log directories to remove." else to_remove.each { |dir_name| path = self.log_dir / dir_name logger.info "Removing old day directory", path: path FileUtils.rm_rf path } logger.info "Done.", log_dir: self.log_dir.to_s, keep_days: keep_days end to_remove end |