zabbixapi 0.1.6.2 → 0.1.6.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,7 @@ You can:
20
20
  "dns" => 'my.example.com',
21
21
  "host" => 'my.example.com',
22
22
  "useip" => 1,
23
- "groups" => [get_group_id('some_group')]
23
+ "groups" => [zbx.get_group_id('some_group')]
24
24
  }
25
25
  if zbx.create_host(host_options)
26
26
  puts zbx.get_host_id('my.example.com')
@@ -33,6 +33,8 @@ You can:
33
33
  puts zbx.get_group_id('some_group')
34
34
  end
35
35
 
36
+ More see in spec please...
37
+
36
38
  == Dependencies
37
39
 
38
40
  * net/http
@@ -55,17 +55,17 @@ module Zabbix
55
55
  request.body=(message_json)
56
56
 
57
57
  begin
58
- puts "[ZBXAPI] : INFO : Do request. Body => #{request.body}" if @debug
58
+ puts "[ZBXAPI] : #{Time.now()} : INFO : Do request. Body => #{request.body}" if @debug
59
59
  response = http.request(request)
60
60
  rescue ::SocketError => e
61
- puts "[ZBXAPI] : ERROR : SocketError => #{e.message}" if @debug
61
+ puts "[ZBXAPI] : #{Time.now()} : ERROR : SocketError => #{e.message}" if @debug
62
62
  raise Zabbix::SocketError.new(e.message)
63
63
  end
64
64
 
65
65
  if @debug
66
- puts "[ZBXAPI] : INFO : Response start"
67
- puts response
68
- puts "[ZBXAPI] : INFO : Response end"
66
+ puts "[ZBXAPI] : #{Time.now()} : INFO : Response start"
67
+ puts "[ZBXAPI] : #{Time.now()} : INFO : Response body: #{response.inspect}"
68
+ puts "[ZBXAPI] : #{Time.now()} : INFO : Response end"
69
69
  end
70
70
 
71
71
  if response.code != "200"
@@ -75,7 +75,7 @@ module Zabbix
75
75
  response_body_hash = JSON.parse(response.body)
76
76
 
77
77
  if @debug
78
- puts "[ZBXAPI] : INFO : Response body"
78
+ puts "[ZBXAPI] : #{Time.now()} : INFO : Response body"
79
79
  puts response_body_hash.inspect
80
80
  end
81
81
 
@@ -90,6 +90,8 @@ module Zabbix
90
90
  case error_code.to_s
91
91
  when '-32602'
92
92
  raise Zabbix::AlreadyExist.new(e_message)
93
+ when '-32500'
94
+ raise Zabbix::ArgumentError.new(e_message)
93
95
  else
94
96
  raise Zabbix::ResponseError.new(e_message)
95
97
  end
@@ -7,7 +7,7 @@ module Zabbix
7
7
  'params' => graph
8
8
  }
9
9
  response = send_request(message)
10
- return 0
10
+ response.empty? ? nil : response[0]['graphid']
11
11
  end
12
12
 
13
13
  def get_graph_id(host_id, graph_name)
@@ -11,7 +11,7 @@ module Zabbix
11
11
  }
12
12
  }
13
13
  response = send_request(message)
14
- response.empty? ? nil : response[0]['groupid']
14
+ response.empty? ? nil : response[0]['groupid'].to_i
15
15
  end
16
16
 
17
17
  def group_exist?(pattern)
@@ -31,15 +31,16 @@ module Zabbix
31
31
  end
32
32
 
33
33
  def delete_group(groupname)
34
- group_id = get_group_id(groupname)
35
- message = {
36
- 'method' => 'hostgroup.delete',
37
- 'params' => {
38
- 'groupid' => group_id
39
- }
40
- }
41
- response = send_request(message)
42
- response ? response['groupids'][0].to_i : nil
34
+ if group_id = get_group_id(groupname)
35
+ message = {
36
+ 'method' => 'hostgroup.delete',
37
+ 'params' => {
38
+ 'groupid' => group_id
39
+ }
40
+ }
41
+ response = send_request(message)
42
+ response ? response['groupids'][0].to_i : nil
43
+ end
43
44
  end
44
45
 
45
46
  def add_host_to_group(host_id, group_id)
@@ -51,7 +52,7 @@ module Zabbix
51
52
  }
52
53
  }
53
54
  response = send_request(message)
54
- response.empty? ? false : true
55
+ response ? response[0]['groupid'].to_i : nil
55
56
  end
56
57
 
57
58
  end
@@ -9,7 +9,7 @@ module Zabbix
9
9
  'params' => host
10
10
  }
11
11
  responce = send_request(message)
12
- responce.empty? ? nil : responce['hostids'][0].to_i
12
+ responce.empty? ? nil : response['hostids'][0].to_i
13
13
  end
14
14
 
15
15
  def add_host(host_options)
@@ -54,14 +54,16 @@ module Zabbix
54
54
  end
55
55
 
56
56
  def delete_host(hostname)
57
- host_id = get_host_id(hostname)
58
- message = {
59
- 'method' => 'host.delete',
60
- 'params' => {
61
- 'hostid' => host_id
62
- }
63
- }
64
- send_request(message)
57
+ if host_id = get_host_id(hostname)
58
+ message = {
59
+ 'method' => 'host.delete',
60
+ 'params' => {
61
+ 'hostid' => host_id
62
+ }
63
+ }
64
+ response = send_request(message)
65
+ response.empty? ? nil : response['hostids'][0].to_i
66
+ end
65
67
  end
66
68
 
67
69
  end
@@ -82,12 +82,7 @@ module Zabbix
82
82
 
83
83
  def item_exist?(host_id, item_name)
84
84
  item_id = get_item_id(host_id, item_name)
85
- if item_id
86
- result = true
87
- else
88
- result = false
89
- end
90
- return result
85
+ item_id ? true : false
91
86
  end
92
87
 
93
88
  def update_item(item_id, options)
@@ -37,14 +37,15 @@ module Zabbix
37
37
  end
38
38
 
39
39
  def delete_mediatype(mediatype)
40
- mediatype_id = get_mediatype_id(mediatype)
41
- message = {
42
- 'method' => 'mediatype.delete',
43
- 'params' =>
44
- [mediatype_id]
45
- }
46
- response = send_request(message)
47
- response ? true : false
40
+ if mediatype_id = get_mediatype_id(mediatype)
41
+ message = {
42
+ 'method' => 'mediatype.delete',
43
+ 'params' =>
44
+ [mediatype_id]
45
+ }
46
+ response = send_request(message)
47
+ response.empty? ? nil : response['mediatypeids'][0]
48
+ end
48
49
  end
49
50
 
50
51
  end
@@ -59,7 +59,7 @@ module Zabbix
59
59
  }
60
60
  }
61
61
  response = send_request(message)
62
- response.empty? ? false : true
62
+ response.empty? ? nil : response[0]['screenids'].to_i
63
63
  end
64
64
 
65
65
  def del_all_graphs_from_screen(screen_id)
@@ -70,7 +70,7 @@ module Zabbix
70
70
  }
71
71
  }
72
72
  response = send_request(message)
73
- response ? response : nil
73
+ response ? response[0]['screenids'].to_i : nil
74
74
  end
75
75
 
76
76
  def add_graph_to_screen(screen_id, graph_id, x, y)
@@ -86,7 +86,7 @@ module Zabbix
86
86
  }
87
87
  }
88
88
  response = send_request(message)
89
- return response
89
+ response.empty? ? nil : response['templateid'][0].to_i
90
90
  end
91
91
 
92
92
  def unlink_templates_from_hosts(templates_id, hosts_id)
@@ -101,7 +101,7 @@ module Zabbix
101
101
  }
102
102
  }
103
103
  response = send_request(message)
104
- return response
104
+ response.empty? ? nil : response['templateids'][0].to_i
105
105
  end
106
106
 
107
107
  end
@@ -42,18 +42,5 @@ module Zabbix
42
42
  return result
43
43
  end
44
44
 
45
- def set_macro_value(host_id, macro_name, macro_value)
46
- message = {
47
- 'method' => 'usermacro.updateValue',
48
- 'params' => {
49
- 'hostid' => host_id,
50
- 'macro' => macro_name,
51
- 'value' => macro_value
52
- }
53
- }
54
- response = send_request(message)
55
- return true
56
- end
57
-
58
45
  end
59
46
  end
@@ -0,0 +1,129 @@
1
+ require 'zabbixapi'
2
+ require 'json'
3
+
4
+ # settings
5
+ api_url = 'http://zabbix.local/api_jsonrpc.php'
6
+ api_login = 'Admin'
7
+ api_password = 'zabbix'
8
+
9
+
10
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
11
+ #zbx.debug = true
12
+
13
+ # 01. Create group
14
+ describe Zabbix::ZabbixApi, "create_group" do
15
+ it "Create some group" do
16
+ result = zbx.add_group('some_group')
17
+ result.should be_kind_of(Integer)
18
+ end
19
+ end
20
+
21
+ # 02. Get group_id
22
+ describe Zabbix::ZabbixApi, "get_group_id" do
23
+ it "Get group_id" do
24
+ result = zbx.get_group_id('some_group')
25
+ result.should be_kind_of(Integer)
26
+ end
27
+ end
28
+
29
+ # 03. Get unknown group_id
30
+ describe Zabbix::ZabbixApi, "get_group_id" do
31
+ it "Get unknown group" do
32
+ result = zbx.get_group_id('___some_group')
33
+ result.should be_nil
34
+ end
35
+ end
36
+
37
+ # 04. Create host
38
+ host_options = {
39
+ "ip" => '127.0.0.1',
40
+ "dns" => 'my.example.com',
41
+ "host" => 'my.example.com',
42
+ "useip" => 1,
43
+ "groups" => [1]
44
+ }
45
+ describe Zabbix::ZabbixApi, "create_host" do
46
+ it "Create host" do
47
+ result = zbx.add_host(host_options)
48
+ result.should be_kind_of(Integer)
49
+ end
50
+ end
51
+
52
+ # 05. Get host
53
+ describe Zabbix::ZabbixApi, "get_host" do
54
+ it "Get host by name" do
55
+ result = zbx.get_host_id('my.example.com')
56
+ result.should be_kind_of(Integer)
57
+ end
58
+ end
59
+
60
+ # 06. Get unknown host
61
+ describe Zabbix::ZabbixApi, "get_host" do
62
+ it "Get unknown host by name" do
63
+ result = zbx.get_host_id('___my.example.com')
64
+ result.should be_nil
65
+ end
66
+ end
67
+
68
+ # 07. Delete host
69
+ describe Zabbix::ZabbixApi, "delete_host" do
70
+ it "Delete host" do
71
+ result = zbx.delete_host('my.example.com')
72
+ result.should be_kind_of(Integer)
73
+ end
74
+ end
75
+
76
+ # 08. Delete unknown host
77
+ describe Zabbix::ZabbixApi, "delete_unknown_host" do
78
+ it "Delete unknown host" do
79
+ result = zbx.delete_host('__my.example.com')
80
+ result.should be_nil
81
+ end
82
+ end
83
+
84
+ # 09. Delete group
85
+ describe Zabbix::ZabbixApi, "delete_group" do
86
+ it "Delete some group" do
87
+ result = zbx.delete_group('some_group')
88
+ result.should be_kind_of(Integer)
89
+ end
90
+ end
91
+
92
+ # 10. Delete unknown group
93
+ describe Zabbix::ZabbixApi, "delete_unknown_group" do
94
+ it "Delete unknown group" do
95
+ result = zbx.delete_group('___some_group')
96
+ result.should be_nil
97
+ end
98
+ end
99
+
100
+ # 11. Mediatype create
101
+ mediatype_options = {
102
+ 'type' => '0', #email
103
+ 'description' => 'example_mediatype',
104
+ 'smtp_server' => 'test.company.com',
105
+ 'smtp_helo' => 'test.company.com',
106
+ 'smtp_email' => 'test@test.company.com',
107
+ }
108
+ describe Zabbix::ZabbixApi, "create_mediatype" do
109
+ it "Create mediatype" do
110
+ result = zbx.add_mediatype(mediatype_options)
111
+ result.should be_kind_of(Integer)
112
+ end
113
+ end
114
+
115
+ # 12. Mediatype unknown delete
116
+ describe Zabbix::ZabbixApi, "delete_unknown_mediatype" do
117
+ it "Delete unknown mediatype" do
118
+ result = zbx.delete_mediatype('__example_mediatype')
119
+ result.should be_nil
120
+ end
121
+ end
122
+
123
+ # 13. Mediatype delete
124
+ describe Zabbix::ZabbixApi, "delete_mediatype" do
125
+ it "Delete mediatype" do
126
+ result = zbx.delete_mediatype('example_mediatype')
127
+ result.should be_kind_of(Integer)
128
+ end
129
+ end
@@ -4,7 +4,7 @@ require 'rake'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
 
7
- spec.version = '0.1.6.2'
7
+ spec.version = '0.1.6.3'
8
8
  spec.name = 'zabbixapi'
9
9
  spec.summary = 'Ruby module for work with zabbix api.'
10
10
 
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.1.6.2
4
+ version: 0.1.6.3
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-10-08 00:00:00.000000000 Z
12
+ date: 2012-10-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby module for work with zabbix api v1.8.
15
15
  email: vadv.mkn@gmail.com
@@ -30,7 +30,7 @@ files:
30
30
  - lib/zabbixapi/template.rb
31
31
  - lib/zabbixapi/trigger.rb
32
32
  - lib/zabbixapi/usermacro.rb
33
- - spec/item.rb
33
+ - spec/localhost.rb
34
34
  - zabbixapi.gemspec
35
35
  - README.rdoc
36
36
  - examples/config.yml
@@ -1,74 +0,0 @@
1
- require 'zabbixapi'
2
- require 'json'
3
-
4
- # settings
5
- api_url = 'http://zabbix.local/api_jsonrpc.php'
6
- api_login = 'Admin'
7
- api_password = 'zabbix'
8
-
9
-
10
- zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
11
- #zbx.debug = true
12
-
13
- # 01. Create group
14
- describe Zabbix::ZabbixApi, "create_group" do
15
- it "Create some group" do
16
- result = zbx.add_group('some_group')
17
- end
18
- end
19
-
20
- # 02. Create host
21
- host_options = {
22
- "ip" => '127.0.0.1',
23
- "dns" => 'my.example.com',
24
- "host" => 'my.example.com',
25
- "useip" => 1,
26
- "groups" => ['some_group']
27
- }
28
- describe Zabbix::ZabbixApi, "create_host" do
29
- it "Create host" do
30
- result = zbx.add_host(host_options)
31
- end
32
- end
33
-
34
- # 03. Get host
35
- describe Zabbix::ZabbixApi, "get_host" do
36
- it "Get host by name" do
37
- result = zbx.get_host_id('my.example.com')
38
- end
39
- end
40
-
41
- # 04. Delete group
42
- describe Zabbix::ZabbixApi, "delete_group" do
43
- it "Delete some group" do
44
- result = zbx.delete_group(zbx.get_group_id'some_group')
45
- end
46
- end
47
-
48
- # 05. Delete host
49
- describe Zabbix::ZabbixApi, "delete_host" do
50
- it "Delete host" do
51
- result = zbx.delete_host('my.example.com')
52
- end
53
- end
54
-
55
- # 06. Mediatype create
56
- mediatype_options = {
57
- 'type' => '0',
58
- 'description' => 'example_mediatype',
59
- 'smtp_server' => 'test.company.com',
60
- 'smtp_helo' => 'test.company.com',
61
- 'smtp_email' => 'test@test.company.com',
62
- }
63
- describe Zabbix::ZabbixApi, "create_mediatype" do
64
- it "Create mediatype" do
65
- result = zbx.add_mediatype(mediatype_options)
66
- end
67
- end
68
-
69
- # 07. Mediatype delete
70
- describe Zabbix::ZabbixApi, "create_mediatype" do
71
- it "Delete mediatype" do
72
- result = zbx.delete_mediatype('example_mediatype')
73
- end
74
- end