Skip to content

Commit

Permalink
Fix implementation of ts3_license checker
Browse files Browse the repository at this point in the history
  • Loading branch information
2called-chaos committed Nov 1, 2016
1 parent ebb240f commit 4eaad66
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 53 deletions.
1 change: 1 addition & 0 deletions lib/watchmonkey_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
108 changes: 55 additions & 53 deletions lib/watchmonkey_cli/checkers/ts3_license.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 4eaad66

Please sign in to comment.