zabbixapi 2.2.3 → 2.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bd481b02c9223088ad234cbca2b81d51bcc5916b
4
- data.tar.gz: 32641a4d6a3f436ae4fb521f78f6e55c10b159dd
3
+ metadata.gz: 0a6a333247a0c997c8ffc1b4baa56c2bd25db344
4
+ data.tar.gz: fce1d341dd272105dfe9464b23331dbf00974f17
5
5
  SHA512:
6
- metadata.gz: 827a84f1bf076a63c25ef9c17d082e4e7d56290015023a7cfbf6e5e8efaacea67d88a35670079e59f77618eef59f371dbe6917f7238a8cbdcf6c16168e76c130
7
- data.tar.gz: fa4c337f755a659da65cabca1bc598c063edc3fc25e63a4c021af04ce1e33497d38429fe00094c7a3e802d8f2779cd5b5b03c345dadb058e5de4d674a68a5fe3
6
+ metadata.gz: 2dc6235a82602dfae50a6073fce82ed54e1a833a4b15ec7fc559bdf6a653b11d8a64b32ba62f0fd43e47ee9c604731c12a1268d85f0a4f2f131584437f47bbb6
7
+ data.tar.gz: 67b659c1db208ad8765f6b441252abab8558092a73f9c3abf7198c3ddae66e2e8023a2e96e2d241f66017cbf581fadc574ef6ba3ffaffe54639421dc565cd063
@@ -1,3 +1,9 @@
1
+ class Hash
2
+ def <=> (other_hash)
3
+ self.keys.collect!{|key| key.to_s} <=> other_hash.keys.collect!{|key| key.to_s}
4
+ end
5
+ end
6
+
1
7
  class ZabbixApi
2
8
  class Basic
3
9
 
@@ -6,8 +12,8 @@ class ZabbixApi
6
12
  end
7
13
 
8
14
  def hash_equals?(a, b)
9
- a_new = normalize_hash(a)
10
- b_new = normalize_hash(b)
15
+ a_new = normalize_obj(a)
16
+ b_new = normalize_obj(b)
11
17
  hash1 = a_new.merge(b_new)
12
18
  hash2 = b_new.merge(a_new)
13
19
  hash1 == hash2
@@ -19,17 +25,22 @@ class ZabbixApi
19
25
  obj
20
26
  end
21
27
 
22
- def normalize_hash(hash)
23
- result = hash.dup
24
- result.delete(:hostid) #TODO remove to logig. TemplateID and HostID has different id
25
- result.each do |key, value|
26
- case value
27
- when Array
28
- result.delete(key)
29
- else
30
- result[key] = value.to_s
31
- end
28
+ def normalize_obj(obj)
29
+ result = nil
30
+ # obj.delete(:hostid) if obj.is_a? Hash #TODO remove to logig. TemplateID and HostID has different id
31
+ case obj
32
+ when Hash
33
+ result = obj.dup
34
+ result.each do |key,value|
35
+ result[key] = normalize_obj(value)
36
+ end
37
+ when Array
38
+ result = obj.dup
39
+ result.collect! {|item| normalize_obj(item)}
40
+ else
41
+ result = obj.to_s
32
42
  end
43
+ result.sort! if result.respond_to?(:sort!)
33
44
  result
34
45
  end
35
46
 
@@ -18,14 +18,14 @@ class ZabbixApi
18
18
  parse_keys result
19
19
  end
20
20
 
21
- def create_or_update(data)
21
+ def create_or_update(data, force = false)
22
22
  log "[DEBUG] Call create_or_update with parametrs: #{data.inspect}"
23
23
 
24
24
  id = get_id(indentify.to_sym => data[indentify.to_sym])
25
- id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
25
+ id ? update(data.merge(key.to_sym => id.to_s), force) : create(data)
26
26
  end
27
27
 
28
- def update(data)
28
+ def update(data, force = false)
29
29
  log "[DEBUG] Call update with parametrs: #{data.inspect}"
30
30
 
31
31
  dump = {}
@@ -34,10 +34,14 @@ class ZabbixApi
34
34
  dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
35
35
  end
36
36
 
37
- if hash_equals?(dump, data)
37
+ dump.delete(key.to_sym)
38
+ data.delete(key.to_sym)
39
+
40
+ if hash_equals?(dump, data) && !force
38
41
  log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
39
42
  item_id
40
43
  else
44
+ data[key.to_sym] = item_id
41
45
  data_update = array_flag ? [data] : data
42
46
  result = @client.api_request(:method => "#{method_name}.update", :params => data_update)
43
47
  parse_keys result
@@ -89,7 +93,7 @@ class ZabbixApi
89
93
 
90
94
  result = symbolize_keys( get_full_data(data) )
91
95
  id = nil
92
- result.each { |item| id = item[key.to_sym].to_i if item[indentify.to_sym] == data[indentify.to_sym] }
96
+ result.each { |item| id = item[key.to_sym].to_i if item[indentify.to_sym].to_s == data[indentify.to_sym].to_s }
93
97
  id
94
98
  end
95
99
 
@@ -9,7 +9,7 @@ class ZabbixApi
9
9
  end
10
10
 
11
11
  def indentify
12
- "hostid"
12
+ "interfaceid"
13
13
  end
14
14
 
15
15
  def key
@@ -35,9 +35,9 @@ class ZabbixApi
35
35
  result.empty? ? false : true
36
36
  end
37
37
 
38
- def create_or_update(data)
38
+ def create_or_update(data, force = false)
39
39
  hostid = get_id(:host => data[:host])
40
- hostid ? update(data.merge(:hostid => hostid)) : create(data)
40
+ hostid ? update(data.merge(:hostid => hostid), force) : create(data)
41
41
  end
42
42
 
43
43
  # to make delete call idempotent for all resources
@@ -49,10 +49,13 @@ class ZabbixApi
49
49
 
50
50
  log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"
51
51
 
52
+ dump.delete(key.to_sym)
53
+ data.delete(key.to_sym)
52
54
  if hash_equals?(dump, data)
53
55
  log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
54
56
  item_id
55
57
  else
58
+ data[key.to_sym] = item_id
56
59
  data[:expression] = old_expression
57
60
  # disable old trigger
58
61
  log "[DEBUG] disable :" + @client.api_request(:method => "#{method_name}.update", :params => [{:triggerid=> data[:triggerid], :status => "1" }]).inspect
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "2.2.3"
2
+ VERSION = "2.2.4"
3
3
  end
data/spec/host.rb CHANGED
@@ -5,7 +5,9 @@ require 'spec_helper'
5
5
  describe 'host' do
6
6
  before :all do
7
7
  @hostgroup = gen_name 'hostgroup'
8
+ @hostgroup3 = gen_name 'hostgroup'
8
9
  @hostgroupid = zbx.hostgroups.create(:name => @hostgroup)
10
+ @hostgroupid3 = zbx.hostgroups.create(:name => @hostgroup3)
9
11
  end
10
12
 
11
13
  context 'when name not exists' do
@@ -120,6 +122,30 @@ describe 'host' do
120
122
  :groups => [:groupid => @hostgroupid]
121
123
  ).should eq @hostid
122
124
  end
125
+
126
+ it "should add ghostgroup" do
127
+ zbx.hosts.create_or_update( {
128
+ :host => @host,
129
+ :interfaces => [
130
+ {
131
+ :type => 1,
132
+ :main => 1,
133
+ :ip => "10.20.48.89",
134
+ :port => 10050,
135
+ :useip => 1,
136
+ :dns => ''
137
+ }
138
+ ],
139
+ :groups => [{:groupid => @hostgroupid},
140
+ {:groupid => @hostgroupid3}]
141
+ }, true)
142
+ zbx.hosts.get_full_data(
143
+ :host => @host,
144
+ :params => {
145
+ :selectGroups => "extend"
146
+ }
147
+ )[0]["groups"].collect { |group| group["groupid"].to_s }.sort.should eq [@hostgroupid, @hostgroupid3].collect{|item| item.to_s}.sort
148
+ end
123
149
  end
124
150
 
125
151
  describe 'update' do
@@ -62,18 +62,27 @@ describe "hostinterface" do
62
62
  :dns => "",
63
63
  :port => 10050,
64
64
  :useip => 1
65
- }
65
+ },
66
66
  ],
67
67
  :groups => [:groupid => @hostgroupid]
68
68
  )
69
- @interfaceid = zbx.hostinterfaces.create(:hostid => @hostid, :type => 1, :main => 0, :ip => "10.20.48.88", :dns => "", :port => 10050, :useip => 1)
69
+ @interfaceid1 = zbx.hostinterfaces.create(:hostid => @hostid, :type => 1, :main => 0, :ip => "10.20.48.88", :dns => "", :port => 10050, :useip => 1)
70
+ @interfaceid2 = zbx.hostinterfaces.create(:hostid => @hostid, :type => 1, :main => 0, :ip => "10.20.48.88", :dns => "", :port => 10050, :useip => 1)
70
71
  end
71
72
 
72
73
  describe 'get_full_data' do
73
74
  it "should contains created host" do
74
- expect(zbx.hostinterfaces.get_full_data(:hostid => "#{@hostid}")[0]).to include("hostid" => "#{@hostid}")
75
+ expect(zbx.hostinterfaces.get_full_data(:params => {:filter => {:hostid => "#{@hostid}"}})[0]).to include("hostid" => "#{@hostid}")
75
76
  end
76
77
  end
77
78
 
79
+ describe 'get_id' do
80
+ it "should update existing interface" do
81
+ zbx.hostinterfaces.create_or_update(:interfaceid => @interfaceid1, :type => 2, :ip => "8.8.8.8")
82
+ dump = zbx.hostinterfaces.get_full_data(:interfaceid => @interfaceid1)
83
+ dump[0]["type"].to_s.should eq 2.to_s
84
+ dump[0]["ip"].to_s.should eq "8.8.8.8"
85
+ end
86
+ end
78
87
  end
79
88
  end
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: 2.2.3
4
+ version: 2.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliev D.V.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-04 00:00:00.000000000 Z
12
+ date: 2016-03-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json