ams-zapix3 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +22 -0
- data/README.md +350 -0
- data/Rakefile +1 -0
- data/lib/zapix.rb +72 -0
- data/lib/zapix/proxies/actions.rb +26 -0
- data/lib/zapix/proxies/applications.rb +27 -0
- data/lib/zapix/proxies/base.rb +7 -0
- data/lib/zapix/proxies/graphs.rb +18 -0
- data/lib/zapix/proxies/hostgroups.rb +97 -0
- data/lib/zapix/proxies/hostinterfaces.rb +20 -0
- data/lib/zapix/proxies/hosts.rb +80 -0
- data/lib/zapix/proxies/proxies.rb +9 -0
- data/lib/zapix/proxies/scenarios.rb +32 -0
- data/lib/zapix/proxies/screenitems.rb +11 -0
- data/lib/zapix/proxies/screens.rb +26 -0
- data/lib/zapix/proxies/templates.rb +29 -0
- data/lib/zapix/proxies/triggers.rb +50 -0
- data/lib/zapix/proxies/usergroups.rb +32 -0
- data/lib/zapix/proxies/users.rb +26 -0
- data/lib/zapix/version.rb +3 -0
- data/lib/zapix/zabbix_classes/host.rb +51 -0
- data/lib/zapix/zabbix_classes/interface.rb +28 -0
- data/lib/zapix/zabbix_rpc_client.rb +64 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/zapix_specs.rb +516 -0
- data/zapix.gemspec +25 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 88b876017f3932e777523a3a1b01d65809da2882
|
4
|
+
data.tar.gz: 1ca4d5fd9a3fc48a70a90fd57a372181cafe26cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 71a547f485073b522872db5505adf8651cd358676bbf68112e3e534b824114c0ef4ea9f6fd5d6cd749b56be66315a0a12e1176d95955cd76adae151365962358
|
7
|
+
data.tar.gz: 240e85023996b3ed73fb67dace232fe721dd4a2aa06c8c8df660245449d310d5823b6abf870cf12dfd9fcd5677a2100b6bea0a7ce6105e895f7dd6c4e77b38a8
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
zapix3 (0.2.5)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
json (1.8.3)
|
11
|
+
rake (11.1.2)
|
12
|
+
rspec (3.4.0)
|
13
|
+
rspec-core (~> 3.4.0)
|
14
|
+
rspec-expectations (~> 3.4.0)
|
15
|
+
rspec-mocks (~> 3.4.0)
|
16
|
+
rspec-core (3.4.4)
|
17
|
+
rspec-support (~> 3.4.0)
|
18
|
+
rspec-expectations (3.4.0)
|
19
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
20
|
+
rspec-support (~> 3.4.0)
|
21
|
+
rspec-mocks (3.4.1)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.4.0)
|
24
|
+
rspec-support (3.4.1)
|
25
|
+
|
26
|
+
PLATFORMS
|
27
|
+
ruby
|
28
|
+
|
29
|
+
DEPENDENCIES
|
30
|
+
bundler (~> 1.3)
|
31
|
+
json
|
32
|
+
rake
|
33
|
+
rspec
|
34
|
+
zapix3!
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Stoyan Stoyanov
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,350 @@
|
|
1
|
+
# Zapix3
|
2
|
+
|
3
|
+
Zapix is a tool which makes the communication with the zabbix's api simple.
|
4
|
+
|
5
|
+
If you need a more detailed information of how to use zapix see the specs.
|
6
|
+
|
7
|
+
This version of zappix is compatible with zabbix 3.0
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'zapix3', '0.1.2'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it with gem:
|
20
|
+
|
21
|
+
$ gem install zapix3
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### Remote client
|
26
|
+
First create a remote client. Feel free to
|
27
|
+
disable the debug mode if you find it annoying.
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
require 'zapix'
|
31
|
+
zrc = ZabbixAPI.connect(
|
32
|
+
service_url: ENV['ZABBIX_SERVER_URL'],
|
33
|
+
username: ENV['ZABBIX_USERNAME'],
|
34
|
+
password: ENV['ZABBIX_PASSWORD'],
|
35
|
+
debug: true
|
36
|
+
)
|
37
|
+
```
|
38
|
+
|
39
|
+
## Functionality
|
40
|
+
|
41
|
+
### Hostgroup Operations
|
42
|
+
#### Creating a hostgroup
|
43
|
+
```ruby
|
44
|
+
zrc.hostgroups.create('test_hostgroup')
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Checking if a hostgroup has any attached hosts
|
48
|
+
```ruby
|
49
|
+
zrc.hostgroups.any_hosts?('test_hostgroup')
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Getting all host ids of hosts belonging to a hostgroup
|
53
|
+
```ruby
|
54
|
+
zrc.hostgroups.get_host_ids_of('test_hostgroup')
|
55
|
+
```
|
56
|
+
|
57
|
+
#### Deleting a hostgroup
|
58
|
+
Note that deleting a hostgroups with attached hosts also deletes the hosts.
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
zrc.hostgroups.delete('test_hostgroup')
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Getting the id of a hostgroup
|
65
|
+
```ruby
|
66
|
+
zrc.hostgroups.get_id('test_hostgroup')
|
67
|
+
```
|
68
|
+
|
69
|
+
#### Getting all hostgroups names
|
70
|
+
```ruby
|
71
|
+
zrc.hostgroups.get_all
|
72
|
+
```
|
73
|
+
|
74
|
+
### Host Operations
|
75
|
+
|
76
|
+
#### Getting the id of a host
|
77
|
+
```ruby
|
78
|
+
zrc.hosts.get_id('test_host')
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Getting all host names
|
82
|
+
```ruby
|
83
|
+
zrc.hosts.get_all
|
84
|
+
```
|
85
|
+
|
86
|
+
#### Creating a host
|
87
|
+
Note that in zabbix host cannot exists on its own, it always needs a hostgroup.
|
88
|
+
```ruby
|
89
|
+
hostgroup_id = zrc.hostgroups.get_id('test_hostgroup')
|
90
|
+
|
91
|
+
zabbix_interface = Interface.new(
|
92
|
+
'ip' => '127.0.0.1',
|
93
|
+
'dns' => 'abrakadabra.com'
|
94
|
+
)
|
95
|
+
|
96
|
+
jmx_interface = Interface.new(
|
97
|
+
'ip' => '127.0.0.1',
|
98
|
+
'dns' => 'abrakadabra.com',
|
99
|
+
'type' => 4, # JMX
|
100
|
+
'main' => 1, # default jmx interface
|
101
|
+
'port' => 9003
|
102
|
+
)
|
103
|
+
|
104
|
+
template_1 = zrc.templates.get_id('example_template_1')
|
105
|
+
template_2 = zrc.templates.get_id('example_template_2')
|
106
|
+
|
107
|
+
example_host = Host.new
|
108
|
+
example_host.add_name('test_host')
|
109
|
+
example_host.add_interfaces(zabbix_interface.to_hash)
|
110
|
+
example_host.add_interfaces(jmx_interface.to_hash)
|
111
|
+
example_host.add_macros({'macro' => '{$TESTMACRO}', 'value' => 'test123'})
|
112
|
+
example_host.add_group_ids(hostgroup_id)
|
113
|
+
example_host.add_template_ids(template_1, template_2)
|
114
|
+
zrc.hosts.create(example_host.to_hash)
|
115
|
+
```
|
116
|
+
|
117
|
+
#### Deleting a host
|
118
|
+
```ruby
|
119
|
+
zrc.hosts.delete('test_host')
|
120
|
+
```
|
121
|
+
|
122
|
+
### Template Operations
|
123
|
+
|
124
|
+
#### Checking if a template exists
|
125
|
+
```ruby
|
126
|
+
zrc.templates.exists?('test_template')
|
127
|
+
```
|
128
|
+
|
129
|
+
#### Getting the id of a template
|
130
|
+
```ruby
|
131
|
+
zrc.templates.get_id('test_template')
|
132
|
+
```
|
133
|
+
|
134
|
+
#### Getting all templates for a host
|
135
|
+
```ruby
|
136
|
+
zrc.templates.get_templates_for_host(zrc.hosts.get_id('test_host'))
|
137
|
+
```
|
138
|
+
|
139
|
+
### Application Operations
|
140
|
+
|
141
|
+
#### Getting the id of an application
|
142
|
+
Note that an application always belogs to a host.
|
143
|
+
```ruby
|
144
|
+
zrc.applications.get_id({
|
145
|
+
'name' => 'test_app',
|
146
|
+
'hostid' => zrc.hosts.get_id('test_name')
|
147
|
+
})
|
148
|
+
```
|
149
|
+
|
150
|
+
#### Creating an application for host
|
151
|
+
```ruby
|
152
|
+
zrc.applications.create({
|
153
|
+
'name' => 'test_application'
|
154
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
155
|
+
})
|
156
|
+
```
|
157
|
+
|
158
|
+
### Web Scenario Operations
|
159
|
+
Note that a web scenario needs a host and an application in zabbix 2.0.6. This is
|
160
|
+
going to change in the next versions of zabbix. When creating scenarios it also
|
161
|
+
makes sense to create triggers for them.
|
162
|
+
|
163
|
+
#### Checking if a scenario exists
|
164
|
+
```ruby
|
165
|
+
zrc.scenarios.exists?({
|
166
|
+
'name' => 'test_scenario',
|
167
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
168
|
+
})
|
169
|
+
```
|
170
|
+
|
171
|
+
#### Getting the id of a scenario
|
172
|
+
```ruby
|
173
|
+
zrc.scenarios.get_id({
|
174
|
+
'name' => 'test_scenario'
|
175
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
176
|
+
})
|
177
|
+
```
|
178
|
+
|
179
|
+
#### Creating a scenario
|
180
|
+
```ruby
|
181
|
+
|
182
|
+
zrc.applications.create({
|
183
|
+
'name' => 'test_app',
|
184
|
+
'hostid' => zrc.hosts.get_id('test_name')
|
185
|
+
})
|
186
|
+
|
187
|
+
webcheck_options = Hash.new
|
188
|
+
webcheck_options['hostid'] = zrc.hosts.get_id('host')
|
189
|
+
webcheck_options['name'] = 'my first scenario'
|
190
|
+
webcheck_options['applicationid'] = zrc.applications.get_id('test_app')
|
191
|
+
webcheck_options['steps'] = [{'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1}]
|
192
|
+
zrc.scenarios.create(webcheck_options)
|
193
|
+
```
|
194
|
+
|
195
|
+
#### Deleting a scenario
|
196
|
+
```ruby
|
197
|
+
zrc.scenarios.delete({
|
198
|
+
'name' => 'test_scenario',
|
199
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
200
|
+
})
|
201
|
+
```
|
202
|
+
|
203
|
+
### Trigger Operations
|
204
|
+
|
205
|
+
#### Checking if a trigger exists
|
206
|
+
```ruby
|
207
|
+
zrc.triggers.exists?({
|
208
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
209
|
+
})
|
210
|
+
```
|
211
|
+
|
212
|
+
### Getting the id of a trigger
|
213
|
+
```ruby
|
214
|
+
zrc.triggers.get_id({
|
215
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
216
|
+
})
|
217
|
+
```
|
218
|
+
|
219
|
+
### Creating a trigger
|
220
|
+
```ruby
|
221
|
+
options = Hash.new
|
222
|
+
options['description'] = 'Webpage failed on {HOST.NAME}'
|
223
|
+
options['expression'] = "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
224
|
+
options['priority'] = 2 # 2 means Warning
|
225
|
+
zrc.triggers.create(options)
|
226
|
+
```
|
227
|
+
|
228
|
+
### Deleting a trigger
|
229
|
+
```ruby
|
230
|
+
trigger_id = zrc.triggers.get_id({
|
231
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
232
|
+
})
|
233
|
+
|
234
|
+
zrc.triggers.delete(trigger_id)
|
235
|
+
```
|
236
|
+
|
237
|
+
### Usergroups Operations
|
238
|
+
|
239
|
+
#### Geting the id of a usergroup
|
240
|
+
```ruby
|
241
|
+
zrc.usergroups.get_id({
|
242
|
+
'name' = 'test_usergroup'
|
243
|
+
})
|
244
|
+
```
|
245
|
+
|
246
|
+
#### Creating a usergroup
|
247
|
+
```ruby
|
248
|
+
options = Hash.new
|
249
|
+
options['name'] = 'test_usergroup'
|
250
|
+
options['rights'] = {
|
251
|
+
'permission' => 3,
|
252
|
+
'id' => zrc.hostgroups.get_id('test_hostgroup')
|
253
|
+
}
|
254
|
+
zrc.usergroups.create(options)
|
255
|
+
```
|
256
|
+
|
257
|
+
#### Deleting a user group
|
258
|
+
```ruby
|
259
|
+
usergroup_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
|
260
|
+
zrc.usergroups.delete(usergroup_id)
|
261
|
+
```
|
262
|
+
|
263
|
+
#### Getting the id of a user
|
264
|
+
```ruby
|
265
|
+
zrc.users.get_id({'alias' => 'max'})
|
266
|
+
```
|
267
|
+
|
268
|
+
#### Creating a user
|
269
|
+
```ruby
|
270
|
+
group_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
|
271
|
+
user_options = Hash.new
|
272
|
+
|
273
|
+
user_options['alias'] = 'igor'
|
274
|
+
user_options['passwd'] = 'geheim'
|
275
|
+
user_options['usrgrps'] = [{
|
276
|
+
'usrgrpid' => group_id
|
277
|
+
}]
|
278
|
+
|
279
|
+
user_options['user_medias'] = [{
|
280
|
+
'mediatypeid' => 1,
|
281
|
+
'sendto' => 'support@company.com',
|
282
|
+
'active' => 0,
|
283
|
+
'severity' => 63,
|
284
|
+
'period' => '1-7,00:00-24:00'
|
285
|
+
}]
|
286
|
+
|
287
|
+
zrc.users.create(user_options)
|
288
|
+
```
|
289
|
+
|
290
|
+
### Actions Operations
|
291
|
+
|
292
|
+
#### Getting the id of an action
|
293
|
+
```ruby
|
294
|
+
zrc.actions.get_id({'name' => 'Report problems to Zabbix administrators'})
|
295
|
+
```
|
296
|
+
|
297
|
+
#### Creating an action
|
298
|
+
```ruby
|
299
|
+
usergroup_options = Hash.new({'name' = 'test_usergroup'})
|
300
|
+
action_options = Hash.new
|
301
|
+
action_options['name'] = 'Report problems to Zabbix administrators'
|
302
|
+
action_options['eventsource'] = 0
|
303
|
+
action_options['evaltype'] = 1 # AND
|
304
|
+
action_options['status'] = 1 # Disabled
|
305
|
+
action_options['esc_period'] = 3600
|
306
|
+
action_options['def_shortdata'] = '{TRIGGER.NAME}: {TRIGGER.STATUS}'
|
307
|
+
action_options['def_longdata'] = "{TRIGGER.NAME}: {TRIGGER.STATUS}\r\nLast value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}"
|
308
|
+
action_options['conditions'] = [{
|
309
|
+
'conditiontype' => 0, # Hostgroup
|
310
|
+
'operator' => 0, # =
|
311
|
+
'value' => zrc.hostgroups.get_id('Templates')
|
312
|
+
},
|
313
|
+
# not in maintenance
|
314
|
+
{
|
315
|
+
'conditiontype' => 16, # Maintenance
|
316
|
+
'operator' => 7, # not in
|
317
|
+
'value' => 'maintenance'
|
318
|
+
}]
|
319
|
+
|
320
|
+
action_options['operations'] = [{
|
321
|
+
'operationtype' => 0,
|
322
|
+
'esc_period' => 0,
|
323
|
+
'esc_step_from' => 1,
|
324
|
+
'esc_step_to' => 1,
|
325
|
+
'evaltype' => 0,
|
326
|
+
'opmessage_grp' => [{
|
327
|
+
'usrgrpid' => zrc.usergroups.get_id(usergroup_options)
|
328
|
+
}],
|
329
|
+
'opmessage' => {
|
330
|
+
'default_msg' => 1,
|
331
|
+
'mediatypeid' => 1
|
332
|
+
}
|
333
|
+
}]
|
334
|
+
zrc.actions.create(action_options)
|
335
|
+
```
|
336
|
+
|
337
|
+
### Remote client and tests
|
338
|
+
In order to run the rspec tests you need a running zabbix test server.
|
339
|
+
|
340
|
+
### TODOs
|
341
|
+
Open source the docker-compose setup of the whole zabbix installation.
|
342
|
+
|
343
|
+
## Contributing
|
344
|
+
|
345
|
+
1. Fork it
|
346
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
347
|
+
3. Don't forget to write tests
|
348
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
349
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
350
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/lib/zapix.rb
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'zapix/version'
|
2
|
+
require_relative 'zapix/zabbix_rpc_client'
|
3
|
+
|
4
|
+
class ZabbixAPI
|
5
|
+
attr_reader :client
|
6
|
+
|
7
|
+
def self.connect(options = {})
|
8
|
+
new(options)
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
@client = ZabbixRPCClient.new(options)
|
13
|
+
Dir["#{File.dirname(__FILE__)}/zapix/zabbix_classes/*.rb"].each { |f| load(f) }
|
14
|
+
Dir["#{File.dirname(__FILE__)}/zapix/proxies/*.rb"].each { |f| load(f) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def hostgroups
|
18
|
+
@hostgroups ||= HostGroups.new(client)
|
19
|
+
end
|
20
|
+
|
21
|
+
def hosts
|
22
|
+
@hosts ||= Hosts.new(client)
|
23
|
+
end
|
24
|
+
|
25
|
+
def templates
|
26
|
+
@templates ||= Templates.new(client)
|
27
|
+
end
|
28
|
+
|
29
|
+
def applications
|
30
|
+
@applications ||= Applications.new(client)
|
31
|
+
end
|
32
|
+
|
33
|
+
def scenarios
|
34
|
+
@scenarios ||= Scenarios.new(client)
|
35
|
+
end
|
36
|
+
|
37
|
+
def triggers
|
38
|
+
@triggers ||= Triggers.new(client)
|
39
|
+
end
|
40
|
+
|
41
|
+
def hostinterfaces
|
42
|
+
@hostinterfaces ||= Hostinterfaces.new(client)
|
43
|
+
end
|
44
|
+
|
45
|
+
def actions
|
46
|
+
@actions ||= Actions.new(client)
|
47
|
+
end
|
48
|
+
|
49
|
+
def proxies
|
50
|
+
@proxies ||= Proxies.new(client)
|
51
|
+
end
|
52
|
+
|
53
|
+
def usergroups
|
54
|
+
@usergroups ||= Usergroups.new(client)
|
55
|
+
end
|
56
|
+
|
57
|
+
def users
|
58
|
+
@users ||= Users.new(client)
|
59
|
+
end
|
60
|
+
|
61
|
+
def screens
|
62
|
+
@screens ||= Screens.new(client)
|
63
|
+
end
|
64
|
+
|
65
|
+
def screenitems
|
66
|
+
@screenitems ||= ScreenItems.new(client)
|
67
|
+
end
|
68
|
+
|
69
|
+
def graphs
|
70
|
+
@graphs ||= Graphs.new(client)
|
71
|
+
end
|
72
|
+
end
|