zabbixapi 0.5.0b → 0.5.0b2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +24 -2
- data/lib/zabbixapi.rb +3 -0
- data/lib/zabbixapi/mediatypes.rb +110 -0
- data/lib/zabbixapi/usergroups.rb +1 -1
- data/lib/zabbixapi/users.rb +17 -1
- data/lib/zabbixapi/version.rb +1 -1
- data/spec/localhost.rb +50 -3
- data/spec/run.rb +49 -2
- metadata +3 -2
data/README.md
CHANGED
@@ -225,13 +225,35 @@ zbx.usergroups.add_user(
|
|
225
225
|
:userids => [zbx.users.get_id(:name => "Some user")]
|
226
226
|
)
|
227
227
|
# set write and read permissions for UserGroup on all hostgroups
|
228
|
-
zbx.usergroups.
|
228
|
+
zbx.usergroups.set_perm(
|
229
229
|
:usrgrpid => zbx.usergroups.get_or_create(:name => "Some user group"),
|
230
|
-
:hostgroupids => zbx.hostgroups.all.values,
|
230
|
+
:hostgroupids => zbx.hostgroups.all.values, # kind_of Array
|
231
231
|
:permission => 3 # 2- read (by default) and 3 - write and read
|
232
232
|
)
|
233
233
|
```
|
234
234
|
|
235
|
+
### Create MediaType and add it to user ###
|
236
|
+
```ruby
|
237
|
+
zbx.mediatypes.create_or_update(
|
238
|
+
:description => "mediatype",
|
239
|
+
:type => 0,
|
240
|
+
:smtp_server => "127.0.0.1",
|
241
|
+
:smtp_email => "zabbix@test.com"
|
242
|
+
)
|
243
|
+
zbx.users.add_medias(
|
244
|
+
:userids => [zbx.users.get_id(:name => "user")],
|
245
|
+
:media => [
|
246
|
+
{
|
247
|
+
:mediatypeid => zbx.mediatypes.get_id(:description => "mediatype"),
|
248
|
+
:sendto => "test@test",
|
249
|
+
:active => 0,
|
250
|
+
:period => "1-7,00:00-24:00",
|
251
|
+
:severity => "56"
|
252
|
+
}
|
253
|
+
]
|
254
|
+
)
|
255
|
+
```
|
256
|
+
|
235
257
|
### Custom queries
|
236
258
|
```ruby
|
237
259
|
zbx.query(
|
data/lib/zabbixapi.rb
CHANGED
@@ -11,6 +11,7 @@ require "zabbixapi/items"
|
|
11
11
|
require "zabbixapi/graphs"
|
12
12
|
require "zabbixapi/screens"
|
13
13
|
require "zabbixapi/usergroups"
|
14
|
+
require "zabbixapi/mediatypes"
|
14
15
|
|
15
16
|
class ZabbixApi
|
16
17
|
|
@@ -26,6 +27,7 @@ class ZabbixApi
|
|
26
27
|
attr :graphs
|
27
28
|
attr :screens
|
28
29
|
attr :usergroups
|
30
|
+
attr :mediatypes
|
29
31
|
|
30
32
|
def self.connect(options = {})
|
31
33
|
new(options)
|
@@ -52,6 +54,7 @@ class ZabbixApi
|
|
52
54
|
@graphs = Graphs.new(options)
|
53
55
|
@screens = Screens.new(options)
|
54
56
|
@usergroups = Usergroups.new(options)
|
57
|
+
@mediatypes = Mediatypes.new(options)
|
55
58
|
end
|
56
59
|
|
57
60
|
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
class ZabbixApi
|
2
|
+
class Mediatypes
|
3
|
+
|
4
|
+
def initialize(options = {})
|
5
|
+
@client = Client.new(options)
|
6
|
+
@options = options
|
7
|
+
@default_mediatype_options = {
|
8
|
+
:description => "", #Name
|
9
|
+
:type => 0, #0 - Email, 1 - External script, 2 - SMS, 3 - Jabber, 100 - EzTexting
|
10
|
+
:smtp_server => "",
|
11
|
+
:smtp_helo => "",
|
12
|
+
:smtp_email => "", #Email address of Zabbix server
|
13
|
+
:exec_path => "", #Name of external script
|
14
|
+
:gsm_modem => "", #Serial device name of GSM modem
|
15
|
+
:username => "", #Jabber user name used by Zabbix server
|
16
|
+
:passwd => "" #Jabber password used by Zabbix server
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
# Create MediaType
|
21
|
+
#
|
22
|
+
# * *Args* :
|
23
|
+
# - +data+ -> Hash with :description => "MediaGroup" and mediatype options
|
24
|
+
# * *Returns* :
|
25
|
+
# - Nil or Integer
|
26
|
+
def create(data)
|
27
|
+
result = @client.api_request(:method => "mediatype.create", :params => data)
|
28
|
+
result ? result['mediatypeids'][0].to_i : nil
|
29
|
+
end
|
30
|
+
|
31
|
+
# Add MediaType
|
32
|
+
# Synonym create
|
33
|
+
def add(data)
|
34
|
+
create(data)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Delete MediaType
|
38
|
+
#
|
39
|
+
# * *Args* :
|
40
|
+
# - +data+ -> Array with mediatypeids
|
41
|
+
# * *Returns* :
|
42
|
+
# - Nil or Integer
|
43
|
+
def delete(data)
|
44
|
+
result = @client.api_request(:method => "mediatype.delete", :params => [data])
|
45
|
+
result ? result['mediatypeids'][0].to_i : nil
|
46
|
+
end
|
47
|
+
|
48
|
+
# Destroy MediaType
|
49
|
+
# Synonym delete
|
50
|
+
def destroy(data)
|
51
|
+
delete(data)
|
52
|
+
end
|
53
|
+
|
54
|
+
# Get MediaType info
|
55
|
+
#
|
56
|
+
# * *Args* :
|
57
|
+
# - +data+ -> Hash with :description => "MediaType"
|
58
|
+
# * *Returns* :
|
59
|
+
# - Nil or Integer
|
60
|
+
def get_full_data(data)
|
61
|
+
@client.api_request(
|
62
|
+
:method => "mediatype.get",
|
63
|
+
:params => {
|
64
|
+
:search => {:description => data[:description]},
|
65
|
+
:output => "extend"
|
66
|
+
}
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
def get(data)
|
71
|
+
get_full_data(data)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Return MediaTypeid
|
75
|
+
#
|
76
|
+
# * *Args* :
|
77
|
+
# - +data+ -> Hash with :description => "MediaType"
|
78
|
+
# * *Returns* :
|
79
|
+
# - Nil or Integer
|
80
|
+
def get_id(data)
|
81
|
+
result = get_full_data(data)
|
82
|
+
mediatypeid = nil
|
83
|
+
result.each { |mt| mediatypeid = mt['mediatypeid'].to_i if mt['description'] == data[:description] }
|
84
|
+
mediatypeid
|
85
|
+
end
|
86
|
+
|
87
|
+
# Return MediaTypeid
|
88
|
+
#
|
89
|
+
# * *Args* :
|
90
|
+
# - +data+ -> Hash with :description => "MediaType"
|
91
|
+
# * *Returns* :
|
92
|
+
# - Nil or Integer
|
93
|
+
def update(data)
|
94
|
+
result = @client.api_request(:method => "mediatype.update", :params => data)
|
95
|
+
result.empty? ? nil : result['mediatypeids'][0].to_i
|
96
|
+
end
|
97
|
+
|
98
|
+
# Return MediaTypeid
|
99
|
+
#
|
100
|
+
# * *Args* :
|
101
|
+
# - +data+ -> Hash with :name => "UserGroup"
|
102
|
+
# * *Returns* :
|
103
|
+
# - Integer
|
104
|
+
def create_or_update(data)
|
105
|
+
mediatypeid = get_id(:description => data[:description], :mediatypeid => data[:mediatypeid])
|
106
|
+
mediatypeid ? update(data.merge(:mediatypeid => mediatypeid)) : create(data)
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
data/lib/zabbixapi/usergroups.rb
CHANGED
@@ -93,7 +93,7 @@ class ZabbixApi
|
|
93
93
|
# - +data+ -> Hash with :usrgrpids => id, :hostgroupids => [], :permission => 2,3 (read and read write)
|
94
94
|
# * *Returns* :
|
95
95
|
# - Integer
|
96
|
-
def
|
96
|
+
def set_perms(data)
|
97
97
|
permission = data[:permission] || 2
|
98
98
|
result = @client.api_request(
|
99
99
|
:method => "usergroup.massAdd",
|
data/lib/zabbixapi/users.rb
CHANGED
@@ -36,6 +36,22 @@ class ZabbixApi
|
|
36
36
|
get_full_data(data)
|
37
37
|
end
|
38
38
|
|
39
|
+
def create_or_update(data)
|
40
|
+
userid = get_id(:name => data[:name])
|
41
|
+
userid ? update(data.merge(:userid => userid)) : create(data)
|
42
|
+
end
|
43
|
+
|
44
|
+
def add_medias(data)
|
45
|
+
result = @client.api_request(
|
46
|
+
:method => "user.addMedia",
|
47
|
+
:params => {
|
48
|
+
:users => data[:userids].map { |t| {:userid => t} },
|
49
|
+
:medias => data[:media]
|
50
|
+
}
|
51
|
+
)
|
52
|
+
result ? result['userids'][0].to_i : nil
|
53
|
+
end
|
54
|
+
|
39
55
|
def get_id(data)
|
40
56
|
result = get_full_data(data)
|
41
57
|
userid = nil
|
@@ -49,4 +65,4 @@ class ZabbixApi
|
|
49
65
|
end
|
50
66
|
|
51
67
|
end
|
52
|
-
end
|
68
|
+
end
|
data/lib/zabbixapi/version.rb
CHANGED
data/spec/localhost.rb
CHANGED
@@ -11,7 +11,7 @@ zbx = ZabbixApi.connect(
|
|
11
11
|
:url => api_url,
|
12
12
|
:user => api_login,
|
13
13
|
:password => api_password,
|
14
|
-
:debug =>
|
14
|
+
:debug => false
|
15
15
|
)
|
16
16
|
|
17
17
|
hostgroup = "hostgroup______1"
|
@@ -24,6 +24,7 @@ user = "user____1"
|
|
24
24
|
user2 = "user____2"
|
25
25
|
usergroup = "SomeUserGroup"
|
26
26
|
graph = "graph___a"
|
27
|
+
mediatype = "somemediatype"
|
27
28
|
|
28
29
|
|
29
30
|
puts "### Zabbix API server version #{zbx.server.version} ###"
|
@@ -346,6 +347,15 @@ describe ZabbixApi, "test_api" do
|
|
346
347
|
).should be_kind_of(Integer)
|
347
348
|
end
|
348
349
|
|
350
|
+
it "USER: Create or update" do
|
351
|
+
zbx.users.create_or_update(
|
352
|
+
:alias => "Test #{user}",
|
353
|
+
:name => user,
|
354
|
+
:surname => user,
|
355
|
+
:passwd => user
|
356
|
+
).should be_kind_of(Integer)
|
357
|
+
end
|
358
|
+
|
349
359
|
it "USER: Find" do
|
350
360
|
zbx.users.get_full_data(:name => user)[0]['name'].should be_kind_of(String)
|
351
361
|
end
|
@@ -374,11 +384,48 @@ describe ZabbixApi, "test_api" do
|
|
374
384
|
end
|
375
385
|
|
376
386
|
it "USERGROUPS: Set UserGroup read & write perm" do
|
377
|
-
|
387
|
+
zbx.usergroups.set_perms(
|
378
388
|
:usrgrpid => zbx.usergroups.get_or_create(:name => usergroup).to_s,
|
379
389
|
:hostgroupids => zbx.hostgroups.all.values,
|
380
390
|
:permission => 3
|
381
|
-
)
|
391
|
+
).should be_kind_of(Integer)
|
392
|
+
end
|
393
|
+
|
394
|
+
it "MEDIATYPE: Create" do
|
395
|
+
zbx.mediatypes.create(
|
396
|
+
:description => mediatype,
|
397
|
+
:type => 0,
|
398
|
+
:smtp_server => "127.0.0.1",
|
399
|
+
:smtp_email => "zabbix@test.com"
|
400
|
+
).should be_kind_of(Integer)
|
401
|
+
end
|
402
|
+
|
403
|
+
it "MEDIATYPE: Update or create" do
|
404
|
+
zbx.mediatypes.create_or_update(
|
405
|
+
:description => mediatype,
|
406
|
+
:smtp_email => "zabbix2@test.com"
|
407
|
+
).should be_kind_of(Integer)
|
408
|
+
end
|
409
|
+
|
410
|
+
it "USER: Add mediatype" do
|
411
|
+
zbx.users.add_medias(
|
412
|
+
:userids => [zbx.users.get_id(:name => user2)],
|
413
|
+
:media => [
|
414
|
+
{
|
415
|
+
:mediatypeid => zbx.mediatypes.get_id(:description => mediatype),
|
416
|
+
:sendto => "test@test",
|
417
|
+
:active => 0,
|
418
|
+
:period => "1-7,00:00-24:00",
|
419
|
+
:severity => "56"
|
420
|
+
}
|
421
|
+
]
|
422
|
+
).should be_kind_of(Integer)
|
423
|
+
end
|
424
|
+
|
425
|
+
it "MEDIATYPE: Delete" do
|
426
|
+
zbx.mediatypes.delete(
|
427
|
+
zbx.mediatypes.get_id(:description => mediatype)
|
428
|
+
).should be_kind_of(Integer)
|
382
429
|
end
|
383
430
|
|
384
431
|
it "USER: Delete" do
|
data/spec/run.rb
CHANGED
@@ -22,6 +22,7 @@ user = "user"
|
|
22
22
|
user2 = "user2"
|
23
23
|
usergroup = "SomeUserGroup"
|
24
24
|
graph = "graph"
|
25
|
+
mediatype = "somemediatype"
|
25
26
|
|
26
27
|
puts "### Zabbix API server version #{zbx.server.version} ###"
|
27
28
|
|
@@ -343,6 +344,15 @@ describe ZabbixApi, "test_api" do
|
|
343
344
|
).should be_kind_of(Integer)
|
344
345
|
end
|
345
346
|
|
347
|
+
it "USER: Create or update" do
|
348
|
+
zbx.users.create_or_update(
|
349
|
+
:alias => "Test #{user}",
|
350
|
+
:name => user,
|
351
|
+
:surname => user,
|
352
|
+
:passwd => user
|
353
|
+
).should be_kind_of(Integer)
|
354
|
+
end
|
355
|
+
|
346
356
|
it "USER: Find" do
|
347
357
|
zbx.users.get_full_data(:name => user)[0]['name'].should be_kind_of(String)
|
348
358
|
end
|
@@ -371,11 +381,48 @@ describe ZabbixApi, "test_api" do
|
|
371
381
|
end
|
372
382
|
|
373
383
|
it "USERGROUPS: Set UserGroup read & write perm" do
|
374
|
-
|
384
|
+
zbx.usergroups.set_perms(
|
375
385
|
:usrgrpid => zbx.usergroups.get_or_create(:name => usergroup).to_s,
|
376
386
|
:hostgroupids => zbx.hostgroups.all.values,
|
377
387
|
:permission => 3
|
378
|
-
)
|
388
|
+
).should be_kind_of(Integer)
|
389
|
+
end
|
390
|
+
|
391
|
+
it "MEDIATYPE: Create" do
|
392
|
+
zbx.mediatypes.create(
|
393
|
+
:description => mediatype,
|
394
|
+
:type => 0,
|
395
|
+
:smtp_server => "127.0.0.1",
|
396
|
+
:smtp_email => "zabbix@test.com"
|
397
|
+
).should be_kind_of(Integer)
|
398
|
+
end
|
399
|
+
|
400
|
+
it "MEDIATYPE: Update or create" do
|
401
|
+
zbx.mediatypes.create_or_update(
|
402
|
+
:description => mediatype,
|
403
|
+
:smtp_email => "zabbix2@test.com"
|
404
|
+
).should be_kind_of(Integer)
|
405
|
+
end
|
406
|
+
|
407
|
+
it "USER: Add mediatype" do
|
408
|
+
zbx.users.add_medias(
|
409
|
+
:userids => [zbx.users.get_id(:name => user2)],
|
410
|
+
:media => [
|
411
|
+
{
|
412
|
+
:mediatypeid => zbx.mediatypes.get_id(:description => mediatype),
|
413
|
+
:sendto => "test@test",
|
414
|
+
:active => 0,
|
415
|
+
:period => "1-7,00:00-24:00",
|
416
|
+
:severity => "56"
|
417
|
+
}
|
418
|
+
]
|
419
|
+
).should be_kind_of(Integer)
|
420
|
+
end
|
421
|
+
|
422
|
+
it "MEDIATYPE: Delete" do
|
423
|
+
zbx.mediatypes.delete(
|
424
|
+
zbx.mediatypes.get_id(:description => mediatype)
|
425
|
+
).should be_kind_of(Integer)
|
379
426
|
end
|
380
427
|
|
381
428
|
it "USER: Delete" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zabbixapi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.0b2
|
5
5
|
prerelease: 5
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- lib/zabbixapi/hostgroups.rb
|
34
34
|
- lib/zabbixapi/hosts.rb
|
35
35
|
- lib/zabbixapi/items.rb
|
36
|
+
- lib/zabbixapi/mediatypes.rb
|
36
37
|
- lib/zabbixapi/screens.rb
|
37
38
|
- lib/zabbixapi/server.rb
|
38
39
|
- lib/zabbixapi/templates.rb
|
@@ -58,7 +59,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
58
59
|
version: '0'
|
59
60
|
segments:
|
60
61
|
- 0
|
61
|
-
hash:
|
62
|
+
hash: -4429484017589887978
|
62
63
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
64
|
none: false
|
64
65
|
requirements:
|