zabbixapi 0.1.4 → 0.1.5a1

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.
@@ -0,0 +1,74 @@
1
+ module Zabbix
2
+
3
+ class ZabbixApi
4
+ def add_macro(host_id, macro_name, macro_value)
5
+
6
+ message = {
7
+ 'method' => 'Usermacro.create',
8
+ 'params' => {
9
+ 'hostid' => host_id,
10
+ 'macro' => macro_name,
11
+ 'value'=> macro_value
12
+ }
13
+ }
14
+
15
+ response = send_request(message)
16
+
17
+ if hostmacroids = response['hostmacroids'] then
18
+ result = hostmacroids
19
+ else
20
+ result = nil
21
+ end
22
+
23
+ return result
24
+ end
25
+
26
+ def get_macro(host_id, macro_name)
27
+
28
+ message = {
29
+ 'method' => 'Usermacro.get',
30
+ 'params' => {
31
+ 'hostids' => host_id,
32
+ 'macros' => macro_name,
33
+ 'extendoutput' => '1'
34
+ }
35
+ }
36
+
37
+ response = send_request(message)
38
+
39
+ unless response.empty? then
40
+ if hostmacroid = response[0]['hostmacroid'] then
41
+ macro_id = hostmacroid
42
+ macro_value = response[0]['value']
43
+
44
+ result = {
45
+ 'id' => macro_id,
46
+ 'value'=> macro_value
47
+ }
48
+ else
49
+ result = nil
50
+ end
51
+ else
52
+ result = nil
53
+ end
54
+
55
+ return result
56
+ end
57
+
58
+ def set_macro_value(host_id, macro_name, macro_value)
59
+
60
+ message = {
61
+ 'method' => 'usermacro.updateValue',
62
+ 'params' => {
63
+ 'hostid' => host_id,
64
+ 'macro' => macro_name,
65
+ 'value' => macro_value
66
+ }
67
+ }
68
+
69
+ response = send_request(message)
70
+
71
+ return true
72
+ end
73
+ end
74
+ end
data/lib/zabbixapi.rb CHANGED
@@ -1,10 +1,10 @@
1
- require 'base'
2
- require 'graph'
3
- require 'group'
4
- require 'host'
5
- require 'item'
6
- require 'screen'
7
- require 'template'
8
- require 'trigger'
9
- require 'usermacro'
10
- require 'application'
1
+ require 'zabbixapi/application'
2
+ require 'zabbixapi/base'
3
+ require 'zabbixapi/graph'
4
+ require 'zabbixapi/group'
5
+ require 'zabbixapi/host'
6
+ require 'zabbixapi/item'
7
+ require 'zabbixapi/screen'
8
+ require 'zabbixapi/template'
9
+ require 'zabbixapi/trigger'
10
+ require 'zabbixapi/usermacro'
data/spec/item.rb ADDED
@@ -0,0 +1,60 @@
1
+ require 'zabbixapi'
2
+ require 'json'
3
+
4
+ require 'webmock/rspec'
5
+ include WebMock::API
6
+
7
+ # settings
8
+ api_url = 'http://zabbix.local/api_jsonrpc.php'
9
+ api_login = 'admin'
10
+ api_password = 'zabbix'
11
+
12
+ # 01. Add item
13
+ auth_response = '{"jsonrpc":"2.0","result":"a82039d56baba1f92311aa917af9939b","id":83254}'
14
+ add_item_response = '{"jsonrpc":"2.0","result":{"itemids":["19541"]},"id":80163}'
15
+ item_options = {
16
+ 'description' => "Description",
17
+ 'key_' => "key[,avg1]",
18
+ 'hostid' => '10160',
19
+ 'applications' => [ 393 ],
20
+ 'history' => 7,
21
+ 'trends' => 30,
22
+ 'delay' => 60,
23
+ 'value_type' => 0
24
+ }
25
+
26
+ stub_request(:post, api_url).with(:body => /"method":"user\.authenticate"/).to_return(:body => auth_response)
27
+ stub_request(:post, api_url).with(:body => /"method":"item\.create"/).to_return(:body => add_item_response)
28
+
29
+ describe Zabbix::ZabbixApi, "add_item" do
30
+ it "Create item" do
31
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
32
+ result = zbx.add_item(item_options)
33
+ result.should eq("19541")
34
+ end
35
+ end
36
+
37
+ # 02. Delete item
38
+ auth_response = '{"jsonrpc":"2.0","result":"a82039d56baba1f92311aa917af9939b","id":83254}'
39
+ delete_item_response = ''
40
+ item_options = {
41
+ 'description' => "Description",
42
+ 'key_' => "key[,avg1]",
43
+ 'hostid' => '10160',
44
+ 'applications' => [ 393 ],
45
+ 'history' => 7,
46
+ 'trends' => 30,
47
+ 'delay' => 60,
48
+ 'value_type' => 0
49
+ }
50
+
51
+ stub_request(:post, api_url).with(:body => /"method":"user\.authenticate"/).to_return(:body => auth_response)
52
+ stub_request(:post, api_url).with(:body => /"method":"item\.create"/).to_return(:body => add_item_response)
53
+
54
+ describe Zabbix::ZabbixApi, "add_item" do
55
+ it "Create item" do
56
+ zbx = Zabbix::ZabbixApi.new(api_url, api_login, api_password)
57
+ result = zbx.add_item(item_options)
58
+ result.should eq("19541")
59
+ end
60
+ end
data/zabbixapi.gemspec CHANGED
@@ -1,10 +1,10 @@
1
- require 'rubygems'
1
+ # -*- encoding: utf-8 -*-
2
+
2
3
  require 'rake'
3
- require 'echoe'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
 
7
- spec.version = '0.1.4'
7
+ spec.version = '0.1.5a1'
8
8
  spec.name = 'zabbixapi'
9
9
  spec.summary = 'Ruby module for work with zabbix api.'
10
10
 
@@ -16,6 +16,5 @@ Gem::Specification.new do |spec|
16
16
  spec.has_rdoc = true
17
17
  spec.extra_rdoc_files = 'README.rdoc'
18
18
 
19
-
20
- spec.files = FileList["lib/*.rb", "bin/*", "spec/*", 'zabbixapi.gemspec', 'README.rdoc'].to_a
19
+ spec.files = FileList["lib/*.rb", "lib/zabbixapi/*.rb", "bin/*", "spec/*", 'zabbixapi.gemspec', 'README.rdoc', "examples/*"].to_a
21
20
  end
metadata CHANGED
@@ -1,76 +1,66 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: zabbixapi
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 4
9
- version: 0.1.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5a1
5
+ prerelease: 5
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Eduard Snesarev
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-03-28 00:00:00 +04:00
18
- default_executable:
12
+ date: 2012-06-18 00:00:00.000000000Z
19
13
  dependencies: []
20
-
21
- description: "Ruby module for work with zabbix api. "
14
+ description: ! 'Ruby module for work with zabbix api. '
22
15
  email: verm666@gmail.com
23
16
  executables: []
24
-
25
17
  extensions: []
26
-
27
- extra_rdoc_files:
18
+ extra_rdoc_files:
28
19
  - README.rdoc
29
- files:
30
- - lib/application.rb
31
- - lib/base.rb
32
- - lib/graph.rb
33
- - lib/group.rb
34
- - lib/host.rb
35
- - lib/item.rb
36
- - lib/screen.rb
37
- - lib/template.rb
38
- - lib/trigger.rb
39
- - lib/usermacro.rb
20
+ files:
40
21
  - lib/zabbixapi.rb
22
+ - lib/zabbixapi/application.rb
23
+ - lib/zabbixapi/base.rb
24
+ - lib/zabbixapi/graph.rb
25
+ - lib/zabbixapi/group.rb
26
+ - lib/zabbixapi/host.rb
27
+ - lib/zabbixapi/item.rb
28
+ - lib/zabbixapi/screen.rb
29
+ - lib/zabbixapi/template.rb
30
+ - lib/zabbixapi/trigger.rb
31
+ - lib/zabbixapi/usermacro.rb
32
+ - spec/item.rb
41
33
  - zabbixapi.gemspec
42
34
  - README.rdoc
43
- has_rdoc: true
35
+ - examples/config.yml
36
+ - examples/zabbix_availability
37
+ - examples/zabbix_disk_io
38
+ - examples/zabbix_filesystem
39
+ - examples/zabbix_la
40
+ - examples/zabbix_memory
41
+ - examples/zabbix_net
44
42
  homepage: http://github.com/verm666/RubyZabbixApi
45
43
  licenses: []
46
-
47
44
  post_install_message:
48
45
  rdoc_options: []
49
-
50
- require_paths:
46
+ require_paths:
51
47
  - lib
52
- required_ruby_version: !ruby/object:Gem::Requirement
48
+ required_ruby_version: !ruby/object:Gem::Requirement
53
49
  none: false
54
- requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- segments:
58
- - 0
59
- version: "0"
60
- required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
55
  none: false
62
- requirements:
63
- - - ">="
64
- - !ruby/object:Gem::Version
65
- segments:
66
- - 0
67
- version: "0"
56
+ requirements:
57
+ - - ! '>'
58
+ - !ruby/object:Gem::Version
59
+ version: 1.3.1
68
60
  requirements: []
69
-
70
61
  rubyforge_project:
71
- rubygems_version: 1.3.7
62
+ rubygems_version: 1.8.10
72
63
  signing_key:
73
64
  specification_version: 3
74
65
  summary: Ruby module for work with zabbix api.
75
66
  test_files: []
76
-
data/lib/base.rb DELETED
@@ -1,126 +0,0 @@
1
- #!/usr/bin/ruby
2
-
3
- require 'json'
4
- require 'net/http'
5
- require 'net/https'
6
-
7
- module Zabbix
8
-
9
- class JsonMessage < RuntimeError
10
- end
11
-
12
- class ResponceCodeError < RuntimeError
13
- end
14
-
15
- class ResponceBodyHash < RuntimeError
16
- end
17
-
18
- class InvalidAnswerId < RuntimeError
19
- end
20
-
21
- class Error < RuntimeError
22
- end
23
-
24
- class ZabbixApi
25
-
26
- def initialize ( api_url, api_user, api_password )
27
- @api_url = api_url
28
- @api_user = api_user
29
- @api_password = api_password
30
- end
31
-
32
- def do_request(message)
33
-
34
- id = rand 100_000
35
-
36
- message['id'] = id
37
- message['jsonrpc'] = '2.0'
38
-
39
- message_json = JSON.generate(message)
40
-
41
- # puts message.inspect
42
-
43
- uri = URI.parse(@api_url)
44
- http = Net::HTTP.new(uri.host, uri.port)
45
-
46
- if ( uri.scheme == "https" ) then
47
- http.use_ssl = true
48
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
49
- end
50
-
51
- request = Net::HTTP::Post.new(uri.request_uri)
52
- request.add_field('Content-Type', 'application/json-rpc')
53
- request.body=(message_json)
54
-
55
- # TODO сделать проверку невозможности подключения.
56
- responce = http.request(request)
57
-
58
- if ( responce.code != "200" ) then
59
- raise Zabbix::ResponceCodeError.new("Responce code from [" + @api_url + "] is " + responce.code)
60
- end
61
-
62
- responce_body_hash = JSON.parse(responce.body)
63
-
64
- #if not ( responce_body_hash['id'] == id ) then
65
- # raise Zabbix::InvalidAnswerId.new("Wrong ID in zabbix answer")
66
- #end
67
-
68
-
69
- # Check errors in zabbix answer. If error exist - raise exception Zabbix::Error
70
-
71
- # puts responce_body_hash.inspect
72
-
73
- if ( error = responce_body_hash['error'] ) then
74
- error_message = error['message']
75
- error_data = error['data']
76
- error_code = error['code']
77
-
78
- e_message = "Code: [" + error_code.to_s + "]. Message: [" + error_message +\
79
- "]. Data: [" + error_data + "]."
80
-
81
- raise Zabbix::Error.new(e_message)
82
- end
83
-
84
- result = responce_body_hash['result']
85
-
86
- return result
87
- end
88
-
89
- def send_request(message)
90
- message['auth'] = auth()
91
- do_request(message)
92
- end
93
-
94
- def auth()
95
-
96
- auth_message = {
97
- 'auth' => nil,
98
- 'method' => 'user.authenticate',
99
- 'params' => {
100
- 'user' => @api_user,
101
- 'password' => @api_password,
102
- '0' => '0'
103
- }
104
- }
105
-
106
- auth_id = do_request(auth_message)
107
-
108
- return auth_id
109
- end
110
-
111
- def merge_opt(a, b)
112
-
113
- c = {}
114
-
115
- b.each_pair do |key, value|
116
-
117
- if ( a.has_key?(key) ) then
118
- c[key] = value
119
- end
120
-
121
- end
122
-
123
- return a.merge(c)
124
- end
125
- end
126
- end
data/lib/group.rb DELETED
@@ -1,78 +0,0 @@
1
- module Zabbix
2
-
3
- class ZabbixApi
4
- def get_group_id(pattern)
5
-
6
- message = {
7
- 'method' => 'hostgroup.get',
8
- 'params' => {
9
- 'filter' => {
10
- 'name' => pattern
11
- }
12
- }
13
- }
14
-
15
- responce = send_request(message)
16
-
17
- if not ( responce.empty? ) then
18
- result = responce[0]['groupid']
19
- else
20
- result = nil
21
- end
22
-
23
- return result
24
- end
25
-
26
- def group_exist?(pattern)
27
-
28
- group_id = get_groups_id(pattern)
29
-
30
- if ( group_id ) then
31
- return true
32
- else
33
- return false
34
- end
35
- end
36
-
37
- def add_group(groupname)
38
-
39
- message = {
40
- 'method' => 'hostgroup.create',
41
- 'params' => {
42
- 'name' => groupname
43
- }
44
- }
45
-
46
- responce = send_request(message)
47
-
48
- if ( responce ) then
49
- result = responce['groupids']
50
- else
51
- result = nil
52
- end
53
-
54
- return result
55
- end
56
-
57
- def add_host_to_group(host_id, group_id)
58
-
59
- message = {
60
- 'method' => 'hostgroup.massAdd',
61
- 'params' => {
62
- 'groups' => [ group_id ],
63
- 'hosts' => [ host_id ]
64
- }
65
- }
66
-
67
- responce = send_request(message)
68
-
69
- if not ( responce.empty? ) then
70
- result = true
71
- else
72
- result = false
73
- end
74
-
75
- return result
76
- end
77
- end
78
- end
data/lib/host.rb DELETED
@@ -1,98 +0,0 @@
1
- module Zabbix
2
-
3
- # Examples:
4
- # * Create host in zabbix
5
- #
6
- # zbx = Zabbix::ZabbixApi.new(url, user, password)
7
- # host_options => {
8
- # host => 'host.example.org',
9
- # ip => '127.0.0.1',
10
- # groups => [10001, 10002],
11
- # }
12
- # host_id = zbx.add_host(host_options)
13
-
14
- # Method for creation host in zabbix.
15
- # * Input parameter - hash <tt>host_options</tt>. Available keys in hash:
16
- # - host - hostname. Type: string. Default: nil;
17
- # - port - zabbix agent pont. Type: int. Default: 10050;
18
- # - status - host status. Type: int. Possible values: 0 - monitored, 1 - not monitored. Default: 0;
19
- # - useip - use ip or dns name for monitoring host. Possible values: 0 - don't use ip (use dns name), 1 - use ip (don't use dns name);
20
- # - ip - host's ip address. Used for monitoring host if useip set to 1. Default: '0.0.0.0';
21
- # - proxy_hostid - host_id of zabbix proxy (if necessary). See <tt>get_host_id</tt>. Default: 0 (don't use proxy server);
22
- # - groups - array of groups that belong host. Default: [].
23
- # - useipmi - Use or not ipmi. Default: 0 (don't use ipmi);
24
- # - ipmi_ip - Default: '';
25
- # - ipmi_port - Default: 623;
26
- # - ipmi_authtype - Default: 0;
27
- # - ipmi_privilege - Default: 0;
28
- # - ipmi_username - Default: '';
29
- # - ipmi_password - Default: '';
30
- class ZabbixApi
31
- def add_host(host_options)
32
-
33
- host_default = {
34
- 'host' => nil,
35
- 'port' => 10050,
36
- 'status' => 0,
37
- 'useip' => 0,
38
- 'dns' => '',
39
- 'ip' => '0.0.0.0',
40
- 'proxy_hostid' => 0,
41
- 'groups' => [],
42
- 'useipmi' => 0,
43
- 'ipmi_ip' => '',
44
- 'ipmi_port' => 623,
45
- 'ipmi_authtype' => 0,
46
- 'ipmi_privilege' => 0,
47
- 'ipmi_username' => '',
48
- 'ipmi_password' => ''
49
- }
50
-
51
- host_options['groups'].map! { |group_id| {'groupid' => group_id} }
52
-
53
- host = merge_opt(host_default, host_options)
54
-
55
- message = {
56
- 'method' => 'host.create',
57
- 'params' => host
58
- }
59
-
60
- responce = send_request(message)
61
-
62
- if not ( responce.empty? ) then
63
- result = responce['hostids'][0].to_i
64
- else
65
- result = nil
66
- end
67
-
68
- return result
69
- end
70
-
71
- # Method for retrieving host id from zabbix by hostname.
72
- # * Non optional input parameters:
73
- # - hostname - Type: String.
74
- # * Return:
75
- # - host_id - Return finded host_id for passed hostname. If host not found in zabbix - return nil
76
- def get_host_id(hostname)
77
-
78
- message = {
79
- 'method' => 'host.get',
80
- 'params' => {
81
- 'filter' => {
82
- 'host' => hostname
83
- }
84
- }
85
- }
86
-
87
- responce = send_request(message)
88
-
89
- if not ( responce.empty? ) then
90
- result = responce[0]['hostid'].to_i
91
- else
92
- result = nil
93
- end
94
-
95
- return result
96
- end
97
- end
98
- end