mackerel-client 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/example/01_invitation.rb +12 -0
- data/example/02_dashboard.rb +29 -0
- data/example/03_host.rb +97 -0
- data/example/04_metrics.rb +44 -0
- data/example/05_service_role.rb +10 -0
- data/example/06_annotation.rb +26 -0
- data/example/07_monitoring.rb +100 -0
- data/example/08_alert.rb +10 -0
- data/example/Gemfile +6 -0
- data/lib/mackerel/alert.rb +48 -0
- data/lib/mackerel/annotation.rb +61 -0
- data/lib/mackerel/api_command.rb +67 -0
- data/lib/mackerel/channel.rb +32 -0
- data/lib/mackerel/client.rb +30 -154
- data/lib/mackerel/dashboard.rb +71 -0
- data/lib/mackerel/host.rb +62 -0
- data/lib/mackerel/invitation.rb +20 -0
- data/lib/mackerel/metadata.rb +15 -47
- data/lib/mackerel/metric.rb +50 -0
- data/lib/mackerel/monitor.rb +23 -53
- data/lib/mackerel/monitoring.rb +13 -0
- data/lib/mackerel/notification_group.rb +58 -0
- data/lib/mackerel/organization.rb +33 -0
- data/lib/mackerel/role.rb +24 -0
- data/lib/mackerel/service.rb +51 -0
- data/lib/mackerel/user.rb +42 -0
- data/lib/mackerel/version.rb +3 -0
- data/lib/mackerel.rb +2 -3
- data/spec/mackerel/alert_spec.rb +103 -0
- data/spec/mackerel/annotation_spec.rb +208 -0
- data/spec/mackerel/channel_spec.rb +50 -0
- data/spec/mackerel/client_spec.rb +69 -103
- data/spec/mackerel/dashboard_spec.rb +252 -0
- data/spec/mackerel/invitation_spec.rb +93 -0
- data/spec/mackerel/metric_spec.rb +287 -0
- data/spec/mackerel/monitor_spec.rb +4 -4
- data/spec/mackerel/monitoring_spec.rb +71 -0
- data/spec/mackerel/notification_group_spec.rb +274 -0
- data/spec/mackerel/organization_spec.rb +47 -0
- data/spec/mackerel/service_spec.rb +155 -0
- data/spec/mackerel/user_spec.rb +99 -0
- metadata +47 -2
data/lib/mackerel/client.rb
CHANGED
@@ -2,13 +2,39 @@ require 'faraday'
|
|
2
2
|
require 'uri'
|
3
3
|
|
4
4
|
require 'json' unless defined? ::JSON
|
5
|
+
|
6
|
+
require 'mackerel/role'
|
5
7
|
require 'mackerel/host'
|
8
|
+
require 'mackerel/monitor'
|
9
|
+
require 'mackerel/monitoring'
|
10
|
+
require 'mackerel/service'
|
11
|
+
require 'mackerel/alert'
|
12
|
+
require 'mackerel/annotation'
|
13
|
+
require 'mackerel/user'
|
14
|
+
require 'mackerel/invitation'
|
15
|
+
require 'mackerel/organization'
|
16
|
+
require 'mackerel/dashboard'
|
17
|
+
require 'mackerel/metric'
|
18
|
+
require 'mackerel/metadata'
|
19
|
+
require 'mackerel/notification_group'
|
20
|
+
require 'mackerel/channel'
|
6
21
|
|
7
22
|
module Mackerel
|
8
|
-
|
9
23
|
class Client
|
10
|
-
|
11
|
-
|
24
|
+
include Mackerel::REST::Alert
|
25
|
+
include Mackerel::REST::Annotation
|
26
|
+
include Mackerel::REST::Dashboard
|
27
|
+
include Mackerel::REST::Host
|
28
|
+
include Mackerel::REST::Invitation
|
29
|
+
include Mackerel::REST::Metric
|
30
|
+
include Mackerel::REST::Monitor
|
31
|
+
include Mackerel::REST::Monitoring
|
32
|
+
include Mackerel::REST::Organization
|
33
|
+
include Mackerel::REST::Service
|
34
|
+
include Mackerel::REST::User
|
35
|
+
include Mackerel::REST::Metadata
|
36
|
+
include Mackerel::REST::Channel
|
37
|
+
include Mackerel::REST::NotificationGroup
|
12
38
|
|
13
39
|
def initialize(args = {})
|
14
40
|
@origin = args[:mackerel_origin] || 'https://api.mackerelio.com'
|
@@ -17,156 +43,8 @@ module Mackerel
|
|
17
43
|
@open_timeout = args[:open_timeout] || 30 # Ref: apiRequestTimeout at mackerel-agent
|
18
44
|
end
|
19
45
|
|
20
|
-
def post_host(host)
|
21
|
-
response = client.post "/api/v0/hosts" do |req|
|
22
|
-
req.headers['X-Api-Key'] = @api_key
|
23
|
-
req.headers['Content-Type'] = 'application/json'
|
24
|
-
req.body = host.to_json
|
25
|
-
end
|
26
|
-
|
27
|
-
unless response.success?
|
28
|
-
raise "POST /api/v0/hosts failed: #{response.status}"
|
29
|
-
end
|
30
|
-
|
31
|
-
data = JSON.parse(response.body)
|
32
|
-
end
|
33
|
-
|
34
|
-
def get_host(host_id)
|
35
|
-
response = client.get "/api/v0/hosts/#{host_id}" do |req|
|
36
|
-
req.headers['X-Api-Key'] = @api_key
|
37
|
-
end
|
38
|
-
|
39
|
-
unless response.success?
|
40
|
-
raise "GET /api/v0/hosts/#{host_id} failed: #{response.status}"
|
41
|
-
end
|
42
|
-
|
43
|
-
data = JSON.parse(response.body)
|
44
|
-
Host.new(data['host'])
|
45
|
-
end
|
46
|
-
|
47
|
-
def update_host_status(host_id, status)
|
48
|
-
unless [:standby, :working, :maintenance, :poweroff].include?(status.to_sym)
|
49
|
-
raise "no such status: #{status}"
|
50
|
-
end
|
51
|
-
|
52
|
-
response = client.post "/api/v0/hosts/#{host_id}/status" do |req|
|
53
|
-
req.headers['X-Api-Key'] = @api_key
|
54
|
-
req.headers['Content-Type'] = 'application/json'
|
55
|
-
req.body = { "status" => status }.to_json
|
56
|
-
end
|
57
|
-
|
58
|
-
unless response.success?
|
59
|
-
raise "POST /api/v0/hosts/#{host_id}/status failed: #{response.status}"
|
60
|
-
end
|
61
|
-
|
62
|
-
data = JSON.parse(response.body)
|
63
|
-
end
|
64
|
-
|
65
|
-
def retire_host(host_id)
|
66
|
-
response = client.post "/api/v0/hosts/#{host_id}/retire" do |req|
|
67
|
-
req.headers['X-Api-Key'] = @api_key
|
68
|
-
req.headers['Content-Type'] = 'application/json'
|
69
|
-
req.body = { }.to_json
|
70
|
-
end
|
71
|
-
|
72
|
-
unless response.success?
|
73
|
-
raise "POST /api/v0/hosts/#{host_id}/retire failed: #{response.status}"
|
74
|
-
end
|
75
|
-
|
76
|
-
data = JSON.parse(response.body)
|
77
|
-
end
|
78
|
-
|
79
|
-
def post_metrics(metrics)
|
80
|
-
response = client.post '/api/v0/tsdb' do |req|
|
81
|
-
req.headers['X-Api-Key'] = @api_key
|
82
|
-
req.headers['Content-Type'] = 'application/json'
|
83
|
-
req.body = metrics.to_json
|
84
|
-
end
|
85
|
-
|
86
|
-
unless response.success?
|
87
|
-
raise "POST /api/v0/tsdb failed: #{response.status}"
|
88
|
-
end
|
89
|
-
|
90
|
-
data = JSON.parse(response.body)
|
91
|
-
end
|
92
|
-
|
93
|
-
def get_latest_metrics(hostIds, names)
|
94
|
-
query = (hostIds.map{ |hostId| "hostId=#{hostId}" } +
|
95
|
-
names.map{ |name| "name=#{name}" }).join('&')
|
96
|
-
|
97
|
-
response = client.get "/api/v0/tsdb/latest?#{query}" do |req|
|
98
|
-
req.headers['X-Api-Key'] = @api_key
|
99
|
-
end
|
100
|
-
|
101
|
-
unless response.success?
|
102
|
-
raise "/api/v0/tsdb/latest?#{query} failed: #{response.status}"
|
103
|
-
end
|
104
|
-
|
105
|
-
data = JSON.parse(response.body)
|
106
|
-
data["tsdbLatest"]
|
107
|
-
end
|
108
|
-
|
109
|
-
def post_service_metrics(service_name, metrics)
|
110
|
-
response = client.post "/api/v0/services/#{service_name}/tsdb" do |req|
|
111
|
-
req.headers['X-Api-Key'] = @api_key
|
112
|
-
req.headers['Content-Type'] = 'application/json'
|
113
|
-
req.body = metrics.to_json
|
114
|
-
end
|
115
|
-
|
116
|
-
unless response.success?
|
117
|
-
raise "POST /api/v0/services/#{service_name}/tsdb failed: #{response.status}"
|
118
|
-
end
|
119
|
-
|
120
|
-
data = JSON.parse(response.body)
|
121
|
-
end
|
122
|
-
|
123
|
-
def define_graphs(graph_defs)
|
124
|
-
response = client.post '/api/v0/graph-defs/create' do |req|
|
125
|
-
req.headers['X-Api-Key'] = @api_key
|
126
|
-
req.headers['Content-Type'] = 'application/json'
|
127
|
-
req.body = graph_defs.to_json
|
128
|
-
end
|
129
|
-
|
130
|
-
unless response.success?
|
131
|
-
raise "POST /api/v0/graph-defs/create failed: #{response.status} #{response.body}"
|
132
|
-
end
|
133
|
-
|
134
|
-
JSON.parse(response.body)
|
135
|
-
end
|
136
|
-
|
137
|
-
def get_hosts(opts = {})
|
138
|
-
response = client.get '/api/v0/hosts' do |req|
|
139
|
-
req.headers['X-Api-Key'] = @api_key
|
140
|
-
req.params['service'] = opts[:service] if opts[:service]
|
141
|
-
req.params['role'] = opts[:roles] if opts[:roles]
|
142
|
-
req.params['name'] = opts[:name] if opts[:name]
|
143
|
-
req.params['status'] = opts[:status] if opts[:status]
|
144
|
-
req.params['customIdentifier'] = opts[:customIdentifier] if opts[:customIdentifier]
|
145
|
-
end
|
146
|
-
|
147
|
-
unless response.success?
|
148
|
-
raise "GET /api/v0/hosts failed: #{response.status}"
|
149
|
-
end
|
150
|
-
|
151
|
-
data = JSON.parse(response.body)
|
152
|
-
data['hosts'].map{ |host_json| Host.new(host_json) }
|
153
|
-
end
|
154
|
-
|
155
|
-
def post_graph_annotation(annotation)
|
156
|
-
response = client.post '/api/v0/graph-annotations' do |req|
|
157
|
-
req.headers['X-Api-Key'] = @api_key
|
158
|
-
req.headers['Content-Type'] = 'application/json'
|
159
|
-
req.body = annotation.to_json
|
160
|
-
end
|
161
|
-
|
162
|
-
unless response.success?
|
163
|
-
raise "POST /api/v0/graph-annotations failed: #{response.status} #{response.body}"
|
164
|
-
end
|
165
|
-
|
166
|
-
JSON.parse(response.body)
|
167
|
-
end
|
168
|
-
|
169
46
|
private
|
47
|
+
ERROR_MESSAGE_FOR_API_KEY_ABSENCE = "API key is absent. Set your API key in a environment variable called MACKEREL_APIKEY."
|
170
48
|
|
171
49
|
def client
|
172
50
|
@client ||= http_client
|
@@ -181,7 +59,5 @@ module Mackerel
|
|
181
59
|
faraday.options.open_timeout = @open_timeout
|
182
60
|
end
|
183
61
|
end
|
184
|
-
|
185
62
|
end
|
186
|
-
|
187
63
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Mackerel
|
2
|
+
|
3
|
+
class Dashboard
|
4
|
+
attr_accessor :id, :title, :bodyMarkdown, :urlPath, :createdAt, :updatedAt
|
5
|
+
def initialize(args = {})
|
6
|
+
@id = args["id"]
|
7
|
+
@title = args["title"]
|
8
|
+
@bodyMarkdown = args["bodyMarkdown"]
|
9
|
+
@urlPath = args["urlPath"]
|
10
|
+
@createdAt = args["createdAt"]
|
11
|
+
@updatedAt = args["updatedAt"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_h
|
15
|
+
instance_variables.flat_map do |name|
|
16
|
+
respond_to?(name[1..-1]) ? [name[1..-1]] : []
|
17
|
+
end.each_with_object({}) do |name, hash|
|
18
|
+
hash[name] = public_send(name)
|
19
|
+
end.delete_if { |key, val| val == nil }
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_json(options = nil)
|
23
|
+
return to_h.to_json(options)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
module REST
|
29
|
+
module Dashboard
|
30
|
+
def post_dashboard(title, markdown, urlPath)
|
31
|
+
command = ApiCommand.new(:post, '/api/v0/dashboards', @api_key)
|
32
|
+
command.body = {
|
33
|
+
title: title,
|
34
|
+
bodyMarkdown: markdown,
|
35
|
+
urlPath: urlPath
|
36
|
+
}.to_json
|
37
|
+
data = command.execute(client)
|
38
|
+
Mackerel::Dashboard.new(data)
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_dashboard(dashboardId, title, markdown, urlPath)
|
42
|
+
command = ApiCommand.new(:put, "/api/v0/dashboards/#{dashboardId}", @api_key)
|
43
|
+
command.body = {
|
44
|
+
title: title,
|
45
|
+
bodyMarkdown: markdown,
|
46
|
+
urlPath: urlPath
|
47
|
+
}.to_json
|
48
|
+
data = command.execute(client)
|
49
|
+
Mackerel::Dashboard.new(data)
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_dashboards()
|
53
|
+
command = ApiCommand.new(:get, '/api/v0/dashboards', @api_key)
|
54
|
+
data = command.execute(client)
|
55
|
+
data['dashboards'].map{ |d| Mackerel::Dashboard.new(d) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def get_dashboard(dashboardId)
|
59
|
+
command = ApiCommand.new(:get, "/api/v0/dashboards/#{dashboardId}", @api_key)
|
60
|
+
data = command.execute(client)
|
61
|
+
Mackerel::Dashboard.new(data)
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete_dashboard(dashboardId)
|
65
|
+
command = ApiCommand.new(:delete, "/api/v0/dashboards/#{dashboardId}", @api_key)
|
66
|
+
data = command.execute(client)
|
67
|
+
Mackerel::Dashboard.new(data)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/lib/mackerel/host.rb
CHANGED
@@ -39,4 +39,66 @@ module Mackerel
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
+
module REST
|
43
|
+
module Host
|
44
|
+
|
45
|
+
def post_host(host)
|
46
|
+
command = ApiCommand.new(:post, "/api/v0/hosts", @api_key)
|
47
|
+
command.body = host.to_json
|
48
|
+
data = command.execute(client)
|
49
|
+
end
|
50
|
+
|
51
|
+
def update_host(host_id, host)
|
52
|
+
command = ApiCommand.new(:put, "/api/v0/hosts/#{host_id}", @api_key)
|
53
|
+
command.body = host.to_json
|
54
|
+
data = command.execute(client)
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_host_roles(host_id, roles)
|
58
|
+
roles = [roles] if roles.is_a?(String)
|
59
|
+
command = ApiCommand.new(:put, "/api/v0/hosts/#{host_id}/role-fullnames", @api_key)
|
60
|
+
command.body = { "roleFullnames" => roles }.to_json
|
61
|
+
data = command.execute(client)
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_host(host_id)
|
65
|
+
command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}", @api_key)
|
66
|
+
data = command.execute(client)
|
67
|
+
Mackerel::Host.new(data['host'])
|
68
|
+
end
|
69
|
+
|
70
|
+
def update_host_status(host_id, status)
|
71
|
+
unless [:standby, :working, :maintenance, :poweroff].include?(status.to_sym)
|
72
|
+
raise "no such status: #{status}"
|
73
|
+
end
|
74
|
+
|
75
|
+
command = ApiCommand.new(:post, "/api/v0/hosts/#{host_id}/status", @api_key)
|
76
|
+
command.body = { "status" => status }.to_json
|
77
|
+
data = command.execute(client)
|
78
|
+
end
|
79
|
+
|
80
|
+
def retire_host(host_id)
|
81
|
+
command = ApiCommand.new(:post, "/api/v0/hosts/#{host_id}/retire", @api_key)
|
82
|
+
command.body = { }.to_json
|
83
|
+
data = command.execute(client)
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_hosts(opts = {})
|
87
|
+
command = ApiCommand.new(:get, '/api/v0/hosts', @api_key)
|
88
|
+
command.params['service'] = opts[:service] if opts[:service]
|
89
|
+
command.params['role'] = opts[:roles] if opts[:roles]
|
90
|
+
command.params['name'] = opts[:name] if opts[:name]
|
91
|
+
command.params['status'] = opts[:status] if opts[:status]
|
92
|
+
command.params['customIdentifier'] = opts[:customIdentifier] if opts[:customIdentifier]
|
93
|
+
data = command.execute(client)
|
94
|
+
data['hosts'].map{ |host_json| Mackerel::Host.new(host_json) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_host_metric_names(host_id)
|
98
|
+
command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}/metric-names", @api_key)
|
99
|
+
data = command.execute(client)
|
100
|
+
data["names"]
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
42
104
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Mackerel
|
2
|
+
module REST
|
3
|
+
module Invitation
|
4
|
+
def post_invitation(email, authority)
|
5
|
+
command = ApiCommand.new(:post, '/api/v0/invitations', @api_key)
|
6
|
+
command.body = {
|
7
|
+
email: email.to_s,
|
8
|
+
authority: authority.to_s
|
9
|
+
}.to_json
|
10
|
+
data = command.execute(client)
|
11
|
+
end
|
12
|
+
|
13
|
+
def revoke_invitation(email)
|
14
|
+
command = ApiCommand.new(:post, '/api/v0/invitations/revoke', @api_key)
|
15
|
+
command.body = { email: email.to_s }.to_json
|
16
|
+
data = command.execute(client)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/mackerel/metadata.rb
CHANGED
@@ -1,58 +1,26 @@
|
|
1
1
|
module Mackerel
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
req.headers['X-Api-Key'] = @api_key
|
2
|
+
module REST
|
3
|
+
module Metadata
|
4
|
+
def get_metadata(host_id, namespace)
|
5
|
+
command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}/metadata/#{namespace}", @api_key)
|
6
|
+
data = command.execute(client)
|
8
7
|
end
|
9
8
|
|
10
|
-
|
11
|
-
|
9
|
+
def list_metadata(host_id)
|
10
|
+
command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}/metadata", @api_key)
|
11
|
+
data = command.execute(client)
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
response = client.get "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
19
|
-
req.headers['X-Api-Key'] = @api_key
|
14
|
+
def update_metadata(host_id, namespace, metadata)
|
15
|
+
command = ApiCommand.new(:put, "/api/v0/hosts/#{host_id}/metadata/#{namespace}", @api_key)
|
16
|
+
command.body = metadata.to_json
|
17
|
+
data = command.execute(client)
|
20
18
|
end
|
21
19
|
|
22
|
-
|
23
|
-
|
20
|
+
def delete_metadata(host_id, namespace)
|
21
|
+
command = ApiCommand.new(:delete, "/api/v0/hosts/#{host_id}/metadata/#{namespace}", @api_key)
|
22
|
+
data = command.execute(client)
|
24
23
|
end
|
25
|
-
|
26
|
-
JSON.parse(response.body)
|
27
24
|
end
|
28
|
-
|
29
|
-
def update_metadata(host_id, namespace, data)
|
30
|
-
response = client.put "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
31
|
-
req.headers['X-Api-Key'] = @api_key
|
32
|
-
req.headers['Content-Type'] = 'application/json'
|
33
|
-
req.body = data.to_json
|
34
|
-
end
|
35
|
-
|
36
|
-
unless response.success?
|
37
|
-
raise "PUT /api/v0/hosts/#{host_id}/metadata/#{namespace} failed: #{response.status} #{response.body}"
|
38
|
-
end
|
39
|
-
|
40
|
-
JSON.parse(response.body)
|
41
|
-
end
|
42
|
-
|
43
|
-
def delete_metadata(host_id, namespace)
|
44
|
-
response = client.delete "/api/v0/hosts/#{host_id}/metadata/#{namespace}" do |req|
|
45
|
-
req.headers['X-Api-Key'] = @api_key
|
46
|
-
req.headers['Content-Type'] = 'application/json'
|
47
|
-
end
|
48
|
-
|
49
|
-
unless response.success?
|
50
|
-
raise "DELETE /api/v0/hosts/#{host_id}/metadata/#{namespace} failed: #{response.status} #{response.body}"
|
51
|
-
end
|
52
|
-
|
53
|
-
JSON.parse(response.body)
|
54
|
-
end
|
55
|
-
|
56
25
|
end
|
57
|
-
|
58
26
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Mackerel
|
2
|
+
module REST
|
3
|
+
module Metric
|
4
|
+
def post_metrics(metrics)
|
5
|
+
command = ApiCommand.new(:post, '/api/v0/tsdb', @api_key)
|
6
|
+
command.body = metrics.to_json
|
7
|
+
data = command.execute(client)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_host_metrics(host_id, name, from, to)
|
11
|
+
command = ApiCommand.new(:get, "/api/v0/hosts/#{host_id}/metrics", @api_key)
|
12
|
+
command.params['name'] = name
|
13
|
+
command.params['from'] = from
|
14
|
+
command.params['to'] = to
|
15
|
+
data = command.execute(client)
|
16
|
+
data["metrics"]
|
17
|
+
end
|
18
|
+
|
19
|
+
def get_latest_metrics(host_id, name)
|
20
|
+
command = ApiCommand.new(:get, "/api/v0/tsdb/latest", @api_key)
|
21
|
+
command.params['hostId'] = host_id
|
22
|
+
command.params['name'] = name
|
23
|
+
data = command.execute(client)
|
24
|
+
data["tsdbLatest"]
|
25
|
+
end
|
26
|
+
|
27
|
+
def post_service_metrics(service_name, metrics)
|
28
|
+
command = ApiCommand.new(:post, "/api/v0/services/#{service_name}/tsdb", @api_key)
|
29
|
+
command.body = metrics.to_json
|
30
|
+
data = command.execute(client)
|
31
|
+
end
|
32
|
+
|
33
|
+
def get_service_metrics(service_name, name, from, to)
|
34
|
+
command = ApiCommand.new(:get, "/api/v0/services/#{service_name}/metrics", @api_key)
|
35
|
+
command.params['name'] = name
|
36
|
+
command.params['from'] = from
|
37
|
+
command.params['to'] = to
|
38
|
+
data = command.execute(client)
|
39
|
+
data["metrics"]
|
40
|
+
end
|
41
|
+
|
42
|
+
def define_graphs(graph_defs)
|
43
|
+
command = ApiCommand.new(:post, '/api/v0/graph-defs/create', @api_key)
|
44
|
+
command.body = graph_defs.to_json
|
45
|
+
data = command.execute(client)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
data/lib/mackerel/monitor.rb
CHANGED
@@ -32,75 +32,45 @@ module Mackerel
|
|
32
32
|
def to_h
|
33
33
|
instance_variables.flat_map do |name|
|
34
34
|
respond_to?(name[1..-1]) ? [name[1..-1]] : []
|
35
|
-
end.each_with_object({}) do |name, hash|
|
35
|
+
end.each_with_object({}) do |name, hash|
|
36
36
|
hash[name] = public_send(name)
|
37
37
|
end.delete_if { |key, val| val == nil }
|
38
38
|
end
|
39
39
|
|
40
|
-
def to_json
|
41
|
-
return to_h.to_json
|
40
|
+
def to_json(options = nil)
|
41
|
+
return to_h.to_json(options)
|
42
42
|
end
|
43
43
|
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
module REST
|
47
|
+
module Monitor
|
47
48
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
def post_monitor(monitor)
|
50
|
+
command = ApiCommand.new(:post, '/api/v0/monitors', @api_key)
|
51
|
+
command.body = monitor.to_json
|
52
|
+
data = command.execute(client)
|
53
|
+
Mackerel::Monitor.new(data)
|
53
54
|
end
|
54
55
|
|
55
|
-
|
56
|
-
|
56
|
+
def get_monitors()
|
57
|
+
command = ApiCommand.new(:get,'/api/v0/monitors', @api_key)
|
58
|
+
data = command.execute(client)
|
59
|
+
data['monitors'].map{ |m| Mackerel::Monitor.new(m) }
|
57
60
|
end
|
58
61
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
response = client.get '/api/v0/monitors' do |req|
|
65
|
-
req.headers['X-Api-Key'] = @api_key
|
62
|
+
def update_monitor(monitor_id, monitor)
|
63
|
+
command = ApiCommand.new(:put, "/api/v0/monitors/#{monitor_id}", @api_key)
|
64
|
+
command.body = monitor.to_json
|
65
|
+
data = command.execute(client)
|
66
|
+
Mackerel::Monitor.new(data)
|
66
67
|
end
|
67
68
|
|
68
|
-
|
69
|
-
|
69
|
+
def delete_monitor(monitor_id)
|
70
|
+
command = ApiCommand.new(:delete, "/api/v0/monitors/#{monitor_id}", @api_key)
|
71
|
+
data = command.execute(client)
|
72
|
+
Mackerel::Monitor.new(data)
|
70
73
|
end
|
71
|
-
|
72
|
-
data = JSON.parse(response.body)
|
73
|
-
data['monitors'].map{ |monitor_json| Monitor.new(monitor_json) }
|
74
|
-
end
|
75
|
-
|
76
|
-
def update_monitor(monitor_id, monitor)
|
77
|
-
response = client.put "/api/v0/monitors/#{monitor_id}" do |req|
|
78
|
-
req.headers['X-Api-Key'] = @api_key
|
79
|
-
req.headers['Content-Type'] = 'application/json'
|
80
|
-
req.body = monitor.to_json
|
81
|
-
end
|
82
|
-
|
83
|
-
unless response.success?
|
84
|
-
raise "PUT /api/v0/monitors/#{monitor_id} failed: #{response.status}"
|
85
|
-
end
|
86
|
-
|
87
|
-
JSON.parse(response.body)
|
88
74
|
end
|
89
|
-
|
90
|
-
def delete_monitor(monitor_id)
|
91
|
-
response = client.delete "/api/v0/monitors/#{monitor_id}" do |req|
|
92
|
-
req.headers['X-Api-Key'] = @api_key
|
93
|
-
req.headers['Content-Type'] = 'application/json'
|
94
|
-
end
|
95
|
-
|
96
|
-
unless response.success?
|
97
|
-
raise "DELETE /api/v0/monitors/#{monitor_id} failed: #{response.status}"
|
98
|
-
end
|
99
|
-
|
100
|
-
data = JSON.parse(response.body)
|
101
|
-
Monitor.new(data)
|
102
|
-
end
|
103
|
-
|
104
75
|
end
|
105
|
-
|
106
76
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Mackerel
|
2
|
+
module REST
|
3
|
+
module Monitoring
|
4
|
+
|
5
|
+
def post_monitoring_check_report(reports)
|
6
|
+
command = ApiCommand.new(:post,'/api/v0/monitoring/checks/report', @api_key)
|
7
|
+
command.body = reports.to_json
|
8
|
+
data = command.execute(client)
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Mackerel
|
2
|
+
|
3
|
+
class NotificationGroup
|
4
|
+
attr_accessor :id, :name, :notificationLevel, :childNotificationGroupIds, :childChannelIds, :monitors, :services
|
5
|
+
def initialize(args = {})
|
6
|
+
@id = args["id"]
|
7
|
+
@name = args["name"]
|
8
|
+
@notificationLevel = args["notificationLevel"]
|
9
|
+
@childNotificationGroupIds = args["childNotificationGroupIds"]
|
10
|
+
@childChannelIds = args["childChannelIds"]
|
11
|
+
@monitors = args["monitors"]
|
12
|
+
@services = args["services"]
|
13
|
+
end
|
14
|
+
|
15
|
+
def to_h
|
16
|
+
instance_variables.flat_map do |name|
|
17
|
+
respond_to?(name[1..-1]) ? [name[1..-1]] : []
|
18
|
+
end.each_with_object({}) do |name, hash|
|
19
|
+
hash[name] = public_send(name)
|
20
|
+
end.delete_if { |key, val| val == nil }
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_json(options = nil)
|
24
|
+
return to_h.to_json(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
module REST
|
30
|
+
module NotificationGroup
|
31
|
+
def post_notification_group(notification_group)
|
32
|
+
command = ApiCommand.new(:post, '/api/v0/notification-groups', @api_key)
|
33
|
+
command.body = notification_group.to_json
|
34
|
+
data = command.execute(client)
|
35
|
+
Mackerel::NotificationGroup.new(data)
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_notification_groups()
|
39
|
+
command = ApiCommand.new(:get, '/api/v0/notification-groups', @api_key)
|
40
|
+
data = command.execute(client)
|
41
|
+
data['notificationGroups'].map{|a| Mackerel::NotificationGroup.new(a)}
|
42
|
+
end
|
43
|
+
|
44
|
+
def update_notification_group(notification_group_id, notification_group)
|
45
|
+
command = ApiCommand.new(:put, "/api/v0/notification-groups/#{notification_group_id}", @api_key)
|
46
|
+
command.body = notification_group.to_json
|
47
|
+
data = command.execute(client)
|
48
|
+
Mackerel::NotificationGroup.new(data)
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete_notification_group(notification_group_id)
|
52
|
+
command = ApiCommand.new(:delete, "/api/v0/notification-groups/#{notification_group_id}", @api_key)
|
53
|
+
data = command.execute(client)
|
54
|
+
Mackerel::NotificationGroup.new(data)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|