module UptimerobotCmd

Constants

APIKEY
ENDPOINT_BASE_URL
ENDPOINT_SERVICE_URLS
VERSION

Public Class Methods

add_new_monitor(**options) click to toggle source
# File lib/uptimerobot_cmd.rb, line 97
def self.add_new_monitor(**options)
  if ::UptimerobotCmd.apikey_defined
    raise ::UptimerobotCmd::OptionsError, "Please provide url to monitor" unless options[:monitor_url]
    options[:contact_id] ||= ENV['UPTIMEROBOT_DEFAULT_CONTACT']
    options[:monitor_type] ||= 1
    options[:friendly_name] ||= options[:monitor_url]
    raise ::UptimerobotCmd::OptionsError, "Please provide Contact ID" unless options[:contact_id]
    response = HTTParty.get(::UptimerobotCmd.build_service_url(:add_new_monitor, options))
    [response.code, response['stat']]
  end
end
apikey_defined() click to toggle source
# File lib/uptimerobot_cmd.rb, line 38
def self.apikey_defined
  raise ::UptimerobotCmd::APIKEYError, "Please set UPTIMEROBOT_APIKEY environment variable." unless ENV['UPTIMEROBOT_APIKEY']
  true
end
build_service_url(service_key, **options) click to toggle source
# File lib/uptimerobot_cmd.rb, line 43
def self.build_service_url(service_key, **options)
  options[:apikey] = ::UptimerobotCmd::APIKEY
  options[:base_url] = ::UptimerobotCmd::ENDPOINT_BASE_URL
  options[:json_result] = 'format=json&noJsonCallback=1'
  ::UptimerobotCmd::ENDPOINT_SERVICE_URLS[service_key] % options
end
delete_monitor(**options) click to toggle source
# File lib/uptimerobot_cmd.rb, line 109
def self.delete_monitor(**options)
  if ::UptimerobotCmd.apikey_defined
    raise ::UptimerobotCmd::OptionsError, "Please provide site ID" unless options[:monitor_id]
    response = HTTParty.get(::UptimerobotCmd.build_service_url(:delete_monitor, options))
    [response.code, response['stat']]
  end
end
get_alert_contacts() click to toggle source
# File lib/uptimerobot_cmd.rb, line 90
def self.get_alert_contacts
  if ::UptimerobotCmd.apikey_defined
    response = HTTParty.get(::UptimerobotCmd.build_service_url(:get_alert_contacts))
    response["alertcontacts"]["alertcontact"]
  end
end
get_monitors() click to toggle source
# File lib/uptimerobot_cmd.rb, line 50
def self.get_monitors
  if ::UptimerobotCmd.apikey_defined
    limit = 50
    offset = 0
    options = {
      limit: limit,
      offset: offset,
    }
    response = HTTParty.get(::UptimerobotCmd.build_service_url(:get_monitors, options))
    total = response["total"].to_i
    output = response["monitors"]["monitor"]
    
    if total > limit
      max_pages = total / limit
      left_over = total % limit
      max_pages += 1 if left_over > 0
      current_page = 1
      while current_page < max_pages
        options[:offset] = current_page * limit
        puts "fetching... #{options[:offset]}"
        response = HTTParty.get(::UptimerobotCmd.build_service_url(:get_monitors, options))
        output += response["monitors"]["monitor"]
        current_page += 1
      end
    end
    
    output
  end
end
human_readable_status(status_code) click to toggle source
# File lib/uptimerobot_cmd.rb, line 23
def self.human_readable_status(status_code)
  case status_code
  when '0'
    ['paused', :black]
  when '1'
    ['not checked yet', :black]
  when '2'
    ['up', :yellow]
  when '8'
    ['seems down', :red]
  when '9'
    ['down', :red]
  end
end
search_in_monitors(input) click to toggle source
# File lib/uptimerobot_cmd.rb, line 80
def self.search_in_monitors(input)
  if ::UptimerobotCmd.apikey_defined
    monitors = ::UptimerobotCmd.get_monitors
    monitors.select{|monitor|
      monitor['friendlyname'] =~ /#{input}/i ||
      monitor['url'] =~ /#{input}/i
    }
  end
end