Class: WatchList::Driver

Inherits:
Object
  • Object
show all
Includes:
Logger::Helper
Defined in:
lib/watch_list/driver.rb

Instance Method Summary collapse

Methods included from Logger::Helper

#log

Constructor Details

#initialize(uptimerobot, opts = {}) ⇒ Driver

Returns a new instance of Driver.



4
5
6
7
# File 'lib/watch_list/driver.rb', line 4

def initialize(uptimerobot, opts = {})
  @uptimerobot = uptimerobot
  @options = opts
end

Instance Method Details

#delete_alert_contact(attrs) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/watch_list/driver.rb', line 26

def delete_alert_contact(attrs)
  updated = false
  log(:info, "Delete AlertContact: #{describe_alert_contacts(attrs)}", :color => :red)

  unless @options[:dry_run]
    @uptimerobot.deleteAlertContact(
      :alertContactID => attrs[:ID],
    )

    updated = true
  end

  updated
end

#delete_monitor(attrs) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/watch_list/driver.rb', line 56

def delete_monitor(attrs)
  updated = false
  log(:info, "Delete Monitor: #{attrs[:FriendlyName]}", :color => :red)

  unless @options[:dry_run]
    @uptimerobot.deleteMonitor(
      :monitorID => attrs[:ID],
    )

    updated = true
  end

  updated
end

#edit_monitor(monitor_id, monitor_name, attrs, exist_alert_contacts) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/watch_list/driver.rb', line 71

def edit_monitor(monitor_id, monitor_name, attrs, exist_alert_contacts)
  updated = false
  log(:info, "Update Monitor: #{monitor_name}", :color => :green)

  attrs.each do |key, value|
    log(:info, " set #{key}=#{value}", :color => :green)
  end

  unless @options[:dry_run]
    params = monitor_to_params(attrs, exist_alert_contacts)
    params[:monitorID] = monitor_id
    @uptimerobot.editMonitor(params)
    updated = true
  end

  updated
end

#new_alert_contact(attrs) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/watch_list/driver.rb', line 9

def new_alert_contact(attrs)
  updated = false
  log(:info, "Create AlertContact: #{describe_alert_contacts(attrs)}", :color => :cyan)

  unless @options[:dry_run]
    response = @uptimerobot.newAlertContact(
      :alertContactType  => attrs[:Type],
      :alertContactValue => attrs[:Value],
    )

    attrs[:ID] = response['alertcontact']['id']
    updated = true
  end

  updated
end

#new_monitor(attrs, exist_alert_contacts) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/watch_list/driver.rb', line 41

def new_monitor(attrs, exist_alert_contacts)
  updated = false
  log(:info, "Create Monitor: #{attrs[:FriendlyName]}", :color => :cyan)

  unless @options[:dry_run]
    normalize_edit_attrs!(attrs)
    params = monitor_to_params(attrs, exist_alert_contacts)
    response = @uptimerobot.newMonitor(params)
    attrs[:ID] = response['monitor']['id']
    updated = true
  end

  updated
end

#pause_monitor(monitor_id, monitor_name, paused) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/watch_list/driver.rb', line 89

def pause_monitor(monitor_id, monitor_name, paused)
  updated = false
  log(:info, (paused ? 'Pause' : 'Unpause') + " Monitor: #{monitor_name}", :color => :green)

  unless @options[:dry_run]
    @uptimerobot.editMonitor(
      :monitorID     => monitor_id,
      :monitorStatus => (paused ? 0 : 1),
    )

    updated = true
  end

  updated
end