class Lita::Handlers::Sensu2

Public Instance Methods

client(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 81
def client(response)
  client = add_domain(response.matches[0][0])
  client_url = "#{config.api_url}:#{config.api_port}/clients/#{client}"
  resp = http_get(client_url)
  if resp.status == 200
    client = MultiJson.load(resp.body, symbolize_keys: true)
    response.reply(MultiJson.dump(client, pretty: true))
  elsif resp.status == 404
    response.reply("#{client} was not found")
  else
    log.warn "Sensu returned an internal error fetching #{client_url}"
    response.reply("An error occurred fetching client #{client}")
  end
end
client_history(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 96
def client_history(response)
  client = add_domain(response.matches[0][0])
  client_url = "#{config.api_url}:#{config.api_port}/clients/#{client}/history"
  resp = http_get(client_url)
  if resp.status == 200
    response.reply(render_template('client_history', history: sorted_by(resp.body, :check)))
  else
    log.warn("Sensu returned an internal error fetching #{client_url}")
    response.reply("An error occurred fetching client #{client} history")
  end
end
clients(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 108
def clients(response)
  clients_url = "#{config.api_url}:#{config.api_port}/clients"
  resp = http_get(clients_url)
  if resp.status == 200
    response.reply(render_template('clients', clients: sorted_by(resp.body, :name)))
  else
    log.warn("Sensu returned an internal error fetching #{clients_url}")
    response.reply('An error occurred fetching clients')
  end
end
events(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 119
def events(response)
  client = response.matches[0][0] ? '/' + add_domain(response.matches[0][0]) : ''
  client_url = "#{config.api_url}:#{config.api_port}/events#{client}"

  resp = http_get(client_url)
  response.reply(
    if resp.status == 200
      render_template('events', events: sorted_events(resp.body))
    else
      log.warn("Sensu returned an internal error fetching #{client_url}")
      'An error occurred fetching clients'
    end
  )
end
info(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 134
def info(response)
  resp = http_get("#{config.api_url}:#{config.api_port}/info")
  raise RequestError unless resp.status == 200
  info = MultiJson.load(resp.body, symbolize_keys: true)
  response.reply(MultiJson.dump(info, pretty: true))
end
remove_client(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 141
def remove_client(response)
  client = add_domain(response.matches[0][0])
  client_url = "#{config.api_url}:#{config.api_port}/clients/#{client}"
  resp = http_delete(client_url)
  if resp.status == 202
    response.reply("#{client} removed")
  elsif resp.status == 404
    response.reply("#{client} was not found")
  else
    log.warn("Sensu returned an internal error deleting #{client_url}")
    response.reply("An error occurred removing #{client}")
  end
end
resolve(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 155
def resolve(response)
  client = add_domain(response.matches[0][0])
  check = response.matches[0][1]
  res_url = "#{config.api_url}:#{config.api_port}/resolve"

  data = { client: client, check: check }
  post_data = MultiJson.dump(data)
  resp = http_post(res_url, post_data)
  if resp.status == 202
    response.reply("#{client}/#{check} resolved")
  elsif resp.status == 400
    response.reply("Resolve message was malformed: #{post_data}")
  elsif resp.status == 404
    response.reply("#{client}/#{check} was not found")
  else
    log.warn(
      "Sensu returned an internal error resolving #{res_url} with #{post_data}"
    )
    response.reply("There was an error resolving #{client}/#{check}")
  end
end
silence(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 177
def silence(response)
  client = add_domain(response.matches[0][0])
  check = response.matches[0][1]
  duration = response.matches[0][2]
  units = response.matches[0][3]

  expiration, human_duration = calculate_expiration(duration.to_i, units)

  return false unless valid_expiration(response, expiration, units)

  post_data = silence_post_data(response.user, expiration, client, check)
  resp = http_post(silence_url, post_data)
  response.reply silence_post_msg(resp, post_data, human_duration, check_alias(client, check))
end
silences(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 192
def silences(response)
  resp = http_get(silence_url)
  if resp.status == 200
    response.reply(render_template('silences', silences: sorted_by(resp.body, :subscription)))
  else
    log.warn("Sensu returned an internal error resolving #{silence_url}")
    response.reply('An error occurred fetching silences')
  end
end
stashes(response) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 202
def stashes(response)
  stashes_url = "#{config.api_url}:#{config.api_port}/stashes"
  resp = http_get(stashes_url)
  if resp.status == 200
    response.reply(render_template('stashes', stashes: sorted_by(resp.body, :name)))
  else
    log.warn("Sensu returned an internal error resolving #{stashes_url}")
    response.reply('An error occurred fetching stashes')
  end
end

Private Instance Methods

silence_post_msg(resp, post_data, human_duration, chk_alias) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 215
def silence_post_msg(resp, post_data, human_duration, chk_alias)
  if resp.status == 201
    "#{chk_alias} silenced for #{human_duration}"
  else
    log.warn(
      "Sensu returned an internal error posting '#{post_data}' to #{silence_url}"
    )
    "An error occurred silencing to #{chk_alias}"
  end
end
valid_expiration(response, exp, units) click to toggle source
# File lib/lita/handlers/sensu2.rb, line 226
def valid_expiration(response, exp, units)
  return true if exp
  response.reply(
    "Unknown unit (#{units}).  I know s (seconds), m (minutes), h (hours), and d(days)"
  )
  false
end