zinv 0.1.1 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG +0 -0
- data/README.md +11 -6
- data/exe/zinv.rb +38 -55
- data/lib/zinv/version.rb +1 -1
- data/zinv.gemspec +6 -6
- metadata +28 -28
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9ccc5cb8c66fa65febb2ac8f2885c2ac701ae57dc6c32c8f1a25a5676ad2f389
|
4
|
+
data.tar.gz: 137a98b0121bf95ad46a99412110a1a67d4be0643cb521d280276e300891fd01
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1040487875e6b618a2c1b688442cbc2f7f11fdb6b46acd1cf730b1d513e95b429a566a2a6fdbbe2c470769993cf0ef68ddc27fefccd93bd6ee9251b7e7186daa
|
7
|
+
data.tar.gz: f97e4472fafae1e3e70f4de65fd2f069c4a04a7d89f2ba67ca158fabe416f89717572b306f6783ef69c1536405e591c5ca035cd3e93c078fd8f48c7fea5bdeab
|
data/CHANGELOG
ADDED
File without changes
|
data/README.md
CHANGED
@@ -20,6 +20,17 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
```
|
24
|
+
$ zinv.rb --help
|
25
|
+
Options:
|
26
|
+
-l, --list List entire inventory (Ansible convention)
|
27
|
+
-h, --host=<s> List a single host (Ansible convention)
|
28
|
+
-d, --debug Debugging verbosity
|
29
|
+
-g, --groups Dump groups list
|
30
|
+
-t, --templates Dump templates list
|
31
|
+
-e, --help Show this message
|
32
|
+
```
|
33
|
+
|
23
34
|
To use zinv.rb you must (by one of the several methods available) tell Ansible to use zinv.rb as its inventory. Having done that, define the following environment variables for zinv.rb to work properly:
|
24
35
|
|
25
36
|
* ZINV_ZABBIX_URL = your zabbix web interface URL
|
@@ -58,12 +69,6 @@ kilroy: was here
|
|
58
69
|
|
59
70
|
Variables defined in this way will be available to to ansible when it processes the host in question.
|
60
71
|
|
61
|
-
## Development
|
62
|
-
|
63
|
-
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.
|
64
|
-
|
65
|
-
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).
|
66
|
-
|
67
72
|
## Contributing
|
68
73
|
|
69
74
|
Bug reports and pull requests are welcome on GitHub at https://github.com/svdasein/zinv. 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.
|
data/exe/zinv.rb
CHANGED
@@ -1,13 +1,22 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
require '
|
2
|
+
require 'zabbix/api'
|
3
3
|
require 'json'
|
4
|
-
require '
|
5
|
-
require '
|
4
|
+
require 'optimist'
|
5
|
+
require 'amazing_print'
|
6
6
|
require 'set'
|
7
7
|
require 'yaml'
|
8
8
|
|
9
9
|
|
10
|
+
# Assign a comma separated list of host names to this before running
|
11
|
+
# ansible-playbook to have hosts not yet in zabbix listed in inventory
|
12
|
+
runtimehostvar = 'ZINV_ADD_HOSTS'
|
10
13
|
|
14
|
+
opts = Optimist::options do
|
15
|
+
opt :list, "List entire inventory" # required by ansible
|
16
|
+
opt :host, "List a single host", :type => :string # required by ansible
|
17
|
+
opt :groups, "Dump groups list"
|
18
|
+
opt :templates, "Dump templates list"
|
19
|
+
end
|
11
20
|
|
12
21
|
if not (ENV['ZINV_ZABBIX_URL'] and ENV['ZINV_ZABBIX_USER'] and ENV['ZINV_ZABBIX_PASS'])
|
13
22
|
puts "\nYou *must* define the following environment variables for zinv.rb to work properly:\n\n"
|
@@ -21,9 +30,6 @@ if not (ENV['ZINV_ZABBIX_URL'] and ENV['ZINV_ZABBIX_USER'] and ENV['ZINV_ZABBIX_
|
|
21
30
|
exit(1)
|
22
31
|
end
|
23
32
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
33
|
# This list forms the basis for the collection of hosts and
|
28
34
|
# templates included in inventory. Any template directly or
|
29
35
|
# indirectly derived from these will be included. Any host
|
@@ -34,22 +40,10 @@ roottemplatenames = [
|
|
34
40
|
'Template OS Linux Active',
|
35
41
|
'Template SNMP OS Linux'
|
36
42
|
]
|
37
|
-
|
38
43
|
if ENV['ZINV_ROOT_TEMPLATES']
|
39
44
|
roottemplatenames = ENV['ZINV_ROOT_TEMPLATES'].split(',')
|
40
45
|
end
|
41
46
|
|
42
|
-
# Assign a comma separated list of host names to this before running
|
43
|
-
# ansible-playbook to have hosts not yet in zabbix listed in inventory
|
44
|
-
runtimehostvar = 'ZINV_ADD_HOSTS'
|
45
|
-
|
46
|
-
opts = Trollop::options do
|
47
|
-
opt :list, "List entire inventory" # required by ansible
|
48
|
-
opt :host, "List a single host", :type => :string # required by ansible
|
49
|
-
opt :debug, "Debugging verbosity"
|
50
|
-
opt :groups, "Dump groups list"
|
51
|
-
opt :templates, "Dump templates list"
|
52
|
-
end
|
53
47
|
|
54
48
|
# Generate an in-memory host group with the comma delim list in this env var
|
55
49
|
if ENV[runtimehostvar]
|
@@ -67,22 +61,17 @@ if opts[:host]
|
|
67
61
|
end
|
68
62
|
|
69
63
|
# Connect to zabbix.
|
70
|
-
zbx =
|
71
|
-
|
72
|
-
|
73
|
-
:
|
74
|
-
:
|
64
|
+
zbx = Zabbix::Api::Client.new(url: ENV['ZINV_ZABBIX_URL'])
|
65
|
+
|
66
|
+
zbx.login(
|
67
|
+
user: ENV['ZINV_ZABBIX_USER'],
|
68
|
+
pass: ENV['ZINV_ZABBIX_PASS'],
|
75
69
|
)
|
76
70
|
|
77
71
|
|
78
72
|
# Get all the root template objects
|
79
|
-
roottemplates = zbx.
|
80
|
-
:
|
81
|
-
:params => {
|
82
|
-
:filter => {
|
83
|
-
:host => roottemplatenames
|
84
|
-
}
|
85
|
-
}
|
73
|
+
roottemplates = zbx.template.get(
|
74
|
+
filter: { host: roottemplatenames }
|
86
75
|
)
|
87
76
|
|
88
77
|
|
@@ -92,7 +81,7 @@ prevchunk = roottemplates # this is for the "walking the tree" logic
|
|
92
81
|
|
93
82
|
# Set up template groups for the root templates
|
94
83
|
prevchunk.each {|template|
|
95
|
-
name = template
|
84
|
+
name = template.name.gsub(/[ ]/,'_')
|
96
85
|
if not ansibletemplategroups.has_key?(name)
|
97
86
|
ansibletemplategroups[name] = {'hosts' => Set.new}
|
98
87
|
end
|
@@ -101,11 +90,8 @@ prevchunk.each {|template|
|
|
101
90
|
# Find all templates that derive from the root ones, adding to ansibletemplates
|
102
91
|
# and ansibletemplategroups as we go
|
103
92
|
begin
|
104
|
-
nextchunk = zbx.
|
105
|
-
:
|
106
|
-
:params => {
|
107
|
-
:parentTemplateids => prevchunk.collect {|each| each['templateid']}
|
108
|
-
}
|
93
|
+
nextchunk = zbx.template.get(
|
94
|
+
parentTemplateids: prevchunk.collect {|each| each['templateid']}
|
109
95
|
)
|
110
96
|
nextchunk.each {|template|
|
111
97
|
name = template['name'].gsub(/[ ]/,'_')
|
@@ -132,21 +118,17 @@ ansiblehostgroups['New_Hosts'] = {'hosts' => newhostnamelist}
|
|
132
118
|
metadata = Hash.new
|
133
119
|
|
134
120
|
|
121
|
+
|
135
122
|
# Gather all the hosts that use the templates we've collected. We'll put them in
|
136
123
|
# two types of hashes: host group hashes and template group hashes.
|
137
|
-
zbx.
|
124
|
+
zbx.hostgroup.get().each { |hostgroup|
|
138
125
|
hostlist = Array.new
|
139
126
|
# Gather hosts using our templates, ensuring we also grab inventory for this host
|
140
|
-
hosts = zbx.
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
:selectParentTemplates => [
|
146
|
-
'name'
|
147
|
-
],
|
148
|
-
:selectInventory => 'extend'
|
149
|
-
}
|
127
|
+
hosts = zbx.host.get(
|
128
|
+
groupids: [hostgroup.groupid],
|
129
|
+
templateids: ansibletemplates.collect {|each| each['templateid']},
|
130
|
+
selectParentTemplates: [ 'name' ],
|
131
|
+
selectInventory: 'extend'
|
150
132
|
)
|
151
133
|
|
152
134
|
# For each host we've collected, add it to each group it belongs in (both host and template),
|
@@ -155,12 +137,12 @@ zbx.hostgroups.all.each { |name,id|
|
|
155
137
|
hosts.each { |host|
|
156
138
|
# You can put the string DONOTMANAGE anywhere in the zabbix host description
|
157
139
|
# If you do that the node in question will be excluded from inventory
|
158
|
-
if host
|
159
|
-
hostlist.push(host
|
160
|
-
host
|
140
|
+
if host.description !~ /DONOTMANAGE/
|
141
|
+
hostlist.push(host.host)
|
142
|
+
host.parentTemplates.each { |template|
|
161
143
|
templatename = template['name'].gsub(/[ ]/,'_')
|
162
144
|
if ansibletemplategroups.has_key?(templatename)
|
163
|
-
ansibletemplategroups[templatename]['hosts'].add(host
|
145
|
+
ansibletemplategroups[templatename]['hosts'].add(host.name)
|
164
146
|
end
|
165
147
|
}
|
166
148
|
# If you need to define host variables, in the zabbix host definition select "Inventory" and put yaml in the Notes field.
|
@@ -170,11 +152,11 @@ zbx.hostgroups.all.each { |name,id|
|
|
170
152
|
# ereiamjh: The ghost in the machine
|
171
153
|
# youshouldwatch: Brazil
|
172
154
|
#
|
173
|
-
if host.
|
155
|
+
if host.respond_to?(:inventory) and host.inventory.class == Hash and host.inventory.has_key?('notes') and host.inventory['notes'].size > 0
|
174
156
|
begin
|
175
|
-
metadata[host
|
157
|
+
metadata[host.name] = YAML.load(host.inventory['notes'])
|
176
158
|
rescue Exception => e
|
177
|
-
puts "Error parsing inventory notes field for host #{host
|
159
|
+
puts "Error parsing inventory notes field for host #{host.name} - SKIPPING"
|
178
160
|
ap e
|
179
161
|
end
|
180
162
|
end
|
@@ -183,11 +165,12 @@ zbx.hostgroups.all.each { |name,id|
|
|
183
165
|
}
|
184
166
|
|
185
167
|
if hostlist.size > 0
|
186
|
-
ansiblehostgroups[name.gsub(/[ ]/,'_')] = {'hosts'=>hostlist}
|
168
|
+
ansiblehostgroups[hostgroup.name.gsub(/[ ]/,'_')] = {'hosts'=>hostlist}
|
187
169
|
ansiblehostgroups['All_Hosts']['hosts'].merge(hostlist) # probably don't need this
|
188
170
|
end
|
189
171
|
}
|
190
172
|
|
173
|
+
|
191
174
|
# These are just for more informative dumps...
|
192
175
|
hostgroupcount = ansiblehostgroups.keys.size
|
193
176
|
if opts[:groups]
|
data/lib/zinv/version.rb
CHANGED
data/zinv.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["david.parker _AT_ nsight.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{Ansible dynamic inventory script for hosts monitored by zabbix}
|
13
|
-
spec.homepage = "https://
|
13
|
+
spec.homepage = "https://gitlab.com/svdasein/zinv"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
@@ -26,9 +26,9 @@ Gem::Specification.new do |spec|
|
|
26
26
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
27
|
spec.require_paths = ["lib"]
|
28
28
|
|
29
|
-
spec.add_development_dependency "bundler"
|
30
|
-
spec.add_development_dependency "rake"
|
31
|
-
spec.add_runtime_dependency "
|
32
|
-
spec.add_runtime_dependency "
|
33
|
-
spec.add_runtime_dependency "
|
29
|
+
spec.add_development_dependency "bundler"
|
30
|
+
spec.add_development_dependency "rake"
|
31
|
+
spec.add_runtime_dependency "zabbix-api-simple"
|
32
|
+
spec.add_runtime_dependency "optimist"
|
33
|
+
spec.add_runtime_dependency "amazing_print"
|
34
34
|
end
|
metadata
CHANGED
@@ -1,85 +1,85 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zinv
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dave Parker
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-05-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: zabbix-api-simple
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: optimist
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: amazing_print
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '0'
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '0'
|
83
83
|
description:
|
84
84
|
email:
|
85
85
|
- david.parker _AT_ nsight.com
|
@@ -88,6 +88,7 @@ executables:
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
+
- CHANGELOG
|
91
92
|
- CODE_OF_CONDUCT.md
|
92
93
|
- Gemfile
|
93
94
|
- LICENSE
|
@@ -100,7 +101,7 @@ files:
|
|
100
101
|
- lib/zinv.rb
|
101
102
|
- lib/zinv/version.rb
|
102
103
|
- zinv.gemspec
|
103
|
-
homepage: https://
|
104
|
+
homepage: https://gitlab.com/svdasein/zinv
|
104
105
|
licenses:
|
105
106
|
- MIT
|
106
107
|
metadata:
|
@@ -120,8 +121,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
120
121
|
- !ruby/object:Gem::Version
|
121
122
|
version: '0'
|
122
123
|
requirements: []
|
123
|
-
|
124
|
-
rubygems_version: 2.6.11
|
124
|
+
rubygems_version: 3.1.6
|
125
125
|
signing_key:
|
126
126
|
specification_version: 4
|
127
127
|
summary: Ansible dynamic inventory script for hosts monitored by zabbix
|