lib/sys_watchdog/main.rb in sys_watchdog-0.0.2 vs lib/sys_watchdog/main.rb in sys_watchdog-0.0.3

- old
+ new

@@ -1,13 +1,18 @@ class SysWatchdog DEFAULT_CONF_FILE = '/etc/sys_watchdog.yml' DEFAULT_LOG_FILE = '/var/log/sys_watchdog.log' def initialize conf_file: nil, log_file: nil - @logger = WdLogger.new (log_file || DEFAULT_LOG_FILE) + log_file ||= DEFAULT_LOG_FILE + conf_file ||= DEFAULT_CONF_FILE + @trackers = {} - parse_conf (conf_file || DEFAULT_CONF_FILE) + + @logger = WdLogger.new log_file + parse_conf conf_file + setup end def run once: false loop do @@ -47,37 +52,40 @@ end def run_test test, after_restore: false success, exitstatus, output = test.run - if test.notify_on_change - if @trackers[test.name] != output - notify "#{test.name} changed", "old: #{@trackers[test.name]}\nnew: #{output}" - end - @trackers[test.name] = output - end + notify_change test, output + return if success == test.fail + if success - if test.fail - test.fail = false - notify "#{test.name} ok" - end + test.fail = false + notify "#{test.name} ok" else - unless test.fail - if test.restore_cmd and not after_restore - test.restore - run_test test, after_restore: true - else - fail test, exitstatus, output - end + if test.restore_cmd and not after_restore + test.restore + run_test test, after_restore: true + else + fail test, exitstatus, output end end rescue => e @logger.error e.desc end + def notify_change test, output + if test.notify_on_change + if @trackers[test.name] != output + notify "#{test.name} changed", "old: #{@trackers[test.name]}\nnew: #{output}" + end + @trackers[test.name] = output + end + end + def fail test, exitstatus, output test.fail = true - body = "output: #{output}" if body and not body.empty? + body = "exitstatus: #{exitstatus}" + body += "\noutput: #{output}" if output and not output.empty? notify "#{test.name} fail", body end end