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 +4 -4
- data/lib/zabbixapi/basic/basic_func.rb +23 -12
- data/lib/zabbixapi/basic/basic_logic.rb +9 -5
- data/lib/zabbixapi/classes/hostinterfaces.rb +1 -1
- data/lib/zabbixapi/classes/hosts.rb +2 -2
- data/lib/zabbixapi/classes/triggers.rb +3 -0
- data/lib/zabbixapi/version.rb +1 -1
- data/spec/host.rb +26 -0
- data/spec/hostinterface.rb +12 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a6a333247a0c997c8ffc1b4baa56c2bd25db344
|
4
|
+
data.tar.gz: fce1d341dd272105dfe9464b23331dbf00974f17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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 =
|
10
|
-
b_new =
|
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
|
23
|
-
result =
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
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
|
-
|
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
|
|
@@ -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
|
data/lib/zabbixapi/version.rb
CHANGED
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
|
data/spec/hostinterface.rb
CHANGED
@@ -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
|
-
@
|
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.
|
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-
|
12
|
+
date: 2016-03-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: json
|