zapix3 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4e83c4140f098e4ef1f92a91bc0a8384f034c33b
4
- data.tar.gz: d1c8ed83755afb2526214cc4d6e54126ebd5b696
3
+ metadata.gz: 3f3f33a9668f650e6509786c63ccf01b8de2a302
4
+ data.tar.gz: c533c13d7637f018c4797653bd61870557af26d2
5
5
  SHA512:
6
- metadata.gz: 7fc85708f4ba6231efc574c91996cf2ae8980435d90fe613a234d0b18bdb5a83b436441b8dde22c9de5653fa3eae1ec3bd38082ec1ac02543c46746349363925
7
- data.tar.gz: a2477b737f6dbdff67b182cbc2432cb6ac003b5fc618ea490df13d7a5e93e5685da68236678e875e1c679cef8f724abdf52b5bbff938867ce94e824ead8399ae
6
+ metadata.gz: 0ff6fb058f298868d11c4a48d937fce3c278794fd16dc451cbb7df0884c70b54e11c90a1b6169f15affd386b68df11f0a704097b084b0c24f56b03a9fb271872
7
+ data.tar.gz: 0842910a995a4e1cb6442ab81d095dc2481539a46af6a1f6d8b4b25b72220738e45a57255d2404a1af4873e3f1965c9d7d4cbd79171c4221da944fae81589c08
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- zapix3 (0.2.2)
4
+ zapix3 (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -26,19 +26,13 @@ Or install it with gem:
26
26
  First create a remote client. Feel free to
27
27
  disable the debug mode if you find it annoying.
28
28
 
29
- These environment variables also need to be set:
30
-
31
- ZABBIX_API_URL
32
- ZABBIX_API_LOGIN
33
- ZABBIX_API_PASSWORD
34
-
35
29
  ```ruby
36
30
  require 'zapix'
37
31
  zrc = ZabbixAPI.connect(
38
- :service_url => 'https://zabbix-server.foo/api_jsonrpc.php',
39
- :username => 'guybrush',
40
- :password => 'threepwood',
41
- :debug => true
32
+ service_url: ENV['ZABBIX_SERVER_URL'],
33
+ username: ENV['ZABBIX_USERNAME'],
34
+ password: ENV['ZABBIX_PASSWORD'],
35
+ debug: true
42
36
  )
43
37
  ```
44
38
 
data/Rakefile CHANGED
@@ -1 +1 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
@@ -2,8 +2,8 @@ require_relative 'base'
2
2
 
3
3
  class Actions < Base
4
4
  def exists?(options)
5
- result = client.action_get({'filter' => {'name' => options['name']}})
6
- if (result == nil || result.empty?)
5
+ result = client.action_get('filter' => { 'name' => options['name'] })
6
+ if result.nil? || result.empty?
7
7
  return false
8
8
  else
9
9
  return true
@@ -15,9 +15,8 @@ class Actions < Base
15
15
  end
16
16
 
17
17
  def get_id(options)
18
- result = client.action_get({
19
- 'filter' => {'name' => options['name']}})
20
-
18
+ result = client.action_get('filter' => { 'name' => options['name'] })
19
+
21
20
  result.first['actionid']
22
21
  end
23
22
 
@@ -1,14 +1,13 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class Applications < Base
4
-
5
4
  def create(options)
6
5
  client.application_create(options) unless exists?(options)
7
6
  end
8
7
 
9
8
  def exists?(options)
10
- result = client.application_get({'filter' => {'name' => options['name']}})
11
- if (result == nil || result.empty?)
9
+ result = client.application_get('filter' => { 'name' => options['name'] })
10
+ if result.nil? || result.empty?
12
11
  false
13
12
  else
14
13
  true
@@ -17,14 +16,12 @@ class Applications < Base
17
16
 
18
17
  def get_id(options)
19
18
  if exists?(options)
20
- client.application_get({
21
- 'filter' => {'name' => options['name'],
22
- 'hostid' => options['hostid']}}).first['applicationid']
19
+ client.application_get('filter' => { 'name' => options['name'],
20
+ 'hostid' => options['hostid'] }).first['applicationid']
23
21
  else
24
22
  raise NonExistingApplication, "Application #{options['name']} does not exist !"
25
23
  end
26
24
  end
27
25
 
28
26
  class NonExistingApplication < StandardError; end
29
-
30
27
  end
@@ -7,10 +7,10 @@ class Graphs < Base
7
7
 
8
8
  graphs.each do |g|
9
9
  graphs_with_names_and_ids <<
10
- {
11
- 'name' => g['name'],
12
- 'id' => g['graphid']
13
- }
10
+ {
11
+ 'name' => g['name'],
12
+ 'id' => g['graphid']
13
+ }
14
14
  end
15
15
 
16
16
  graphs_with_names_and_ids
@@ -1,7 +1,6 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class HostGroups < Base
4
-
5
4
  def mass_create(*names)
6
5
  names.each do |group_name|
7
6
  create(group_name)
@@ -9,21 +8,21 @@ class HostGroups < Base
9
8
  end
10
9
 
11
10
  def create(name)
12
- client.hostgroup_create({'name' => name}) unless exists?(name)
11
+ client.hostgroup_create('name' => name) unless exists?(name)
13
12
  end
14
13
 
15
14
  def create_or_update(name)
16
- if(exists?(name))
15
+ if exists?(name)
17
16
  id = get_id(name)
18
- client.hostgroup_update({'groupid' => id,'name' => name})
17
+ client.hostgroup_update('groupid' => id, 'name' => name)
19
18
  else
20
19
  create(name)
21
20
  end
22
21
  end
23
22
 
24
23
  def exists?(name)
25
- result = client.hostgroup_get({'filter' => {'name' => [name]}})
26
- if (result == nil || result.empty?)
24
+ result = client.hostgroup_get('filter' => { 'name' => [name] })
25
+ if result.nil? || result.empty?
27
26
  return false
28
27
  else
29
28
  return true
@@ -31,8 +30,8 @@ class HostGroups < Base
31
30
  end
32
31
 
33
32
  def get_id(name)
34
- if(exists?(name))
35
- result = client.hostgroup_get({'filter' => {'name' => [name]}})
33
+ if exists?(name)
34
+ result = client.hostgroup_get('filter' => { 'name' => [name] })
36
35
  result[0]['groupid']
37
36
  else
38
37
  raise NonExistingHostgroup, "Hostgroup #{name} does not exist !"
@@ -46,22 +45,22 @@ class HostGroups < Base
46
45
  end
47
46
 
48
47
  def get_host_ids_of(hostgroup)
49
- result = client.hostgroup_get('filter' => {'name' => [hostgroup]}, 'selectHosts' => 'refer')
48
+ result = client.hostgroup_get('filter' => { 'name' => [hostgroup] }, 'selectHosts' => 'refer')
50
49
  extract_host_ids(result)
51
50
  end
52
51
 
53
52
  def any_hosts?(hostgroup)
54
53
  raise NonExistingHostgroup, "Hostgroup #{hostgroup} does not exist !" unless exists?(hostgroup)
55
- result = client.hostgroup_get('filter' => {'name' => [hostgroup]}, 'selectHosts' => 'count').first['hosts'].to_i
56
- #result = client.hostgroup_get('countOutput' => 'true', 'filter' => {'name' => [hostgroup]}, 'selectHosts' => 'count').to_i
54
+ result = client.hostgroup_get('filter' => { 'name' => [hostgroup] }, 'selectHosts' => 'count').first['hosts'].to_i
55
+ # result = client.hostgroup_get('countOutput' => 'true', 'filter' => {'name' => [hostgroup]}, 'selectHosts' => 'count').to_i
57
56
  result >= 1 ? true : false
58
57
  end
59
58
 
60
59
  def delete(name)
61
- if(exists?(name))
62
- # host cannot exist without a hostgroup, so we need to delete
60
+ if exists?(name)
61
+ # host cannot exist without a hostgroup, so we need to delete
63
62
  # the attached hosts also
64
- if(any_hosts?(name))
63
+ if any_hosts?(name)
65
64
  # delete all hosts attached to a hostgroup
66
65
  host_ids = get_host_ids_of(name)
67
66
  host_ids.each do |id|
@@ -80,7 +79,7 @@ class HostGroups < Base
80
79
  def get_all
81
80
  # the fucking API also returns the ids and that's
82
81
  # why we need to extract the names
83
- host_groups_with_ids = client.hostgroup_get({'output' => ['name']})
82
+ host_groups_with_ids = client.hostgroup_get('output' => ['name'])
84
83
  extract_host_groups(host_groups_with_ids)
85
84
  end
86
85
 
@@ -1,7 +1,6 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class Hostinterfaces < Base
4
-
5
4
  def create(options)
6
5
  client.hostinterface_create(options) unless exists?(options)
7
6
  end
@@ -12,10 +11,10 @@ class Hostinterfaces < Base
12
11
 
13
12
  def get(options)
14
13
  client.hostinterface_get(
15
- {'filter' => {'hostid' => options['hostid'],
16
- 'port' => options['port'],
17
- 'type' => options['type']},
18
- 'output' => 'extend'})
14
+ 'filter' => { 'hostid' => options['hostid'],
15
+ 'port' => options['port'],
16
+ 'type' => options['type'] },
17
+ 'output' => 'extend'
18
+ )
19
19
  end
20
-
21
20
  end
@@ -1,50 +1,49 @@
1
1
  require_relative 'base'
2
2
  class Hosts < Base
3
-
4
3
  def get_id(name)
5
- if(exists?(name))
6
- client.host_get({'filter' => {'host' => name}}).first['hostid']
4
+ if exists?(name)
5
+ client.host_get('filter' => { 'host' => name }).first['hostid']
7
6
  else
8
7
  raise NonExistingHost, "Host #{name} does not exist !"
9
8
  end
10
9
  end
11
10
 
12
11
  def get_hosts_for_hostgroup(group_id)
13
- client.host_get({'groupids' => [group_id]})
12
+ client.host_get('groupids' => [group_id])
14
13
  end
15
14
 
16
- def create(options={})
17
- client.host_create(options) unless exists?(options["host"])
15
+ def create(options = {})
16
+ client.host_create(options) unless exists?(options['host'])
18
17
  end
19
18
 
20
- def create_or_update(options={})
19
+ def create_or_update(options = {})
21
20
  raise EmptyHostname, 'Host name cannot be empty !' if options['host'].nil?
22
21
  if exists?(options['host'])
23
22
  id = get_id(options['host'])
24
- options.merge!('hostid' => id)
23
+ options['hostid'] = id
25
24
  client.host_update(options)
26
25
  else
27
26
  create(options)
28
27
  end
29
28
  end
30
29
 
31
- def unlink_and_clear_templates(options={})
32
- template_ids = options['template_ids'].map { |id| {'templateid' => id}}
33
- client.host_update({'hostid' => options['host_id'], 'templates_clear' => template_ids})
30
+ def unlink_and_clear_templates(options = {})
31
+ template_ids = options['template_ids'].map { |id| { 'templateid' => id } }
32
+ client.host_update('hostid' => options['host_id'], 'templates_clear' => template_ids)
34
33
  end
35
34
 
36
- def update_templates(options={})
37
- template_ids = options['template_ids'].map { |id| {'templateid' => id}}
38
- client.host_update({'hostid' => options['host_id'], 'templates' => template_ids})
35
+ def update_templates(options = {})
36
+ template_ids = options['template_ids'].map { |id| { 'templateid' => id } }
37
+ client.host_update('hostid' => options['host_id'], 'templates' => template_ids)
39
38
  end
40
39
 
41
- def update_macros(options={})
40
+ def update_macros(options = {})
42
41
  client.host_update('hostid' => options['host_id'], 'macros' => options['macros'])
43
42
  end
44
43
 
45
44
  def exists?(name)
46
- result = client.host_get({'filter' => {'host' => name}})
47
- if (result == nil || result.empty?)
45
+ result = client.host_get('filter' => { 'host' => name })
46
+ if result.nil? || result.empty?
48
47
  false
49
48
  else
50
49
  true
@@ -52,7 +51,7 @@ class Hosts < Base
52
51
  end
53
52
 
54
53
  def get_all
55
- host_names_and_ids = client.host_get({'output' => ['name']})
54
+ host_names_and_ids = client.host_get('output' => ['name'])
56
55
 
57
56
  # the fucking api ALWAYS returns the ids and that's
58
57
  # why we need to extract the names separately
@@ -1,13 +1,9 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class Proxies < Base
4
-
5
4
  def get_id(proxy_name)
6
- result = client.proxy_get({
7
- 'filter' => {'host' => proxy_name}
8
- })
5
+ result = client.proxy_get('filter' => { 'host' => proxy_name })
9
6
 
10
7
  result.first['proxyid']
11
8
  end
12
-
13
- end
9
+ end
@@ -6,8 +6,7 @@ class Scenarios < Base
6
6
  end
7
7
 
8
8
  def get_id(options)
9
- client.httptest_get({
10
- 'filter' => {'name' => options['name'], 'hostid' => options['hostid']}}).first['httptestid']
9
+ client.httptest_get('filter' => { 'name' => options['name'], 'hostid' => options['hostid'] }).first['httptestid']
11
10
  end
12
11
 
13
12
  def delete(options)
@@ -15,20 +14,19 @@ class Scenarios < Base
15
14
  end
16
15
 
17
16
  def exists?(options)
18
- result = client.httptest_get({
19
- 'countOutput' => true,
20
- 'filter' => {'name' => options['name'],
21
- 'hostid' => options['hostid']}})
17
+ result = client.httptest_get('countOutput' => true,
18
+ 'filter' => { 'name' => options['name'],
19
+ 'hostid' => options['hostid'] })
22
20
 
23
21
  result.to_i >= 1 ? true : false
24
22
  end
25
23
 
26
24
  def get_all
27
- scenarios = client.httptest_get({'output' => 'extend'})
25
+ scenarios = client.httptest_get('output' => 'extend')
28
26
  names = extract_names(scenarios)
29
27
  end
30
28
 
31
29
  def extract_names(scenarios)
32
- scenarios.map {|scenario| scenario['name']}
30
+ scenarios.map { |scenario| scenario['name'] }
33
31
  end
34
32
  end
@@ -2,9 +2,7 @@ require_relative 'base'
2
2
 
3
3
  class Screens < Base
4
4
  def get_id(options)
5
- result = client.screen_get({
6
- 'filter' => {'name' => options['name']}
7
- })
5
+ result = client.screen_get('filter' => { 'name' => options['name'] })
8
6
 
9
7
  result.first['screenid']
10
8
  end
@@ -18,8 +16,8 @@ class Screens < Base
18
16
  end
19
17
 
20
18
  def exists?(options)
21
- result = client.screen_get({'filter' => {'name' => options['name']}})
22
- if (result == nil || result.empty?)
19
+ result = client.screen_get('filter' => { 'name' => options['name'] })
20
+ if result.nil? || result.empty?
23
21
  false
24
22
  else
25
23
  true
@@ -1,9 +1,8 @@
1
1
  require_relative 'base'
2
2
  class Templates < Base
3
-
4
3
  def exists?(name)
5
- result = client.template_get({'filter' => {'name' => name}})
6
- if (result == nil || result.empty?)
4
+ result = client.template_get('filter' => { 'name' => name })
5
+ if result.nil? || result.empty?
7
6
  false
8
7
  else
9
8
  true
@@ -15,15 +14,15 @@ class Templates < Base
15
14
  end
16
15
 
17
16
  def get_id(name)
18
- if(exists?(name))
19
- client.template_get({'filter' => {'name' => name}}).first['templateid']
17
+ if exists?(name)
18
+ client.template_get('filter' => { 'name' => name }).first['templateid']
20
19
  else
21
20
  raise NonExistingTemplate, "Template #{name} does not exist !"
22
21
  end
23
22
  end
24
23
 
25
24
  def get_templates_for_host(id)
26
- client.template_get({'hostids' => [id]}).map { |result_set| result_set['templateid'] }
25
+ client.template_get('hostids' => [id]).map { |result_set| result_set['templateid'] }
27
26
  end
28
27
 
29
28
  class NonExistingTemplate < StandardError; end
@@ -1,22 +1,35 @@
1
1
  require_relative 'base'
2
2
  class Triggers < Base
3
-
4
3
  def create(options)
5
- client.trigger_create(options)
4
+ client.trigger_create(options) unless exists?(options)
6
5
  end
7
6
 
8
7
  def delete(*trigger_ids)
9
8
  client.trigger_delete(trigger_ids)
10
9
  end
11
10
 
12
- def get_id(options)
13
- result = client.trigger_get({'output' => 'extend',
14
- 'expandExpression' => true})
11
+ # this is very unefficient
12
+ # but there is no other way to verify that a trigger exists..
13
+ def exists?(options)
14
+ result = client.trigger_get('output' => 'extend',
15
+ 'expandExpression' => true)
16
+
15
17
  id = extract_id(result, options['expression'])
16
- unless id.nil?
17
- id
18
+ if id.nil?
19
+ false
18
20
  else
21
+ true
22
+ end
23
+ end
24
+
25
+ def get_id(options)
26
+ result = client.trigger_get('output' => 'extend',
27
+ 'expandExpression' => true)
28
+ id = extract_id(result, options['expression'])
29
+ if id.nil?
19
30
  raise NonExistingTrigger, "Trigger with expression #{options['expression']} not found."
31
+ else
32
+ id
20
33
  end
21
34
  end
22
35
 
@@ -34,7 +47,4 @@ class Triggers < Base
34
47
  end
35
48
  result
36
49
  end
37
-
38
50
  end
39
-
40
-
@@ -1,15 +1,14 @@
1
1
  require_relative 'base'
2
2
 
3
3
  class Usergroups < Base
4
-
5
4
  def create(options)
6
5
  client.usergroup_create(options) unless exists?(options)
7
6
  end
8
7
 
9
8
  def exists?(options)
10
- #client.usergroup_exists(options)
11
- result = client.usergroup_get({'filter' => {'name' => options['name']}})
12
- if result.empty? || result == nil
9
+ # client.usergroup_exists(options)
10
+ result = client.usergroup_get('filter' => { 'name' => options['name'] })
11
+ if result.empty? || result.nil?
13
12
  false
14
13
  else
15
14
  true
@@ -17,9 +16,8 @@ class Usergroups < Base
17
16
  end
18
17
 
19
18
  def get_id(options)
20
- if(exists?(options))
21
- result = client.usergroup_get({
22
- 'filter' => {'name' => options['name']}})
19
+ if exists?(options)
20
+ result = client.usergroup_get('filter' => { 'name' => options['name'] })
23
21
  result.first['usrgrpid']
24
22
  else
25
23
  raise NonExistingUsergroup, "Usergroup #{options['name']} does not exist !"
@@ -31,5 +29,4 @@ class Usergroups < Base
31
29
  end
32
30
 
33
31
  class NonExistingUsergroup < StandardError; end
34
-
35
32
  end
@@ -6,13 +6,13 @@ class Users < Base
6
6
  end
7
7
 
8
8
  def exists?(options)
9
- result = client.user_get({'filter' => {'alias' => options['alias']}})
9
+ result = client.user_get('filter' => { 'alias' => options['alias'] })
10
10
  result.empty? ? false : true
11
11
  end
12
12
 
13
13
  def get_id(options)
14
- if(exists?(options))
15
- client.user_get({'filter' => {'alias' => options['alias']}}).first['userid']
14
+ if exists?(options)
15
+ client.user_get('filter' => { 'alias' => options['alias'] }).first['userid']
16
16
  else
17
17
  raise NonExistingUser, "User #{options['alias']} does not exist !"
18
18
  end
@@ -24,5 +24,3 @@ class Users < Base
24
24
 
25
25
  class NonExistingUser < StandardError; end
26
26
  end
27
-
28
-
data/lib/zapix/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Zapix
2
- VERSION = "0.2.2"
2
+ VERSION = '0.2.3'.freeze
3
3
  end
@@ -1,13 +1,12 @@
1
1
  class Host
2
-
3
2
  attr_accessor :group_ids, :template_ids, :interfaces, :macros
4
3
 
5
- def initialize()
6
- @group_ids = Array.new
7
- @template_ids = Array.new
8
- @interfaces = Array.new
9
- @macros = Array.new
10
- @properties = Hash.new
4
+ def initialize
5
+ @group_ids = []
6
+ @template_ids = []
7
+ @interfaces = []
8
+ @macros = []
9
+ @properties = {}
11
10
  end
12
11
 
13
12
  def add_name(name)
@@ -20,7 +19,7 @@ class Host
20
19
 
21
20
  def add_group_ids(*ids)
22
21
  ids.each do |id|
23
- group_ids << {'groupid' => id}
22
+ group_ids << { 'groupid' => id }
24
23
  end
25
24
  @properties.merge!('groups' => group_ids)
26
25
  end
@@ -36,7 +35,7 @@ class Host
36
35
 
37
36
  def add_template_ids(*ids)
38
37
  ids.each do |id|
39
- template_ids << {'templateid' => id}
38
+ template_ids << { 'templateid' => id }
40
39
  end
41
40
  @properties.merge!('templates' => template_ids)
42
41
  end
@@ -49,7 +48,4 @@ class Host
49
48
  def to_hash
50
49
  @properties
51
50
  end
52
-
53
51
  end
54
-
55
-
@@ -10,9 +10,9 @@ class Interface
10
10
  # https://www.zabbix.com/documentation/2.0/manual/appendix/api/hostinterface/definitions#host_interface
11
11
  # we assume ip and dns shall always be set
12
12
 
13
- validates_inclusion_of :type, :in => 1..4
14
- validates_inclusion_of :main, :in => 0..1
15
- validates_inclusion_of :useip, :in => 0..1
13
+ validates_inclusion_of :type, in: 1..4
14
+ validates_inclusion_of :main, in: 0..1
15
+ validates_inclusion_of :useip, in: 0..1
16
16
 
17
17
  def initialize(attributes)
18
18
  @type = attributes['type'] ||= 1
@@ -20,7 +20,7 @@ class Interface
20
20
  @useip = attributes['useip'] ||= 1
21
21
  @ip = attributes['ip'] = attributes['ip']
22
22
  @dns = attributes['dns'] = attributes['dns']
23
- @port = attributes['port'] = attributes['port'] ||= 10050
23
+ @port = attributes['port'] = attributes['port'] ||= 10_050
24
24
  @result = {
25
25
  'type' => type,
26
26
  'main' => main,
@@ -38,5 +38,4 @@ class Interface
38
38
  def to_hash
39
39
  @result
40
40
  end
41
-
42
41
  end
@@ -1,9 +1,8 @@
1
1
  require 'net/http'
2
2
  require 'uri'
3
3
  require 'json'
4
-
5
- class ZabbixRPCClient
6
4
 
5
+ class ZabbixRPCClient
7
6
  attr_reader :uri, :debug
8
7
 
9
8
  def initialize(options)
@@ -13,7 +12,7 @@ class ZabbixRPCClient
13
12
  @debug = options[:debug]
14
13
  @auth_token = authenticate
15
14
  end
16
-
15
+
17
16
  def method_missing(name, *args)
18
17
  method_name = map_name(name)
19
18
 
@@ -22,17 +21,17 @@ class ZabbixRPCClient
22
21
  'params' => args.first,
23
22
  'id' => id,
24
23
  'jsonrpc' => '2.0',
25
- 'auth' => @auth_token
24
+ 'auth' => @auth_token
26
25
  }.to_json
27
26
 
28
- resp = JSON.parse( http_post_request(post_body) )
27
+ resp = JSON.parse(http_post_request(post_body))
29
28
  raise JSONRPCError, resp['error'] if resp['error']
30
29
  puts "[DEBUG] Get answer: #{resp}" if debug
31
- resp["result"]
30
+ resp['result']
32
31
  end
33
-
32
+
34
33
  def http_post_request(post_body)
35
- http = Net::HTTP.new(uri.host, uri.port)
34
+ http = Net::HTTP.new(uri.host, uri.port)
36
35
  http.use_ssl = true
37
36
  request = Net::HTTP::Post.new(uri.request_uri)
38
37
  request.content_type = 'application/json'
@@ -42,16 +41,16 @@ class ZabbixRPCClient
42
41
  end
43
42
 
44
43
  def authenticate
45
- p user_login({'user' => @username, 'password' => @password})
46
- user_login({'user' => @username, 'password' => @password})
44
+ p user_login('user' => @username, 'password' => @password)
45
+ user_login('user' => @username, 'password' => @password)
47
46
  end
48
47
 
49
48
  def id
50
- rand(100000)
49
+ rand(100_000)
51
50
  end
52
51
 
53
52
  def map_name(name)
54
- name.to_s.sub('_', '.')
53
+ name.to_s.sub('_', '.')
55
54
  end
56
55
 
57
56
  class JSONRPCError < RuntimeError; end
data/lib/zapix.rb CHANGED
@@ -69,5 +69,4 @@ class ZabbixAPI
69
69
  def graphs
70
70
  @graphs ||= Graphs.new(client)
71
71
  end
72
-
73
72
  end
data/spec/spec_helper.rb CHANGED
@@ -6,7 +6,6 @@
6
6
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
7
  require 'zapix'
8
8
  RSpec.configure do |config|
9
-
10
9
  config.treat_symbols_as_metadata_keys_with_true_values = true
11
10
  config.run_all_when_everything_filtered = true
12
11
  config.filter_run :focus
data/spec/zapix_specs.rb CHANGED
@@ -1,11 +1,11 @@
1
1
  require_relative 'spec_helper'
2
2
 
3
3
  zrc = ZabbixAPI.connect(
4
- :service_url => ENV['ZABBIX_API_URL'],
5
- :username => ENV['ZABBIX_API_LOGIN'],
6
- :password => ENV['ZABBIX_API_PASSWORD'],
7
- :debug => true
8
- )
4
+ service_url: ENV['ZABBIX_API_URL'],
5
+ username: ENV['ZABBIX_API_LOGIN'],
6
+ password: ENV['ZABBIX_API_PASSWORD'],
7
+ debug: true
8
+ )
9
9
 
10
10
  hostgroup = 'hostgroup123'
11
11
  another_hostgroup = 'anotherhostgroup'
@@ -17,8 +17,8 @@ application = 'web scenarios'
17
17
  host = 'hostname'
18
18
  scenario = 'scenario'
19
19
  trigger_description = 'Webpage failed on {HOST.NAME}'
20
- #trigger_expression = "{#{host}:web.test.fail[#{scenario}].max(#3)}#0"
21
- trigger_expression = "{#{host}:system.cpu.load[percpu,avg1].last()}>5"
20
+ # trigger_expression = "{#{host}:web.test.fail[#{scenario}].max(#3)}#0"
21
+ trigger_expression = "{#{host}:system.cpu.load[percpu,avg1].last()}>5"
22
22
  non_existing_trigger_expression = '{vfs.file.cksum[/etc/passwd].diff(0)}>0'
23
23
  existing_action_name = 'Report problems to Zabbix administrators'
24
24
  non_existing_action_name = 'No action defined'
@@ -31,7 +31,6 @@ test_user = 'Jim'
31
31
  test_action = 'Test Action'
32
32
 
33
33
  describe ZabbixAPI do
34
-
35
34
  context 'hostgroup' do
36
35
  before(:all) do
37
36
  zrc.hostgroups.create(hostgroup)
@@ -61,7 +60,7 @@ describe ZabbixAPI do
61
60
 
62
61
  it 'returns hostgroup id' do
63
62
  result = zrc.hostgroups.get_id(hostgroup)
64
- (result.to_i).should >= 0
63
+ result.to_i.should >= 0
65
64
  end
66
65
 
67
66
  it 'throws exception if hostgroup id does not exist' do
@@ -69,7 +68,7 @@ describe ZabbixAPI do
69
68
  end
70
69
 
71
70
  it 'throws exception if someone checks for attached hosts of nonexisting group' do
72
- expect { zrc.hostgroups.any_hosts?('nonexisting') }.to raise_error(HostGroups::NonExistingHostgroup)
71
+ expect { zrc.hostgroups.any_hosts?('nonexisting') }.to raise_error(HostGroups::NonExistingHostgroup)
73
72
  end
74
73
 
75
74
  it 'returns false if a hostgroup has no attached hosts' do
@@ -77,9 +76,8 @@ describe ZabbixAPI do
77
76
  end
78
77
 
79
78
  it 'returns all hostgroups' do
80
- (zrc.hostgroups.get_all).should include(hostgroup, another_hostgroup)
79
+ zrc.hostgroups.get_all.should include(hostgroup, another_hostgroup)
81
80
  end
82
-
83
81
  end
84
82
  context 'complex hostgroup consisting hosts' do
85
83
  before(:each) do
@@ -90,16 +88,15 @@ describe ZabbixAPI do
90
88
  example_host.add_interfaces(create_interface.to_hash)
91
89
  example_host.add_group_ids(hostgroup_id)
92
90
  example_host.add_template_ids(zrc.templates.get_id(template_1), zrc.templates.get_id(template_2))
93
- example_host.add_macros({'macro' => '{$TESTMACRO}', 'value' => 'test123'})
91
+ example_host.add_macros('macro' => '{$TESTMACRO}', 'value' => 'test123')
94
92
  zrc.hosts.create_or_update(example_host.to_hash)
95
93
  end
96
94
 
97
- it 'deletes a hostgroup with attached hosts' do
95
+ it 'deletes a hostgroup with attached hosts' do
98
96
  zrc.hosts.exists?(host).should be true
99
97
  zrc.hostgroups.delete(hostgroup_with_hosts)
100
98
  zrc.hostgroups.exists?(hostgroup_with_hosts).should be false
101
99
  end
102
-
103
100
  end
104
101
 
105
102
  context 'complex hostgroup' do
@@ -109,7 +106,7 @@ describe ZabbixAPI do
109
106
  example_host = Host.new
110
107
  example_host.add_name(host)
111
108
  example_host.add_interfaces(create_interface.to_hash)
112
- example_host.add_macros({'macro' => '{$TESTMACRO}', 'value' => 'test123'})
109
+ example_host.add_macros('macro' => '{$TESTMACRO}', 'value' => 'test123')
113
110
  example_host.add_group_ids(hostgroup_id)
114
111
  example_host.add_template_ids(zrc.templates.get_id(template_1), zrc.templates.get_id(template_2))
115
112
  zrc.hosts.create_or_update(example_host.to_hash)
@@ -125,7 +122,7 @@ describe ZabbixAPI do
125
122
  webcheck_options['hostid'] = zrc.hosts.get_id(host)
126
123
  webcheck_options['name'] = scenario
127
124
  webcheck_options['applicationid'] = zrc.applications.get_id(application_options)
128
- webcheck_options['steps'] = [{'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1}]
125
+ webcheck_options['steps'] = [{ 'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1 }]
129
126
  zrc.scenarios.create(webcheck_options)
130
127
 
131
128
  # creates a trigger
@@ -141,7 +138,6 @@ describe ZabbixAPI do
141
138
  end
142
139
 
143
140
  describe 'hosts' do
144
-
145
141
  it 'returns true if a hostgroup has attached hosts' do
146
142
  zrc.hostgroups.any_hosts?(hostgroup_with_hosts).should be true
147
143
  end
@@ -202,13 +198,13 @@ describe ZabbixAPI do
202
198
  host_id = zrc.hosts.get_id(host)
203
199
  options = {}
204
200
  options['host_id'] = host_id
205
- options['macros'] = [{'macro' => '{$TESTMACRO}', 'value' => 'this is only a test macro'}]
201
+ options['macros'] = [{ 'macro' => '{$TESTMACRO}', 'value' => 'this is only a test macro' }]
206
202
  zrc.hosts.update_macros(options)
207
203
  end
208
204
 
209
205
  it 'creates a template' do
210
206
  template_name = 'Template Tomcat'
211
- options = {'host' => template_name}
207
+ options = { 'host' => template_name }
212
208
  options['groups'] = zrc.hostgroups.get_id(templates_hostgroup)
213
209
  zrc.templates.create(options)
214
210
  zrc.templates.exists?(template_name).should be true
@@ -235,12 +231,12 @@ describe ZabbixAPI do
235
231
  options['name'] = application
236
232
  options['hostid'] = zrc.hosts.get_id(host)
237
233
  result = zrc.applications.get_id(options)
238
- (result.to_i).should >= 0
234
+ result.to_i.should >= 0
239
235
  end
240
236
 
241
237
  it 'throws exception on non existing application' do
242
238
  options = {}
243
- options['name'] = "nonexisting"
239
+ options['name'] = 'nonexisting'
244
240
  options['hostid'] = zrc.hosts.get_id(host)
245
241
  expect { zrc.applications.get_id(options) }.to raise_error(Applications::NonExistingApplication)
246
242
  end
@@ -256,7 +252,7 @@ describe ZabbixAPI do
256
252
 
257
253
  it 'returns all web scenarios' do
258
254
  options = {}
259
- (zrc.scenarios.get_all).should include(scenario)
255
+ zrc.scenarios.get_all.should include(scenario)
260
256
  end
261
257
 
262
258
  it 'gets the id of a web scenario' do
@@ -265,12 +261,12 @@ describe ZabbixAPI do
265
261
  options['hostid'] = zrc.hosts.get_id(host)
266
262
  zrc.scenarios.exists?(options).should be true
267
263
  result = zrc.scenarios.get_id(options)
268
- (result['result'].to_i).should >= 0
264
+ result['result'].to_i.should >= 0
269
265
  end
270
266
 
271
267
  it 'returns false if a web scenario does not exist' do
272
268
  options = {}
273
- options['name'] = "nonexisting"
269
+ options['name'] = 'nonexisting'
274
270
  options['hostid'] = zrc.hosts.get_id(host)
275
271
  zrc.scenarios.exists?(options).should be false
276
272
  end
@@ -320,7 +316,7 @@ describe ZabbixAPI do
320
316
  it 'check if interface exists for host' do
321
317
  options = {}
322
318
  options['hostid'] = zrc.hosts.get_id(host)
323
- options['port'] = 10050
319
+ options['port'] = 10_050
324
320
  options['type'] = 1
325
321
  zrc.hostinterfaces.exists?(options).should be true
326
322
 
@@ -355,12 +351,12 @@ describe ZabbixAPI do
355
351
  'operator' => 0, # =
356
352
  'value' => zrc.hostgroups.get_id('Templates')
357
353
  },
358
- # not in maintenance
359
- {
360
- 'conditiontype' => 16, # Maintenance
361
- 'operator' => 7, # not in
362
- 'value' => 'maintenance'
363
- }]
354
+ # not in maintenance
355
+ {
356
+ 'conditiontype' => 16, # Maintenance
357
+ 'operator' => 7, # not in
358
+ 'value' => 'maintenance'
359
+ }]
364
360
  options['operations'] = [{
365
361
  'operationtype' => 0,
366
362
  'esc_period' => 0,
@@ -397,7 +393,7 @@ describe ZabbixAPI do
397
393
  options = {}
398
394
  options['name'] = test_action
399
395
  result = zrc.actions.get_id(options)
400
- (result.to_i).should >= 0
396
+ result.to_i.should >= 0
401
397
  end
402
398
  end
403
399
 
@@ -431,7 +427,7 @@ describe ZabbixAPI do
431
427
  options = {}
432
428
  options['name'] = test_usergroup
433
429
  result = zrc.usergroups.get_id(options)
434
- (result.to_i).should >= 0
430
+ result.to_i.should >= 0
435
431
  options['name'] = non_existing_usergroup
436
432
  expect { zrc.usergroups.get_id(options) }.to raise_error(Usergroups::NonExistingUsergroup)
437
433
  end
@@ -478,18 +474,18 @@ describe ZabbixAPI do
478
474
  options = {}
479
475
  options['alias'] = test_user
480
476
  result = zrc.users.get_id(options)
481
- (result.to_i).should >= 0
477
+ result.to_i.should >= 0
482
478
  options['alias'] = non_existing_user
483
479
  expect { zrc.users.get_id(options) }.to raise_error(Users::NonExistingUser)
484
480
  end
485
-
486
481
  end
487
482
  end
488
483
 
489
484
  def create_interface
490
485
  Interface.new(
491
486
  'ip' => random_local_ip,
492
- 'dns' => random_domain)
487
+ 'dns' => random_domain
488
+ )
493
489
  end
494
490
 
495
491
  def create_jmx_interface
@@ -498,7 +494,8 @@ describe ZabbixAPI do
498
494
  'dns' => random_domain,
499
495
  'type' => 4, # JMX
500
496
  'main' => 1, # default jmx interface
501
- 'port' => 9003)
497
+ 'port' => 9003
498
+ )
502
499
  end
503
500
 
504
501
  def random_string
@@ -516,5 +513,4 @@ describe ZabbixAPI do
516
513
  def random_domain
517
514
  "#{random_string}.our-cloud.de"
518
515
  end
519
-
520
516
  end
data/zapix.gemspec CHANGED
@@ -4,24 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'zapix/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "zapix3"
7
+ spec.name = 'zapix3'
8
8
  spec.version = Zapix::VERSION
9
- spec.authors = ["stoyan"]
10
- spec.email = ["stoyanoff.s@gmail.com"]
11
- spec.description = %q{Communication with the Zabbix API made easy. This version is compatible with zabbix 3.0}
12
- spec.summary = %q{A cool gem}
13
- spec.homepage = "https://github.com/mrsn/zapix3"
14
- spec.license = "MIT"
9
+ spec.authors = ['stoyan']
10
+ spec.email = ['stoyanoff.s@gmail.com']
11
+ spec.description = 'Communication with the Zabbix API made easy. This version is compatible with zabbix 3.0'
12
+ spec.summary = 'A cool gem'
13
+ spec.homepage = 'https://github.com/mrsn/zapix3'
14
+ spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "json"
24
- spec.add_development_dependency "rspec"
25
- spec.add_development_dependency "activerecord"
19
+ spec.require_paths = ['lib']
26
20
 
21
+ spec.add_development_dependency 'bundler', '~> 1.3'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'json'
24
+ spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'activerecord'
27
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zapix3
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - stoyan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-14 00:00:00.000000000 Z
11
+ date: 2016-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler