zapix 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +352 -4
- data/lib/zapix/proxies/scenarios.rb +9 -0
- data/lib/zapix/version.rb +1 -1
- data/spec/zapix_specs.rb +7 -2
- metadata +4 -4
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Zapix
|
2
2
|
|
3
|
-
|
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.
|
4
6
|
|
5
7
|
## Installation
|
6
8
|
|
@@ -18,12 +20,358 @@ Or install it yourself as:
|
|
18
20
|
|
19
21
|
## Usage
|
20
22
|
|
21
|
-
|
23
|
+
### Remote client
|
24
|
+
First we need the zapix remote client. Feel free to
|
25
|
+
disable the debug mode if you find it annoying.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'zapix'
|
29
|
+
zrc = ZabbixAPI.connect(
|
30
|
+
:service_url => http://ourzabbix.server-cp,,
|
31
|
+
:username => guybrush,
|
32
|
+
:password => threepwood,
|
33
|
+
:debug => true
|
34
|
+
)
|
35
|
+
```
|
36
|
+
|
37
|
+
### Remote client and the tests
|
38
|
+
In order to run the tests you need a running zabbix server.
|
39
|
+
You can get one if you run the test-kitchen tests for zabbix.
|
40
|
+
|
41
|
+
These environment variables also need to be set:
|
42
|
+
|
43
|
+
ZABBIX_API_URL
|
44
|
+
ZABBIX_API_LOGIN
|
45
|
+
ZABBIX_API_PASSWORD
|
46
|
+
|
47
|
+
### Hostgroup Operations
|
48
|
+
#### Creating a hostgroup
|
49
|
+
```ruby
|
50
|
+
zrc.hostgroups.create('test_hostgroup')
|
51
|
+
```
|
52
|
+
|
53
|
+
#### Checking if a hostgroup exists
|
54
|
+
```ruby
|
55
|
+
zrc.hostgroups.exists?('test_hostgroup')
|
56
|
+
```
|
57
|
+
|
58
|
+
#### Checking if a hostgroup has any attached hosts
|
59
|
+
```ruby
|
60
|
+
zrc.hostgroups.any_hosts?('test_hostgroup')
|
61
|
+
```
|
62
|
+
|
63
|
+
#### Getting all host ids of hosts belonging to a hostgroup
|
64
|
+
```ruby
|
65
|
+
zrc.hostgroups.get_host_ids_of('test_hostgroup')
|
66
|
+
```
|
67
|
+
|
68
|
+
#### Deleting a hostgroup
|
69
|
+
Note that deleting a hostgroups with attached hosts also deletes the hosts.
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
zrc.hostgroups.delete('test_hostgroup')
|
73
|
+
```
|
74
|
+
|
75
|
+
#### Getting the id of a hostgroup
|
76
|
+
```ruby
|
77
|
+
zrc.hostgroups.get_id('test_hostgroup')
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Getting all hostgroups names
|
81
|
+
```ruby
|
82
|
+
zrc.hostgroups.get_all
|
83
|
+
```
|
84
|
+
|
85
|
+
### Host Operations
|
86
|
+
|
87
|
+
#### Getting the id of a host
|
88
|
+
```ruby
|
89
|
+
zrc.hosts.get_id('test_host')
|
90
|
+
```
|
91
|
+
|
92
|
+
#### Checking if a host exists
|
93
|
+
```ruby
|
94
|
+
zrc.hosts.exists?('test_host')
|
95
|
+
```
|
96
|
+
|
97
|
+
#### Getting all host names
|
98
|
+
```ruby
|
99
|
+
zrc.hosts.get_all
|
100
|
+
```
|
101
|
+
|
102
|
+
#### Creating a host
|
103
|
+
Note that in zabbix host cannot exists on its own, it always needs a hostgroup.
|
104
|
+
```ruby
|
105
|
+
hostgroup_id = zrc.hostgroups.get_id('test_hostgroup')
|
106
|
+
|
107
|
+
zabbix_interface = Interface.new(
|
108
|
+
'ip' => '127.0.0.1',
|
109
|
+
'dns' => 'abrakadabra.com'
|
110
|
+
)
|
111
|
+
|
112
|
+
jmx_interface = Interface.new(
|
113
|
+
'ip' => '127.0.0.1',
|
114
|
+
'dns' => 'abrakadabra.com',
|
115
|
+
'type' => 4, # JMX
|
116
|
+
'main' => 1, # default jmx interface
|
117
|
+
'port' => 9003
|
118
|
+
)
|
119
|
+
|
120
|
+
template_1 = zrc.templates.get_id('example_template_1')
|
121
|
+
template_2 = zrc.templates.get_id('example_template_2')
|
122
|
+
|
123
|
+
example_host = Host.new
|
124
|
+
example_host.add_name('test_host')
|
125
|
+
example_host.add_interfaces(zabbix_interface.to_hash)
|
126
|
+
example_host.add_interfaces(jmx_interface.to_hash)
|
127
|
+
example_host.add_macros({'macro' => '{$TESTMACRO}', 'value' => 'test123'})
|
128
|
+
example_host.add_group_ids(hostgroup_id)
|
129
|
+
example_host.add_template_ids(template_1, template_2)
|
130
|
+
zrc.hosts.create(example_host.to_hash)
|
131
|
+
```
|
132
|
+
|
133
|
+
#### Deleting a host
|
134
|
+
```ruby
|
135
|
+
zrc.hosts.delete('test_host')
|
136
|
+
```
|
137
|
+
|
138
|
+
### Template Operations
|
139
|
+
|
140
|
+
#### Checking if a template exists
|
141
|
+
```ruby
|
142
|
+
zrc.templates.exists?('test_template')
|
143
|
+
```
|
144
|
+
|
145
|
+
#### Getting the id of a template
|
146
|
+
```ruby
|
147
|
+
zrc.templates.get_id('test_template')
|
148
|
+
```
|
149
|
+
|
150
|
+
#### Getting all templates for a host
|
151
|
+
```ruby
|
152
|
+
zrc.templates.get_templates_for_host(zrc.hosts.get_id('test_host'))
|
153
|
+
```
|
154
|
+
|
155
|
+
### Application Operations
|
156
|
+
|
157
|
+
#### Getting the id of an application
|
158
|
+
Note that an application always belogs to a host.
|
159
|
+
```ruby
|
160
|
+
zrc.applications.get_id({
|
161
|
+
'name' => 'test_app',
|
162
|
+
'hostid' => zrc.hosts.get_id('test_name')
|
163
|
+
})
|
164
|
+
```
|
165
|
+
|
166
|
+
#### Creating an application for host
|
167
|
+
```ruby
|
168
|
+
zrc.applications.create({
|
169
|
+
'name' => 'test_application'
|
170
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
171
|
+
})
|
172
|
+
```
|
173
|
+
|
174
|
+
### Web Scenario Operations
|
175
|
+
Note that a web scenario needs a host and an application in zabbix 2.0.6. This is
|
176
|
+
going to change in the next versions of zabbix. When creating scenarios it also
|
177
|
+
makes sense to create triggers for them.
|
178
|
+
|
179
|
+
#### Checking if a scenario exists
|
180
|
+
```ruby
|
181
|
+
zrc.scenarios.exists?({
|
182
|
+
'name' => 'test_scenario'
|
183
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
184
|
+
})
|
185
|
+
```
|
186
|
+
|
187
|
+
#### Getting the id of a scenario
|
188
|
+
```ruby
|
189
|
+
zrc.scenarios.get_id({
|
190
|
+
'name' => 'test_scenario'
|
191
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
192
|
+
})
|
193
|
+
```
|
194
|
+
|
195
|
+
#### Creating a scenario
|
196
|
+
```ruby
|
197
|
+
|
198
|
+
zrc.applications.create({
|
199
|
+
'name' => 'test_app',
|
200
|
+
'hostid' => zrc.hosts.get_id('test_name')
|
201
|
+
})
|
202
|
+
|
203
|
+
webcheck_options = Hash.new
|
204
|
+
webcheck_options['hostid'] = zrc.hosts.get_id('host')
|
205
|
+
webcheck_options['name'] = 'my first scenario'
|
206
|
+
webcheck_options['applicationid'] = zrc.applications.get_id('test_app')
|
207
|
+
webcheck_options['steps'] = [{'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1}]
|
208
|
+
zrc.scenarios.create(webcheck_options)
|
209
|
+
```
|
210
|
+
|
211
|
+
#### Deleting a scenario
|
212
|
+
```ruby
|
213
|
+
zrc.scenarios.delete({
|
214
|
+
'name' => 'test_scenario'
|
215
|
+
'hostid' => zrc.hosts.get_id('test_host')
|
216
|
+
})
|
217
|
+
```
|
218
|
+
|
219
|
+
### Trigger Operations
|
220
|
+
|
221
|
+
#### Checking if a trigger exists
|
222
|
+
```ruby
|
223
|
+
zrc.triggers.exists?({
|
224
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
225
|
+
})
|
226
|
+
```
|
227
|
+
|
228
|
+
### Getting the id of a trigger
|
229
|
+
```ruby
|
230
|
+
zrc.triggers.get_id({
|
231
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
232
|
+
})
|
233
|
+
```
|
234
|
+
|
235
|
+
### Creating a trigger
|
236
|
+
```ruby
|
237
|
+
options = Hash.new
|
238
|
+
options['description'] = 'Webpage failed on {HOST.NAME}'
|
239
|
+
options['expression'] = "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
240
|
+
options['priority'] = 2 # 2 means Warning
|
241
|
+
zrc.triggers.create(options)
|
242
|
+
```
|
243
|
+
|
244
|
+
### Deleting a trigger
|
245
|
+
```ruby
|
246
|
+
trigger_id = zrc.triggers.get_id({
|
247
|
+
'expression' => "{test_host:web.test.fail[test_scenario].max(#3)}#0"
|
248
|
+
})
|
249
|
+
|
250
|
+
zrc.triggers.delete(trigger_id)
|
251
|
+
```
|
252
|
+
|
253
|
+
### Usergroups Operations
|
254
|
+
|
255
|
+
#### Checking if a usergroup exists
|
256
|
+
```ruby
|
257
|
+
zrc.usergroups.exists?({
|
258
|
+
'name' => 'test_usergroup'
|
259
|
+
})
|
260
|
+
```
|
261
|
+
#### Geting the id of a usergroup
|
262
|
+
```ruby
|
263
|
+
zrc.usergroups.get_id({
|
264
|
+
'name' = 'test_usergroup'
|
265
|
+
})
|
266
|
+
```
|
267
|
+
|
268
|
+
#### Creating a usergroup
|
269
|
+
```ruby
|
270
|
+
options = Hash.new
|
271
|
+
options['name'] = 'test_usergroup'
|
272
|
+
options['rights'] = {
|
273
|
+
'permission' => 3,
|
274
|
+
'id' => zrc.hostgroups.get_id('test_hostgroup')
|
275
|
+
}
|
276
|
+
zrc.usergroups.create(options)
|
277
|
+
```
|
278
|
+
|
279
|
+
#### Deleting a user group
|
280
|
+
```ruby
|
281
|
+
usergroup_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
|
282
|
+
zrc.usergroups.delete(usergroup_id)
|
283
|
+
```
|
284
|
+
|
285
|
+
### User Operations
|
286
|
+
#### Checking if a user exists
|
287
|
+
```ruby
|
288
|
+
zrc.users.exists?({'alias' => 'max'})
|
289
|
+
```
|
290
|
+
|
291
|
+
#### Getting the id of a user
|
292
|
+
```ruby
|
293
|
+
zrc.users.get_id({'alias' => 'max'})
|
294
|
+
```
|
295
|
+
|
296
|
+
#### Creating a user
|
297
|
+
```ruby
|
298
|
+
group_id = zrc.usergroups.get_id({'name' => 'test_usergroup'})
|
299
|
+
user_options = Hash.new
|
300
|
+
|
301
|
+
user_options['alias'] = 'igor'
|
302
|
+
user_options['passwd'] = 'geheim'
|
303
|
+
user_options['usrgrps'] = [{
|
304
|
+
'usrgrpid' => group_id
|
305
|
+
}]
|
306
|
+
|
307
|
+
user_options['user_medias'] = [{
|
308
|
+
'mediatypeid' => 1,
|
309
|
+
'sendto' => 'support@company.com',
|
310
|
+
'active' => 0,
|
311
|
+
'severity' => 63,
|
312
|
+
'period' => '1-7,00:00-24:00'
|
313
|
+
}]
|
314
|
+
|
315
|
+
zrc.users.create(user_options)
|
316
|
+
```
|
317
|
+
|
318
|
+
### Actions Operations
|
319
|
+
|
320
|
+
#### Checking if an action exists
|
321
|
+
```ruby
|
322
|
+
zrc.actions.exists?({'name' => 'Report problems to Zabbix administrators'})
|
323
|
+
```
|
324
|
+
|
325
|
+
#### Getting the id of an action
|
326
|
+
```ruby
|
327
|
+
zrc.actions.get_id({'name' => 'Report problems to Zabbix administrators'})
|
328
|
+
```
|
329
|
+
|
330
|
+
#### Creating an action
|
331
|
+
```ruby
|
332
|
+
usergroup_options = Hash.new({'name' = 'test_usergroup'})
|
333
|
+
action_options = Hash.new
|
334
|
+
action_options['name'] = 'Report problems to Zabbix administrators'
|
335
|
+
action_options['eventsource'] = 0
|
336
|
+
action_options['evaltype'] = 1 # AND
|
337
|
+
action_options['status'] = 1 # Disabled
|
338
|
+
action_options['esc_period'] = 3600
|
339
|
+
action_options['def_shortdata'] = '{TRIGGER.NAME}: {TRIGGER.STATUS}'
|
340
|
+
action_options['def_longdata'] = "{TRIGGER.NAME}: {TRIGGER.STATUS}\r\nLast value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}"
|
341
|
+
action_options['conditions'] = [{
|
342
|
+
'conditiontype' => 0, # Hostgroup
|
343
|
+
'operator' => 0, # =
|
344
|
+
'value' => zrc.hostgroups.get_id('Templates')
|
345
|
+
},
|
346
|
+
# not in maintenance
|
347
|
+
{
|
348
|
+
'conditiontype' => 16, # Maintenance
|
349
|
+
'operator' => 7, # not in
|
350
|
+
'value' => 'maintenance'
|
351
|
+
}]
|
352
|
+
|
353
|
+
action_options['operations'] = [{
|
354
|
+
'operationtype' => 0,
|
355
|
+
'esc_period' => 0,
|
356
|
+
'esc_step_from' => 1,
|
357
|
+
'esc_step_to' => 1,
|
358
|
+
'evaltype' => 0,
|
359
|
+
'opmessage_grp' => [{
|
360
|
+
'usrgrpid' => zrc.usergroups.get_id(usergroup_options)
|
361
|
+
}],
|
362
|
+
'opmessage' => {
|
363
|
+
'default_msg' => 1,
|
364
|
+
'mediatypeid' => 1
|
365
|
+
}
|
366
|
+
}]
|
367
|
+
zrc.actions.create(action_options)
|
368
|
+
```
|
22
369
|
|
23
370
|
## Contributing
|
24
371
|
|
25
372
|
1. Fork it
|
26
373
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
-
3.
|
28
|
-
4.
|
374
|
+
3. Don't forget to write tests
|
375
|
+
4. Commit your changes (`git commit -am 'Add some feature'`)
|
376
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
29
377
|
5. Create new Pull Request
|
@@ -23,4 +23,13 @@ class Scenarios < Base
|
|
23
23
|
|
24
24
|
result.to_i >= 1 ? true : false
|
25
25
|
end
|
26
|
+
|
27
|
+
def get_all
|
28
|
+
scenarios = client.webcheck_get({'output' => 'extend'})
|
29
|
+
names = extract_names(scenarios)
|
30
|
+
end
|
31
|
+
|
32
|
+
def extract_names(scenarios)
|
33
|
+
scenarios.map {|scenario| scenario['name']}
|
34
|
+
end
|
26
35
|
end
|
data/lib/zapix/version.rb
CHANGED
data/spec/zapix_specs.rb
CHANGED
@@ -133,7 +133,6 @@ describe ZabbixAPI do
|
|
133
133
|
options['expression'] = trigger_expression
|
134
134
|
options['priority'] = 2 # 2 means Warning
|
135
135
|
zrc.triggers.create(options)
|
136
|
-
|
137
136
|
end
|
138
137
|
|
139
138
|
after(:each) do
|
@@ -254,12 +253,18 @@ describe ZabbixAPI do
|
|
254
253
|
zrc.scenarios.exists?(options).should be_true
|
255
254
|
end
|
256
255
|
|
256
|
+
it 'returns all web scenarios' do
|
257
|
+
options = {}
|
258
|
+
(zrc.scenarios.get_all).should include(scenario)
|
259
|
+
end
|
260
|
+
|
257
261
|
it 'gets the id of a web scenario' do
|
258
262
|
options = {}
|
259
263
|
options['name'] = scenario
|
260
264
|
options['hostid'] = zrc.hosts.get_id(host)
|
261
265
|
zrc.scenarios.exists?(options).should be_true
|
262
|
-
zrc.scenarios.get_id(options)
|
266
|
+
result = zrc.scenarios.get_id(options)
|
267
|
+
(result.to_i).should >= 0
|
263
268
|
end
|
264
269
|
|
265
270
|
it 'returns false if a web scenario does not exist' do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zapix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-09-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
@@ -138,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
138
138
|
version: '0'
|
139
139
|
segments:
|
140
140
|
- 0
|
141
|
-
hash:
|
141
|
+
hash: -2133943749775290873
|
142
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
143
143
|
none: false
|
144
144
|
requirements:
|
@@ -147,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
147
147
|
version: '0'
|
148
148
|
segments:
|
149
149
|
- 0
|
150
|
-
hash:
|
150
|
+
hash: -2133943749775290873
|
151
151
|
requirements: []
|
152
152
|
rubyforge_project:
|
153
153
|
rubygems_version: 1.8.24
|