hawkular-client 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -3
  3. data/CHANGES.rdoc +8 -0
  4. data/README.rdoc +12 -5
  5. data/hawkularclient.gemspec +2 -2
  6. data/lib/alerts/alerts_api.rb +193 -0
  7. data/lib/{hawkularclient.rb → hawkular.rb} +93 -52
  8. data/lib/hawkular_all.rb +7 -0
  9. data/lib/inventory/inventory_api.rb +378 -0
  10. data/lib/metrics/metric_api.rb +20 -10
  11. data/lib/metrics/metrics_client.rb +52 -0
  12. data/lib/metrics/tenant_api.rb +1 -1
  13. data/lib/metrics/version.rb +3 -1
  14. data/lib/version.rb +8 -0
  15. data/spec/integration/alerts_spec.rb +143 -0
  16. data/spec/integration/inventory_spec.rb +191 -0
  17. data/spec/integration/metric_spec.rb +33 -0
  18. data/spec/spec_helper.rb +14 -1
  19. data/spec/unit/base_spec.rb +57 -0
  20. data/spec/vcr_cassettes/Alert/Alerts/Should_acknowledge_an_alert.yml +183 -0
  21. data/spec/vcr_cassettes/Alert/Alerts/Should_fetch_single_alert.yml +69 -0
  22. data/spec/vcr_cassettes/Alert/Alerts/Should_list_alerts.yml +85 -0
  23. data/spec/vcr_cassettes/Alert/Alerts/Should_list_alerts_for_trigger.yml +142 -0
  24. data/spec/vcr_cassettes/Alert/Alerts/Should_list_alerts_for_unknown_trigger.yml +46 -0
  25. data/spec/vcr_cassettes/Alert/Alerts/Should_resolve_an_alert.yml +181 -0
  26. data/spec/vcr_cassettes/Alert/Alerts/Should_resolve_an_alert2.yml +49 -0
  27. data/spec/vcr_cassettes/Alert/Triggers/Should_List_Triggers.yml +62 -0
  28. data/spec/vcr_cassettes/Alert/Triggers/Should_List_Triggers_for_ID.yml +55 -0
  29. data/spec/vcr_cassettes/Alert/Triggers/Should_List_Triggers_for_Tag.yml +68 -0
  30. data/spec/vcr_cassettes/Alert/Triggers/Should_List_Triggers_for_Tags.yml +68 -0
  31. data/spec/vcr_cassettes/Alert/Triggers/Should_get_a_single_Trigger_with_conditions.yml +138 -0
  32. data/spec/vcr_cassettes/Alert/Triggers/Should_get_a_single_metric_Trigger.yml +50 -0
  33. data/spec/vcr_cassettes/Gauge_metrics/Platform_mem.yml +44 -0
  34. data/spec/vcr_cassettes/Gauge_metrics/Platform_mem_def.yml +45 -0
  35. data/spec/vcr_cassettes/Inventory/Should_List_datasources_with_no_props.yml +228 -0
  36. data/spec/vcr_cassettes/Inventory/Should_list_URLs.yml +105 -0
  37. data/spec/vcr_cassettes/Inventory/Should_list_WildFlys.yml +104 -0
  38. data/spec/vcr_cassettes/Inventory/Should_list_WildFlys_with_props.yml +162 -0
  39. data/spec/vcr_cassettes/Inventory/Should_list_children_of_WildFly.yml +180 -0
  40. data/spec/vcr_cassettes/Inventory/Should_list_feeds.yml +102 -0
  41. data/spec/vcr_cassettes/Inventory/Should_list_heap_metrics_for_WildFlys.yml +308 -0
  42. data/spec/vcr_cassettes/Inventory/Should_list_metrics_for_WildFlys.yml +172 -0
  43. data/spec/vcr_cassettes/Inventory/Should_list_types_with_bad_feed.yml +101 -0
  44. data/spec/vcr_cassettes/Inventory/Should_list_types_with_feed.yml +109 -0
  45. data/spec/vcr_cassettes/Inventory/Should_list_types_without_feed.yml +110 -0
  46. data/spec/vcr_cassettes/Metrics/Status.yml +44 -0
  47. data/spec/vcr_cassettes/No_Tenant/Should_fail.yml +42 -0
  48. data/spec/vcr_cassettes/Tenants/Should_Get_Tenant_For_Explicit_Credentials.yml +50 -0
  49. data/spec/vcr_cassettes/Tenants/Should_Get_Tenant_For_Implicit_Credentials.yml +50 -0
  50. metadata +74 -3
@@ -0,0 +1,52 @@
1
+
2
+ require 'hawkular'
3
+ require 'json'
4
+ require 'rest-client'
5
+ require 'English'
6
+
7
+ require 'metrics/types'
8
+ require 'metrics/tenant_api'
9
+ require 'metrics/metric_api'
10
+
11
+ # Metrics module provides access to Hawkular Metrics REST API
12
+ # @see http://www.hawkular.org/docs/rest/rest-metrics.html Hawkular Metrics REST API Documentation
13
+ # @example Create Hawkular-Metrics client and start pushing some metric data
14
+ # # create client instance
15
+ # client = Hawkular::Metrics::Client::new("http://server","username",
16
+ # "password",{"tenant" => "your tenant ID"})
17
+ # # push gauge metric data for metric called "myGauge" (no need to create metric definition
18
+ # # unless you wish to specify data retention)
19
+ # client.gauges.push_data("myGauge", {:value => 3.1415925})
20
+ module Hawkular::Metrics
21
+ class Client < Hawkular::BaseClient
22
+ # @return [Tenants] access tenants API
23
+ attr_reader :tenants
24
+ # @return [Counters] access counters API
25
+ attr_reader :counters
26
+ # @return [Gauges] access gauges API
27
+ attr_reader :gauges
28
+ # @return [Availability] access counters API
29
+ attr_reader :avail
30
+
31
+ # Construct a new Hawkular Metrics client class.
32
+ # optional parameters
33
+ # @param entrypoint [String] Base url of the Hawkular (metrics) server
34
+ # @param credentials Hash of username, password, token(optional)
35
+ # @param options [Hash{String=>String}] client options
36
+ # @example initialize with Hawkular-tenant option
37
+ # Hawkular::Metrics::Client::new("http://server",
38
+ # {username:"username",password:"password"},
39
+ # {"tenant" => "your tenant ID"})
40
+ #
41
+ def initialize(entrypoint = 'http://localhost:8080/hawkular/metrics',
42
+ credentials = {},
43
+ options = {})
44
+
45
+ super(entrypoint, credentials, options)
46
+ @tenants = Client::Tenants.new self
47
+ @counters = Client::Counters.new self
48
+ @gauges = Client::Gauges.new self
49
+ @avail = Client::Availability.new self
50
+ end
51
+ end
52
+ end
@@ -1,5 +1,5 @@
1
1
  module Hawkular::Metrics
2
- class Client
2
+ class Client < Hawkular::BaseClient
3
3
  # Provides access to tenants API
4
4
  class Tenants
5
5
  # @param client [Client]
@@ -1,5 +1,7 @@
1
+ require 'hakwular'
2
+
1
3
  module Hawkular
2
4
  module Metrics
3
- VERSION = '0.1.2'
5
+ VERSION = Hawkular.VERSION
4
6
  end
5
7
  end
data/lib/version.rb ADDED
@@ -0,0 +1,8 @@
1
+ # Module to talk to Hawkular (http://hawkular.org) components.
2
+ # Submodules are provided for individual components
3
+ # @see http://hawkular.org
4
+ # @see https://github.com/hawkular
5
+ module Hawkular
6
+ # Version of the Hawkular Ruby Gem
7
+ VERSION = '0.2.0'
8
+ end
@@ -0,0 +1,143 @@
1
+ require "#{File.dirname(__FILE__)}/../vcr/vcr_setup"
2
+ require "#{File.dirname(__FILE__)}/../spec_helper"
3
+
4
+ module Hawkular::Alerts::RSpec
5
+ ALERTS_BASE = 'http://localhost:8080/hawkular/alerts'
6
+ creds = { username: 'jdoe', password: 'password' }
7
+
8
+ describe 'Alert/Triggers', :vcr do
9
+ it 'Should List Triggers' do
10
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
11
+
12
+ triggers = client.list_triggers
13
+
14
+ expect(triggers.size).to be(3)
15
+ end
16
+
17
+ it 'Should List Triggers for Tag' do
18
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
19
+
20
+ triggers = client.list_triggers [], ['resourceId|75bfdd05-d03d-481e-bf32-c724c7719d8b~Local']
21
+
22
+ expect(triggers.size).to be(7)
23
+ end
24
+
25
+ it 'Should List Triggers for Tags' do
26
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
27
+
28
+ triggers = client.list_triggers [], ['resourceId|75bfdd05-d03d-481e-bf32-c724c7719d8b~Local',
29
+ 'app|MyShop']
30
+
31
+ expect(triggers.size).to be(7)
32
+ end
33
+
34
+ it 'Should List Triggers for ID' do
35
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
36
+
37
+ triggers = client.list_triggers ['75bfdd05-d03d-481e-bf32-c724c7719d8b~Local_jvm_pheap']
38
+
39
+ expect(triggers.size).to be(1)
40
+ end
41
+
42
+ it 'Should get a single metric Trigger' do
43
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
44
+
45
+ trigger = client.get_single_trigger('snert~Local_jvm_nheap')
46
+
47
+ expect(trigger).not_to be_nil
48
+ end
49
+
50
+ it 'Should get a single Trigger with conditions' do
51
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
52
+
53
+ trigger = client.get_single_trigger 'snert~Local_jvm_nheap', true
54
+
55
+ expect(trigger).not_to be_nil
56
+ expect(trigger.conditions.size).to be(1)
57
+ expect(trigger.dampenings.size).to be(1)
58
+ end
59
+ end
60
+
61
+ describe 'Alert/Alerts', :vcr do
62
+ it 'Should list alerts' do
63
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
64
+
65
+ alerts = client.list_alerts
66
+
67
+ expect(alerts).to_not be_nil
68
+ expect(alerts.size).to be(2)
69
+ end
70
+
71
+ it 'Should list alerts for trigger' do
72
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
73
+
74
+ alerts = client.get_alerts_for_trigger '75bfdd05-d03d-481e-bf32-c724c7719d8b~Local_jvm_pheap'
75
+
76
+ expect(alerts).to_not be_nil
77
+ expect(alerts.size).to be(1)
78
+ end
79
+
80
+ it 'Should list alerts for unknown trigger' do
81
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
82
+
83
+ alerts = client.get_alerts_for_trigger 'does-not-exist'
84
+
85
+ expect(alerts).to_not be_nil
86
+ expect(alerts.size).to be(0)
87
+ end
88
+
89
+ it 'Should fetch single alert' do
90
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
91
+
92
+ alert = client.get_single_alert(
93
+ '28026b36-8fe4-4332-84c8-524e173a68bf-snert~Local_jvm_garba-1446977734134')
94
+
95
+ expect(alert).to_not be_nil
96
+ expect(alert.alertId)
97
+ .to eql('28026b36-8fe4-4332-84c8-524e173a68bf-snert~Local_jvm_garba-1446977734134')
98
+ end
99
+
100
+ it 'Should resolve an alert' do
101
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
102
+
103
+ alert_id = '28026b36-8fe4-4332-84c8-524e173a68bf-snert~Local_jvm_garba-1446977734134'
104
+ alert = client.get_single_alert alert_id
105
+
106
+ expect(alert.status).to eql('OPEN')
107
+
108
+ client.resolve_alert(alert_id, 'Heiko', 'Hello Ruby World :-)')
109
+
110
+ alert = client.get_single_alert alert_id
111
+ expect(alert.status).to eql('RESOLVED')
112
+ end
113
+
114
+ # # TODO enable when the semantics on the server side is known
115
+ # it 'Should resolve an alert2' do
116
+ # client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE,creds)
117
+ #
118
+ # alert_id = '28026b36-8fe4-4332-84c8-524e173a68bf-snert~Local_jvm_garba-1446977734134'
119
+ # client.resolve_alert alert_id
120
+ #
121
+ # end
122
+
123
+ it 'Should acknowledge an alert' do
124
+ client = Hawkular::Alerts::AlertsClient.new(ALERTS_BASE, creds)
125
+
126
+ alert_id = '28026b36-8fe4-4332-84c8-524e173a68bf-snert~Local_jvm_garba-1446977734134'
127
+ client.get_single_alert alert_id
128
+
129
+ client.acknowledge_alert(alert_id, 'Heiko', 'Hello Ruby World :-)')
130
+
131
+ alert = client.get_single_alert alert_id
132
+ expect(alert.ackBy).to eql('Heiko')
133
+ end
134
+ end
135
+
136
+ # TODO: enable when alerts supports it
137
+ # describe 'Alerts' do
138
+ # it 'Should return the version' do
139
+ # data = @client.get_version_and_status
140
+ # expect(data).not_to be_nil
141
+ # end
142
+ # end
143
+ end
@@ -0,0 +1,191 @@
1
+ require "#{File.dirname(__FILE__)}/../vcr/vcr_setup"
2
+ require "#{File.dirname(__FILE__)}/../spec_helper"
3
+
4
+ module Hawkular::Inventory::RSpec
5
+ INVENTORY_BASE = 'http://localhost:8080/hawkular/inventory'
6
+ describe 'Tenants', :vcr do
7
+ it 'Should Get Tenant For Explicit Credentials' do
8
+ creds = { username: 'jdoe', password: 'password' }
9
+
10
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
11
+
12
+ tenant = client.get_tenant(creds)
13
+
14
+ expect tenant.nil?
15
+ expect tenant.eql?('28026b36-8fe4-4332-84c8-524e173a68bf')
16
+ end
17
+
18
+ it 'Should Get Tenant For Implicit Credentials' do
19
+ creds = { username: 'jdoe', password: 'password' }
20
+
21
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
22
+
23
+ tenant = client.get_tenant
24
+
25
+ expect tenant.nil?
26
+
27
+ expect tenant.eql?('28026b36-8fe4-4332-84c8-524e173a68bf')
28
+ end
29
+ end
30
+
31
+ describe 'Inventory', :vcr do
32
+ it 'Should list feeds' do
33
+ creds = { username: 'jdoe', password: 'password' }
34
+
35
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
36
+ client.impersonate
37
+
38
+ feeds = client.list_feeds
39
+
40
+ expect(feeds.size).to be(1)
41
+ end
42
+
43
+ it 'Should list types without feed' do
44
+ creds = { username: 'jdoe', password: 'password' }
45
+
46
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
47
+ client.impersonate
48
+
49
+ types = client.list_resource_types
50
+
51
+ expect(types.size).to be(17)
52
+ end
53
+
54
+ it 'Should list types with feed' do
55
+ creds = { username: 'jdoe', password: 'password' }
56
+
57
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
58
+ client.impersonate
59
+
60
+ types = client.list_resource_types('snert')
61
+
62
+ expect(types.size).to be(16)
63
+ end
64
+
65
+ it 'Should list types with bad feed' do
66
+ creds = { username: 'jdoe', password: 'password' }
67
+
68
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
69
+ client.impersonate
70
+
71
+ type = 'does not exist'
72
+ types = client.list_resource_types(type)
73
+ expect(type).to eq('does not exist')
74
+
75
+ expect(types.size).to be(0)
76
+ end
77
+
78
+ it 'Should list WildFlys' do
79
+ creds = { username: 'jdoe', password: 'password' }
80
+
81
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
82
+ client.impersonate
83
+
84
+ resources = client.list_resources_for_type('snert', 'WildFly Server')
85
+
86
+ expect(resources.size).to be(1)
87
+ wf = resources.first
88
+ expect(wf.properties.size).to be(0)
89
+ end
90
+
91
+ it 'Should list WildFlys with props' do
92
+ creds = { username: 'jdoe', password: 'password' }
93
+
94
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
95
+ client.impersonate
96
+
97
+ resources = client.list_resources_for_type('snert', 'WildFly Server', true)
98
+
99
+ expect(resources.size).to be(1)
100
+ wf = resources.first
101
+ expect(wf.properties['Hostname']).to eq('snert')
102
+ end
103
+
104
+ it 'Should List datasources with no props' do
105
+ creds = { username: 'jdoe', password: 'password' }
106
+
107
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
108
+ client.impersonate
109
+
110
+ resources = client.list_resources_for_type('snert', 'Datasource', true)
111
+
112
+ expect(resources.size).to be(2)
113
+ wf = resources.first
114
+ expect(wf.properties.size).to be(0) # They have no props
115
+ end
116
+
117
+ it 'Should list URLs' do
118
+ creds = { username: 'jdoe', password: 'password' }
119
+
120
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
121
+ client.impersonate
122
+
123
+ resources = client.list_resources_for_type(nil, 'URL')
124
+
125
+ expect(resources.size).to be(1)
126
+ resource = resources[0]
127
+ expect(resource.instance_of? Hawkular::Inventory::Resource).to be_truthy
128
+ expect(resource.properties.size).to be(5)
129
+ expect(resource.properties['url']).to eq('http://bsd.de')
130
+ end
131
+
132
+ it 'Should list metrics for WildFlys' do
133
+ creds = { username: 'jdoe', password: 'password' }
134
+
135
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
136
+ client.impersonate
137
+
138
+ resources = client.list_resources_for_type('snert', 'WildFly Server')
139
+ expect(resources.size).to be(1)
140
+
141
+ wild_fly = resources[0]
142
+
143
+ metrics = client.list_metrics_for_resource(wild_fly)
144
+
145
+ expect(metrics.size).to be(14)
146
+ end
147
+
148
+ it 'Should list children of WildFly' do
149
+ creds = { username: 'jdoe', password: 'password' }
150
+
151
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
152
+ client.impersonate
153
+
154
+ resources = client.list_resources_for_type('snert', 'WildFly Server')
155
+ expect(resources.size).to be(1)
156
+
157
+ wild_fly = resources[0]
158
+
159
+ metrics = client.list_child_resources(wild_fly)
160
+
161
+ expect(metrics.size).to be(21)
162
+ end
163
+
164
+ it 'Should list heap metrics for WildFlys' do
165
+ creds = { username: 'jdoe', password: 'password' }
166
+
167
+ client = Hawkular::Inventory::InventoryClient.new(INVENTORY_BASE, creds)
168
+ client.impersonate
169
+
170
+ resources = client.list_resources_for_type('snert', 'WildFly Server')
171
+ expect(resources.size).to be(1)
172
+
173
+ wild_fly = resources[0]
174
+
175
+ metrics = client.list_metrics_for_resource(wild_fly, type: 'GAUGE', match: 'Metrics~Heap')
176
+ expect(metrics.size).to be(3)
177
+
178
+ metrics = client.list_metrics_for_resource(wild_fly, match: 'Metrics~Heap')
179
+ expect(metrics.size).to be(3)
180
+
181
+ metrics = client.list_metrics_for_resource(wild_fly, type: 'GAUGE')
182
+ expect(metrics.size).to be(10)
183
+ end
184
+
185
+ # TODO: enable when inventory supports it
186
+ # it 'Should return the version' do
187
+ # data = @client.get_version_and_status
188
+ # expect(data).not_to be_nil
189
+ # end
190
+ end
191
+ end
@@ -350,4 +350,37 @@ describe 'Gauge metrics' do
350
350
  expect(data[0][1]).to eql(now)
351
351
  end
352
352
  end
353
+
354
+ it 'Should return platform memory def' do
355
+ tenant_id = '28026b36-8fe4-4332-84c8-524e173a68bf'
356
+ setup_client tenant: tenant_id
357
+
358
+ VCR.use_cassette('Gauge_metrics/Platform mem def') do
359
+ # The next id is not a real one, but shortened for rubocpo
360
+ mem_id = 'MI~R~[snert~platform~/OP_SYSTEM=Apple (Capitan) 42/MEMORY=Mem]~MT~Total Memory'
361
+ data = @client.gauges.get(mem_id)
362
+
363
+ expect(data).not_to be_nil
364
+ expect(data.id).not_to be_nil
365
+ expect(data.tenant_id).to eq(tenant_id)
366
+ end
367
+ end
368
+
369
+ it 'Should return platform memory' do
370
+ setup_client tenant: '28026b36-8fe4-4332-84c8-524e173a68bf'
371
+
372
+ VCR.use_cassette('Gauge_metrics/Platform mem') do
373
+ # The next id is not a real one, but shortened for rubocpo
374
+ mem_id = 'MI~R~[snert~platform~/OP_SYSTEM=Apple (Capitan) 42/MEMORY=Mem]~MT~Total Memory'
375
+ data = @client.gauges.get_data(mem_id)
376
+ expect(data.size).to be 71
377
+ end
378
+ end
379
+
380
+ it 'Should return the version' do
381
+ VCR.use_cassette('Metrics/Status') do
382
+ data = @client.fetch_version_and_status
383
+ expect(data).not_to be_nil
384
+ end
385
+ end
353
386
  end