zabbixapi 0.4.8 → 0.4.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -212,11 +212,25 @@ zbx.graphs.delete(zbx.graphs.get_id(:name => "graph"))
212
212
  ### Create screen for host ###
213
213
  ```ruby
214
214
  zbx.screens.get_or_create_for_host(
215
- :hostname => "hostname",
215
+ :host => "hostname",
216
216
  :graphids => zbx.graphs.get_ids_by_host(:host => "hostname")
217
217
  )
218
218
  ```
219
219
 
220
+ ### Create UserGroup, add user and add rights ###
221
+ ```ruby
222
+ zbx.usergroups.add(:name => "Some user group")
223
+ zbx.usergroups.add_user(
224
+ :usrgrpids => [zbx.usergroups.get_id(:name => "Some user group")],
225
+ :userids => [zbx.users.get_id(:name => "Some user")]
226
+ )
227
+ # get read permissions for UserGroup on all hostgroup
228
+ zbx.usergroups.set_perm_read(
229
+ :usrgrpids => zbx.usergroups.get_or_create(:name => "Some user group"),
230
+ :hostgroupids => zbx.hostgroups.all.values
231
+ )
232
+ ```
233
+
220
234
  ### Custom queries
221
235
  ```ruby
222
236
  zbx.query(
@@ -40,6 +40,18 @@ class ZabbixApi
40
40
  end
41
41
  end
42
42
 
43
+ # Return all hostgroups
44
+ #
45
+ # * *Returns* :
46
+ # - Hash with {"Hostgroup1" => "id1", "Hostgroup2" => "id2"}
47
+ def all
48
+ result = {}
49
+ @client.api_request(:method => "hostgroup.get", :params => {:output => "extend"}).each do |hostgrp|
50
+ result[hostgrp['name']] = hostgrp['groupid']
51
+ end
52
+ result
53
+ end
54
+
43
55
  def get_id(data)
44
56
  result = get_full_data(data)
45
57
  hostgroupid = nil
@@ -48,4 +60,4 @@ class ZabbixApi
48
60
  end
49
61
 
50
62
  end
51
- end
63
+ end
@@ -0,0 +1,125 @@
1
+ class ZabbixApi
2
+ class Usergroups
3
+
4
+ def initialize(options = {})
5
+ @client = Client.new(options)
6
+ @options = options
7
+ end
8
+
9
+ # Create UserGroup
10
+ #
11
+ # * *Args* :
12
+ # - +data+ -> Hash with :name => "UserGroup"
13
+ # * *Returns* :
14
+ # - Nil or Integer
15
+ def create(data)
16
+ result = @client.api_request(:method => "usergroup.create", :params => data)
17
+ result ? result['usrgrpids'][0].to_i : nil
18
+ end
19
+
20
+ # Add UserGroup
21
+ # Synonym create
22
+ def add(data)
23
+ create(data)
24
+ end
25
+
26
+ # Delete UserGroup
27
+ #
28
+ # * *Args* :
29
+ # - +data+ -> Array with usrgrpids
30
+ # * *Returns* :
31
+ # - Nil or Integer
32
+ def delete(data)
33
+ result = @client.api_request(:method => "usergroup.delete", :params => data)
34
+ result ? result['usrgrpids'][0].to_i : nil
35
+ end
36
+
37
+ # Destroy UserGroup
38
+ # Synonym delete
39
+ def destroy(data)
40
+ delete(data)
41
+ end
42
+
43
+ # Get UserGroup info
44
+ #
45
+ # * *Args* :
46
+ # - +data+ -> Hash with :name => "UserGroup"
47
+ # * *Returns* :
48
+ # - Nil or Integer
49
+ def get_full_data(data)
50
+ @client.api_request(
51
+ :method => "usergroup.get",
52
+ :params => {
53
+ :filter => [data[:name]],
54
+ :output => "extend"
55
+ }
56
+ )
57
+ end
58
+
59
+ def get(data)
60
+ get_full_data(data)
61
+ end
62
+
63
+ # Return usrgrpid
64
+ #
65
+ # * *Args* :
66
+ # - +data+ -> Hash with :name => "UserGroup"
67
+ # * *Returns* :
68
+ # - Nil or Integer
69
+ def get_id(data)
70
+ result = get_full_data(data)
71
+ usrgrpid = nil
72
+ result.each { |usr| usrgrpid = usr['usrgrpid'].to_i if usr['name'] == data[:name] }
73
+ usrgrpid
74
+ end
75
+
76
+ # Return usrgrpid
77
+ #
78
+ # * *Args* :
79
+ # - +data+ -> Hash with :name => "UserGroup"
80
+ # * *Returns* :
81
+ # - Integer
82
+ def get_or_create(data)
83
+ usrgrpid = get_id(data)
84
+ if usrgrpid.nil?
85
+ usrgrpid = create(data)
86
+ end
87
+ usrgrpid
88
+ end
89
+
90
+ # Set read permisssion for usrgrp on some hostgroup
91
+ #
92
+ # * *Args* :
93
+ # - +data+ -> Hash with :usrgrpids => id, :hostgroupids => []
94
+ # * *Returns* :
95
+ # - Integer
96
+ def set_perm_read(data)
97
+ result = @client.api_request(
98
+ :method => "usergroup.massAdd",
99
+ :params => {
100
+ :usrgrpids => [data[:usrgrpids]],
101
+ :rights => data[:hostgroupids].map { |t| {:permission => 2, :id => t} }
102
+ }
103
+ )
104
+ result ? result['usrgrpids'][0].to_i : nil
105
+ end
106
+
107
+ # Update usergroup, add user
108
+ #
109
+ # * *Args* :
110
+ # - +data+ -> Hash with :usrgrpids => id, :userids => []
111
+ # * *Returns* :
112
+ # - Integer
113
+ def add_user(data)
114
+ result = @client.api_request(
115
+ :method => "usergroup.massAdd",
116
+ :params => {
117
+ :usrgrpids => data[:usrgrpids],
118
+ :userids => data[:userids]
119
+ }
120
+ )
121
+ result ? result['usrgrpids'][0].to_i : nil
122
+ end
123
+
124
+ end
125
+ end
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "0.4.8"
2
+ VERSION = "0.4.9"
3
3
  end
data/lib/zabbixapi.rb CHANGED
@@ -10,6 +10,7 @@ require "zabbixapi/triggers"
10
10
  require "zabbixapi/items"
11
11
  require "zabbixapi/graphs"
12
12
  require "zabbixapi/screens"
13
+ require "zabbixapi/usergroups"
13
14
 
14
15
  class ZabbixApi
15
16
 
@@ -24,6 +25,7 @@ class ZabbixApi
24
25
  attr :triggers
25
26
  attr :graphs
26
27
  attr :screens
28
+ attr :usergroups
27
29
 
28
30
  def self.connect(options = {})
29
31
  new(options)
@@ -49,6 +51,7 @@ class ZabbixApi
49
51
  @triggers = Triggers.new(options)
50
52
  @graphs = Graphs.new(options)
51
53
  @screens = Screens.new(options)
54
+ @usergroups = Usergroups.new(options)
52
55
  end
53
56
 
54
57
  end
data/spec/localhost.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require 'zabbixapi'
2
2
 
3
3
  # settings
4
- #api_url = 'http://zabbix/zabbix12/api_jsonrpc.php'
5
4
  api_url = 'http://zabbix/api_jsonrpc.php'
6
5
  #api_url = 'http://zabbix/zabbix20/api_jsonrpc.php'
7
6
 
@@ -12,7 +11,7 @@ zbx = ZabbixApi.connect(
12
11
  :url => api_url,
13
12
  :user => api_login,
14
13
  :password => api_password,
15
- :debug => false
14
+ :debug => true
16
15
  )
17
16
 
18
17
  hostgroup = "hostgroup______1"
@@ -23,8 +22,10 @@ host = "hostname____1"
23
22
  trigger = "trigger____1"
24
23
  user = "user____1"
25
24
  user2 = "user____2"
25
+ usergroup = "SomeUserGroup"
26
26
  graph = "graph___a"
27
27
 
28
+
28
29
  puts "### Zabbix API server version #{zbx.server.version} ###"
29
30
 
30
31
  describe ZabbixApi, "test_api" do
@@ -49,6 +50,10 @@ describe ZabbixApi, "test_api" do
49
50
  zbx.hostgroups.get_or_create(:name => hostgroup).should be_kind_of(Integer)
50
51
  end
51
52
 
53
+ it "HOSTGROUP: Get all" do
54
+ zbx.hostgroups.all.should be_kind_of(Hash)
55
+ end
56
+
52
57
  it "TEMPLATE: Create" do
53
58
  zbx.templates.create(
54
59
  :host => template,
@@ -353,10 +358,36 @@ describe ZabbixApi, "test_api" do
353
358
  zbx.users.get_id(:name => "#{user}_____")
354
359
  end
355
360
 
361
+ it "USERGROUPS: Create" do
362
+ zbx.usergroups.create(:name => usergroup).should be_kind_of(Integer)
363
+ end
364
+
365
+ it "USERGROUPS: Create or update" do
366
+ zbx.usergroups.get_or_create(:name => usergroup).should be_kind_of(Integer)
367
+ end
368
+
369
+ it "USERGROUPS: Add user" do
370
+ zbx.usergroups.add_user(
371
+ :usrgrpids => [zbx.usergroups.get_id(:name => usergroup)],
372
+ :userids => [zbx.users.get_id(:name => user2)]
373
+ ).should be_kind_of(Integer)
374
+ end
375
+
376
+ it "USERGROUPS: Set UserGroup read perm" do
377
+ puts zbx.usergroups.set_perm_read(
378
+ :usrgrpids => zbx.usergroups.get_or_create(:name => usergroup).to_s,
379
+ :hostgroupids => zbx.hostgroups.all.values
380
+ )
381
+ end
382
+
356
383
  it "USER: Delete" do
357
384
  zbx.users.delete(zbx.users.get_id(:name => user2)).should be_kind_of(Integer)
358
385
  end
359
386
 
387
+ it "USERGROUPS: Delete" do
388
+ zbx.usergroups.delete([zbx.usergroups.get_id(:name => usergroup)]).should be_kind_of(Integer)
389
+ end
390
+
360
391
  it "QUERY" do
361
392
  zbx.query(
362
393
  :method => "apiinfo.version",
data/spec/run.rb CHANGED
@@ -20,6 +20,7 @@ host = "hostname"
20
20
  trigger = "trigger"
21
21
  user = "user"
22
22
  user2 = "user2"
23
+ usergroup = "SomeUserGroup"
23
24
  graph = "graph"
24
25
 
25
26
  puts "### Zabbix API server version #{zbx.server.version} ###"
@@ -46,6 +47,10 @@ describe ZabbixApi, "test_api" do
46
47
  zbx.hostgroups.get_or_create(:name => hostgroup).should be_kind_of(Integer)
47
48
  end
48
49
 
50
+ it "HOSTGROUP: Get all" do
51
+ zbx.hostgroups.all.should be_kind_of(Hash)
52
+ end
53
+
49
54
  it "TEMPLATE: Create" do
50
55
  zbx.templates.create(
51
56
  :host => template,
@@ -350,10 +355,36 @@ describe ZabbixApi, "test_api" do
350
355
  zbx.users.get_id(:name => "#{user}_____")
351
356
  end
352
357
 
358
+ it "USERGROUPS: Create" do
359
+ zbx.usergroups.create(:name => usergroup).should be_kind_of(Integer)
360
+ end
361
+
362
+ it "USERGROUPS: Create or update" do
363
+ zbx.usergroups.get_or_create(:name => usergroup).should be_kind_of(Integer)
364
+ end
365
+
366
+ it "USERGROUPS: Add user" do
367
+ zbx.usergroups.add_user(
368
+ :usrgrpids => [zbx.usergroups.get_id(:name => usergroup)],
369
+ :userids => [zbx.users.get_id(:name => user2)]
370
+ ).should be_kind_of(Integer)
371
+ end
372
+
373
+ it "USERGROUPS: Set UserGroup read perm" do
374
+ puts zbx.usergroups.set_perm_read(
375
+ :usrgrpids => zbx.usergroups.get_or_create(:name => usergroup).to_s,
376
+ :hostgroupids => zbx.hostgroups.all.values
377
+ )
378
+ end
379
+
353
380
  it "USER: Delete" do
354
381
  zbx.users.delete(zbx.users.get_id(:name => user2)).should be_kind_of(Integer)
355
382
  end
356
383
 
384
+ it "USERGROUPS: Delete" do
385
+ zbx.usergroups.delete([zbx.usergroups.get_id(:name => usergroup)]).should be_kind_of(Integer)
386
+ end
387
+
357
388
  it "QUERY" do
358
389
  zbx.query(
359
390
  :method => "apiinfo.version",
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.4.8
4
+ version: 0.4.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -37,6 +37,7 @@ files:
37
37
  - lib/zabbixapi/server.rb
38
38
  - lib/zabbixapi/templates.rb
39
39
  - lib/zabbixapi/triggers.rb
40
+ - lib/zabbixapi/usergroups.rb
40
41
  - lib/zabbixapi/users.rb
41
42
  - lib/zabbixapi/version.rb
42
43
  - spec/localhost.rb
@@ -57,7 +58,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
58
  version: '0'
58
59
  segments:
59
60
  - 0
60
- hash: 1867199744098275115
61
+ hash: 772966212321473365
61
62
  required_rubygems_version: !ruby/object:Gem::Requirement
62
63
  none: false
63
64
  requirements: