Class: Hotdog::Sources::Datadog

Inherits:
BaseSource show all
Defined in:
lib/hotdog/sources/datadog.rb

Instance Attribute Summary

Attributes inherited from BaseSource

#application, #logger, #options

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Datadog

Returns a new instance of Datadog.



12
13
14
15
16
17
18
# File 'lib/hotdog/sources/datadog.rb', line 12

def initialize(application)
  super(application)
  options[:endpoint] = ENV.fetch("DATADOG_HOST", "https://app.datadoghq.com")
  options[:api_key] = ENV["DATADOG_API_KEY"]
  options[:application_key] = ENV["DATADOG_APPLICATION_KEY"]
  @dog = nil # lazy initialization
end

Instance Method Details

#add_tags(host_name, tags, options = {}) ⇒ Object



95
96
97
98
99
100
101
# File 'lib/hotdog/sources/datadog.rb', line 95

def add_tags(host_name, tags, options={})
  code, resp = dog.add_tags(host_name, tags, options)
  if code.to_i / 100 != 2
    raise("dog.add_tags(#{host_name.inspect}, #{tags.inspect}, #{options.inspect}) returns [#{code.inspect}, #{resp.inspect}]")
  end
  resp
end

#api_keyObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/hotdog/sources/datadog.rb', line 32

def api_key()
  if options[:api_key]
    options[:api_key]
  else
    update_api_key!
    if options[:api_key]
      options[:api_key]
    else
      raise("DATADOG_API_KEY is not set")
    end
  end
end

#application_keyObject



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/hotdog/sources/datadog.rb', line 45

def application_key()
  if options[:application_key]
    options[:application_key]
  else
    update_application_key!
    if options[:application_key]
      options[:application_key]
    else
      raise("DATADOG_APPLICATION_KEY is not set")
    end
  end
end

#cancel_downtime(id, options = {}) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/hotdog/sources/datadog.rb', line 67

def cancel_downtime(id, options={})
  code, cancel = dog.cancel_downtime(id)
  if code.to_i / 100 != 2
    raise("dog.cancel_downtime(%s) returns [%s, %s]" % [id.inspect, code.inspect, cancel.inspect])
  end
  cancel
end

#detach_tags(host_name, options = {}) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/hotdog/sources/datadog.rb', line 103

def detach_tags(host_name, options={})
  code, detach_tags = dog.detach_tags(host_name, options)
  if code.to_i / 100 != 2
    raise("dog.detach_tags(#{host_name.inspect}, #{options.inspect}) returns [#{code.inspect}, #{detach_tags.inspect}]")
  end
  detach_tags
end

#endpointObject



28
29
30
# File 'lib/hotdog/sources/datadog.rb', line 28

def endpoint()
  options[:endpoint]
end

#get_all_downtimes(options = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/hotdog/sources/datadog.rb', line 75

def get_all_downtimes(options={})
  now = Time.new.to_i
  Array(datadog_get("/api/v1/downtime")).select { |downtime|
    # active downtimes
    downtime["active"] and ( downtime["start"].nil? or downtime["start"] < now ) and ( downtime["end"].nil? or now <= downtime["end"] ) and downtime["monitor_id"].nil?
  }
end

#get_all_tags(options = {}) ⇒ Object



83
84
85
# File 'lib/hotdog/sources/datadog.rb', line 83

def get_all_tags(options={})
  Hash(datadog_get("/api/v1/tags/hosts")).fetch("tags", {})
end

#get_host_tags(host_name, options = {}) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/hotdog/sources/datadog.rb', line 87

def get_host_tags(host_name, options={})
  code, host_tags = dog.host_tags(host_name, options)
  if code.to_i / 100 != 2
    raise("dog.host_tags(#{host_name.inspect}, #{options.inspect}) returns [#{code.inspect}, #{host_tags.inspect}]")
  end
  host_tags
end

#idObject



20
21
22
# File 'lib/hotdog/sources/datadog.rb', line 20

def id()
  Hotdog::SOURCE_DATADOG
end

#nameObject



24
25
26
# File 'lib/hotdog/sources/datadog.rb', line 24

def name()
  "datadog"
end

#schedule_downtime(scope, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/hotdog/sources/datadog.rb', line 58

def schedule_downtime(scope, options={})
  code, schedule = dog.schedule_downtime(scope, :start => options[:start].to_i, :end => (options[:start]+options[:downtime]).to_i)
  logger.debug("dog.schedule_donwtime(%s, :start => %s, :end => %s) #==> [%s, %s]" % [scope.inspect, options[:start].to_i, (options[:start]+options[:downtime]).to_i, code.inspect, schedule.inspect])
  if code.to_i / 100 != 2
    raise("dog.schedule_downtime(%s, ...) returns [%s, %s]" % [scope.inspect, code.inspect, schedule.inspect])
  end
  schedule
end

#update_tags(host_name, tags, options = {}) ⇒ Object



111
112
113
114
115
116
117
# File 'lib/hotdog/sources/datadog.rb', line 111

def update_tags(host_name, tags, options={})
  code, update_tags = dog.update_tags(host_name, tags, options)
  if code.to_i / 100 != 2
    raise("dog.update_tags(#{host_name.inspect}, #{tags.inspect}, #{options.inspect}) returns [#{code.inspect}, #{update_tags.inspect}]")
  end
  update_tags
end