zenoss_api 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/zenapi +205 -0
- data/lib/zenoss_api/version.rb +3 -0
- data/lib/zenoss_api.rb +207 -0
- data/zenoss_api.gemspec +38 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 02ea431c2beadc81cf178cdd7b1a8986fa0d0d3d
|
4
|
+
data.tar.gz: 6df145478e48b9e4cdec8bd5e38b40bb16ed6d5b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 912a9fe207ca7021244f94a23a74585310b03597e09101b96d4358fd5c47dd017172abfec1a69dbb4bca5b032345a0fdbd01feaccd35529ca468369f54dbabcf
|
7
|
+
data.tar.gz: 3af7d6c35a9588277cafdeb43795d2d21588b679808ff554c1c8586a226512ac47a579a2ec903e93d05f37b40fc23909eac9a98564562b6449c7b16d92d054df
|
data/.gitignore
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at TODO: Write your email address. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Write your name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# ZenossApi
|
2
|
+
|
3
|
+
Command line interface abstraction of zenoss api method calls.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
*Not currently published to rubygems.org, see Development*
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'zenoss_api'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install zenoss_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Get all device classes:
|
26
|
+
|
27
|
+
```bash
|
28
|
+
zenapi -m getDevices -p name:admin01.ctl.gdl.englab.netapp.com | jq '.devices[]'
|
29
|
+
```
|
30
|
+
|
31
|
+
Get all groups:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
zenapi -m getGroups | jq '.groups[] | .name'
|
35
|
+
```
|
36
|
+
|
37
|
+
Get devices in device class, limit to 30:
|
38
|
+
|
39
|
+
```bash
|
40
|
+
zenapi --uid '/Devices/Server/Linux/CTL' --limit 30 | jq '.devices[] | .name'
|
41
|
+
```
|
42
|
+
Get device by name:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
zenapi -m getDevices -a name:admin01.ctl.gdl.englab.netapp.com
|
46
|
+
```
|
47
|
+
|
48
|
+
Get most recent events, limit to 10:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
zenapi -m query -r EventsRouter --limit 10 | jq '.events[] | .device | .text'
|
52
|
+
```
|
53
|
+
|
54
|
+
Get event detail:
|
55
|
+
````bash
|
56
|
+
zenapi -r EventsRouter -m detail -a evid:0025b520-10e1-bfad-11e6-9abe93a9a0ae | jq '.'
|
57
|
+
````
|
58
|
+
|
59
|
+
Get all routers:
|
60
|
+
````bash
|
61
|
+
zenapi -r IntrospectionRouter -m getAllRouters | jq '.result | .data[] | .name'
|
62
|
+
````
|
63
|
+
|
64
|
+
Introspect router methods:
|
65
|
+
````bash
|
66
|
+
zenapi -r IntrospectionRouter -m getRouterMethods -a router:DeviceRouter | jq '.result | .data'
|
67
|
+
````
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
72
|
+
|
73
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitLab at http://https://gitlab.com/twstoll/zenoss_api This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
78
|
+
|
79
|
+
|
80
|
+
## License
|
81
|
+
|
82
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zenoss_api"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/bin/zenapi
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "zenoss_api"
|
4
|
+
require 'optparse'
|
5
|
+
require 'yaml'
|
6
|
+
require 'io/console'
|
7
|
+
|
8
|
+
zenrc_path = File.join(Dir.home, ".zenrc")
|
9
|
+
if File.exists?(zenrc_path)
|
10
|
+
zenrc = YAML.load_file(zenrc_path)
|
11
|
+
else
|
12
|
+
zenrc = {}
|
13
|
+
end
|
14
|
+
|
15
|
+
options = {
|
16
|
+
:username => zenrc['username'],
|
17
|
+
:password => zenrc['password'],
|
18
|
+
:zenoss_instance => zenrc['url'],
|
19
|
+
:router => 'DeviceRouter',
|
20
|
+
:method => 'getDevices',
|
21
|
+
:filters => {},
|
22
|
+
:limit => nil,
|
23
|
+
:uid => nil,
|
24
|
+
:keys => nil,
|
25
|
+
:arguments => [],
|
26
|
+
:debug => false,
|
27
|
+
:verify_ssl => false,
|
28
|
+
:command => "api",
|
29
|
+
:name => nil,
|
30
|
+
:prodState => nil,
|
31
|
+
:hashcheck => nil,
|
32
|
+
:action => nil,
|
33
|
+
:uids_file => nil,
|
34
|
+
:offset => nil,
|
35
|
+
:paginate => false,
|
36
|
+
}
|
37
|
+
|
38
|
+
opts_parser = OptionParser.new do |opts|
|
39
|
+
opts.banner = "Usage: zenoss_api.rb [options]"
|
40
|
+
opts.on('-r', '--router api_router', 'Zenoss API Router: default is DeviceRouter') do |router|
|
41
|
+
options[:router] = router
|
42
|
+
end
|
43
|
+
opts.on('-m', '--method method', 'method: default is getDevices') do |method|
|
44
|
+
options[:method] = method
|
45
|
+
end
|
46
|
+
opts.on('-f', '--filters key:value', 'method filter: default is none') do |filters|
|
47
|
+
filters_hash = {}
|
48
|
+
if filters.match /^({.*})$/
|
49
|
+
filters_hash = JSON.parse(filters)
|
50
|
+
else
|
51
|
+
filters_array = filters.split(",")
|
52
|
+
filters_array.each do |i|
|
53
|
+
e = i.split(":")
|
54
|
+
filters_hash[e[0]] = e[1]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
options[:filters] = filters_hash
|
58
|
+
end
|
59
|
+
opts.on('-l', '--limit integer', 'limit results: default 50') do |limit|
|
60
|
+
options[:limit] = limit.to_i
|
61
|
+
end
|
62
|
+
opts.on('-u', '--uid uid', 'dev passed to api methods') do |uid|
|
63
|
+
options[:uid] = uid
|
64
|
+
end
|
65
|
+
opts.on('--uids uid1,uid2,uid3', Array,'list for api method uids param') do |uids|
|
66
|
+
options[:uids] = uids
|
67
|
+
end
|
68
|
+
opts.on('-k', '--keys key1,key2,key3', Array, 'list of keys to return: defaull all') do |keys|
|
69
|
+
options[:keys] = keys
|
70
|
+
end
|
71
|
+
opts.on('-a', '--args arg1:val1,arg2:val2', Array, 'arugments:values passed to api method') do |arguments|
|
72
|
+
key_pairs = arguments.map { |x| x.split(':') }
|
73
|
+
key_hashes = key_pairs.map { |y| { y[0] => y[1] } }
|
74
|
+
key_hashes.each { |k| options[:arguments].push k }
|
75
|
+
end
|
76
|
+
opts.on('--username username', 'Username') do |username|
|
77
|
+
options[:username] = username
|
78
|
+
end
|
79
|
+
opts.on('--password password', 'Password') do |password|
|
80
|
+
options[:password] = password
|
81
|
+
end
|
82
|
+
opts.on('-c', '--command command', 'Command') do |command|
|
83
|
+
options[:command] = command
|
84
|
+
end
|
85
|
+
opts.on('-n', '--name name', 'Device name') do |name|
|
86
|
+
options[:name] = name
|
87
|
+
end
|
88
|
+
opts.on('--prodState int', 'Production State') do |state|
|
89
|
+
options[:prodState] = state
|
90
|
+
end
|
91
|
+
opts.on('--url url', 'Zenoss instance') do |url|
|
92
|
+
options[:zenoss_instance] = url
|
93
|
+
end
|
94
|
+
opts.on('--debug') do
|
95
|
+
options[:debug] = true
|
96
|
+
end
|
97
|
+
opts.on('--paginate') do
|
98
|
+
options[:paginate] = true
|
99
|
+
end
|
100
|
+
opts.on('--hashcheck h') do |h|
|
101
|
+
options[:hashcheck] = h
|
102
|
+
end
|
103
|
+
opts.on('--action a') do |a|
|
104
|
+
options[:action] = a
|
105
|
+
end
|
106
|
+
opts.on('--offset int') do |o|
|
107
|
+
options[:offset] = o.to_i
|
108
|
+
end
|
109
|
+
opts.on('--uids_file file') do |f|
|
110
|
+
uids = File.read(f)
|
111
|
+
json = JSON.parse(uids)
|
112
|
+
options[:uids_file] = json
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
opts_parser.parse!
|
117
|
+
|
118
|
+
if options[:uids_file]
|
119
|
+
uids = []
|
120
|
+
options[:uids_file]['devices'].each do |u|
|
121
|
+
uids.push u['uid']
|
122
|
+
end
|
123
|
+
options[:uids] = uids
|
124
|
+
end
|
125
|
+
|
126
|
+
if options[:zenoss_instance].nil?
|
127
|
+
print "Zenoss Instance: "
|
128
|
+
options[:zenoss_instance] = gets.chomp
|
129
|
+
end
|
130
|
+
|
131
|
+
if options[:username].nil?
|
132
|
+
print "Username: "
|
133
|
+
options[:username] = gets.chomp
|
134
|
+
end
|
135
|
+
|
136
|
+
if options[:password].nil?
|
137
|
+
print "Password: "
|
138
|
+
options[:password] = STDIN.noecho(&:gets).chomp
|
139
|
+
puts ""
|
140
|
+
STDOUT.flush
|
141
|
+
end
|
142
|
+
|
143
|
+
z = ZenossAPI::Client.new(options[:zenoss_instance], options[:username], options[:password], options[:verify_ssl])
|
144
|
+
|
145
|
+
method_params = {}
|
146
|
+
|
147
|
+
if ZenossAPI::METHOD_PARAMS.key? options[:router].to_sym
|
148
|
+
default_method_parms = ZenossAPI::METHOD_PARAMS[options[:router].to_sym][options[:method].to_sym]
|
149
|
+
end
|
150
|
+
|
151
|
+
if default_method_parms
|
152
|
+
default_method_parms.each do |k,v|
|
153
|
+
# options override default params
|
154
|
+
method_params[k] = options[k.to_sym] || v
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
method_params['uid'] = z.validateUids(options[:uid]) unless not options[:uid]
|
159
|
+
method_params['uids'] = z.validateUids(options[:uids]) unless not options[:uids]
|
160
|
+
method_params['keys'] = options[:keys] unless not options[:keys]
|
161
|
+
method_params['limit'] = options[:limit] unless not options[:limit]
|
162
|
+
method_params['hashcheck'] = options[:hashcheck] unless not options[:hashcheck]
|
163
|
+
method_params['action'] = options[:action] unless not options[:action]
|
164
|
+
method_params['start'] = options[:offset] unless not options[:offset]
|
165
|
+
|
166
|
+
|
167
|
+
options[:arguments].each do |arg|
|
168
|
+
method_params.merge! arg
|
169
|
+
end
|
170
|
+
|
171
|
+
# filters set in method parameter 'params' from key:value hash
|
172
|
+
method_params['params'] = options[:filters] unless options[:filters].empty?
|
173
|
+
#method_params['params'] = { 'productionState' => 1000 }
|
174
|
+
|
175
|
+
if options[:command] == "api"
|
176
|
+
response = z.router(options[:router], options[:method], method_params, options)
|
177
|
+
|
178
|
+
if options[:paginate]
|
179
|
+
response.each do |x|
|
180
|
+
puts x.to_json
|
181
|
+
end
|
182
|
+
else
|
183
|
+
if options[:debug]
|
184
|
+
p response.headers.to_json
|
185
|
+
end
|
186
|
+
|
187
|
+
response['query'] = method_params
|
188
|
+
puts response.to_hash.to_json
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
if options[:command] == "getUID"
|
193
|
+
response = z.getDeviceProps(options[:name], ["uid"])
|
194
|
+
puts response.to_json
|
195
|
+
end
|
196
|
+
|
197
|
+
if options[:command] == "setProductionState"
|
198
|
+
response = z.setProductionState(options[:name], options[:prodState].to_i)
|
199
|
+
puts response.to_json
|
200
|
+
end
|
201
|
+
|
202
|
+
exit 0
|
203
|
+
#########################################
|
204
|
+
#[{"tid": 1, "type": "rpc", "method_params": [], "method": "getDeviceClasses", "action": "DeviceRouter"}]
|
205
|
+
|
data/lib/zenoss_api.rb
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
require "zenoss_api/version"
|
2
|
+
|
3
|
+
module ZenossAPI
|
4
|
+
require 'json'
|
5
|
+
require 'httparty'
|
6
|
+
|
7
|
+
ROUTERS = {
|
8
|
+
:MessagingRouter => 'messaging',
|
9
|
+
:EventsRouter => 'evconsole',
|
10
|
+
:ProcessRouter => 'process',
|
11
|
+
:ServiceRouter => 'service',
|
12
|
+
:DeviceRouter => 'device',
|
13
|
+
:NetworkRouter => 'network',
|
14
|
+
:TemplateRouter => 'template',
|
15
|
+
:DetailNavRouter => 'detailnav',
|
16
|
+
:ReportRouter => 'report',
|
17
|
+
:MibRouter => 'mib',
|
18
|
+
:ZenPackRouter => 'zenpack',
|
19
|
+
:TriggersRouter => 'triggers',
|
20
|
+
:SettingsRouter => 'settings',
|
21
|
+
:JobsRouter => 'jobs',
|
22
|
+
:IntrospectionRouter => 'introspection',
|
23
|
+
:UsersRouter => 'users',
|
24
|
+
}
|
25
|
+
|
26
|
+
METHOD_PARAMS = {
|
27
|
+
:DeviceRouter => {
|
28
|
+
:getDevices => {
|
29
|
+
'uid' => nil,
|
30
|
+
'start' => 0,
|
31
|
+
'params' => nil,
|
32
|
+
'limit' => 50,
|
33
|
+
'sort' => 'name',
|
34
|
+
'page' => nil,
|
35
|
+
'dir' => 'ASC',
|
36
|
+
'keys' => nil,
|
37
|
+
},
|
38
|
+
:getInfo => {
|
39
|
+
'uid' => nil,
|
40
|
+
'keys' => nil,
|
41
|
+
},
|
42
|
+
},
|
43
|
+
:EventsRouter => {
|
44
|
+
:query => {
|
45
|
+
'limit' => 50,
|
46
|
+
},
|
47
|
+
},
|
48
|
+
}
|
49
|
+
|
50
|
+
EVENT_STATE = {
|
51
|
+
:new => 0,
|
52
|
+
:acknowledged => 1,
|
53
|
+
:suppressed => 2,
|
54
|
+
:closed => 3,
|
55
|
+
:cleared => 4,
|
56
|
+
:aged => 6,
|
57
|
+
|
58
|
+
}
|
59
|
+
|
60
|
+
EVENT_SEVERITY = {
|
61
|
+
:clear => 0, # Green
|
62
|
+
:debug => 1, # Grey
|
63
|
+
:info => 2, # Blue
|
64
|
+
:warning => 3, # Yellow
|
65
|
+
:error => 4, # Orange
|
66
|
+
:critical => 5, # Red
|
67
|
+
}
|
68
|
+
|
69
|
+
class Client
|
70
|
+
|
71
|
+
def initialize(url, username, password, verify_ssl = true)
|
72
|
+
@url = url
|
73
|
+
@auth = {:username => username, :password => password}
|
74
|
+
@rec_count = 1
|
75
|
+
@headers = {
|
76
|
+
'Content-Type' => 'application/json',
|
77
|
+
'Accept' => 'application/json',
|
78
|
+
}
|
79
|
+
@verify = verify_ssl
|
80
|
+
end
|
81
|
+
|
82
|
+
def paginated_get(url, payload, xoffset=0)
|
83
|
+
Enumerator.new do |y|
|
84
|
+
total = 0
|
85
|
+
offset = 0
|
86
|
+
limit = payload[0]['data'][0]['limit']
|
87
|
+
loop do
|
88
|
+
response = self._post(url, payload.to_json)
|
89
|
+
totalCount = response['result']['totalCount']
|
90
|
+
data = response['result']['events']
|
91
|
+
total += data.length
|
92
|
+
offset += limit
|
93
|
+
payload[0]['data'][0]['start'] = offset
|
94
|
+
|
95
|
+
data.each do |element|
|
96
|
+
y.yield element
|
97
|
+
end
|
98
|
+
|
99
|
+
break if (data.empty? || total >= totalCount)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def _post(url, payload)
|
105
|
+
#### TODO wrap in rescue and handle exceptions
|
106
|
+
response = HTTParty.post(url,
|
107
|
+
{
|
108
|
+
:body => payload,
|
109
|
+
:basic_auth => @auth,
|
110
|
+
:headers => @headers,
|
111
|
+
:timeout => 300,
|
112
|
+
:verify => @verify,
|
113
|
+
})
|
114
|
+
if response.code == 200
|
115
|
+
response
|
116
|
+
else
|
117
|
+
response
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
def router(router, method, method_params={}, options={})
|
123
|
+
if method_params.empty?
|
124
|
+
_method_params = []
|
125
|
+
else
|
126
|
+
_method_params = [ method_params ]
|
127
|
+
end
|
128
|
+
|
129
|
+
raise "Error: #{router} is not a valid route." unless ROUTERS.key?(router.to_sym)
|
130
|
+
|
131
|
+
api_call = "#{@url}/zport/dmd/#{ROUTERS[router.to_sym]}_router"
|
132
|
+
payload = [{
|
133
|
+
'action' => router,
|
134
|
+
'method' => method,
|
135
|
+
'data' => _method_params,
|
136
|
+
'type' => 'rpc',
|
137
|
+
'tid' => @rec_count
|
138
|
+
}]
|
139
|
+
|
140
|
+
if options[:paginate]
|
141
|
+
self.paginated_get(api_call, payload)
|
142
|
+
else
|
143
|
+
self._post(api_call, payload.to_json)
|
144
|
+
end
|
145
|
+
#@rec_count += 1
|
146
|
+
end
|
147
|
+
|
148
|
+
def _returnDeviceProperty()
|
149
|
+
end
|
150
|
+
|
151
|
+
def validateUids(uids, type="devices")
|
152
|
+
if uids.kind_of?(Array)
|
153
|
+
uids.collect { |x| self._validateUid(x, type) }
|
154
|
+
else
|
155
|
+
self._validateUid(uids, type)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
def _validateUid(u, type)
|
160
|
+
if u.match(/^\/zport\/dmd\/.*$/)
|
161
|
+
u
|
162
|
+
else
|
163
|
+
response = self.getDeviceProps(u, [ "uid" ])
|
164
|
+
if not response.nil?
|
165
|
+
response['result'][type][0]['uid']
|
166
|
+
else
|
167
|
+
nil
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def getDeviceProps(device, props=[])
|
173
|
+
response = self.router("DeviceRouter", "getDevices", { 'keys' => props, 'params' => { 'name' => device } })
|
174
|
+
if response['result']['success'] == true and response['result']['totalCount'] == 1
|
175
|
+
response
|
176
|
+
else
|
177
|
+
nil
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def setProductionState(fqdn, state = 1000)
|
182
|
+
# set a single device prod state
|
183
|
+
|
184
|
+
# Production:1000
|
185
|
+
# Pre-Production:500
|
186
|
+
# Test:400
|
187
|
+
# Maintenance:300
|
188
|
+
# Decommissioned:-1
|
189
|
+
|
190
|
+
# lookup device uid
|
191
|
+
node = self.getDeviceProps(fqdn, ["uid"])
|
192
|
+
# set prod state
|
193
|
+
if not node.nil?
|
194
|
+
uids = [ node['result']['devices'][0]['uid'] ]
|
195
|
+
hashcheck = node['result']['hash']
|
196
|
+
method_params = {
|
197
|
+
'uids' => uids,
|
198
|
+
'prodState' => state,
|
199
|
+
'hashcheck' => hashcheck,
|
200
|
+
}
|
201
|
+
self.router("DeviceRouter", "setProductionState", method_params)
|
202
|
+
else
|
203
|
+
"Set production state falied"
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
data/zenoss_api.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'zenoss_api/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zenoss_api"
|
8
|
+
spec.version = ZenossApi::VERSION
|
9
|
+
spec.authors = ["Todd Stoll"]
|
10
|
+
spec.email = ["twstoll@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Zenoss API via command line.}
|
13
|
+
spec.description = %q{Execute api calls given the router, method, and parameters. Returns response as json}
|
14
|
+
spec.homepage = ""
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^(test|spec|features)/})
|
28
|
+
end
|
29
|
+
spec.bindir = "bin"
|
30
|
+
spec.executables = ["zenapi"]
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_dependency "json"
|
36
|
+
spec.add_dependency "io-console"
|
37
|
+
spec.add_dependency "httparty"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zenoss_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Todd Stoll
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: io-console
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: httparty
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Execute api calls given the router, method, and parameters. Returns response
|
84
|
+
as json
|
85
|
+
email:
|
86
|
+
- twstoll@gmail.com
|
87
|
+
executables:
|
88
|
+
- zenapi
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- .gitignore
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- bin/zenapi
|
101
|
+
- lib/zenoss_api.rb
|
102
|
+
- lib/zenoss_api/version.rb
|
103
|
+
- zenoss_api.gemspec
|
104
|
+
homepage: ''
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - '>='
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.0.14.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Zenoss API via command line.
|
129
|
+
test_files: []
|