zabbixapi 0.4.7 → 0.4.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -144,6 +144,10 @@ zbx.graphs.create_or_update(
144
144
  :height => "200"
145
145
  )
146
146
  ```
147
+ ### Get ids by host ###
148
+ ```ruby
149
+ zbx.graphs.get_ids_by_host(:host => "hostname")
150
+ ```
147
151
 
148
152
  ### Delete graph
149
153
  ```ruby
@@ -205,6 +209,14 @@ zbx.users.update(:userid => zbx.users.get_id(:name => "user"), :name => "user2")
205
209
  zbx.graphs.delete(zbx.graphs.get_id(:name => "graph"))
206
210
  ```
207
211
 
212
+ ### Create screen for host ###
213
+ ```ruby
214
+ zbx.screens.get_or_create_for_host(
215
+ :hostname => "hostname",
216
+ :graphids => zbx.graphs.get_ids_by_host(:host => "hostname")
217
+ )
218
+ ```
219
+
208
220
  ### Custom queries
209
221
  ```ruby
210
222
  zbx.query(
@@ -229,4 +241,4 @@ zbx.query(
229
241
  ## Zabbix documentation
230
242
 
231
243
  * [Zabbix Project Homepage](http://zabbix.com/)
232
- * [Zabbix Api docs](http://www.zabbix.com/documentation/1.8/api)
244
+ * [Zabbix Api docs](http://www.zabbix.com/documentation/1.8/api)
data/lib/zabbixapi.rb CHANGED
@@ -9,6 +9,7 @@ require "zabbixapi/hosts"
9
9
  require "zabbixapi/triggers"
10
10
  require "zabbixapi/items"
11
11
  require "zabbixapi/graphs"
12
+ require "zabbixapi/screens"
12
13
 
13
14
  class ZabbixApi
14
15
 
@@ -22,6 +23,7 @@ class ZabbixApi
22
23
  attr :hosts
23
24
  attr :triggers
24
25
  attr :graphs
26
+ attr :screens
25
27
 
26
28
  def self.connect(options = {})
27
29
  new(options)
@@ -46,6 +48,7 @@ class ZabbixApi
46
48
  @hostgroups = HostGroups.new(options)
47
49
  @triggers = Triggers.new(options)
48
50
  @graphs = Graphs.new(options)
51
+ @screens = Screens.new(options)
49
52
  end
50
53
 
51
- end
54
+ end
@@ -33,6 +33,15 @@ class ZabbixApi
33
33
  @client.api_request(:method => "graph.get", :params => {:search => {:name => data}, :output => "extend"})
34
34
  end
35
35
 
36
+ def get_ids_by_host(data)
37
+ ids = []
38
+ result = @client.api_request(:method => "graph.get", :params => {:filter => {:host => data[:host]}, :output => "extend"})
39
+ result.each do |graph|
40
+ ids << graph['graphid']
41
+ end
42
+ ids
43
+ end
44
+
36
45
  def get_items(data)
37
46
  @client.api_request(:method => "graphitem.get", :params => { :graphids => [data], :output => "extend" } )
38
47
  end
@@ -72,4 +81,4 @@ class ZabbixApi
72
81
  end
73
82
 
74
83
  end
75
- end
84
+ end
@@ -0,0 +1,129 @@
1
+ class ZabbixApi
2
+ class Screens
3
+
4
+ # extracted from frontends/php/include/defines.inc.php
5
+ #SCREEN_RESOURCE_GRAPH => 0,
6
+ #SCREEN_RESOURCE_SIMPLE_GRAPH => 1,
7
+ #SCREEN_RESOURCE_MAP => 2,
8
+ #SCREEN_RESOURCE_PLAIN_TEXT => 3,
9
+ #SCREEN_RESOURCE_HOSTS_INFO => 4,
10
+ #SCREEN_RESOURCE_TRIGGERS_INFO => 5,
11
+ #SCREEN_RESOURCE_SERVER_INFO => 6,
12
+ #SCREEN_RESOURCE_CLOCK => 7,
13
+ #SCREEN_RESOURCE_SCREEN => 8,
14
+ #SCREEN_RESOURCE_TRIGGERS_OVERVIEW => 9,
15
+ #SCREEN_RESOURCE_DATA_OVERVIEW => 10,
16
+ #SCREEN_RESOURCE_URL => 11,
17
+ #SCREEN_RESOURCE_ACTIONS => 12,
18
+ #SCREEN_RESOURCE_EVENTS => 13,
19
+ #SCREEN_RESOURCE_HOSTGROUP_TRIGGERS => 14,
20
+ #SCREEN_RESOURCE_SYSTEM_STATUS => 15,
21
+ #SCREEN_RESOURCE_HOST_TRIGGERS => 16
22
+
23
+ def initialize(options = {})
24
+ @client = Client.new(options)
25
+ @options = options
26
+ @screeb_default_options = {
27
+ :vsize => 3
28
+ }
29
+ end
30
+
31
+ # Create screen
32
+ #
33
+ # * *Args* :
34
+ # - +data+ -> Hash with :name => "Screen name", hsize (rows) and vsize (columns) and array :screenitems => []
35
+ # screenitems contains :resourcetype (0 - graph), :resourcetypeid (id item) and :x and :y position
36
+ # * *Returns* :
37
+ # - Nil or Integer
38
+ def create(data)
39
+ result = @client.api_request(:method => "screen.create", :params => data)
40
+ result ? result['screenids'][0].to_i : nil
41
+ end
42
+
43
+ # Create screen
44
+ # Synonym create
45
+ def add(data)
46
+ create(data)
47
+ end
48
+
49
+ # Update screen
50
+ #
51
+ # * *Args* :
52
+ # - +data+ -> Hash with :screenid => [ "screenid" ]
53
+ # * *Returns* :
54
+ # - Nil or Integer
55
+ def update(data)
56
+ result = @client.api_request(:method => "screen.update", :params => data)
57
+ result ? result['screenids'][0].to_i : nil
58
+ end
59
+
60
+ # Return info about screen
61
+ #
62
+ # * *Args* :
63
+ # - +data+ -> Hash with :name => "Screen name"
64
+ # * *Returns* :
65
+ # - Hash with screen info
66
+ def get_full_data(data)
67
+ result = @client.api_request(:method => "screen.get", :params => {:search => data, :output => "extend"})
68
+ result.empty? ? [] : result
69
+ end
70
+
71
+ # Return screenid
72
+ #
73
+ # * *Args* :
74
+ # - +data+ -> Hash with :name => "Screen name"
75
+ # * *Returns* :
76
+ # - Nil or Integer
77
+ def get_id(data)
78
+ result = get_full_data(data)
79
+ screenid = nil
80
+ result.each { |screen| screenid = screen['screenid'].to_i if screen['name'] == data[:name] }
81
+ screenid
82
+ end
83
+
84
+ # Delete screen
85
+ #
86
+ # * *Args* :
87
+ # - +data+ -> Hash with :params => [screenid]
88
+ # * *Returns* :
89
+ # - Nil or Integer
90
+ def delete(data)
91
+ result = @client.api_request(:method => "screen.delete", :params => data)
92
+ result.empty? ? nil : result['screenids'][0].to_i
93
+ end
94
+
95
+ # Create screen all graphs for host
96
+ #
97
+ # * *Args* :
98
+ # - +data+ -> Hash with :host=> "hostname", :graphsid => [], [:hsize, :vsize]
99
+ # * *Returns* :
100
+ # - Nil or Integer
101
+ def get_or_create_for_host(data)
102
+ screen_name = data[:host].to_s + "_graphs"
103
+ graphids = data[:graphids]
104
+ screenitems = []
105
+ hsize = data[:hsize] || 3
106
+ vsize = data[:vsize] || ((graphids.size/hsize) + 1).to_i
107
+ screenid = get_id(:name => screen_name)
108
+ unless screenid
109
+ # Create screen
110
+ graphids.each_with_index do |graphid, index|
111
+ screenitems << {
112
+ :resourcetype => 0,
113
+ :resourceid => graphid,
114
+ :x => (index % hsize).to_i,
115
+ :y => (index % graphids.size/hsize).to_i
116
+ }
117
+ end
118
+ screenid = create(
119
+ :name => screen_name,
120
+ :hsize => hsize,
121
+ :vsize => vsize,
122
+ :screenitems => screenitems
123
+ )
124
+ end
125
+ screenid
126
+ end
127
+
128
+ end
129
+ end
@@ -112,7 +112,9 @@ class ZabbixApi
112
112
  :method => "template.massRemove",
113
113
  :params => {
114
114
  :hostids => data[:hosts_id],
115
- :templateids => data[:templates_id]
115
+ :templateids => data[:templates_id],
116
+ :groupids => data[:group_id],
117
+ :force => 1
116
118
  }
117
119
  )
118
120
  result.empty? ? false : true
@@ -1,3 +1,3 @@
1
1
  class ZabbixApi
2
- VERSION = "0.4.7"
2
+ VERSION = "0.4.8"
3
3
  end
data/spec/localhost.rb CHANGED
@@ -2,8 +2,7 @@ require 'zabbixapi'
2
2
 
3
3
  # settings
4
4
  #api_url = 'http://zabbix/zabbix12/api_jsonrpc.php'
5
- api_url = 'http://zabbix/zabbix13/api_jsonrpc.php'
6
- api_url = 'http://monitor02.undev.cc/api_jsonrpc.php'
5
+ api_url = 'http://zabbix/api_jsonrpc.php'
7
6
  #api_url = 'http://zabbix/zabbix20/api_jsonrpc.php'
8
7
 
9
8
  api_login = 'Admin'
@@ -191,13 +190,6 @@ describe ZabbixApi, "test_api" do
191
190
  ).should be_kind_of(TrueClass)
192
191
  end
193
192
 
194
- it "TEMPLATE: Unlink hosts from templates" do
195
- zbx.templates.mass_remove(
196
- :hosts_id => [zbx.hosts.get_id(:host => host)],
197
- :templates_id => [zbx.templates.get_id(:host => template)]
198
- ).should be_kind_of(TrueClass)
199
- end
200
-
201
193
  it "TEMPLATE: Get all" do
202
194
  zbx.templates.all.should be_kind_of(Hash)
203
195
  end
@@ -258,6 +250,10 @@ describe ZabbixApi, "test_api" do
258
250
  zbx.graphs.get_id( :name => graph ).should be_kind_of(Integer)
259
251
  end
260
252
 
253
+ it "GRAPH: get_ids_by_host" do
254
+ zbx.graphs.get_ids_by_host( :host => host ).should be_kind_of(Array)
255
+ end
256
+
261
257
  it "GRAPH: Update" do
262
258
  zbx.graphs.update(
263
259
  :graphid => zbx.graphs.get_id(
@@ -284,6 +280,26 @@ describe ZabbixApi, "test_api" do
284
280
  ).should be_kind_of(Integer)
285
281
  end
286
282
 
283
+ it "SCREEN: Get or create for host" do
284
+ zbx.screens.get_or_create_for_host(
285
+ :host => host,
286
+ :graphids => zbx.graphs.get_ids_by_host(:host => host)
287
+ ).should be_kind_of(Integer)
288
+ end
289
+
290
+ it "TEMPLATE: Unlink hosts from templates" do
291
+ zbx.templates.mass_remove(
292
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
293
+ :templates_id => [zbx.templates.get_id(:host => template)]
294
+ ).should be_kind_of(TrueClass)
295
+ end
296
+
297
+ it "SCREEN: Delete" do
298
+ zbx.screens.delete(
299
+ [zbx.screens.get_id(:name => "#{host}_graphs")]
300
+ ).should be_kind_of(Integer)
301
+ end
302
+
287
303
  it "GRAPH: Delete" do
288
304
  zbx.graphs.delete(zbx.graphs.get_id(:name => graph)).should be_kind_of(Integer)
289
305
  end
data/spec/run.rb CHANGED
@@ -24,8 +24,6 @@ graph = "graph"
24
24
 
25
25
  puts "### Zabbix API server version #{zbx.server.version} ###"
26
26
 
27
- puts "### Zabbix API server version #{zbx.server.version} ###"
28
-
29
27
  describe ZabbixApi, "test_api" do
30
28
 
31
29
  it "SERVER: Get version api" do
@@ -189,13 +187,6 @@ describe ZabbixApi, "test_api" do
189
187
  ).should be_kind_of(TrueClass)
190
188
  end
191
189
 
192
- it "TEMPLATE: Unlink hosts from templates" do
193
- zbx.templates.mass_remove(
194
- :hosts_id => [zbx.hosts.get_id(:host => host)],
195
- :templates_id => [zbx.templates.get_id(:host => template)]
196
- ).should be_kind_of(TrueClass)
197
- end
198
-
199
190
  it "TEMPLATE: Get all" do
200
191
  zbx.templates.all.should be_kind_of(Hash)
201
192
  end
@@ -256,6 +247,10 @@ describe ZabbixApi, "test_api" do
256
247
  zbx.graphs.get_id( :name => graph ).should be_kind_of(Integer)
257
248
  end
258
249
 
250
+ it "GRAPH: get_ids_by_host" do
251
+ zbx.graphs.get_ids_by_host( :host => host ).should be_kind_of(Array)
252
+ end
253
+
259
254
  it "GRAPH: Update" do
260
255
  zbx.graphs.update(
261
256
  :graphid => zbx.graphs.get_id(
@@ -282,6 +277,26 @@ describe ZabbixApi, "test_api" do
282
277
  ).should be_kind_of(Integer)
283
278
  end
284
279
 
280
+ it "SCREEN: Get or create for host" do
281
+ zbx.screens.get_or_create_for_host(
282
+ :host => host,
283
+ :graphids => zbx.graphs.get_ids_by_host(:host => host)
284
+ ).should be_kind_of(Integer)
285
+ end
286
+
287
+ it "TEMPLATE: Unlink hosts from templates" do
288
+ zbx.templates.mass_remove(
289
+ :hosts_id => [zbx.hosts.get_id(:host => host)],
290
+ :templates_id => [zbx.templates.get_id(:host => template)]
291
+ ).should be_kind_of(TrueClass)
292
+ end
293
+
294
+ it "SCREEN: Delete" do
295
+ zbx.screens.delete(
296
+ [zbx.screens.get_id(:name => "#{host}_graphs")]
297
+ ).should be_kind_of(Integer)
298
+ end
299
+
285
300
  it "GRAPH: Delete" do
286
301
  zbx.graphs.delete(zbx.graphs.get_id(:name => graph)).should be_kind_of(Integer)
287
302
  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: 0.4.7
4
+ version: 0.4.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-23 00:00:00.000000000 Z
12
+ date: 2012-11-27 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Allows you to work with zabbix api from ruby.
15
15
  email:
@@ -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/screens.rb
36
37
  - lib/zabbixapi/server.rb
37
38
  - lib/zabbixapi/templates.rb
38
39
  - lib/zabbixapi/triggers.rb
@@ -56,7 +57,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
56
57
  version: '0'
57
58
  segments:
58
59
  - 0
59
- hash: 3388077476884673274
60
+ hash: 1867199744098275115
60
61
  required_rubygems_version: !ruby/object:Gem::Requirement
61
62
  none: false
62
63
  requirements: