mackerel-client 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/VERSION +1 -1
  3. data/example/01_invitation.rb +12 -0
  4. data/example/02_dashboard.rb +29 -0
  5. data/example/03_host.rb +97 -0
  6. data/example/04_metrics.rb +44 -0
  7. data/example/05_service_role.rb +10 -0
  8. data/example/06_annotation.rb +26 -0
  9. data/example/07_monitoring.rb +100 -0
  10. data/example/08_alert.rb +10 -0
  11. data/example/Gemfile +6 -0
  12. data/lib/mackerel/alert.rb +48 -0
  13. data/lib/mackerel/annotation.rb +61 -0
  14. data/lib/mackerel/api_command.rb +67 -0
  15. data/lib/mackerel/channel.rb +32 -0
  16. data/lib/mackerel/client.rb +30 -154
  17. data/lib/mackerel/dashboard.rb +71 -0
  18. data/lib/mackerel/host.rb +62 -0
  19. data/lib/mackerel/invitation.rb +20 -0
  20. data/lib/mackerel/metadata.rb +15 -47
  21. data/lib/mackerel/metric.rb +50 -0
  22. data/lib/mackerel/monitor.rb +23 -53
  23. data/lib/mackerel/monitoring.rb +13 -0
  24. data/lib/mackerel/notification_group.rb +58 -0
  25. data/lib/mackerel/organization.rb +33 -0
  26. data/lib/mackerel/role.rb +24 -0
  27. data/lib/mackerel/service.rb +51 -0
  28. data/lib/mackerel/user.rb +42 -0
  29. data/lib/mackerel/version.rb +3 -0
  30. data/lib/mackerel.rb +2 -3
  31. data/spec/mackerel/alert_spec.rb +103 -0
  32. data/spec/mackerel/annotation_spec.rb +208 -0
  33. data/spec/mackerel/channel_spec.rb +50 -0
  34. data/spec/mackerel/client_spec.rb +69 -103
  35. data/spec/mackerel/dashboard_spec.rb +252 -0
  36. data/spec/mackerel/invitation_spec.rb +93 -0
  37. data/spec/mackerel/metric_spec.rb +287 -0
  38. data/spec/mackerel/monitor_spec.rb +4 -4
  39. data/spec/mackerel/monitoring_spec.rb +71 -0
  40. data/spec/mackerel/notification_group_spec.rb +274 -0
  41. data/spec/mackerel/organization_spec.rb +47 -0
  42. data/spec/mackerel/service_spec.rb +155 -0
  43. data/spec/mackerel/user_spec.rb +99 -0
  44. metadata +47 -2
@@ -0,0 +1,287 @@
1
+ require 'mackerel'
2
+ require 'mackerel/host'
3
+ require 'json'
4
+
5
+ describe Mackerel::Client do
6
+ let(:api_key) { 'xxxxxxxx' }
7
+ let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
+
9
+ describe '#post_metrics' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object)
15
+ ]
16
+ }
17
+
18
+ let(:test_client) {
19
+ Faraday.new do |builder|
20
+ builder.adapter :test do |stubs|
21
+ stubs.post(api_path) { stubbed_response }
22
+ end
23
+ end
24
+ }
25
+
26
+ let(:hostId) { '21obeF4PhZN' }
27
+
28
+ let(:api_path) { "/api/v0/tsdb" }
29
+
30
+ let(:response_object) {
31
+ { 'success' => true }
32
+ }
33
+
34
+ let(:metrics) { [
35
+ { 'hostId' => hostId, 'name' => 'custom.metrics.loadavg', 'time' => 1401537844, 'value' => 1.4 },
36
+ { 'hostId' => hostId, 'name' => 'custom.metrics.uptime', 'time' => 1401537844, 'value' => 500 },
37
+ ] }
38
+
39
+ before do
40
+ allow(client).to receive(:http_client).and_return(test_client)
41
+ end
42
+
43
+ it "successfully post metrics" do
44
+ expect(client.post_metrics(metrics)).to eq(response_object)
45
+ end
46
+ end
47
+
48
+ describe "#get_host_metrics" do
49
+ let(:stubbed_response) {
50
+ [
51
+ 200,
52
+ {},
53
+ JSON.dump(response_object)
54
+ ]
55
+ }
56
+
57
+ let(:test_client) {
58
+ Faraday.new do |builder|
59
+ builder.adapter :test do |stubs|
60
+ stubs.get(api_path) { stubbed_response }
61
+ end
62
+ end
63
+ }
64
+
65
+ let(:hostId) { '21obeF4PhZN' }
66
+
67
+ let(:metric_name) { "loadavg5" }
68
+ let(:from) { 1407898100 }
69
+ let(:to) { 1407898300 }
70
+
71
+ let(:api_path) { "/api/v0/hosts/#{hostId}/metrics" }
72
+
73
+ let(:response_object) {
74
+ {
75
+ 'metrics' => [
76
+ {
77
+ "time"=>1407898200,
78
+ "value"=>0.03666666666666667
79
+ }
80
+ ]
81
+ }
82
+ }
83
+
84
+ before do
85
+ allow(client).to receive(:http_client).and_return(test_client)
86
+ end
87
+
88
+ it "successfully get host metrics" do
89
+ expect(client.get_host_metrics(hostId, metric_name, from, to)).to eq(response_object["metrics"])
90
+ end
91
+ end
92
+
93
+
94
+ describe "#get_latest_metrics" do
95
+ let(:stubbed_response) {
96
+ [
97
+ 200,
98
+ {},
99
+ JSON.dump(response_object)
100
+ ]
101
+ }
102
+
103
+ let(:test_client) {
104
+ Faraday.new do |builder|
105
+ builder.adapter :test do |stubs|
106
+ stubs.get(api_path) { stubbed_response }
107
+ end
108
+ end
109
+ }
110
+
111
+ let(:hostId) { '21obeF4PhZN' }
112
+
113
+ let(:metric_name) { "loadavg5" }
114
+
115
+ let(:api_path) { "/api/v0/tsdb/latest" }
116
+
117
+ let(:response_object) {
118
+ {
119
+ "tsdbLatest" => {
120
+ hostId => {
121
+ metric_name => {"time"=>1407898200, "value"=>0.03666666666666667},
122
+ }
123
+ }
124
+ }
125
+ }
126
+
127
+ before do
128
+ allow(client).to receive(:http_client).and_return(test_client)
129
+ end
130
+
131
+ it "successfully get latest metrics" do
132
+ expect(client.get_latest_metrics([hostId], [metric_name])).to eq(response_object["tsdbLatest"])
133
+ end
134
+ end
135
+
136
+
137
+ describe '#post_service_metrics' do
138
+ let(:stubbed_response) {
139
+ [
140
+ 200,
141
+ {},
142
+ JSON.dump(response_object)
143
+ ]
144
+ }
145
+
146
+ let(:test_client) {
147
+ Faraday.new do |builder|
148
+ builder.adapter :test do |stubs|
149
+ stubs.post(api_path) { stubbed_response }
150
+ end
151
+ end
152
+ }
153
+
154
+ let(:service_name) { 'service_name' }
155
+
156
+ let(:api_path) { "/api/v0/services/#{service_name}/tsdb" }
157
+
158
+ let(:response_object) {
159
+ { 'success' => true }
160
+ }
161
+
162
+ let(:metrics) { [
163
+ { 'name' => 'custom.metrics.latency', 'time' => 1401537844, 'value' => 0.5 },
164
+ { 'name' => 'custom.metrics.uptime', 'time' => 1401537844, 'value' => 500 },
165
+ ] }
166
+
167
+ before do
168
+ allow(client).to receive(:http_client).and_return(test_client)
169
+ end
170
+
171
+ it "successfully post service metrics" do
172
+ expect(client.post_service_metrics(service_name, metrics)).to eq(response_object)
173
+ end
174
+ end
175
+
176
+
177
+ describe '#get_service_metrics' do
178
+ let(:stubbed_response) {
179
+ [
180
+ 200,
181
+ {},
182
+ JSON.dump(response_object)
183
+ ]
184
+ }
185
+
186
+ let(:test_client) {
187
+ Faraday.new do |builder|
188
+ builder.adapter :test do |stubs|
189
+ stubs.get(api_path) { stubbed_response }
190
+ end
191
+ end
192
+ }
193
+
194
+ let(:service_name) { "LAB" }
195
+ let(:name) { 'custom.metrics.loadavg' }
196
+ let(:from) { 1401537744 }
197
+ let(:to) { 1401537944 }
198
+ let(:api_path) { "/api/v0/services/#{service_name}/metrics" }
199
+ let(:response_object) {
200
+ { 'metrics' => metrics }
201
+ }
202
+
203
+ let(:metrics) { [
204
+ { 'name' => 'custom.metrics.loadavg', 'time' => 1401537844, 'value' => 1.4 },
205
+ { 'name' => 'custom.metrics.uptime', 'time' => 1401537844, 'value' => 500 },
206
+ ] }
207
+
208
+ before do
209
+ allow(client).to receive(:http_client).and_return(test_client)
210
+ end
211
+
212
+ it "successfully get service metrics" do
213
+ expect(client.get_service_metrics(service_name, name, from, to)).to eq(metrics)
214
+ end
215
+ end
216
+
217
+
218
+ describe "#define_graphs" do
219
+ let(:stubbed_response) {
220
+ [
221
+ 200,
222
+ {},
223
+ JSON.dump(response_object)
224
+ ]
225
+ }
226
+
227
+ let(:test_client) {
228
+ Faraday.new do |builder|
229
+ builder.adapter :test do |stubs|
230
+ stubs.post(api_path) { stubbed_response }
231
+ end
232
+ end
233
+ }
234
+
235
+ let(:graph_def){
236
+ [
237
+ {
238
+ "name" => "custom.cpu.foo",
239
+ "displayName" => "CPU",
240
+ "unit" =>"percentage",
241
+ "metrics" => [
242
+ {
243
+ "name" => "custom.cpu.foo.user",
244
+ "displayName" => "CPU user",
245
+ "isStacked" => true
246
+ },
247
+ {
248
+ "name" => "custom.cpu.foo.idle",
249
+ "displayName" => "CPU idle",
250
+ "isStacked" => true
251
+ }
252
+ ]
253
+ },
254
+ {
255
+ "name" => "custom.wild.#",
256
+ "displayName" => "wildcard",
257
+ "unit" => "float",
258
+ "metrics" => [
259
+ {
260
+ "name" => "custom.wild.#.foo",
261
+ "displayName" => "wild foo"
262
+ },
263
+ {
264
+ "name" => "custom.wild.#.bar",
265
+ "displayName" => "wild bar"
266
+ }
267
+ ]
268
+ }
269
+ ]
270
+ }
271
+ let(:api_path) { "/api/v0/graph-defs/create" }
272
+
273
+ let(:response_object) {
274
+ { "success" => true }
275
+ }
276
+
277
+ before do
278
+ allow(client).to receive(:http_client).and_return(test_client)
279
+ end
280
+
281
+ it "successfully post metrics" do
282
+ expect(client.define_graphs(graph_def)).to eq({"success" => true})
283
+ end
284
+ end
285
+
286
+
287
+ end
@@ -140,7 +140,7 @@ describe Mackerel::Client do
140
140
  let(:api_path) { "/api/v0/monitors/#{monitorId}" }
141
141
 
142
142
  let(:monitor) {
143
- Mackerel::Host.new(
143
+ {
144
144
  'type' => 'host',
145
145
  'name' => 'monitor001',
146
146
  'duration' => 5,
@@ -150,11 +150,11 @@ describe Mackerel::Client do
150
150
  'critical' => 6,
151
151
  'notificationInterval' => 600,
152
152
  'isMute' => false,
153
- )
153
+ }
154
154
  }
155
155
 
156
156
  let(:response_object) {
157
- { 'id' => monitorId }
157
+ Mackerel::Monitor.new(monitor)
158
158
  }
159
159
 
160
160
  before do
@@ -162,7 +162,7 @@ describe Mackerel::Client do
162
162
  end
163
163
 
164
164
  it "successfully update a monitor" do
165
- expect(client.update_monitor(monitorId, monitor)).to eq(response_object)
165
+ expect(client.update_monitor(monitorId, monitor).to_h).to eq(monitor)
166
166
  end
167
167
  end
168
168
 
@@ -0,0 +1,71 @@
1
+ require 'mackerel'
2
+ require 'mackerel/host'
3
+ require 'json'
4
+
5
+ describe Mackerel::Client do
6
+ let(:api_key) { 'xxxxxxxx' }
7
+ let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
+
9
+ describe '#post_monitoring_check_report' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object)
15
+ ]
16
+ }
17
+
18
+ let(:test_client) {
19
+ Faraday.new do |builder|
20
+ builder.adapter :test do |stubs|
21
+ stubs.post(api_path) { stubbed_response }
22
+ end
23
+ end
24
+ }
25
+
26
+ let(:api_path) { '/api/v0/monitoring/checks/report' }
27
+ let(:hostId) { '21obeF4PhZN' }
28
+
29
+ let(:reports) {
30
+ {
31
+ reports: [
32
+ {
33
+ name: "check-report-test",
34
+ status: "OK",
35
+ message: "YOKERO!",
36
+ occurredAt: Time.now.to_i.to_s,
37
+ source: {
38
+ type: "host",
39
+ hostId: hostId
40
+ }
41
+ },
42
+ {
43
+ name: "check-report-test2",
44
+ status: "OK",
45
+ message: "OKOKOKOK!",
46
+ occurredAt: Time.now.to_i.to_s,
47
+ source: {
48
+ type: "host",
49
+ hostId: hostId
50
+ }
51
+ }
52
+ ]
53
+ }
54
+ }
55
+
56
+ let(:response_object) {
57
+ {
58
+ "success" => "true"
59
+ }
60
+ }
61
+
62
+ before do
63
+ allow(client).to receive(:http_client).and_return(test_client)
64
+ end
65
+
66
+ it "successfully post monitoring checks report" do
67
+ expect(client.post_monitoring_check_report(reports)).to eq(response_object)
68
+ end
69
+ end
70
+
71
+ end
@@ -0,0 +1,274 @@
1
+ require 'mackerel'
2
+ require 'mackerel/host'
3
+ require 'json'
4
+
5
+ describe Mackerel::Client do
6
+ let(:api_key) { 'xxxxxxxx' }
7
+ let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
+
9
+ describe '#post_notification_group' do
10
+ let(:stubbed_response) {
11
+ [
12
+ 200,
13
+ {},
14
+ JSON.dump(response_object)
15
+ ]
16
+ }
17
+
18
+ let(:test_client) {
19
+ Faraday.new do |builder|
20
+ builder.adapter :test do |stubs|
21
+ stubs.post(api_path) { stubbed_response }
22
+ end
23
+ end
24
+ }
25
+
26
+ let(:api_path) { '/api/v0/notification-groups' }
27
+
28
+ let(:notificationGroupId) { '123abEFabcd' }
29
+ let(:notificationGroup) {
30
+ {
31
+ "id" => "270DgxukABC",
32
+ "name" => "Example notification group",
33
+ "notificationLevel" => "all",
34
+ "childNotificationGroupIds" => [],
35
+ "childChannelIds" => [
36
+ "2vh7AZ21abc"
37
+ ],
38
+ "monitors" => [
39
+ {
40
+ "id" => "2qtozU21abc",
41
+ "skipDefault" => false
42
+ }
43
+ ],
44
+ "services" => [
45
+ {
46
+ "name" => "Example-Service-1"
47
+ },
48
+ {
49
+ "name" => "Example-Service-2"
50
+ }
51
+ ]
52
+ }
53
+ }
54
+
55
+ let(:response_object) {
56
+ notificationGroup.merge({'id' => notificationGroupId})
57
+ }
58
+
59
+ before do
60
+ allow(client).to receive(:http_client).and_return(test_client)
61
+ end
62
+
63
+ it "successfully post notification groups" do
64
+ expect(client.post_notification_group(notificationGroup).to_h).to eq(response_object)
65
+ end
66
+ end
67
+
68
+
69
+
70
+ describe '#get_notification_groups' do
71
+
72
+ let(:api_path) { "/api/v0/notification-groups" }
73
+ let(:stubbed_response) {
74
+ [
75
+ 200,
76
+ {},
77
+ JSON.dump(response_object)
78
+ ]
79
+ }
80
+
81
+ let(:test_client) {
82
+ Faraday.new do |builder|
83
+ builder.adapter :test do |stubs|
84
+ stubs.get(api_path) { stubbed_response }
85
+ end
86
+ end
87
+ }
88
+
89
+ let(:notificationGroups) {
90
+ {
91
+ "notificationGroups" => [
92
+ {
93
+ "id" => "270DgxukABC",
94
+ "name" => "Example notification group",
95
+ "notificationLevel" => "all",
96
+ "childNotificationGroupIds" => [],
97
+ "childChannelIds" => [
98
+ "2vh7AZ21abc"
99
+ ],
100
+ "monitors" => [
101
+ {
102
+ "id" => "2qtozU21abc",
103
+ "skipDefault" => false
104
+ }
105
+ ],
106
+ "services" => [
107
+ {
108
+ "name" => "Example-Service-1"
109
+ },
110
+ {
111
+ "name" => "Example-Service-2"
112
+ }
113
+ ]
114
+ },
115
+ {
116
+ "id" => "162DhijkABC",
117
+ "name" => "Example notification group2",
118
+ "notificationLevel" => "all",
119
+ "childNotificationGroupIds" => [],
120
+ "childChannelIds" => [
121
+ "3vh7AZ21def"
122
+ ],
123
+ "monitors" => [
124
+ {
125
+ "id" => "3vh7AZ21def",
126
+ "skipDefault" => false
127
+ }
128
+ ],
129
+ "services" => [
130
+ {
131
+ "name" => "Example-Service-1"
132
+ },
133
+ {
134
+ "name" => "Example-Service-2"
135
+ }
136
+ ]
137
+ }
138
+ ]
139
+ }
140
+ }
141
+
142
+ let(:response_object) {
143
+ notificationGroups
144
+ }
145
+
146
+ before do
147
+ allow(client).to receive(:http_client).and_return(test_client)
148
+ end
149
+
150
+ it "successfully get notification groups" do
151
+ expect(client.get_notification_groups().map(&:to_h)).to eq(notificationGroups['notificationGroups'])
152
+ end
153
+ end
154
+
155
+ describe '#update_notification_group' do
156
+ let(:stubbed_response) {
157
+ [
158
+ 200,
159
+ {},
160
+ JSON.dump(response_object)
161
+ ]
162
+ }
163
+
164
+ let(:test_client) {
165
+ Faraday.new do |builder|
166
+ builder.adapter :test do |stubs|
167
+ stubs.put(api_path) { stubbed_response }
168
+ end
169
+ end
170
+ }
171
+
172
+ let(:api_path) { "/api/v0/notification-groups/#{notificationGroupId}"}
173
+ let(:notificationGroupId) { '123abEFabcd' }
174
+ let(:notificationGroup) {
175
+ {
176
+ "id" => "270DgxukABC",
177
+ "name" => "Example notification group",
178
+ "notificationLevel" => "all",
179
+ "childNotificationGroupIds" => [],
180
+ "childChannelIds" => [
181
+ "2vh7AZ21abc"
182
+ ],
183
+ "monitors" => [
184
+ {
185
+ "id" => "2qtozU21abc",
186
+ "skipDefault" => false
187
+ }
188
+ ],
189
+ "services" => [
190
+ {
191
+ "name" => "Example-Service-1"
192
+ },
193
+ {
194
+ "name" => "Example-Service-2"
195
+ }
196
+ ]
197
+ }
198
+ }
199
+
200
+
201
+ let(:response_object) {
202
+ notificationGroup.merge({ "id" => notificationGroupId })
203
+ }
204
+
205
+ before do
206
+ allow(client).to receive(:http_client).and_return(test_client)
207
+ end
208
+
209
+ it "successfully update notification group" do
210
+ expect(client.update_notification_group(notificationGroupId, notificationGroup).to_h ).to eq(notificationGroup.merge({"id" => notificationGroupId}))
211
+ end
212
+ end
213
+
214
+
215
+ describe '#delete_notification_group' do
216
+ let(:stubbed_response) {
217
+ [
218
+ 200,
219
+ {},
220
+ JSON.dump(response_object)
221
+ ]
222
+ }
223
+
224
+ let(:test_client) {
225
+ Faraday.new do |builder|
226
+ builder.adapter :test do |stubs|
227
+ stubs.delete(api_path) { stubbed_response }
228
+ end
229
+ end
230
+ }
231
+
232
+ let(:api_path) { "/api/v0/notification-groups/#{notificationGroupId}"}
233
+ let(:notificationGroupId) { '123abEFabcd' }
234
+ let(:notificationGroup) {
235
+ {
236
+ "id" => "270DgxukABC",
237
+ "name" => "Example notification group",
238
+ "notificationLevel" => "all",
239
+ "childNotificationGroupIds" => [],
240
+ "childChannelIds" => [
241
+ "2vh7AZ21abc"
242
+ ],
243
+ "monitors" => [
244
+ {
245
+ "id" => "2qtozU21abc",
246
+ "skipDefault" => false
247
+ }
248
+ ],
249
+ "services" => [
250
+ {
251
+ "name" => "Example-Service-1"
252
+ },
253
+ {
254
+ "name" => "Example-Service-2"
255
+ }
256
+ ]
257
+ }
258
+ }
259
+
260
+ let(:response_object) {
261
+ notificationGroup.merge({ "id" => notificationGroupId })
262
+ }
263
+
264
+ before do
265
+ allow(client).to receive(:http_client).and_return(test_client)
266
+ end
267
+
268
+ it "successfully delete notification group" do
269
+ expect(client.delete_notification_group(notificationGroupId).to_h ).to eq(notificationGroup.merge({"id" => notificationGroupId}))
270
+ end
271
+ end
272
+
273
+
274
+ end
@@ -0,0 +1,47 @@
1
+ require 'mackerel'
2
+ require 'mackerel/host'
3
+ require 'json'
4
+
5
+ describe Mackerel::Client do
6
+ let(:api_key) { 'xxxxxxxx' }
7
+ let(:client) { Mackerel::Client.new(:mackerel_api_key => api_key) }
8
+
9
+ describe '#get_organizations' do
10
+
11
+ let(:api_path) { "/api/v0/org" }
12
+ let(:stubbed_response) {
13
+ [
14
+ 200,
15
+ {},
16
+ JSON.dump(response_object)
17
+ ]
18
+ }
19
+
20
+ let(:test_client) {
21
+ Faraday.new do |builder|
22
+ builder.adapter :test do |stubs|
23
+ stubs.get(api_path) { stubbed_response }
24
+ end
25
+ end
26
+ }
27
+
28
+ let(:organization) {
29
+ {
30
+ "name" => "HogeH",
31
+ }
32
+ }
33
+
34
+ let(:response_object) {
35
+ organization
36
+ }
37
+
38
+ before do
39
+ allow(client).to receive(:http_client).and_return(test_client)
40
+ end
41
+
42
+ it "successfully get organization" do
43
+ expect(client.get_organization().to_h).to eq(organization)
44
+ end
45
+ end
46
+
47
+ end