class DeadmanCheck::SwitchMonitor

Switch class

Attributes

alert_to_slack[RW]
alert_to_sns[RW]
alert_to_sns_region[RW]
daemon_sleep[RW]
host[RW]
port[RW]
recurse[RW]
target[RW]

Public Class Methods

new(host, port, target, alert_to_slack, alert_to_sns, alert_to_sns_region, recurse, daemon_sleep, consul_token) click to toggle source
# File lib/deadman_check_switch.rb, line 14
def initialize(host, port, target, alert_to_slack, alert_to_sns,
              alert_to_sns_region, recurse, daemon_sleep, consul_token)
  @host = host
  @port = port
  @target = target
  @alert_to_slack = alert_to_slack
  @alert_to_sns = alert_to_sns
  @alert_to_sns_region = alert_to_sns_region
  @recurse = recurse
  @daemon_sleep = daemon_sleep.to_i
  @consul_token = consul_token

  unless @alert_to_slack.nil?
    Slack.configure do |config|
      config.token = ENV['SLACK_API_TOKEN']
    end
  end

  unless @alert_to_sns.nil?
    @sns = Aws::SNS::Client.new(
      region: @alert_to_sns_region
      )
  end
end

Public Instance Methods

run_check_daemon() click to toggle source
# File lib/deadman_check_switch.rb, line 50
def run_check_daemon
  loop do
    run_check_once
    sleep(@daemon_sleep)
  end
end
run_check_once() click to toggle source
# File lib/deadman_check_switch.rb, line 39
def run_check_once
  recorded_epochs = get_recorded_epochs(@host, @port, @target, @recurse)
  current_epoch = DeadmanCheck::DeadmanCheckGlobal.new.get_epoch_time.to_i
  if @recurse
    check_recursive_recorded_epochs(recorded_epochs, current_epoch)
  else
    record = parse_recorded_epoch(recorded_epochs)
    check_recorded_epoch(record, current_epoch)
  end
end

Private Instance Methods

alert_if_epoch_greater_than_frequency(epoch_diff, target, frequency) click to toggle source
# File lib/deadman_check_switch.rb, line 96
def alert_if_epoch_greater_than_frequency(epoch_diff, target, frequency)
  if epoch_diff > frequency
    slack_alert(
      @alert_to_slack, target, epoch_diff) unless @alert_to_slack.nil?
    sns_alert(
      @alert_to_sns, target, epoch_diff) unless @alert_to_sns.nil?
  end
end
check_recorded_epoch(parse_recorded_epoch, current_epoch) click to toggle source
# File lib/deadman_check_switch.rb, line 89
def check_recorded_epoch(parse_recorded_epoch, current_epoch)
  recorded_epoch = parse_recorded_epoch[0].to_i
  frequency = parse_recorded_epoch[1].to_i
  epoch_diff = diff_epoch(current_epoch, recorded_epoch)
  alert_if_epoch_greater_than_frequency(epoch_diff, @target, frequency)
end
check_recursive_recorded_epochs(recorded_epochs, current_epoch) click to toggle source
# File lib/deadman_check_switch.rb, line 69
def check_recursive_recorded_epochs(recorded_epochs, current_epoch)
  recorded_epochs.each do |recorded_service|
    value_json = JSON.parse(recorded_service[:value])
    frequency = value_json["frequency"].to_i
    epoch = value_json["epoch"].to_i
    epoch_diff = diff_epoch(current_epoch, epoch)
    alert_if_epoch_greater_than_frequency(epoch_diff,
                                          recorded_service[:key],
                                          frequency)
  end
end
diff_epoch(current_epoch, recorded_epoch) click to toggle source
# File lib/deadman_check_switch.rb, line 58
def diff_epoch(current_epoch, recorded_epoch)
  epoch_difference = current_epoch - recorded_epoch
  return epoch_difference
end
get_recorded_epochs(host, port, target, recurse) click to toggle source
# File lib/deadman_check_switch.rb, line 63
def get_recorded_epochs(host, port, target, recurse)
  DeadmanCheck::DeadmanCheckGlobal.new.configure_diplomat(host, port, @consul_token)
  recorded_epochs = Diplomat::Kv.get(target, recurse: recurse)
  return recorded_epochs
end
parse_recorded_epoch(recorded_epochs) click to toggle source
# File lib/deadman_check_switch.rb, line 81
def parse_recorded_epoch(recorded_epochs)
  # {"epoch":1493000501,"frequency":"300"}
  value_json = JSON.parse(recorded_epochs)
  frequency = value_json["frequency"]
  epoch = value_json["epoch"]
  return epoch, frequency
end
slack_alert(alert_to_slack, target, epoch_diff) click to toggle source
# File lib/deadman_check_switch.rb, line 105
def slack_alert(alert_to_slack, target, epoch_diff)
  client = Slack::Web::Client.new
  client.chat_postMessage(channel: "\##{alert_to_slack}",
    text: "Alert: Deadman Switch
    Triggered for #{target}, with #{epoch_diff} seconds since last run",
    username: 'deadman')
end
sns_alert(alert_to_sns, target, epoch_diff) click to toggle source
# File lib/deadman_check_switch.rb, line 113
def sns_alert(alert_to_sns, target, epoch_diff)
  @sns.publish(
    target_arn: @alert_to_sns,
    message_structure: 'json',
    message: {
      :default => "Alert: Deadman Switch triggered for #{target}",
      :email => "Alert: Deadman Switch triggered for #{target}, with
      #{epoch_diff} seconds since last run",
      :sms => "Alert: Deadman Switch for #{target}"
    }.to_json
  )
end