Module: UptimerobotCmd

Defined in:
lib/uptimerobot_cmd.rb,
lib/uptimerobot_cmd/cli.rb,
lib/uptimerobot_cmd/version.rb

Defined Under Namespace

Classes: APIKEYError, CLI, OptionsError

Constant Summary collapse

APIKEY =
ENV['UPTIMEROBOT_APIKEY'] || nil
ENDPOINT_BASE_URL =
'https://api.uptimerobot.com'
ENDPOINT_SERVICE_URLS =
{
  get_monitors: '%{base_url}/getMonitors?apiKey=%{apikey}&limit=%{limit}&offset=%{offset}&%{json_result}',
  get_alert_contacts: '%{base_url}/getAlertContacts?apiKey=%{apikey}&%{json_result}',
  add_new_monitor: '%{base_url}/newMonitor?apiKey=%{apikey}&%{json_result}'\
                   '&monitorType=%{monitor_type}'\
                   '&monitorFriendlyName=%{friendly_name}'\
                   '&monitorURL=%{monitor_url}'\
                   '&monitorAlertContacts=%{contact_id}',
  delete_monitor: '%{base_url}/deleteMonitor?apiKey=%{apikey}&%{json_result}'\
                  '&monitorID=%{monitor_id}',
}
VERSION =
"0.3.5"

Class Method Summary collapse

Class Method Details

.add_new_monitor(**options) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
# 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_definedObject



38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
# 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) ⇒ Object



109
110
111
112
113
114
115
# 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_contactsObject



90
91
92
93
94
95
# 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_monitorsObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
# 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