Class: DeadmanCheck::SwitchMonitor

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

Overview

Switch class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, key, freshness, alert_to, daemon_sleep) ⇒ SwitchMonitor

Returns a new instance of SwitchMonitor.



11
12
13
14
15
16
17
18
# File 'lib/deadman_check_switch.rb', line 11

def initialize(host, port, key, freshness, alert_to, daemon_sleep)
  @host = host
  @port = port
  @key  = key
  @freshness = freshness.to_i
  @alert_to = alert_to
  @daemon_sleep = daemon_sleep.to_i
end

Instance Attribute Details

#alert_toObject

Returns the value of attribute alert_to.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def alert_to
  @alert_to
end

#daemon_sleepObject

Returns the value of attribute daemon_sleep.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def daemon_sleep
  @daemon_sleep
end

#freshnessObject

Returns the value of attribute freshness.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def freshness
  @freshness
end

#hostObject

Returns the value of attribute host.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def host
  @host
end

#keyObject

Returns the value of attribute key.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def key
  @key
end

#portObject

Returns the value of attribute port.



9
10
11
# File 'lib/deadman_check_switch.rb', line 9

def port
  @port
end

Instance Method Details

#_diff_epoc(current_epoch, recorded_epoch) ⇒ Object



24
25
26
27
# File 'lib/deadman_check_switch.rb', line 24

def _diff_epoc(current_epoch, recorded_epoch)
  epoch_difference = current_epoch - recorded_epoch
  return epoch_difference
end

#_get_recorded_epoch(host, port, key) ⇒ Object



29
30
31
32
33
# File 'lib/deadman_check_switch.rb', line 29

def _get_recorded_epoch(host, port, key)
  DeadmanCheck::DeadmanCheckGlobal.new.configure_diplomat(host, port)
  recorded_epoch = Diplomat::Kv.get(key)
  return recorded_epoch
end

#run_check_daemonObject



51
52
53
54
55
56
# File 'lib/deadman_check_switch.rb', line 51

def run_check_daemon
  loop do
    run_check_once
    sleep(@daemon_sleep)
  end
end

#run_check_onceObject



42
43
44
45
46
47
48
49
# File 'lib/deadman_check_switch.rb', line 42

def run_check_once
  recorded_epoch = _get_recorded_epoch(@host, @port, @key).to_i
  current_epoch = DeadmanCheck::DeadmanCheckGlobal.new.get_epoch_time.to_i
  epoch_diff = _diff_epoc(current_epoch, recorded_epoch)
  if epoch_diff > @freshness
    slack_alert(@alert_to, @key, epoch_diff)
  end
end

#slack_alert(alert_to, key, epoch_diff) ⇒ Object



35
36
37
38
39
40
# File 'lib/deadman_check_switch.rb', line 35

def slack_alert(alert_to, key, epoch_diff)
  client = Slack::Web::Client.new
  client.chat_postMessage(channel: "\##{alert_to}", text: "Alert: Deadman Switch
    Triggered for #{key}, with #{epoch_diff} seconds since last run",
    username: 'deadman')
end