zabbixapi 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,6 +20,13 @@ class ZabbixApi
20
20
  result.empty? ? nil : result['applicationids'][0].to_i
21
21
  end
22
22
 
23
+ def get_or_create(data)
24
+ unless appid = get_id(data)
25
+ appid = create(data)
26
+ end
27
+ appid
28
+ end
29
+
23
30
  def destroy(data)
24
31
  delete(data)
25
32
  end
@@ -24,9 +24,11 @@ class ZabbixApi
24
24
  delete(data)
25
25
  end
26
26
 
27
- def create_or_update(data)
28
- hostgroupid = get_id(:name => data[:name])
29
- hostgroupid ? update(data.merge(:groupid => hostgroupid)) : create(data)
27
+ def get_or_create(data)
28
+ unless hostgroupid = get_id(data)
29
+ hostgroupid = update(data)
30
+ end
31
+ hostgroupid
30
32
  end
31
33
 
32
34
  def get_full_data(data)
@@ -54,6 +54,36 @@ class ZabbixApi
54
54
  result
55
55
  end
56
56
 
57
+ # Return templateid
58
+ #
59
+ # * *Args* :
60
+ # - +data+ -> Hash with :host => "Template_Name" and :groups => array with hostgroup ids
61
+ # * *Returns* :
62
+ # - Integer
63
+ def get_or_create(data)
64
+ unless templateid = get_id(:host => data[:host])
65
+ templateid = create(data)
66
+ end
67
+ templateid
68
+ end
69
+
70
+ # Analog Zabbix api call massUpdate
71
+ #
72
+ # * *Args* :
73
+ # - +data+ -> Hash with :hosts_id => [hostid1, hostid2 ...], and :templates_id => [templateid1, templateid2 ...]
74
+ # * *Returns* :
75
+ # - True or False
76
+ def mass_update(data)
77
+ result = @client.api_request(
78
+ :method => "template.massAdd",
79
+ :params => {
80
+ :hosts => data[:hosts_id].map { |t| {:hostid => t} },
81
+ :templates => data[:templates_id].map { |t| {:templateid => t} }
82
+ }
83
+ )
84
+ result.empty? ? false : true
85
+ end
86
+
57
87
  # Analog Zabbix api call massAdd
58
88
  #
59
89
  # * *Args* :
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "0.4.3"
2
+ VERSION = "0.4.4"
3
3
  end
data/spec/localhost.rb CHANGED
@@ -45,8 +45,8 @@ describe ZabbixApi, "test_api" do
45
45
  zbx.hostgroups.get_id(:name => "#{hostgroup}______").should be_kind_of(NilClass)
46
46
  end
47
47
 
48
- it "HOSTGROUP: Create or update" do
49
- zbx.hostgroups.create_or_update(:name => hostgroup).should be_kind_of(Integer)
48
+ it "HOSTGROUP: Create or get" do
49
+ zbx.hostgroups.get_or_create(:name => hostgroup).should be_kind_of(Integer)
50
50
  end
51
51
 
52
52
  it "TEMPLATE: Create" do
@@ -56,6 +56,13 @@ describe ZabbixApi, "test_api" do
56
56
  ).should be_kind_of(Integer)
57
57
  end
58
58
 
59
+ it "TEMPLATE: Get get or create" do
60
+ zbx.templates.get_or_create(
61
+ :host => template,
62
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
63
+ ).should be_kind_of(Integer)
64
+ end
65
+
59
66
  it "TEMPLATE: Check full data" do
60
67
  zbx.templates.get_full_data(:host => template)[0]['host'].should be_kind_of(String)
61
68
  end
@@ -75,6 +82,13 @@ describe ZabbixApi, "test_api" do
75
82
  )
76
83
  end
77
84
 
85
+ it "APPLICATION: Get or create" do
86
+ zbx.applications.get_or_create(
87
+ :name => application,
88
+ :hostid => zbx.templates.get_id(:host => template)
89
+ )
90
+ end
91
+
78
92
  it "APPLICATION: Full info check" do
79
93
  zbx.applications.get_full_data(:name => application)[0]['applicationid'].should be_kind_of(String)
80
94
  end
@@ -169,6 +183,13 @@ describe ZabbixApi, "test_api" do
169
183
  ).should be_kind_of(TrueClass)
170
184
  end
171
185
 
186
+ it "TEMPLATE: Update hosts with templates" do
187
+ zbx.templates.mass_update(
188
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
189
+ :templates_id => [zbx.templates.get_id(:host => template)]
190
+ ).should be_kind_of(TrueClass)
191
+ end
192
+
172
193
  it "TEMPLATE: Unlink hosts from templates" do
173
194
  zbx.templates.mass_remove(
174
195
  :hosts_id => [zbx.hosts.get_id(:host => host)],
data/spec/run.rb CHANGED
@@ -42,6 +42,10 @@ describe ZabbixApi, "test_api" do
42
42
  zbx.hostgroups.get_id(:name => "#{hostgroup}______").should be_kind_of(NilClass)
43
43
  end
44
44
 
45
+ it "HOSTGROUP: Get or create" do
46
+ zbx.hostgroups.get_or_create(:name => hostgroup).should be_kind_of(Integer)
47
+ end
48
+
45
49
  it "TEMPLATE: Create" do
46
50
  zbx.templates.create(
47
51
  :host => template,
@@ -49,6 +53,13 @@ describe ZabbixApi, "test_api" do
49
53
  ).should be_kind_of(Integer)
50
54
  end
51
55
 
56
+ it "TEMPLATE: Get get or create" do
57
+ zbx.templates.get_or_create(
58
+ :host => template,
59
+ :groups => [:groupid => zbx.hostgroups.get_id(:name => hostgroup)]
60
+ ).should be_kind_of(Integer)
61
+ end
62
+
52
63
  it "TEMPLATE: Check full data" do
53
64
  zbx.templates.get_full_data(:host => template)[0]['host'].should be_kind_of(String)
54
65
  end
@@ -68,6 +79,13 @@ describe ZabbixApi, "test_api" do
68
79
  )
69
80
  end
70
81
 
82
+ it "APPLICATION: Get or create" do
83
+ zbx.applications.get_or_create(
84
+ :name => application,
85
+ :hostid => zbx.templates.get_id(:host => template)
86
+ )
87
+ end
88
+
71
89
  it "APPLICATION: Full info check" do
72
90
  zbx.applications.get_full_data(:name => application)[0]['applicationid'].should be_kind_of(String)
73
91
  end
@@ -162,6 +180,13 @@ describe ZabbixApi, "test_api" do
162
180
  ).should be_kind_of(TrueClass)
163
181
  end
164
182
 
183
+ it "TEMPLATE: Update hosts with templates" do
184
+ zbx.templates.mass_update(
185
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
186
+ :templates_id => [zbx.templates.get_id(:host => template)]
187
+ ).should be_kind_of(TrueClass)
188
+ end
189
+
165
190
  it "TEMPLATE: Unlink hosts from templates" do
166
191
  zbx.templates.mass_remove(
167
192
  :hosts_id => [zbx.hosts.get_id(:host => host)],
@@ -169,7 +194,7 @@ describe ZabbixApi, "test_api" do
169
194
  ).should be_kind_of(TrueClass)
170
195
  end
171
196
 
172
- it "TEMPLATE: Get all" do
197
+ it "TEMPLATE: Get all" do
173
198
  zbx.templates.all.should be_kind_of(Hash)
174
199
  end
175
200
 
@@ -189,12 +214,12 @@ describe ZabbixApi, "test_api" do
189
214
  zbx.triggers.get_id(:description => trigger).should be_kind_of(Integer)
190
215
  end
191
216
 
192
- it "GRAPH: Create" do
217
+ it "GRAPH: Create" do
193
218
  gitems = {
194
- :itemid => zbx.items.get_id(:description => item),
195
- :calc_fnc => "2",
196
- :type => "0",
197
- :periods_cnt => "5"
219
+ :itemid => zbx.items.get_id(:description => item),
220
+ :calc_fnc => "2",
221
+ :type => "0",
222
+ :periods_cnt => "5"
198
223
  }
199
224
  zbx.graphs.create(
200
225
  :gitems => [gitems],
@@ -218,25 +243,25 @@ describe ZabbixApi, "test_api" do
218
243
  :graphid => zbx.graphs.get_id(
219
244
  :name => graph,
220
245
  :hostid => zbx.hosts.get_id(:host => host)
221
- ),
246
+ ),
222
247
  :ymax_type => 1
223
248
  ).should be_kind_of(Integer)
224
249
  end
225
250
 
226
251
  it "GRAPH: Create or Update" do
227
252
  gitems = {
228
- :itemid => zbx.items.get_id(:description => item),
253
+ :itemid => zbx.items.get_id(:description => item),
229
254
  :calc_fnc => "3",
230
255
  :type => "0",
231
256
  :periods_cnt => "5"
232
257
  }
233
- zbx.graphs.create_or_update(
234
- :gitems => [gitems],
235
- :show_triggers => "1",
236
- :name => graph,
237
- :width => "900",
238
- :height => "200"
239
- ).should be_kind_of(Integer)
258
+ zbx.graphs.create_or_update(
259
+ :gitems => [gitems],
260
+ :show_triggers => "1",
261
+ :name => graph,
262
+ :width => "900",
263
+ :height => "200"
264
+ ).should be_kind_of(Integer)
240
265
  end
241
266
 
242
267
  it "GRAPH: Delete" do
@@ -298,9 +323,9 @@ describe ZabbixApi, "test_api" do
298
323
 
299
324
  it "QUERY" do
300
325
  zbx.query(
301
- :method => "apiinfo.version",
326
+ :method => "apiinfo.version",
302
327
  :params => {}
303
328
  ).should be_kind_of(String)
304
329
  end
305
-
330
+
306
331
  end
data/zabbixapi.gemspec CHANGED
@@ -5,7 +5,7 @@ require "zabbixapi/version"
5
5
  Gem::Specification.new do |s|
6
6
  s.name = "zabbixapi"
7
7
  s.version = ZabbixApi::VERSION
8
- s.authors = %w("Vasiliev D.V.")
8
+ s.authors = ["Vasiliev D.V."]
9
9
  s.email = %w(vadv.mkn@gmail.com)
10
10
  s.homepage = "https://github.com/vadv/zabbixapi"
11
11
  s.summary = %q{Realization for Zabbix API.}
metadata CHANGED
@@ -1,12 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zabbixapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - ! '"Vasiliev'
9
- - D.V."
8
+ - Vasiliev D.V.
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
@@ -57,7 +56,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
57
56
  version: '0'
58
57
  segments:
59
58
  - 0
60
- hash: -2337601613432576299
59
+ hash: -2858425249023380591
61
60
  required_rubygems_version: !ruby/object:Gem::Requirement
62
61
  none: false
63
62
  requirements: