From 4eaad66ed6549df19d1e634ed0720b624d2a5f54 Mon Sep 17 00:00:00 2001 From: Sven Pachnit Date: Tue, 1 Nov 2016 15:18:33 +0100 Subject: [PATCH] Fix implementation of ts3_license checker --- lib/watchmonkey_cli.rb | 1 + lib/watchmonkey_cli/checkers/ts3_license.rb | 108 ++++++++++---------- 2 files changed, 56 insertions(+), 53 deletions(-) diff --git a/lib/watchmonkey_cli.rb b/lib/watchmonkey_cli.rb index 0fd265a..31764dd 100644 --- a/lib/watchmonkey_cli.rb +++ b/lib/watchmonkey_cli.rb @@ -41,4 +41,5 @@ require "watchmonkey_cli/checkers/unix_load" require "watchmonkey_cli/checkers/unix_mdadm" require "watchmonkey_cli/checkers/unix_memory" +require "watchmonkey_cli/checkers/ts3_license" require "watchmonkey_cli/checkers/www_availability" diff --git a/lib/watchmonkey_cli/checkers/ts3_license.rb b/lib/watchmonkey_cli/checkers/ts3_license.rb index 73f319d..3bc101f 100644 --- a/lib/watchmonkey_cli/checkers/ts3_license.rb +++ b/lib/watchmonkey_cli/checkers/ts3_license.rb @@ -1,66 +1,68 @@ -module WatchmonkeyCliCustom - class Ts3License < WatchmonkeyCli::Checker - self.checker_name = "ts3_license" +module WatchmonkeyCli + module Checkers + class Ts3License < Checker + self.checker_name = "ts3_license" - def enqueue host, file, opts = {} - opts = { threshold: 1.months }.merge(opts) - host = app.fetch_connection(:loopback, :local) if !host || host == :local - host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol) - app.enqueue(self, host, file, opts) - end + def enqueue host, file, opts = {} + opts = { threshold: 1.months }.merge(opts) + host = app.fetch_connection(:loopback, :local) if !host || host == :local + host = app.fetch_connection(:ssh, host) if host.is_a?(Symbol) + app.enqueue(self, host, file, opts) + end - def check! result, host, file, opts = {} - result.command = "cat #{Shellwords.escape(file)}" - result.result = host.exec(result.command).force_encoding('UTF-8') + def check! result, host, file, opts = {} + result.command = "cat #{Shellwords.escape(file)}" + result.result = host.exec(result.command).force_encoding('UTF-8') - if result.result.downcase["no such file"] - result.error! "Failed to read file #{file} (#{result.result})" - else - result.data = _parse_response(result.result) - start_at = zone_parse "UTC", result.data["start date"] - end_at = zone_parse "UTC", result.data["end date"] + if result.result.downcase["no such file"] + result.error! "Failed to read file #{file} (#{result.result})" + else + result.data = _parse_response(result.result) + start_at = zone_parse "UTC", result.data["start date"] + end_at = zone_parse "UTC", result.data["end date"] - if start_at > Time.current - result.error! "TS3 license is not yet valid (will in #{human_seconds(start_at - Time.current)}, #{start_at})!" - return - end + if start_at > Time.current + result.error! "TS3 license is not yet valid (will in #{human_seconds(start_at - Time.current)}, #{start_at})!" + return + end - if end_at <= Time.current - result.error! "TS3 license is EXPIRED (since #{human_seconds(end_at - Time.current)}, #{end_at})!" - return - end + if end_at <= Time.current + result.error! "TS3 license is EXPIRED (since #{human_seconds(end_at - Time.current)}, #{end_at})!" + return + end - if end_at <= Time.current + opts[:threshold] - result.error! "TS3 license is about to expire within threshold (in #{human_seconds(end_at - Time.current)}, #{end_at})!" - return - else - result.info! "TS3 license for `#{page}' expires in #{human_seconds(end_at - Time.current)} (#{end_at})!" + if end_at <= Time.current + opts[:threshold] + result.error! "TS3 license is about to expire within threshold (in #{human_seconds(end_at - Time.current)}, #{end_at})!" + return + else + result.info! "TS3 license for `#{page}' expires in #{human_seconds(end_at - Time.current)} (#{end_at})!" + end end end - end - def zone_parse tz, date - tz_was = Time.zone - Time.zone = "UTC" - Time.zone.parse(date) - ensure - Time.zone = tz_was - end + def zone_parse tz, date + tz_was = Time.zone + Time.zone = "UTC" + Time.zone.parse(date) + ensure + Time.zone = tz_was + end - def _parse_response res - {}.tap do |r| - lines = res.split("\n") - reached_key = false - lines.each do |l| - next if l.blank? - if l[":"] - c = l.split(":", 2) - r[c[0].strip] = c[1].strip - elsif l["==key=="] - reached_key = true - elsif reached_key - r["key"] ||= "" - r["key"] += l + def _parse_response res + {}.tap do |r| + lines = res.split("\n") + reached_key = false + lines.each do |l| + next if l.blank? + if l[":"] + c = l.split(":", 2) + r[c[0].strip] = c[1].strip + elsif l["==key=="] + reached_key = true + elsif reached_key + r["key"] ||= "" + r["key"] += l + end end end end