zenoss_client 0.7.0 → 0.10.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +8 -9
  3. data/README.rdoc +87 -0
  4. data/VERSION +1 -1
  5. data/lib/zenoss/connection.rb +1 -0
  6. data/lib/zenoss/events/event.rb +14 -2
  7. data/lib/zenoss/jsonapi/device_router.rb +28 -0
  8. data/lib/zenoss/jsonapi/report_router.rb +5 -1
  9. data/lib/zenoss/model/devices/device.rb +5 -0
  10. data/test/fixtures/vcr_cassettes/6_2_1_initial_connection.yml +232 -0
  11. data/test/fixtures/vcr_cassettes/6_2_1_test_0001_returns_an_Array_of_devices_when_searched_by_name.yml +107 -0
  12. data/test/fixtures/vcr_cassettes/6_2_1_test_0002_returns_device_uptime_when_asked.yml +131 -0
  13. data/test/fixtures/vcr_cassettes/6_2_1_test_0003_returns_an_Array_of_events_for_a_device.yml +100 -0
  14. data/test/fixtures/vcr_cassettes/6_2_1_test_0004_returns_an_Array_of_historical_events_for_a_device.yml +100 -0
  15. data/test/fixtures/vcr_cassettes/6_2_1_test_0005_returns_info_for_a_device_in_the_form_of_a_Hash.yml +265 -0
  16. data/test/fixtures/vcr_cassettes/6_2_1_test_0006_returns_an_Array_of_events_for_all_devices.yml +100 -0
  17. data/test/fixtures/vcr_cassettes/6_2_1_test_0007_fetches_the_report_tree.yml +164 -0
  18. data/test/fixtures/vcr_cassettes/6_2_1_test_0008_fetches_available_report_types_and_returns_a_Hash.yml +100 -0
  19. data/test/fixtures/vcr_cassettes/6_2_1_test_0009_renames_the_device.yml +184 -0
  20. data/test/fixtures/vcr_cassettes/6_2_1_test_0010_sets_info_for_a_device.yml +100 -0
  21. data/test/fixtures/vcr_cassettes/6_2_1_test_0011_sets_info_for_a_device_on_a_device_object.yml +100 -0
  22. data/test/fixtures/vcr_cassettes/6_2_1_test_0012_remodels_a_device.yml +99 -0
  23. data/test/test_helper.rb +14 -0
  24. data/test/zenoss_client_test.rb +89 -2
  25. data/test/zenoss_model_device_test.rb +25 -0
  26. data/zenoss_client.gemspec +7 -6
  27. metadata +30 -106
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0604c70eea29dc67d7083fb3521bf52ff3a1adff59160791b8756b06cca753c4
4
- data.tar.gz: 385c6c6b898364bb88a16832db0846dbaf594ade55859131e6cd08b3c30b6cf6
3
+ metadata.gz: 4b19b969c4c6e18b9a4e012dfb56b627c5bdd43e960cdc64a8d7a9e5036df389
4
+ data.tar.gz: ab09ef31c00e1e38aa6761448c84190c7bf5ee90868d97e12f2bb1aa1eef733c
5
5
  SHA512:
6
- metadata.gz: 1102a72533a0ca09895d70a9468af7a5766c90f418e63c8bd9fa6616a7f9a4487fb0342d01b3768e2177b8f934755068de39c044d9dbbe50f6e3128c4683fa74
7
- data.tar.gz: 5f34aa8d4ce4f292640d6460abfea740aa16857d560e8976468e5f416c72eb5f0910788b49ceffd4e8853135bcad3de3d39c6afc29601b63ed0635ef9016c3bf
6
+ metadata.gz: b86a0b85a3e8f36c9cc1a360e4620eff9da777389b732de7083782f9d925f353df261b647b7e8b46de29a4f035d73b116e62fb9d64d5cbcb68fb7e0d2040118e
7
+ data.tar.gz: aa516bb6da3f17bc94871fdc1dda6ee0582737eda7ba5e9aee33e5095aa0b2fcf9261c152ddd30d06217c36fca1df364c2f0c9f8c69d476b08831976bf258750
@@ -1,16 +1,15 @@
1
1
  language: ruby
2
2
  script: bundle exec rake test
3
3
  rvm:
4
- - 1.9.3
5
- - 2.0.0
4
+ - 2.0.0-p648
6
5
  - 2.1.10
7
- - 2.2.8
8
- - 2.3.6
9
- - 2.4.3
10
- - 2.5.0
11
- - ruby-head
12
- - jruby-head
6
+ - 2.2.10
7
+ - 2.3.8
8
+ - 2.4.10
9
+ - 2.5.8
10
+ - 2.6.6
11
+ - 2.7.1
13
12
 
14
13
  install:
15
- - gem install bundler
14
+ - gem install bundler -v 1.17.3
16
15
  - bundle install --jobs=3 --retry=3
@@ -0,0 +1,87 @@
1
+ h1. zenoss_client: A Ruby library for JSON/REST access to Zenoss
2
+
3
+ This is a work-in-progress to create an easy to use client JSON/REST API for
4
+ Zenoss (http://www.zenoss.com) written in Ruby. I love Zenoss as a product,
5
+ but I am much more efficient in Ruby than Python so I decided to start
6
+ hacking this library together. It is very incomplete and I am just adding
7
+ functionality as I need it or it is requested.
8
+
9
+ Cheers,
10
+
11
+ Dan Wanek
12
+
13
+ h2. CHANGES SINCE THE 0.1.x BRANCH
14
+
15
+ Starting with 0.5.0 I am trying to incorporate as much of the new JSON API
16
+ into this library as possible because the type conversion hackery that was
17
+ present in the REST code is largely unnecessary. Please note that the
18
+ initialization is much different because we are using the sign-in form auth
19
+ instead of the Basic auth of the previous version. This is because the JSON
20
+ calls do not support Basic authentication.
21
+
22
+ There is still much work that needs to be done in order to fully implement
23
+ all of the JSON methods. For now many of the older features fall back to
24
+ REST and some of those will always rely on REST because they are not
25
+ currently supported in the JSON API.
26
+
27
+
28
+ h2. REQUIREMENTS:
29
+ Gems:
30
+
31
+ * tzinfo: For Python DateTime to Ruby DateTime conversion
32
+
33
+
34
+ h2. UNSUPPORTED REST METHODS:
35
+ Some methods within Zope are unsupported due to type conversion issues.
36
+ I have created a work-around but you must add a custom Python script
37
+ to Zope in order to do this. Please see this blog post for information
38
+ on how add the custom script:
39
+
40
+ http://distributed-frostbite.blogspot.com/2010/04/using-ruby-with-zenoss-part-1.html
41
+
42
+ UPDATE: The script itself is now part of the source tree and can be found here: tools/callZenossMethod.py
43
+ It should still be installed in the same fashion as the blog post steps through.
44
+
45
+ == TO USE:
46
+ A gem is now available. 'gem install zenoss_client'
47
+
48
+ <pre>
49
+ <code>
50
+ require 'zenoss'
51
+ require 'date'
52
+
53
+ server = 'https://zenhost:port/zport/dmd'
54
+ user, pass = 'zuser', 'zpass'
55
+ @zen = Zenoss.connect server, user, pass
56
+ dev = (@zen.find_devices_by_name 'myservername').first
57
+
58
+ # Get RRD data for this device
59
+ rrdpts = dev.get_rrd_data_points
60
+ datapoints = dev.fetch_rrd_value rrdpts.first.name, (DateTime.now - 1)
61
+
62
+ # Get the uptime of the device
63
+ dev.sys_uptime
64
+
65
+ # Get a list of events for this system
66
+ dev.get_events
67
+ </code>
68
+ </pre>
69
+
70
+ Have fun and let me know what needs to be fixed / added.
71
+
72
+ == Testing
73
+ You can invoke a series of minitest based spec test using the supplied
74
+ tests and rake task. Simply run the following to kick off the tests
75
+ @bundle exec rake test@
76
+
77
+ You should see output similar to the following (The number of tests will
78
+ of course grow over time):
79
+ @3 tests, 6 assertions, 0 failures, 0 errors, 0 skips@
80
+
81
+ If you get any failures, you should of course address them
82
+
83
+ Since the test suite requires a working Zenoss installation to test against,
84
+ some docker files are provided under test/docker. From a docker host, clone this
85
+ repo, cd to the appropriate test/docker/ subdirectory, and run @sh start.sh@ to
86
+ build and start the container. Once the container is running, you should be able
87
+ to run the tests as described above.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.0
1
+ 0.10.1
@@ -32,6 +32,7 @@ module Zenoss
32
32
  include Zenoss::RESTAPI
33
33
 
34
34
  def initialize(url, user, pass, opts = {}, &block)
35
+ @zenoss_version = opts[:version]
35
36
  @zenoss_uri = (url.is_a?(URI) ? url : URI.parse(url))
36
37
  @request_number = 1
37
38
  @httpcli = HTTPClient.new
@@ -28,8 +28,20 @@ module Zenoss
28
28
  def initialize(zenoss,zhash)
29
29
  @zenoss = zenoss
30
30
  super zhash
31
- self.firstTime = DateTime.parse(self.firstTime) if self.firstTime
32
- self.lastTime = DateTime.parse(self.lastTime) if self.lastTime
31
+ self.firstTime &&= convert_time(self.firstTime)
32
+ self.lastTime &&= convert_time(self.lastTime)
33
+ end
34
+
35
+ # Converts a string or float to a Time object
36
+ # Zenoss version 4 emits the time format as a string
37
+ # Zenoss version 6 emits the time as a float
38
+ # @return[Time]
39
+ def convert_time(time)
40
+ if time.is_a?(String)
41
+ Time.parse(time)
42
+ else
43
+ Time.at(time)
44
+ end
33
45
  end
34
46
 
35
47
  def detail(history = false)
@@ -61,6 +61,34 @@ module Zenoss
61
61
  json_request('DeviceRouter', 'getInfo', [data])
62
62
  end
63
63
 
64
+ # @param [Hash] opts arguments to pass to setInfo
65
+ # @option opts [String] :uid required; device id in Zenoss
66
+ def set_info(opts = {})
67
+ if @zenoss_version && @zenoss_version > '6'
68
+ json_request('DeviceRouter', 'setInfo', [opts])
69
+ else
70
+ raise ZenossError, 'setInfo method on DeviceRouter is only allowed '\
71
+ 'for version 6 and above'
72
+ end
73
+ end
74
+
75
+ # @param [String] uid required; device id in Zenoss
76
+ # @param [String] plugins takes a string. A plugin respresents a Modeler
77
+ # plugin such as cisco.snmp.Interfaces
78
+ # @param [Boolean] background whether to schedule a job in background
79
+ def remodel(uid, plugins = nil, background = false)
80
+ if @zenoss_version && @zenoss_version > '6'
81
+ data = {}
82
+ data[:deviceUid] = uid
83
+ data[:collectPlugins] = plugins || ''
84
+ data[:background] = background
85
+ json_request('DeviceRouter', 'remodel', [data])
86
+ else
87
+ raise ZenossError, 'remodel method on DeviceRouter is only allowed '\
88
+ 'for version 6 and above'
89
+ end
90
+ end
91
+
64
92
  # =============== Non-API Helper methods ===============
65
93
 
66
94
  # This method will allow you to search for devices by name. If you put a partial name
@@ -26,7 +26,11 @@ module Zenoss
26
26
  end
27
27
 
28
28
  def get_report_tree(id = '/zport/dmd/Reports')
29
- json_request('ReportRouter', 'getTree', [{:id => id}])
29
+ if @zenoss_version && @zenoss_version > '6'
30
+ json_request('ReportRouter', 'asyncGetTree', [id])
31
+ else
32
+ json_request('ReportRouter', 'getTree', [{:id => id}])
33
+ end
30
34
  end
31
35
 
32
36
  end # ReportRouter
@@ -50,6 +50,11 @@ module Zenoss
50
50
  @zenoss.get_info(self.uid, keys)
51
51
  end
52
52
 
53
+ def set_info(opts = {})
54
+ opts[:uid] = self.uid
55
+ @zenoss.set_info(opts)
56
+ end
57
+
53
58
  # ------------------ Legacy REST Calls ------------------ #
54
59
 
55
60
  # Move this Device to the given DeviceClass.
@@ -0,0 +1,232 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://localhost:8080/zport/dmd/zport/acl_users/cookieAuthHelper/login
6
+ body:
7
+ encoding: UTF-8
8
+ string: __ac_name=admin&__ac_password=zenoss&submitted=true&came_from=http://localhost:8080/zport/dmd
9
+ headers:
10
+ User-Agent:
11
+ - HTTPClient/1.0 (2.8.3, ruby 2.3.3 (2016-11-21))
12
+ Accept:
13
+ - "*/*"
14
+ Date:
15
+ - Wed, 15 Apr 2020 20:38:47 GMT
16
+ Content-Type:
17
+ - application/x-www-form-urlencoded
18
+ Cookie: ''
19
+ response:
20
+ status:
21
+ code: 302
22
+ message: Found
23
+ headers:
24
+ Content-Length:
25
+ - '25'
26
+ Content-Type:
27
+ - text/plain; charset=utf-8
28
+ Date:
29
+ - Wed, 15 Apr 2020 20:38:50 GMT
30
+ Location:
31
+ - "/zport/dmd?submitted=true"
32
+ Server: ''
33
+ Set-Cookie: ''
34
+ Strict-Transport-Security:
35
+ - max-age=31536000
36
+ X-Frame-Options:
37
+ - SAMEORIGIN
38
+ X-Xss-Protection:
39
+ - 1; mode=block
40
+ body:
41
+ encoding: UTF-8
42
+ string: "/zport/dmd?submitted=true"
43
+ http_version:
44
+ recorded_at: Wed, 15 Apr 2020 20:38:51 GMT
45
+ - request:
46
+ method: post
47
+ uri: http://localhost:8080/zport/dmd//zport/dmd?submitted=true
48
+ body:
49
+ encoding: UTF-8
50
+ string: __ac_name=admin&__ac_password=zenoss&submitted=true&came_from=http://localhost:8080/zport/dmd
51
+ headers:
52
+ User-Agent:
53
+ - HTTPClient/1.0 (2.8.3, ruby 2.3.3 (2016-11-21))
54
+ Accept:
55
+ - "*/*"
56
+ Date:
57
+ - Wed, 15 Apr 2020 20:38:51 GMT
58
+ Content-Type:
59
+ - application/x-www-form-urlencoded
60
+ Cookie: ''
61
+ response:
62
+ status:
63
+ code: 200
64
+ message: OK
65
+ headers:
66
+ Cache-Control:
67
+ - max-age=0, no-cache
68
+ Content-Type:
69
+ - text/html; charset=utf-8
70
+ Date:
71
+ - Wed, 15 Apr 2020 20:38:51 GMT
72
+ Server: ''
73
+ Set-Cookie: ''
74
+ Strict-Transport-Security:
75
+ - max-age=31536000
76
+ Vary:
77
+ - Accept-Encoding
78
+ X-Frame-Options:
79
+ - SAMEORIGIN
80
+ X-Page-Speed:
81
+ - 1.11.33.4-0
82
+ X-Xss-Protection:
83
+ - 1; mode=block
84
+ Transfer-Encoding:
85
+ - chunked
86
+ body:
87
+ encoding: UTF-8
88
+ string: "\\n\\n <!DOCTYPE html>\\n<html>\\n"
89
+ http_version:
90
+ recorded_at: Wed, 15 Apr 2020 20:38:52 GMT
91
+ - request:
92
+ method: post
93
+ uri: http://localhost:8080/zport/dmd/zport/dmd/device_router
94
+ body:
95
+ encoding: UTF-8
96
+ string: __ac_name=admin&__ac_password=zenoss&submitted=true&came_from=https%3A%2F%2Fhttp:://localhost:8080/zport/dmd%2Fzport%2Fdmd
97
+ headers:
98
+ User-Agent:
99
+ - HTTPClient/1.0 (2.8.3, ruby 2.3.3 (2016-11-21))
100
+ Accept:
101
+ - "*/*"
102
+ Date:
103
+ - Wed, 15 Apr 2020 20:38:52 GMT
104
+ Content-Type:
105
+ - application/json; charset=utf-8
106
+ Cookie: ''
107
+ response:
108
+ status:
109
+ code: 200
110
+ message: OK
111
+ headers:
112
+ Content-Type:
113
+ - application/json
114
+ Date:
115
+ - Wed, 15 Apr 2020 20:38:54 GMT
116
+ Server: ''
117
+ Strict-Transport-Security:
118
+ - max-age=31536000
119
+ Vary:
120
+ - Accept-Encoding
121
+ X-Frame-Options:
122
+ - SAMEORIGIN
123
+ X-Xss-Protection:
124
+ - 1; mode=block
125
+ Content-Length:
126
+ - '309'
127
+ Set-Cookie: ''
128
+ body:
129
+ encoding: UTF-8
130
+ string: '{"uuid": "a34caf64-2209-480c-96ed-dd94117ef7ac", "action": "DeviceRouter",
131
+ "result": {"new_jobs": [{"uuid": "b0c01851-394f-49a1-8547-acd02633194e", "description":
132
+ "Create UnitTestDevice under /Devices/Server", "uid": "/zport/dmd/JobManager"}],
133
+ "success": true}, "tid": 1, "type": "rpc", "method": "addDevice"}'
134
+ http_version:
135
+ recorded_at: Wed, 15 Apr 2020 20:38:54 GMT
136
+ - request:
137
+ method: post
138
+ uri: http://localhost:8080/zport/dmd/zport/dmd/device_router
139
+ body:
140
+ encoding: UTF-8
141
+ string: __ac_name=admin&__ac_password=zenoss&submitted=true&came_from=https%3A%2F%2Fhttp:://localhost:8080/zport/dmd%2Fzport%2Fdmd
142
+ headers:
143
+ User-Agent:
144
+ - HTTPClient/1.0 (2.8.3, ruby 2.3.3 (2016-11-21))
145
+ Accept:
146
+ - "*/*"
147
+ Date:
148
+ - Wed, 15 Apr 2020 20:38:54 GMT
149
+ Content-Type:
150
+ - application/json; charset=utf-8
151
+ Cookie: ''
152
+ response:
153
+ status:
154
+ code: 200
155
+ message: OK
156
+ headers:
157
+ Content-Type:
158
+ - application/json
159
+ Date:
160
+ - Wed, 15 Apr 2020 20:38:54 GMT
161
+ Server: ''
162
+ Strict-Transport-Security:
163
+ - max-age=31536000
164
+ Vary:
165
+ - Accept-Encoding
166
+ X-Frame-Options:
167
+ - SAMEORIGIN
168
+ X-Xss-Protection:
169
+ - 1; mode=block
170
+ Content-Length:
171
+ - '197'
172
+ Set-Cookie: ''
173
+ body:
174
+ encoding: UTF-8
175
+ string: '{"uuid": "48c067ba-5c1e-4d81-aac0-3c49aadba9fc", "action": "DeviceRouter",
176
+ "result": {"totalCount": 0, "hash": "0", "success": true, "devices": []},
177
+ "tid": 2, "type": "rpc", "method": "getDevices"}'
178
+ http_version:
179
+ recorded_at: Wed, 15 Apr 2020 20:38:54 GMT
180
+ - request:
181
+ method: post
182
+ uri: http://localhost:8080/zport/dmd/zport/dmd/device_router
183
+ body:
184
+ encoding: UTF-8
185
+ string: __ac_name=admin&__ac_password=zenoss&submitted=true&came_from=https%3A%2F%2Fhttp:://localhost:8080/zport/dmd%2Fzport%2Fdmd
186
+ headers:
187
+ User-Agent:
188
+ - HTTPClient/1.0 (2.8.3, ruby 2.3.3 (2016-11-21))
189
+ Accept:
190
+ - "*/*"
191
+ Date:
192
+ - Wed, 15 Apr 2020 20:39:09 GMT
193
+ Content-Type:
194
+ - application/json; charset=utf-8
195
+ Cookie: ''
196
+ response:
197
+ status:
198
+ code: 200
199
+ message: OK
200
+ headers:
201
+ Content-Type:
202
+ - application/json
203
+ Date:
204
+ - Wed, 15 Apr 2020 20:39:11 GMT
205
+ Server: ''
206
+ Strict-Transport-Security:
207
+ - max-age=31536000
208
+ Vary:
209
+ - Accept-Encoding
210
+ X-Frame-Options:
211
+ - SAMEORIGIN
212
+ X-Xss-Protection:
213
+ - 1; mode=block
214
+ Content-Length:
215
+ - '905'
216
+ Set-Cookie: ''
217
+ body:
218
+ encoding: UTF-8
219
+ string: '{"uuid": "91ffde13-d44b-4a5e-9846-2155070ba803", "action": "DeviceRouter",
220
+ "result": {"totalCount": 1, "hash": "1", "success": true, "devices": [{"ipAddressString":
221
+ null, "serialNumber": "", "pythonClass": "Products.ZenModel.Device", "hwManufacturer":
222
+ null, "collector": "localhost", "osModel": null, "productionState": 1000,
223
+ "systems": [], "priority": 3, "hwModel": null, "tagNumber": "", "osManufacturer":
224
+ null, "location": null, "groups": [], "uid": "/zport/dmd/Devices/Server/devices/UnitTestDevice",
225
+ "ipAddress": null, "events": {"info": {"count": 0, "acknowledged_count": 0},
226
+ "clear": {"count": 0, "acknowledged_count": 0}, "warning": {"count": 0, "acknowledged_count":
227
+ 0}, "critical": {"count": 0, "acknowledged_count": 0}, "error": {"count":
228
+ 0, "acknowledged_count": 0}, "debug": {"count": 0, "acknowledged_count": 0}},
229
+ "name": "UnitTestDevice"}]}, "tid": 3, "type": "rpc", "method": "getDevices"}'
230
+ http_version:
231
+ recorded_at: Wed, 15 Apr 2020 20:39:11 GMT
232
+ recorded_with: VCR 4.0.0