zabbixapi 0.1.6.3 → 0.1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Simple and lightweight ruby module for work with zabbix api version 1.8.x
4
4
 
5
+ {<img src="https://travis-ci.org/vadv/zabbixapi.png">}[https://travis-ci.org/vadv/zabbixapi]
6
+
5
7
  You can:
6
8
  * Create and delete host/template/application/items/triggers and screens;
7
9
  * Get info about all zabbix essences;
@@ -1,6 +1,6 @@
1
1
  module Zabbix
2
-
3
2
  class ZabbixApi
3
+
4
4
  def add_application(app_options)
5
5
  app_options_default = {
6
6
  'hostid' => nil,
@@ -14,20 +14,27 @@ module Zabbix
14
14
  responce = send_request(message)
15
15
  responce.empty? ? nil : responce['applicationids'][0].to_i
16
16
  end
17
- end
18
17
 
19
- def get_app_id(host_id, app_name)
20
- message = {
21
- 'method' => 'application.get',
22
- 'params' => {
23
- 'filter' => {
24
- 'name' => app_name,
25
- 'hostid' => host_id
26
- }
27
- }
28
- }
29
- responce = send_request(message)
30
- responce.empty? ? nil : responce[0]['applicationid']
31
- end
18
+ def add_or_get_application(host_id, app_options)
19
+ unless a_id = get_app_id(host_id, app_options['name'])
20
+ a_id = add_application(app_options)
21
+ end
22
+ return a_id
23
+ end
32
24
 
25
+ def get_app_id(host_id, app_name)
26
+ message = {
27
+ 'method' => 'application.get',
28
+ 'params' => {
29
+ 'filter' => {
30
+ 'name' => app_name,
31
+ 'hostid' => host_id
32
+ }
33
+ }
34
+ }
35
+ responce = send_request(message)
36
+ responce.empty? ? nil : responce[0]['applicationid']
37
+ end
38
+
39
+ end
33
40
  end
@@ -7,7 +7,7 @@ module Zabbix
7
7
  'params' => graph
8
8
  }
9
9
  response = send_request(message)
10
- response.empty? ? nil : response[0]['graphid']
10
+ response.empty? ? nil : response['graphids'][0]
11
11
  end
12
12
 
13
13
  def get_graph_id(host_id, graph_name)
@@ -24,6 +24,13 @@ module Zabbix
24
24
  response.empty? ? nil : response[0]['graphid']
25
25
  end
26
26
 
27
+ def add_or_get_graph(host_id, graph)
28
+ unless g_id = get_graph_id(host_id, graph['name'])
29
+ g_id = add_graph(graph)
30
+ end
31
+ return g_id
32
+ end
33
+
27
34
  def get_graphs(host_id)
28
35
  message = {
29
36
  'method' => 'graph.get',
@@ -30,6 +30,13 @@ module Zabbix
30
30
  response ? response['groupids'][0].to_i : nil
31
31
  end
32
32
 
33
+ def add_or_get_group(groupname)
34
+ unless g_id = get_group_id([groupname])
35
+ g_id = add_group(groupname)
36
+ end
37
+ return g_id
38
+ end
39
+
33
40
  def delete_group(groupname)
34
41
  if group_id = get_group_id(groupname)
35
42
  message = {
@@ -80,6 +80,13 @@ module Zabbix
80
80
  response.empty? ? nil : response[0]['itemid']
81
81
  end
82
82
 
83
+ def add_or_get_item(host_id, item_options)
84
+ unless i_id = get_item_id(host_id, item_options['description'])
85
+ i_id = add_item(item_options)
86
+ end
87
+ return i_id
88
+ end
89
+
83
90
  def item_exist?(host_id, item_name)
84
91
  item_id = get_item_id(host_id, item_name)
85
92
  item_id ? true : false
@@ -16,6 +16,13 @@ module Zabbix
16
16
  response.empty? ? nil : response['templateids'][0].to_i
17
17
  end
18
18
 
19
+ def add_or_get_template(template_options)
20
+ unless t_id = get_template_id(template_options['host'])
21
+ t_id = add_template(template_options)
22
+ end
23
+ return t_id
24
+ end
25
+
19
26
  def get_template_ids_by_host(host_id)
20
27
  message = {
21
28
  'method' => 'template.get',
@@ -72,7 +79,7 @@ module Zabbix
72
79
  }
73
80
  }
74
81
  response = send_request(message)
75
- response.empty? ? nil : response.keys[0]
82
+ response.empty? ? nil : response[0]['templateid'].to_i
76
83
  end
77
84
 
78
85
  def link_templates_with_hosts(templates_id, hosts_id)
@@ -104,5 +111,18 @@ module Zabbix
104
111
  response.empty? ? nil : response['templateids'][0].to_i
105
112
  end
106
113
 
114
+ def delete_template(template_name)
115
+ message = {
116
+ 'method' => 'template.delete',
117
+ 'params' => [
118
+ {
119
+ "templateid" => get_template_id(template_name)
120
+ }
121
+ ]
122
+ }
123
+ response = send_request(message)
124
+ response.empty? ? nil : response['templateids'][0].to_i
125
+ end
126
+
107
127
  end
108
128
  end
@@ -4,19 +4,26 @@ module Zabbix
4
4
  def add_trigger(trigger)
5
5
  message = {
6
6
  'method' => 'trigger.create',
7
- 'params' => [trigger]
7
+ 'params' => [ trigger ]
8
8
  }
9
9
  response = send_request(message)
10
10
  response.empty? ? nil : response['triggerids'][0]
11
11
  end
12
12
 
13
+ def add_or_get_trigger(host_id, trigger)
14
+ unless tr_id = get_trigger_id(host_id, trigger['description'])
15
+ tr_id = add_trigger(trigger)
16
+ end
17
+ return tr_id
18
+ end
19
+
13
20
  def get_trigger_id(host_id, trigger_name)
14
21
  message = {
15
22
  'method' => 'trigger.get',
16
23
  'params' => {
17
24
  'filter' => {
18
25
  'hostid' => host_id,
19
- 'description' => trigger_name
26
+ 'description' => [ trigger_name ]
20
27
  }
21
28
  }
22
29
  }
File without changes
data/spec/localhost.rb CHANGED
@@ -2,7 +2,7 @@ require 'zabbixapi'
2
2
  require 'json'
3
3
 
4
4
  # settings
5
- api_url = 'http://zabbix.local/api_jsonrpc.php'
5
+ api_url = 'http://localhost/zabbix/api_jsonrpc.php'
6
6
  api_login = 'Admin'
7
7
  api_password = 'zabbix'
8
8
 
@@ -10,120 +10,97 @@ api_password = 'zabbix'
10
10
  zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
11
11
  #zbx.debug = true
12
12
 
13
- # 01. Create group
14
13
  describe Zabbix::ZabbixApi, "create_group" do
14
+
15
+ # 01. Create group
15
16
  it "Create some group" do
16
17
  result = zbx.add_group('some_group')
17
18
  result.should be_kind_of(Integer)
18
19
  end
19
- end
20
20
 
21
- # 02. Get group_id
22
- describe Zabbix::ZabbixApi, "get_group_id" do
21
+ # 02. Get group_id
23
22
  it "Get group_id" do
24
23
  result = zbx.get_group_id('some_group')
25
24
  result.should be_kind_of(Integer)
26
25
  end
27
- end
28
26
 
29
- # 03. Get unknown group_id
30
- describe Zabbix::ZabbixApi, "get_group_id" do
27
+ # 03. Get unknown group_id
31
28
  it "Get unknown group" do
32
29
  result = zbx.get_group_id('___some_group')
33
30
  result.should be_nil
34
31
  end
35
- end
36
32
 
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
33
+ # 04. Create host
34
+ host_options = {
35
+ "ip" => '127.0.0.1',
36
+ "dns" => 'my.example.com',
37
+ "host" => 'my.example.com',
38
+ "useip" => 1,
39
+ "groups" => [1]
40
+ }
46
41
  it "Create host" do
47
42
  result = zbx.add_host(host_options)
48
43
  result.should be_kind_of(Integer)
49
44
  end
50
- end
51
45
 
52
- # 05. Get host
53
- describe Zabbix::ZabbixApi, "get_host" do
46
+ # 05. Get host
54
47
  it "Get host by name" do
55
48
  result = zbx.get_host_id('my.example.com')
56
49
  result.should be_kind_of(Integer)
57
50
  end
58
- end
59
51
 
60
- # 06. Get unknown host
61
- describe Zabbix::ZabbixApi, "get_host" do
52
+ # 06. Get unknown host
62
53
  it "Get unknown host by name" do
63
54
  result = zbx.get_host_id('___my.example.com')
64
55
  result.should be_nil
65
56
  end
66
- end
67
57
 
68
- # 07. Delete host
69
- describe Zabbix::ZabbixApi, "delete_host" do
58
+ # 07. Delete host
70
59
  it "Delete host" do
71
60
  result = zbx.delete_host('my.example.com')
72
61
  result.should be_kind_of(Integer)
73
62
  end
74
- end
75
63
 
76
- # 08. Delete unknown host
77
- describe Zabbix::ZabbixApi, "delete_unknown_host" do
64
+ # 08. Delete unknown host
78
65
  it "Delete unknown host" do
79
66
  result = zbx.delete_host('__my.example.com')
80
67
  result.should be_nil
81
68
  end
82
- end
83
69
 
84
- # 09. Delete group
85
- describe Zabbix::ZabbixApi, "delete_group" do
70
+ # 09. Delete group
86
71
  it "Delete some group" do
87
72
  result = zbx.delete_group('some_group')
88
73
  result.should be_kind_of(Integer)
89
74
  end
90
- end
91
75
 
92
- # 10. Delete unknown group
93
- describe Zabbix::ZabbixApi, "delete_unknown_group" do
76
+ # 10. Delete unknown group
94
77
  it "Delete unknown group" do
95
78
  result = zbx.delete_group('___some_group')
96
79
  result.should be_nil
97
80
  end
98
- end
99
81
 
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
82
+ # 11. Mediatype create
83
+ mediatype_options = {
84
+ 'type' => '0', #email
85
+ 'description' => 'example_mediatype',
86
+ 'smtp_server' => 'test.company.com',
87
+ 'smtp_helo' => 'test.company.com',
88
+ 'smtp_email' => 'test@test.company.com',
89
+ }
109
90
  it "Create mediatype" do
110
91
  result = zbx.add_mediatype(mediatype_options)
111
92
  result.should be_kind_of(Integer)
112
93
  end
113
- end
114
-
115
- # 12. Mediatype unknown delete
116
- describe Zabbix::ZabbixApi, "delete_unknown_mediatype" do
94
+ # 12. Mediatype unknown delete
117
95
  it "Delete unknown mediatype" do
118
96
  result = zbx.delete_mediatype('__example_mediatype')
119
97
  result.should be_nil
120
98
  end
121
- end
122
99
 
123
- # 13. Mediatype delete
124
- describe Zabbix::ZabbixApi, "delete_mediatype" do
100
+ # 13. Mediatype delete
125
101
  it "Delete mediatype" do
126
102
  result = zbx.delete_mediatype('example_mediatype')
127
103
  result.should be_kind_of(Integer)
128
104
  end
105
+
129
106
  end
data/spec/request.rb ADDED
@@ -0,0 +1,130 @@
1
+ require 'zabbixapi'
2
+ require 'json'
3
+ require 'servers/server18'
4
+
5
+ # settings
6
+ api_url = 'http://somehost'
7
+ api_login = ''
8
+ api_password = ''
9
+
10
+
11
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
12
+ #zbx.debug = true
13
+
14
+ describe Zabbix::ZabbixApi do
15
+
16
+ before :each do
17
+ # stub auth tocken
18
+ Zabbix::ZabbixApi.any_instance.stub(:auth).and_return "a9a1f569d10d6339f23c4d122a7f5c46"
19
+ # stub resquest
20
+ @request = mock
21
+ Net::HTTP.any_instance.stub(:request).and_return @request
22
+ @request.stub(:code).and_return "200"
23
+ end
24
+
25
+ # 01. Create group
26
+ it "Create some group" do
27
+ @request.stub(:body).and_return Server18.hostgroup_create
28
+ result = zbx.add_group('some_group')
29
+ result.should equal(107819)
30
+ end
31
+
32
+ # 02. Get group_id
33
+ it "Get group_id" do
34
+ @request.stub(:body).and_return Server18.hostgroup_get
35
+ result = zbx.get_group_id('some_group')
36
+ result.should equal(100100000000002)
37
+ end
38
+
39
+ # 03. Get unknown group_id
40
+ it "Get unknown group" do
41
+ @request.stub(:body).and_return Server18.hostgroup_get_error
42
+ result = zbx.get_group_id('___some_group')
43
+ result.should be_nil
44
+ end
45
+
46
+ # 04. Create host
47
+ host_options = {
48
+ "ip" => '127.0.0.1',
49
+ "dns" => 'my.example.com',
50
+ "host" => 'my.example.com',
51
+ "useip" => 1,
52
+ "groups" => [1]
53
+ }
54
+ it "Create host" do
55
+ @request.stub(:body).and_return Server18.host_create
56
+ result = zbx.add_host(host_options)
57
+ result.should equal(107819)
58
+ end
59
+
60
+ # 05. Get host
61
+ it "Get host by name" do
62
+ @request.stub(:body).and_return Server18.host_get
63
+ result = zbx.get_host_id('my.example.com')
64
+ result.should equal(100100000010017)
65
+ end
66
+
67
+ # 06. Get unknown host
68
+ it "Get unknown host by name" do
69
+ @request.stub(:body).and_return Server18.host_get_error
70
+ result = zbx.get_host_id('___my.example.com')
71
+ result.should be_nil
72
+ end
73
+
74
+ # 07. Delete host
75
+ it "Delete host" do
76
+ @request.stub(:body).and_return Server18.host_delete
77
+ result = zbx.delete_host('my.example.com')
78
+ result.should be_kind_of(Integer)
79
+ end
80
+
81
+ # 08. Delete unknown host
82
+ it "Delete unknown host" do
83
+ @request.stub(:body).and_return Server18.host_delete_error
84
+ result = zbx.delete_host('__my.example.com')
85
+ result.should be_nil
86
+ end
87
+
88
+ # 09. Delete group
89
+ it "Delete some group" do
90
+ @request.stub(:body).and_return Server18.hostgroup_delete
91
+ result = zbx.delete_group('some_group')
92
+ result.should be_kind_of(Integer)
93
+ end
94
+
95
+ # 10. Delete unknown group
96
+ it "Delete unknown group" do
97
+ @request.stub(:body).and_return Server18.hostgroup_delete_error
98
+ result = zbx.delete_group('___some_group')
99
+ result.should be_nil
100
+ end
101
+
102
+ # 11. Mediatype create
103
+ mediatype_options = {
104
+ 'type' => '0', #email
105
+ 'description' => 'example_mediatype',
106
+ 'smtp_server' => 'test.company.com',
107
+ 'smtp_helo' => 'test.company.com',
108
+ 'smtp_email' => 'test@test.company.com',
109
+ }
110
+ it "Create mediatype" do
111
+ @request.stub(:body).and_return Server18.mediatype_create
112
+ result = zbx.add_mediatype(mediatype_options)
113
+ result.should equal(100100000214797)
114
+ end
115
+
116
+ # 12. Mediatype unknown delete
117
+ it "Delete unknown mediatype" do
118
+ @request.stub(:body).and_return Server18.mediatype_delete_error
119
+ result = zbx.delete_mediatype('__example_mediatype')
120
+ result.should be_nil
121
+ end
122
+
123
+ # 13. Mediatype delete
124
+ it "Delete mediatype" do
125
+ @request.stub(:body).and_return Server18.mediatype_delete
126
+ result = zbx.delete_mediatype('example_mediatype')
127
+ result.should be_kind_of(Integer)
128
+ end
129
+
130
+ end
data/zabbixapi.gemspec CHANGED
@@ -4,7 +4,7 @@ require 'rake'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
 
7
- spec.version = '0.1.6.3'
7
+ spec.version = '0.1.6.4'
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.3
4
+ version: 0.1.6.4
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-09 00:00:00.000000000 Z
12
+ date: 2012-10-15 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
@@ -29,8 +29,10 @@ files:
29
29
  - lib/zabbixapi/screen.rb
30
30
  - lib/zabbixapi/template.rb
31
31
  - lib/zabbixapi/trigger.rb
32
+ - lib/zabbixapi/user.rb
32
33
  - lib/zabbixapi/usermacro.rb
33
34
  - spec/localhost.rb
35
+ - spec/request.rb
34
36
  - zabbixapi.gemspec
35
37
  - README.rdoc
36
38
  - examples/config.yml