dogapi-demo 0.1.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 (61) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.tailor +106 -0
  5. data/.travis.yml +8 -0
  6. data/CHANGELOG.md +157 -0
  7. data/Gemfile +16 -0
  8. data/LICENSE +25 -0
  9. data/README.rdoc +143 -0
  10. data/Rakefile +50 -0
  11. data/dogapi-demo.gemspec +32 -0
  12. data/examples/Capfile +19 -0
  13. data/examples/custom_event.rb +37 -0
  14. data/examples/custom_metric.rb +35 -0
  15. data/lib/capistrano/README.md +13 -0
  16. data/lib/capistrano/datadog.rb +125 -0
  17. data/lib/capistrano/datadog/v2.rb +74 -0
  18. data/lib/capistrano/datadog/v3.rb +64 -0
  19. data/lib/dogapi-demo.rb +5 -0
  20. data/lib/dogapi-demo/common.rb +168 -0
  21. data/lib/dogapi-demo/event.rb +129 -0
  22. data/lib/dogapi-demo/facade.rb +475 -0
  23. data/lib/dogapi-demo/metric.rb +34 -0
  24. data/lib/dogapi-demo/v1.rb +13 -0
  25. data/lib/dogapi-demo/v1/alert.rb +112 -0
  26. data/lib/dogapi-demo/v1/comment.rb +62 -0
  27. data/lib/dogapi-demo/v1/dash.rb +94 -0
  28. data/lib/dogapi-demo/v1/embed.rb +106 -0
  29. data/lib/dogapi-demo/v1/event.rb +101 -0
  30. data/lib/dogapi-demo/v1/metric.rb +118 -0
  31. data/lib/dogapi-demo/v1/monitor.rb +264 -0
  32. data/lib/dogapi-demo/v1/screenboard.rb +110 -0
  33. data/lib/dogapi-demo/v1/search.rb +27 -0
  34. data/lib/dogapi-demo/v1/service_check.rb +32 -0
  35. data/lib/dogapi-demo/v1/snapshot.rb +30 -0
  36. data/lib/dogapi-demo/v1/tag.rb +141 -0
  37. data/lib/dogapi-demo/v1/user.rb +113 -0
  38. data/lib/dogapi-demo/version.rb +3 -0
  39. data/spec/alerts_spec.rb +33 -0
  40. data/spec/common_spec.rb +37 -0
  41. data/spec/facade_spec.rb +166 -0
  42. data/spec/spec_helper.rb +30 -0
  43. data/spec/support/cassettes/Alerts/create/returns_HTTP_code_200.yml +114 -0
  44. data/spec/support/cassettes/Alerts/create/returns_a_valid_event_ID.yml +114 -0
  45. data/spec/support/cassettes/Alerts/create/returns_the_same_query_as_sent.yml +114 -0
  46. data/spec/support/cassettes/Facade/Events/emits_aggregate_events.yml +193 -0
  47. data/spec/support/cassettes/Facade/Events/emits_events_and_retrieves_them.yml +100 -0
  48. data/spec/support/cassettes/Facade/Events/emits_events_with_specified_priority.yml +98 -0
  49. data/spec/support/cassettes/Facade/Tags/adds_updates_and_detaches_tags.yml +442 -0
  50. data/tests/test_alerts.rb +38 -0
  51. data/tests/test_base.rb +30 -0
  52. data/tests/test_client.rb +23 -0
  53. data/tests/test_comments.rb +39 -0
  54. data/tests/test_dashes.rb +85 -0
  55. data/tests/test_embed.rb +194 -0
  56. data/tests/test_monitors.rb +192 -0
  57. data/tests/test_screenboard.rb +90 -0
  58. data/tests/test_search.rb +20 -0
  59. data/tests/test_snapshot.rb +28 -0
  60. data/tests/test_users.rb +65 -0
  61. metadata +178 -0
@@ -0,0 +1,38 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestAlerts < Minitest::Test
6
+ include TestBase
7
+
8
+ def test_alerts
9
+ dog = Dogapi::Client.new(@api_key, @app_key)
10
+
11
+ query = 'avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100'
12
+
13
+ alert_id = dog.alert(query)[1]['id']
14
+ status, alert = dog.get_alert(alert_id)
15
+ assert_equal alert['query'], query, alert['query']
16
+ assert_equal alert['silenced'], false, alert['silenced']
17
+
18
+ dog.update_alert(alert_id, query, :silenced => true)
19
+ status, alert = dog.get_alert(alert_id)
20
+ assert_equal alert['query'], query, alert['query']
21
+ assert_equal alert['silenced'], true, alert['silenced']
22
+
23
+ dog.delete_alert(alert_id)
24
+
25
+ query1 = 'avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100'
26
+ query2 = 'avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 200'
27
+
28
+ alert_id1 = dog.alert(query1)[1]['id']
29
+ alert_id2 = dog.alert(query2)[1]['id']
30
+ status, alerts = dog.get_all_alerts()
31
+ alerts = alerts['alerts']
32
+ alert1 = alerts.select {|a| a['id'] == alert_id1}
33
+ alert2 = alerts.select {|a| a['id'] == alert_id2}
34
+ assert_equal alert1[0]['query'], query1, alert1
35
+ assert_equal alert2[0]['query'], query2, alert2
36
+
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ require 'minitest/autorun'
2
+ require 'dogapi'
3
+ require 'time'
4
+
5
+ module TestBase
6
+
7
+ def config_client_test_env
8
+ @api_key = ENV['DATADOG_API_KEY']
9
+ @app_key = ENV['DATADOG_APP_KEY']
10
+ if not @api_key or not @app_key
11
+ puts "\n"
12
+ abort "To run tests in your environment, set 'DATADOG_API_KEY' and 'DATADOG_APP_KEY' to appropriate values for your account. Be aware that the tests will submit data, some of which won't be removed at the end.\n\n"
13
+ end
14
+ end
15
+
16
+ def setup
17
+ config_client_test_env()
18
+ end
19
+
20
+ def random
21
+ Kernel.rand 100000
22
+ end
23
+
24
+ def job_number
25
+ # Get the Travis ENV and use that to 'seed' the dashboard titles, in attempt
26
+ # to prevent parallel testing conflicts
27
+ job_number = ENV['TRAVIS_JOB_NUMBER'] || '1'
28
+ end
29
+
30
+ end
@@ -0,0 +1,23 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestClient < Minitest::Test
6
+ include TestBase
7
+
8
+ def test_find_localhost
9
+ val = Dogapi.find_localhost
10
+ assert !val.nil?
11
+ end
12
+
13
+ def test_metrics
14
+ # FIXME: actually verify this once there's a way to look at metrics through the api
15
+ dog = Dogapi::Client.new(@api_key, @app_key)
16
+ dog_r = Dogapi::Client.new(@api_key)
17
+
18
+ dog_r.emit_point('test.metric.metric', 10, :host => 'test.metric.host')
19
+ dog_r.emit_points('test.metric.metric', [[Time.now-5*60, 0]], :host => 'test.metric.host')
20
+
21
+ dog_r.emit_points('test.metric.metric', [[Time.now-60, 20], [Time.now-30, 10], [Time.now, 5]], :tags => ["test:tag.1", "test:tag2"])
22
+ end
23
+ end
@@ -0,0 +1,39 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestComments < Minitest::Test
6
+ include TestBase
7
+
8
+
9
+ def test_comments
10
+ dog = Dogapi::Client.new(@api_key, @app_key)
11
+
12
+ # Create a comment
13
+ dog.comment('test comment')
14
+
15
+ # Create a comment as a user.
16
+ handle = "carlo+14.1@datadoghq.com"
17
+ status, comment_response = dog.comment('test comment with handle', :handle => handle)
18
+ comment = comment_response["comment"]
19
+ assert_equal "200", status, "Comment did not succeed, response: #{comment_response}"
20
+
21
+ sleep 1
22
+
23
+ # Reply to a comment.
24
+ status, reply_response = dog.comment('replying!', :related_event_id => comment["id"])
25
+ reply = reply_response["comment"]
26
+ assert_equal "200", status, "Reply did not work, response: #{reply_response}"
27
+ # HACK: issue #900 on dogweb. id types should be the same.
28
+ assert_equal comment["id"].to_s, reply["related_event_id"]
29
+
30
+ # Update a comment.
31
+ dog.update_comment(reply["related_event_id"], :message => "Updating the comment")
32
+
33
+ # Delete a comment.
34
+ status, to_delete_response = dog.comment("im dead")
35
+ to_delete = to_delete_response["comment"]
36
+ dog.delete_comment(to_delete["id"])
37
+
38
+ end
39
+ end
@@ -0,0 +1,85 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestDashes < Minitest::Test
6
+ include TestBase
7
+
8
+ def test_dashes
9
+ dog = Dogapi::Client.new(@api_key, @app_key)
10
+ now = Time.now.to_i.to_s
11
+
12
+ # Create a dashboard.
13
+ title = "foobar-#{job_number}-#{now}"
14
+ description = 'desc'
15
+ graphs = [{
16
+ "definition" => {
17
+ "events" => [],
18
+ "requests" => [
19
+ {"q" => "avg:system.mem.free{*}"}
20
+ ],
21
+ "viz" => "timeseries"
22
+ },
23
+ "title" => "Average Memory Free"
24
+ }]
25
+
26
+ status, dash_response = dog.create_dashboard(title, description, graphs)
27
+ assert_equal "200", status, "Creation failed, response: #{dash_response}"
28
+
29
+ dash_id = dash_response["dash"]["id"]
30
+
31
+ # Fetch the dashboard and assert all is well.
32
+ status, dash_response = dog.get_dashboard(dash_id)
33
+ assert_equal "200", status, "Fetch failed, response: #{dash_response}"
34
+ dash = dash_response["dash"]
35
+ assert_equal title, dash["title"]
36
+ assert_equal description, dash["description"]
37
+ assert_equal graphs, dash["graphs"]
38
+
39
+ # Update the dashboard.
40
+ title = "blahfoobar-#{job_number}-#{now}"
41
+ description = 'asdfdesc'
42
+ graphs = [{
43
+ "definition" => {
44
+ "events" => [],
45
+ "requests" => [
46
+ {"q" => "sum:system.mem.free{*}"}
47
+ ],
48
+ "viz" => "timeseries"
49
+ },
50
+ "title" => "Sum Memory Free"
51
+ }]
52
+ tpl_vars = [{"default" => nil, "prefix" => nil, "name" => "foo"},
53
+ {"default" => nil, "prefix" => nil, "name" => "bar"}]
54
+
55
+ status, dash_response = dog.update_dashboard(dash_id, title, description, graphs, tpl_vars)
56
+ assert_equal "200", status, "Updated failed, response: #{dash_response}"
57
+
58
+ # Fetch the dashboard and assert all is well.
59
+ status, dash_response = dog.get_dashboard(dash_id)
60
+ assert_equal "200", status, "Fetch failed, response: #{dash_response}"
61
+ dash = dash_response["dash"]
62
+ assert_equal title, dash["title"]
63
+ assert_equal description, dash["description"]
64
+ assert_equal graphs, dash["graphs"]
65
+ assert_equal tpl_vars, dash["template_variables"]
66
+
67
+ # Fetch all the dashboards, assert our created one is in the list of all
68
+ status, dash_response = dog.get_dashboards()
69
+ assert_equal "200", status, "fetch failed, response: #{dash_response}"
70
+ dashes = dash_response["dashes"]
71
+ assert dashes.any? { |d| title == d["title"] }
72
+ dash = dashes.find { |d| title == d["title"] }
73
+ assert_equal title, dash["title"]
74
+ assert_equal dash_id.to_s, dash["id"]
75
+
76
+ # Delete the dashboard.
77
+ status, dash_response = dog.delete_dashboard(dash_id)
78
+ assert_equal "204", status, "Deleted failed, response: #{dash_response}"
79
+
80
+ # Fetch the dashboard and assert all it's gone.
81
+ status, dash_response = dog.get_dashboard(dash_id)
82
+ assert_equal "404", status, "Still there failed, response: #{dash_response}"
83
+
84
+ end
85
+ end
@@ -0,0 +1,194 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestEmbed < Minitest::Test
6
+ include TestBase
7
+
8
+ def test_get_all_embeds
9
+ dog = Dogapi::Client.new(@api_key, @app_key)
10
+
11
+ # Attempt API Call
12
+ status, result = dog.get_all_embeds()
13
+ # Sanity check results
14
+ assert_equal status, "200", "invalid HTTP response => #{status}"
15
+ assert result.has_key?("embedded_graphs")
16
+ end
17
+
18
+ def test_create_embed
19
+ dog = Dogapi::Client.new(@api_key, @app_key)
20
+ # create an embed using the embed id and verify that it is valid
21
+ graph_json = '{
22
+ "viz": "toplist",
23
+ "requests": [{
24
+ "q": "top(system.disk.free{$var} by {device}, 10, \'mean\', \'desc\')",
25
+ "style": {
26
+ "palette": "dog_classic"
27
+ },
28
+ "conditional_formats": [{
29
+ "palette": "red",
30
+ "comparator": ">",
31
+ "value": 50000000000
32
+ }, {
33
+ "palette": "green",
34
+ "comparator": ">",
35
+ "value": 30000000000
36
+ }]
37
+ }]
38
+ }'
39
+ timeframe = "1_hour"
40
+ size = "medium"
41
+ legend = "no"
42
+ title = "Sick Title!"
43
+ description = {
44
+ :timeframe => timeframe,
45
+ :size => size,
46
+ :legend => legend,
47
+ :title => title
48
+ }
49
+ status, result = dog.create_embed(graph_json, description)
50
+ # Sanity check results
51
+ assert result["graph_title"] == title
52
+ assert_equal status, "200", "invalid HTTP response => #{status}"
53
+ assert result["html"].include? "legend=false"
54
+ assert result["html"].include? "height=300"
55
+ assert result["html"].include? "width=600"
56
+ end
57
+
58
+ def test_get_embed
59
+ dog = Dogapi::Client.new(@api_key, @app_key)
60
+ # Create an Embed to get a valid embed_id
61
+ graph_json = '{
62
+ "viz": "toplist",
63
+ "requests": [{
64
+ "q": "top(system.disk.free{$var} by {device}, 10, \'mean\', \'desc\')",
65
+ "style": {
66
+ "palette": "dog_classic"
67
+ },
68
+ "conditional_formats": [{
69
+ "palette": "red",
70
+ "comparator": ">",
71
+ "value": 50000000000
72
+ }, {
73
+ "palette": "green",
74
+ "comparator": ">",
75
+ "value": 30000000000
76
+ }]
77
+ }]
78
+ }'
79
+ timeframe = "1_hour"
80
+ size = "medium"
81
+ legend = "no"
82
+ title = "Sick Title!"
83
+ description = {
84
+ :timeframe => timeframe,
85
+ :size => size,
86
+ :legend => legend,
87
+ :title => title
88
+ }
89
+ status, create_result = dog.create_embed(graph_json, description)
90
+ # Sanity check results
91
+ assert_equal status, "200", "invalid HTTP response => #{status}"
92
+ assert create_result["html"].include? "legend=false"
93
+ assert create_result["html"].include? "height=300"
94
+ assert create_result["html"].include? "width=600"
95
+ embed_id = create_result["embed_id"]
96
+ # Get the embed using that embed id
97
+ status, embed_without_var = dog.get_embed(embed_id)
98
+ assert_equal status, "200", "invalid HTTP response => #{status}"
99
+ get_embed_description = {
100
+ :size => size,
101
+ :legend => legend,
102
+ :var => "val"
103
+ }
104
+ status, embed_with_var = dog.get_embed(embed_id, get_embed_description)
105
+ assert_equal status, "200", "invalid HTTP response => #{status}"
106
+ # Verify that the get requests are good
107
+ assert embed_without_var["html"] == create_result["html"]
108
+ assert embed_with_var["html"] != embed_without_var["html"]
109
+ assert embed_with_var["html"].include? "legend=false"
110
+ assert embed_with_var["html"].include? "height=300"
111
+ assert embed_with_var["html"].include? "width=600"
112
+ assert embed_with_var["html"].include? "var=val"
113
+ end
114
+
115
+ def test_enable_embed
116
+ dog = Dogapi::Client.new(@api_key, @app_key)
117
+ # Create an Embed to get a valid embed_id
118
+ graph_json = '{
119
+ "viz": "toplist",
120
+ "requests": [{
121
+ "q": "top(system.disk.free{$var} by {device}, 10, \'mean\', \'desc\')",
122
+ "style": {
123
+ "palette": "dog_classic"
124
+ },
125
+ "conditional_formats": [{
126
+ "palette": "red",
127
+ "comparator": ">",
128
+ "value": 50000000000
129
+ }, {
130
+ "palette": "green",
131
+ "comparator": ">",
132
+ "value": 30000000000
133
+ }]
134
+ }]
135
+ }'
136
+ timeframe = "1_hour"
137
+ size = "medium"
138
+ legend = "no"
139
+ title = "Sick Title!"
140
+ description = {
141
+ :timeframe => timeframe,
142
+ :size => size,
143
+ :legend => legend,
144
+ :title => title
145
+ }
146
+ status, create_result = dog.create_embed(graph_json, description)
147
+ # Sanity check results
148
+ assert_equal status, "200", "invalid HTTP response => #{status}"
149
+ embed_id = create_result["embed_id"]
150
+ status, result = dog.enable_embed(embed_id)
151
+ assert_equal status, "200", "invalid HTTP response => #{status}"
152
+ assert result.has_key?("success")
153
+ end
154
+
155
+ def test_revoke_embed
156
+ dog = Dogapi::Client.new(@api_key, @app_key)
157
+ # Create an Embed to get a valid embed_id
158
+ graph_json = '{
159
+ "viz": "toplist",
160
+ "requests": [{
161
+ "q": "top(system.disk.free{$var} by {device}, 10, \'mean\', \'desc\')",
162
+ "style": {
163
+ "palette": "dog_classic"
164
+ },
165
+ "conditional_formats": [{
166
+ "palette": "red",
167
+ "comparator": ">",
168
+ "value": 50000000000
169
+ }, {
170
+ "palette": "green",
171
+ "comparator": ">",
172
+ "value": 30000000000
173
+ }]
174
+ }]
175
+ }'
176
+ timeframe = "1_hour"
177
+ size = "medium"
178
+ legend = "no"
179
+ title = "Sick Title!"
180
+ description = {
181
+ :timeframe => timeframe,
182
+ :size => size,
183
+ :legend => legend,
184
+ :title => title
185
+ }
186
+ status, create_result = dog.create_embed(graph_json, description)
187
+ # Sanity check results
188
+ assert_equal status, "200", "invalid HTTP response => #{status}"
189
+ embed_id = create_result["embed_id"]
190
+ status, result = dog.revoke_embed(embed_id)
191
+ assert_equal status, "200", "invalid HTTP response => #{status}"
192
+ assert result.has_key?("success")
193
+ end
194
+ end
@@ -0,0 +1,192 @@
1
+ require 'dogapi'
2
+ require 'time'
3
+ require 'test_base.rb'
4
+
5
+ class TestAlerts < Minitest::Test
6
+ include TestBase
7
+
8
+ def test_metric_alerts
9
+ dog = Dogapi::Client.new(@api_key, @app_key)
10
+
11
+ query = 'sum(last_1d):sum:system.net.bytes_rcvd{host:host0} > 100'
12
+ options = {
13
+ 'silenced' => {'*' => nil},
14
+ 'notify_no_data' => false
15
+ }
16
+
17
+ monitor_id = dog.monitor('metric alert', query, :options=>options)[1]['id']
18
+ status, monitor = dog.get_monitor(monitor_id)
19
+ assert_equal monitor['query'], query, monitor['query']
20
+ assert_equal monitor['options']['silenced'], options['silenced'], monitor['options']
21
+
22
+ query2 = 'avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 200'
23
+ updated_monitor_id = dog.update_monitor(monitor_id, query2,
24
+ :options => options)[1]['id']
25
+ status, monitor = dog.get_monitor(updated_monitor_id)
26
+ assert_equal monitor['query'], query2, monitor['query']
27
+
28
+ name = 'test_monitors'
29
+ monitor_id = dog.update_monitor(monitor_id, query2, :name => name,
30
+ :options=>{'notify_no_data' => true})[1]['id']
31
+ status, monitor = dog.get_monitor(monitor_id)
32
+ assert_equal monitor['name'], name, monitor['name']
33
+ assert_equal monitor['options']['notify_no_data'], true, monitor['options']
34
+
35
+ dog.delete_monitor(monitor_id)
36
+ status, monitor = dog.get_monitor(monitor_id)
37
+ assert_equal status.to_i, 404, status.to_i
38
+
39
+ query1 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100"
40
+ query2 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 200"
41
+ query3 = "avg(last_1h):sum:system.net.bytes_rcvd{host:host1} > 200"
42
+
43
+ monitor_id1 = dog.monitor('metric alert', query1)[1]['id']
44
+ monitor_id2 = dog.monitor('metric alert', query2)[1]['id']
45
+ monitor_id3 = dog.monitor('metric alert', query3)[1]['id']
46
+ status, monitors = dog.get_all_monitors(:group_states => ['alert', 'warn'])
47
+ monitor1 = monitors.map{|m| m if m['id'] == monitor_id1}.compact[0]
48
+ monitor2 = monitors.map{|m| m if m['id'] == monitor_id2}.compact[0]
49
+ assert_equal monitor1['query'], query1, monitor1['query']
50
+ assert_equal monitor2['query'], query2, monitor2['query']
51
+
52
+ status, monitors = dog.get_all_monitors(:tags => ['host:host1'])
53
+ monitor3 = monitors.map{|m| m if m['id'] == monitor_id3}.compact[0]
54
+ assert_equal monitor3['query'], query3, monitor3['query']
55
+ assert_equal nil, monitors.map{|m| m if m['id'] == monitor_id1}.compact[0]
56
+ assert_equal nil, monitors.map{|m| m if m['id'] == monitor_id2}.compact[0]
57
+
58
+ dog.delete_monitor(monitor_id1)
59
+ dog.delete_monitor(monitor_id2)
60
+ dog.delete_monitor(monitor_id3)
61
+ end
62
+
63
+ def test_checks
64
+ dog = Dogapi::Client.new(@api_key, @app_key)
65
+
66
+ query = '"ntp.in_sync".over("role:herc").last(3).count_by_status()'
67
+ options = {
68
+ 'notify_no_data' => false,
69
+ 'thresholds' => {
70
+ 'ok' => 3,
71
+ 'warning' => 2,
72
+ 'critical' => 1,
73
+ 'no data' => 3
74
+ }
75
+ }
76
+ monitor_id = dog.monitor('service check', query, :options => options)[1]['id']
77
+ status, monitor = dog.get_monitor(monitor_id, :group_states => ['all'])
78
+
79
+ assert_equal monitor['query'], query, monitor['query']
80
+ assert_equal monitor['options']['notify_no_data'], options['notify_no_data'], monitor['options']
81
+ assert_equal monitor['options']['thresholds'], options['thresholds'], monitor['options']
82
+
83
+ query2 = '"ntp.in_sync".over("role:sobotka").last(3).count_by_status()'
84
+ monitor_id = dog.update_monitor(monitor_id, query2)[1]['id']
85
+ status, monitor = dog.get_monitor(monitor_id)
86
+ assert_equal monitor['query'], query2, monitor['query']
87
+
88
+ dog.delete_monitor(monitor_id)
89
+ status, monitor = dog.get_monitor(monitor_id)
90
+ assert_equal status.to_i, 404, status.to_i
91
+ end
92
+
93
+ def test_monitor_muting
94
+ dog = Dogapi::Client.new(@api_key, @app_key)
95
+
96
+ query = 'avg(last_1h):sum:system.net.bytes_rcvd{host:host0} > 100'
97
+ monitor_id = dog.monitor('metric alert', query)[1]['id']
98
+ status, monitor = dog.get_monitor(monitor_id)
99
+ assert_equal monitor['query'], query, monitor['query']
100
+
101
+ status, dt = dog.mute_monitors()
102
+ assert_equal dt['active'], true, dt['active']
103
+ assert_equal dt['scope'], ['*'], dt['scope']
104
+
105
+ status, dt = dog.unmute_monitors()
106
+ assert_equal status.to_i, 204, status.to_i
107
+
108
+ # We shouldn't be able to mute a simple alert on a scope.
109
+ status, _ = dog.mute_monitor(monitor_id, :scope => 'env:staging')
110
+ assert_equal status.to_i, 400, status.to_i
111
+
112
+ query2 = 'avg(last_1h):sum:system.net.bytes_rcvd{*} by {host} > 100'
113
+ monitor_id = dog.monitor('metric alert', query2)[1]['id']
114
+ status, monitor = dog.get_monitor(monitor_id)
115
+ assert_equal monitor['query'], query2, monitor['query']
116
+
117
+ dog.mute_monitor(monitor_id, :scope => 'host:foo')
118
+ status, monitor = dog.get_monitor(monitor_id)
119
+ assert_equal monitor['options']['silenced'], {'host:foo' => nil}, monitor['options']
120
+
121
+ dog.unmute_monitor(monitor_id, :scope => 'host:foo')
122
+ status, monitor = dog.get_monitor(monitor_id)
123
+ assert_equal monitor['options']['silenced'], {}, monitor['options']
124
+
125
+ dog.delete_monitor(monitor_id)
126
+ end
127
+
128
+ def test_downtime
129
+ dog = Dogapi::Client.new(@api_key, @app_key)
130
+ start_ts = Time.now.to_i
131
+ end_ts = start_ts + 1000
132
+ downtime_id = dog.schedule_downtime('env:staging', :start => start_ts,
133
+ :end => end_ts, :message=>'Message!')[1]['id']
134
+ status, dt = dog.get_downtime(downtime_id)
135
+ assert_equal dt['start'], start_ts, dt['start']
136
+ assert_equal dt['end'], end_ts, dt['end']
137
+ assert_equal dt['scope'], ['env:staging'], dt['scope']
138
+ assert_equal dt['message'], 'Message!', dt['messsage']
139
+
140
+ dog.update_downtime(downtime_id, :start => start_ts + 1, :end => end_ts + 1,
141
+ :scope => 'env:prod', :message => 'New Message!')
142
+ status, dt = dog.get_downtime(downtime_id)
143
+ assert_equal dt['start'], start_ts + 1, dt['start']
144
+ assert_equal dt['end'], end_ts + 1, dt['end']
145
+ assert_equal dt['scope'], ['env:prod'], dt['scope']
146
+ assert_equal dt['message'], 'New Message!', dt['messsage']
147
+
148
+ dog.cancel_downtime(downtime_id)
149
+ end
150
+
151
+ def test_host_muting
152
+ dog = Dogapi::Client.new(@api_key, @app_key)
153
+
154
+ hostname = 'test.host%d' % rand(10000)
155
+
156
+ # Reset test
157
+ dog.unmute_host(hostname)
158
+
159
+ message = "Muting this host for a test."
160
+ end_ts = Time.now.to_i + 60 * 60
161
+
162
+ res_code, res = dog.mute_host(hostname, :end => end_ts, :message => message)
163
+ assert_equal res_code, "200", res_code
164
+ assert_equal res["action"], "Muted", res["action"]
165
+ assert_equal res["hostname"], hostname, res["hostname"]
166
+ assert_equal res["message"], message, res["message"]
167
+ assert_equal res["end"], end_ts, res["end"]
168
+
169
+ # muting the same host multiple times should fail unless override is true
170
+ end_ts2 = end_ts + 60 * 15
171
+ res_code, res = dog.mute_host(hostname, :end => end_ts2)
172
+ assert_equal res_code, "400", res_code
173
+
174
+ res_code, res = dog.mute_host(hostname, :end => end_ts2, :override => true)
175
+ assert_equal res_code, "200", res_code
176
+ assert_equal res["action"], "Muted", res["action"]
177
+ assert_equal res["hostname"], hostname, res["hostname"]
178
+ assert_equal res["end"], end_ts2, res["end"]
179
+
180
+ res_code, res = dog.unmute_host(hostname)
181
+ assert_equal res_code, "200", res_code
182
+ assert_equal res["action"], "Unmuted", res["action"]
183
+ assert_equal res["hostname"], hostname, res["hostname"]
184
+ end
185
+
186
+ def test_service_checks
187
+ dog = Dogapi::Client.new(@api_key, @app_key)
188
+ status, response = dog.service_check('app.ok', 'app1', 1)
189
+ assert_equal status.to_i, 202
190
+ end
191
+
192
+ end