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.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3069a1328fe22ade45d038f43f84e0d284d31147
4
- data.tar.gz: 358f4acb097b4bf6184ed740555916f4875b5aee
3
+ metadata.gz: 8223df7347ec3ad38411bc07a47ee281dfa903ce
4
+ data.tar.gz: 455e08dfe09c1f13d97ae24798cfa24faa3738b3
5
5
  SHA512:
6
- metadata.gz: 22f6985dd1027bb676ae5ef310a32d35f619b2624d95069b411d051952e3410c109a19415ab65b09964a38fe83996ee3944b451540d5f4e169dd3ca2bbac67fd
7
- data.tar.gz: 673b03abd2573c2aecc583412c3c89678974bf1d0436fda32ac5101b3c02710d93e7c4d8bd131899f967e4dbfd0e87f9e8b1906467cefd2c46a7eaac1609b7fd
6
+ metadata.gz: 5cb671cf7f4de85fdd7581441192f0dc0126bbe6114647b1120d4dd80f1c71393af0ad1163d55d5d416bc000f960908b8f551f0606dd0de0a4317ec3496ab927
7
+ data.tar.gz: eaeec9170583fd338b270b4a7f5587219278ed8ac8af7f86c5298a4253b40d01a76f30a9598ad1a50fae2f7cbff45d5acc7870a02031bc522ef2aadf6dc5d601
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -0,0 +1,12 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+ pp mc.get_organization()
9
+ pp mc.post_invitation(ENV['EMAIL'], "viewer")
10
+ #pp mc.revoke_invitation(ENV['EMAIL'])
11
+ pp user = mc.get_users()
12
+ pp mc.delete_user(user.id) unless user.any?
@@ -0,0 +1,29 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+
9
+ # make a dashboard
10
+ title = "Dashboard Example"
11
+ markdown = "
12
+ # Inform to labors
13
+
14
+ Working time is eight hour.
15
+ May day is May 1st.
16
+ "
17
+ urlPath = "DashboardExample"
18
+ pp mc.post_dashboard(title, markdown, urlPath)
19
+
20
+ # Update the dashboard
21
+ new_title = "That Song"
22
+ pp dashboards = mc.get_dashboards
23
+ target_dashboard = dashboards.select{|d| d.urlPath =~ /#{urlPath}/ }.first
24
+ mc.get_dashboard(target_dashboard.id)
25
+ pp mc.update_dashboard(target_dashboard.id, new_title, markdown, urlPath)
26
+
27
+ # Delete the dashboard
28
+ pp mc.delete_dashboard(target_dashboard.id)
29
+
@@ -0,0 +1,97 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+
9
+ # Regist two hosts
10
+ pp mc.post_host({
11
+ 'name' => 'web001',
12
+ 'meta' => {
13
+ 'agent-name' => 'mackerel-agent/0.6.1',
14
+ 'agent-revision' => 'bc2f9f6',
15
+ 'agent-version' => '0.6.1',
16
+ },
17
+ 'type' => 'unknown',
18
+ 'status' => 'working',
19
+ 'memo' => 'test web host',
20
+ 'isRetired' => false,
21
+ 'createdAt' => '1401291970',
22
+ 'roleFullnames' => [
23
+ 'mackerel:web'
24
+ ],
25
+ 'interfaces' => [{
26
+ "ipAddress" => "10.1.1.1",
27
+ "macAddress" => "08:00:27:ce:08:3d",
28
+ "name" => "eth0"
29
+ }]
30
+ })
31
+ pp mc.post_host({
32
+ 'name' => 'db001',
33
+ 'meta' => {
34
+ 'agent-name' => 'mackerel-agent/0.6.1',
35
+ 'agent-revision' => 'bc2f9f6',
36
+ 'agent-version' => '0.6.1',
37
+ },
38
+ 'type' => 'unknown',
39
+ 'status' => 'working',
40
+ 'memo' => 'test db host',
41
+ 'isRetired' => false,
42
+ 'createdAt' => '1401291976',
43
+ 'roleFullnames' => [
44
+ 'mackerel:db'
45
+ ],
46
+ 'interfaces' => [{
47
+ "ipAddress" => "10.0.0.1",
48
+ "macAddress" => "08:00:27:ce:08:3d",
49
+ "name" => "eth0"
50
+ }]
51
+ })
52
+
53
+ # get information of host
54
+ pp hosts = mc.get_hosts
55
+ target_host = hosts.first
56
+ pp mc.get_host(target_host.id)
57
+ pp mc.get_host_metric_names(target_host.id)
58
+
59
+ # add host metadata
60
+ namespace = "Scenario3"
61
+ pp mc.update_metadata(target_host.id, namespace, "metadata test")
62
+ pp metadata = mc.list_metadata(target_host.id)
63
+ pp mc.get_metadata(target_host.id, namespace)
64
+ pp mc.delete_metadata(target_host.id, namespace)
65
+
66
+ # Update the role and the status
67
+ pp mc.update_host_roles(target_host.id, [ "mackerel:web" ])
68
+ pp mc.update_host_status(target_host.id, 'poweroff')
69
+
70
+ # Update the host
71
+ pp mc.update_host(target_host.id,{
72
+ 'name' => 'db001',
73
+ 'meta' => {
74
+ 'agent-name' => 'mackerel-agent/0.6.1',
75
+ 'agent-revision' => 'bc2f9f6',
76
+ 'agent-version' => '0.6.1',
77
+ },
78
+ 'type' => 'unknown',
79
+ 'status' => 'working',
80
+ 'memo' => 'test host',
81
+ 'isRetired' => false,
82
+ 'createdAt' => '1401291976',
83
+ 'id' => target_host.id,
84
+ 'roleFullnames' => [
85
+ 'mackerel:db'
86
+ ],
87
+ 'interfaces' => [{
88
+ "ipAddress" => "10.0.0.1",
89
+ "macAddress" => "08:00:27:ce:08:3d",
90
+ "name" => "eth0"
91
+ }]
92
+ })
93
+
94
+ # retire the host
95
+ pp mc.retire_host(target_host.id)
96
+
97
+
@@ -0,0 +1,44 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+
9
+ host = mc.get_hosts.first
10
+ current_time = Time.now.to_i
11
+ rand = Random.new(current_time.to_i)
12
+
13
+
14
+ pp mc.get_service_metric_names("mackerel")
15
+
16
+
17
+ pp mc.get_host_metrics(host.id, "loadavg5", current_time - 6000, current_time)
18
+ pp mc.get_latest_metrics([host.id], ["loadavg5"])
19
+ pp mc.post_metrics([{
20
+ hostId: host.id,
21
+ name: "custom.host_metrics.example",
22
+ time: current_time,
23
+ value: rand.rand(20)
24
+ }])
25
+
26
+ pp mc.define_graphs([
27
+ {
28
+ "name" => "custom.define_graph",
29
+ "displayName" => "defined_graph",
30
+ "unit" => "percentage",
31
+ "metrics" => [
32
+ { "name" => "custom.define_graph.example", "displayName" => "Example", "isStacked" => false}
33
+ ]
34
+ }
35
+ ])
36
+
37
+ pp mc.post_service_metrics("mackerel", [{
38
+ name: "custom.define_graph.example",
39
+ time: current_time,
40
+ value: rand.rand(20)
41
+ }])
42
+
43
+ pp mc.get_service_metrics("mackerel", "custom.define_graph.example", current_time - 6000, current_time)
44
+
@@ -0,0 +1,10 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+
9
+ pp mc.get_services
10
+ pp mc.get_roles("mackerel")
@@ -0,0 +1,26 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+
9
+ current_time = Time.now.to_i
10
+ service = "mackerel"
11
+ pp mc.post_graph_annotation({
12
+ title: "First Annotation",
13
+ description: "Scenario Test Annotation",
14
+ from: current_time - 6000,
15
+ to: current_time - 60,
16
+ service: service
17
+ })
18
+ pp annotation = mc.get_graph_annotations(service, current_time - 6000, current_time).first
19
+ pp mc.update_graph_annotation(annotation.id, {
20
+ title: "Second Annotation",
21
+ description: "Scenario Test Annotation",
22
+ from: current_time - 6000,
23
+ to: current_time - 60,
24
+ service: service
25
+ })
26
+ pp mc.delete_graph_annotation(annotation.id)
@@ -0,0 +1,100 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+ rand = Random.new(Time.now.to_i)
9
+ current_time = Time.now.to_i
10
+
11
+ host = mc.get_hosts.first
12
+ pp mc.post_monitoring_check_report({
13
+ reports: [
14
+ {
15
+ name: "check-report-test",
16
+ status: "OK",
17
+ message: "YOKERO!",
18
+ occurredAt: current_time,
19
+ source: {
20
+ type: "host",
21
+ hostId: host.id
22
+ }
23
+ }
24
+ ]
25
+ })
26
+
27
+ pp mc.post_monitor({
28
+ type: "host",
29
+ name: "custom.define_graph.example",
30
+ memo: "This is monitor test.",
31
+ duration: 3,
32
+ metric: "custom.define_graph.example",
33
+ operator: ">",
34
+ warning: 30.0,
35
+ critical: 40.0,
36
+ notificationInterval: 60
37
+ })
38
+ pp monitor = mc.get_monitors.first
39
+
40
+ pp mc.update_monitor(monitor.id , {
41
+ type: "host",
42
+ name: "custom.define_graph.example",
43
+ memo: "This is monitor test.",
44
+ duration: 1,
45
+ metric: "custom.define_graph.example",
46
+ operator: ">",
47
+ warning: 5.0,
48
+ critical: 10.0,
49
+ notificationInterval: 60
50
+ })
51
+
52
+
53
+ pp channels = mc.get_channels().first
54
+
55
+ pp notification_group = mc.post_notification_group(
56
+ {
57
+ name: "Example notification group",
58
+ notificationLevel: "all",
59
+ childNotificationGroupIds: [],
60
+ childChannelIds: [
61
+ channels.id
62
+ ],
63
+ monitors: [
64
+ {
65
+ id: monitor.id,
66
+ skipDefault: false
67
+ }
68
+ ],
69
+ services: [
70
+ {
71
+ name: "mackerel"
72
+ }
73
+ ]
74
+ }
75
+ )
76
+
77
+ pp mc.update_notification_group(notification_group.id,{
78
+ name: "Example notification group(updated)",
79
+ notificationLevel: "all",
80
+ childNotificationGroupIds: [],
81
+ childChannelIds: [
82
+ channels.id
83
+ ],
84
+ monitors: [
85
+ {
86
+ id: monitor.id,
87
+ skipDefault: false
88
+ }
89
+ ],
90
+ services: [
91
+ {
92
+ name: "mackerel"
93
+ }
94
+ ]
95
+ }
96
+ )
97
+
98
+ pp mc.get_notification_groups()
99
+ mc.delete_notification_group(notification_group.id)
100
+ mc.delete_monitor(monitor.id)
@@ -0,0 +1,10 @@
1
+ require 'mackerel-client'
2
+ require 'dotenv'
3
+ require 'pp'
4
+ require 'time'
5
+
6
+ Dotenv.load
7
+ mc = Mackerel::Client.new(:mackerel_api_key => ENV['MACKEREL_APIKEY'])
8
+ pp alert = mc.get_alerts().first
9
+ pp mc.close_alert(alert.id,"Scenario Test") unless alert.nil?
10
+
data/example/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ source "https://rubygems.org"
3
+ gem "dotenv"
4
+ gem "json"
5
+ gem "mackerel-client", path: '../'
6
+ gem "pry"
@@ -0,0 +1,48 @@
1
+ module Mackerel
2
+
3
+ class Alert
4
+ attr_accessor :id, :status, :monitorId, :type, :hostId, :value, :message, :reason, :openedAt, :closedAt
5
+
6
+ def initialize(args = {})
7
+ @id = args["id"]
8
+ @status = args["status"]
9
+ @monitorId = args["monitorId"]
10
+ @type = args["type"]
11
+ @hostId = args["hostId"]
12
+ @value = args["value"]
13
+ @message = args["message"]
14
+ @reason = args["reason"]
15
+ @openedAt = args["openedAt"]
16
+ @closedAt = args["closedAt"]
17
+ end
18
+
19
+ def to_h
20
+ instance_variables.flat_map do |name|
21
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
22
+ end.each_with_object({}) do |name, hash|
23
+ hash[name] = public_send(name)
24
+ end.delete_if { |key, val| val == nil }
25
+ end
26
+
27
+ def to_json(options = nil)
28
+ return to_h.to_json(options)
29
+ end
30
+ end
31
+
32
+ module REST
33
+ module Alert
34
+ def get_alerts()
35
+ command = ApiCommand.new(:get, "/api/v0/alerts", @api_key)
36
+ data = command.execute(client)
37
+ data["alerts"].map { |a| Mackerel::Alert.new(a) }
38
+ end
39
+
40
+ def close_alert(alertId, reason)
41
+ command = ApiCommand.new(:post, "/api/v0/alerts/#{alertId}/close", @api_key)
42
+ command.body = { reason: reason.to_s }.to_json
43
+ data = command.execute(client)
44
+ Mackerel::Alert.new(data)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,61 @@
1
+ module Mackerel
2
+
3
+ class Annotation
4
+ attr_accessor :id, :service, :from, :to, :description, :title, :roles
5
+ def initialize(args = {})
6
+ @id = args["id"]
7
+ @service = args["service"]
8
+ @from = args["from"]
9
+ @to = args["to"]
10
+ @description = args["description"]
11
+ @title = args["title"]
12
+ @roles = args["roles"]
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 Annotation
31
+ def post_graph_annotation(annotation)
32
+ command = ApiCommand.new(:post, '/api/v0/graph-annotations', @api_key)
33
+ command.body = annotation.to_json
34
+ data = command.execute(client)
35
+ Mackerel::Annotation.new(data)
36
+ end
37
+
38
+ def get_graph_annotations(service, from, to)
39
+ command = ApiCommand.new(:get, '/api/v0/graph-annotations', @api_key)
40
+ command.params['service'] = service
41
+ command.params['from'] = from
42
+ command.params['to'] = to
43
+ data = command.execute(client)
44
+ data['graphAnnotations'].map{|a| Mackerel::Annotation.new(a)}
45
+ end
46
+
47
+ def update_graph_annotation(annotation_id, annotation)
48
+ command = ApiCommand.new(:put, "/api/v0/graph-annotations/#{annotation_id}", @api_key)
49
+ command.body = annotation.to_json
50
+ data = command.execute(client)
51
+ Mackerel::Annotation.new(data)
52
+ end
53
+
54
+ def delete_graph_annotation(annotation_id)
55
+ command = ApiCommand.new(:delete, "/api/v0/graph-annotations/#{annotation_id}", @api_key)
56
+ data = command.execute(client)
57
+ Mackerel::Annotation.new(data)
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,67 @@
1
+ require 'cgi'
2
+ require 'pp'
3
+
4
+ module Mackerel
5
+ class ApiCommand
6
+ METHODS = [:get, :delete, :put, :post]
7
+ JSON_CONTENT_TYPE = 'application/json'
8
+
9
+ attr_accessor :headers, :body, :params, :query
10
+
11
+ def initialize(method, path, api_key)
12
+ @path = path
13
+ @method = method
14
+ @api_key = api_key
15
+
16
+ @headers = {}
17
+ @body = ''
18
+ @params = {}
19
+ @query = {}
20
+ end
21
+
22
+ def execute(client)
23
+ return unless METHODS.include?(@method)
24
+
25
+ request_path = @path
26
+ request_path << "?#{make_escaped_query}" if @query.any?
27
+
28
+ client_method = client.method(@method)
29
+ response = client_method.call(request_path) do |req|
30
+ req.headers = @headers
31
+ req.headers['x-api-key'] = @api_key
32
+ req.headers['Content-Type'] ||= JSON_CONTENT_TYPE
33
+ req.params = @params
34
+ req.body = @body
35
+ end
36
+ check_status(response.status)
37
+
38
+ JSON.parse(response.body)
39
+ end
40
+
41
+ private
42
+
43
+ def check_status(status)
44
+ case status
45
+ when 200...300
46
+ nil
47
+ when 400
48
+ message ="Invalid parameter"
49
+ raise "#{@method.upcase} #{@path} failed: #{status} #{message}"
50
+ when 403
51
+ message ="Not authorized"
52
+ raise "#{@method.upcase} #{@path} failed: #{status} #{message}"
53
+ when 404
54
+ message ="Resource not found"
55
+ raise "#{@method.upcase} #{@path} failed: #{status} #{message}"
56
+ when 409
57
+ message ="Already exists"
58
+ raise "#{@method.upcase} #{@path} failed: #{status} #{message}"
59
+ end
60
+ end
61
+
62
+ def make_escaped_query
63
+ @query.map{|k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}"}.join("&")
64
+ end
65
+
66
+ end
67
+ end
@@ -0,0 +1,32 @@
1
+ module Mackerel
2
+ class Channel
3
+ attr_accessor :id, :name, :type
4
+ def initialize(args = {})
5
+ @id = args["id"]
6
+ @name = args["name"]
7
+ @type = args["type"]
8
+ end
9
+
10
+ def to_h
11
+ instance_variables.flat_map do |name|
12
+ respond_to?(name[1..-1]) ? [name[1..-1]] : []
13
+ end.each_with_object({}) do |name, hash|
14
+ hash[name] = public_send(name)
15
+ end.delete_if { |key, val| val == nil }
16
+ end
17
+
18
+ def to_json(options = nil)
19
+ return to_h.to_json(options)
20
+ end
21
+ end
22
+
23
+ module REST
24
+ module Channel
25
+ def get_channels()
26
+ command = ApiCommand.new(:get, '/api/v0/channels', @api_key)
27
+ data = command.execute(client)
28
+ data['channels'].map{|d| Mackerel::Channel.new(d) }
29
+ end
30
+ end
31
+ end
32
+ end