Class: Mackerel::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/mackerel/client.rb

Constant Summary collapse

ERROR_MESSAGE_FOR_API_KEY_ABSENCE =
"API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY."

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



13
14
15
16
# File 'lib/mackerel/client.rb', line 13

def initialize(args = {})
  @origin  = args[:mackerel_origin] || 'https://mackerel.io'
  @api_key = args[:mackerel_api_key] || raise(ERROR_MESSAGE_FOR_API_KEY_ABSENCE)
end

Instance Method Details

#get_host(host_id) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/mackerel/client.rb', line 18

def get_host(host_id)
  response = client.get "/api/v0/hosts/#{host_id}" do |req|
    req.headers['X-Api-Key'] = @api_key
  end

  unless response.success?
    raise "GET /api/v0/hosts/#{host_id} failed: #{response.status}"
  end

  data = JSON.parse(response.body)
  Host.new(data['host'])
end

#get_hosts(opts = {}) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mackerel/client.rb', line 107

def get_hosts(opts = {})
  response = client.get '/api/v0/hosts.json' do |req|
    req.headers['X-Api-Key'] = @api_key
    req.params['service']    = opts[:service] if opts[:service]
    req.params['role']       = opts[:roles]   if opts[:roles]
    req.params['name']       = opts[:name]    if opts[:name]
  end

  unless response.success?
    raise "GET /api/v0/hosts.json failed: #{response.status}"
  end

  data = JSON.parse(response.body)
  data['hosts'].map{ |host_json| Host.new(host_json) }
end

#get_latest_metrics(hostIds, names) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/mackerel/client.rb', line 77

def get_latest_metrics(hostIds, names)
  query = (hostIds.map{ |hostId| "hostId=#{hostId}" } +
           names.map{ |name| "name=#{name}" }).join('&')

  response = client.get "/api/v0/tsdb/latest?#{query}" do |req|
    req.headers['X-Api-Key'] = @api_key
  end

  unless response.success?
    raise "/api/v0/tsdb/latest?#{query} failed: #{response.status}"
  end

  data = JSON.parse(response.body)
  data["tsdbLatest"]
end

#post_metrics(metrics) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/mackerel/client.rb', line 63

def post_metrics(metrics)
  response = client.post '/api/v0/tsdb' do |req|
    req.headers['X-Api-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = metrics.to_json
  end

  unless response.success?
    raise "POST /api/v0/tsdb failed: #{response.status}"
  end

  data = JSON.parse(response.body)
end

#post_service_metrics(service_name, metrics) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mackerel/client.rb', line 93

def post_service_metrics(service_name, metrics)
  response = client.post "/api/v0/services/#{service_name}/tsdb" do |req|
    req.headers['X-Api-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = metrics.to_json
  end

  unless response.success?
    raise "POST /api/v0/services/#{service_name}/tsdb failed: #{response.status}"
  end

  data = JSON.parse(response.body)
end

#retire_host(host_id) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/mackerel/client.rb', line 49

def retire_host(host_id)
  response = client.post "/api/v0/hosts/#{host_id}/retire" do |req|
    req.headers['X-Api-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = { }.to_json
  end

  unless response.success?
    raise "POST /api/v0/hosts/#{host_id}/retire failed: #{response.status}"
  end

  data = JSON.parse(response.body)
end

#update_host_status(host_id, status) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/mackerel/client.rb', line 31

def update_host_status(host_id, status)
  unless [:standby, :working, :maintenance, :poweroff].include?(status.to_sym)
    raise "no such status: #{status}"
  end

  response = client.post "/api/v0/hosts/#{host_id}/status" do |req|
    req.headers['X-Api-Key'] = @api_key
    req.headers['Content-Type'] = 'application/json'
    req.body = { "status" => status }.to_json
  end

  unless response.success?
    raise "POST /api/v0/hosts/#{host_id}/status failed: #{response.status}"
  end

  data = JSON.parse(response.body)
end