iteh-zabbixapi 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
1
  module Zabbix
2
-
3
2
  class ZabbixApi
4
3
  def get_group_id(pattern)
5
4
 
@@ -13,25 +12,12 @@ module Zabbix
13
12
  }
14
13
 
15
14
  response = send_request(message)
16
-
17
- if not ( response.empty? ) then
18
- result = response[0]['groupid']
19
- else
20
- result = nil
21
- end
22
-
23
- return result
15
+ response.empty? ? nil : response[0]['groupid']
24
16
  end
25
17
 
26
18
  def group_exist?(pattern)
27
-
28
19
  group_id = get_groups_id(pattern)
29
-
30
- if ( group_id ) then
31
- return true
32
- else
33
- return false
34
- end
20
+ group_id ? true : false
35
21
  end
36
22
 
37
23
  def add_group(groupname)
@@ -44,14 +30,7 @@ module Zabbix
44
30
  }
45
31
 
46
32
  response = send_request(message)
47
-
48
- if ( response ) then
49
- result = response['groupids']
50
- else
51
- result = nil
52
- end
53
-
54
- return result
33
+ response ? response['groupids'] : nil
55
34
  end
56
35
 
57
36
  def add_host_to_group(host_id, group_id)
@@ -66,13 +45,7 @@ module Zabbix
66
45
 
67
46
  response = send_request(message)
68
47
 
69
- if not ( response.empty? ) then
70
- result = true
71
- else
72
- result = false
73
- end
74
-
75
- return result
48
+ response.empty? ? false : true
76
49
  end
77
50
  end
78
51
  end
@@ -12,6 +12,7 @@ module Zabbix
12
12
  'ip' => '0.0.0.0',
13
13
  'proxy_hostid' => 0,
14
14
  'groups' => [],
15
+ 'templates' => [],
15
16
  'useipmi' => 0,
16
17
  'ipmi_ip' => '',
17
18
  'ipmi_port' => 623,
@@ -22,6 +23,7 @@ module Zabbix
22
23
  }
23
24
 
24
25
  host_options['groups'].map! { |group_id| {'groupid' => group_id} }
26
+ host_options['templates'].map! { |template_id| {'templateid' => template_id} }
25
27
 
26
28
  host = merge_opt(host_default, host_options)
27
29
 
@@ -89,15 +89,7 @@ module Zabbix
89
89
  }
90
90
 
91
91
  response = send_request(message)
92
-
93
- unless response.empty? then
94
- result = response.keys[0]
95
- else
96
- result = nil
97
- end
98
-
99
- return result
100
-
92
+ (response.is_a?(Array) && response.first.is_a?(Hash)) ? response.first["templateid"] : nil
101
93
  end
102
94
 
103
95
  def link_templates_with_hosts(templates_id, hosts_id)
@@ -16,5 +16,5 @@
16
16
  # limitations under the License.
17
17
 
18
18
  module Zabbix
19
- VERSION = "0.2.2"
19
+ VERSION = "0.2.3"
20
20
  end
@@ -92,6 +92,65 @@ describe Zabbix::ZabbixApi do
92
92
  host.should be_nil
93
93
  end
94
94
 
95
+ it "should add a host with groups and templates" do
96
+ FakeWeb.allow_net_connect = true
97
+
98
+ new_host = {
99
+ "host" => "new host",
100
+ "ip" => "10.10.10.10",
101
+ "port" => "10055",
102
+ "useip" => 1,
103
+ "dns" => "myhost.com",
104
+ "groups" => [2],
105
+ "templates" => [10001]
106
+ }
107
+ zbx = Zabbix::ZabbixApi.new(@api_url, @api_login, @api_password)
108
+
109
+ new_host_id = zbx.add_host(new_host)
110
+ new_host_id.should_not be_nil
111
+
112
+ created_host = zbx.get_host({"templated_hosts" => 1, "output" => "extend", "filter" => {"dns" => new_host["dns"]}})
113
+ ap created_host
114
+ end
115
+
116
+ end
117
+
118
+ context 'get template' do
119
+ it "should get a template" do
120
+ FakeWeb.allow_net_connect = true
121
+
122
+ zbx = Zabbix::ZabbixApi.new(@api_url, @api_login, @api_password)
123
+ template_id = zbx.get_template_id("Template_Linux")
124
+ template_id.should eq("10001")
125
+ end
126
+
127
+ it "should return nil for no template found" do
128
+ FakeWeb.allow_net_connect = true
129
+
130
+ zbx = Zabbix::ZabbixApi.new(@api_url, @api_login, @api_password)
131
+ template_id = zbx.get_template_id("not there")
132
+ template_id.should be_nil
133
+ end
134
+
135
+ end
136
+
137
+ context 'get group' do
138
+ it "should get a group" do
139
+ FakeWeb.allow_net_connect = true
140
+
141
+ zbx = Zabbix::ZabbixApi.new(@api_url, @api_login, @api_password)
142
+ group_id = zbx.get_group_id("Linux servers")
143
+ group_id.should eq("2")
144
+ end
145
+
146
+ it "should return nil for no group found" do
147
+ FakeWeb.allow_net_connect = true
148
+
149
+ zbx = Zabbix::ZabbixApi.new(@api_url, @api_login, @api_password)
150
+ group_id = zbx.get_group_id("not there")
151
+ group_id.should be_nil
152
+ end
153
+
95
154
  end
96
155
 
97
156
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iteh-zabbixapi
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Eduard Snesarev
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-06-24 00:00:00 +02:00
19
+ date: 2011-06-25 00:00:00 +02:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency