zabbix_manager 5.0.5 → 5.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +2 -4
  3. data/LICENSE.txt +1 -1
  4. data/README.md +48 -21
  5. data/lib/zabbix_manager/basic/basic_alias.rb +2 -0
  6. data/lib/zabbix_manager/basic/basic_extend.rb +11 -9
  7. data/lib/zabbix_manager/basic/basic_func.rb +9 -5
  8. data/lib/zabbix_manager/basic/basic_init.rb +6 -4
  9. data/lib/zabbix_manager/basic/basic_logic.rb +7 -5
  10. data/lib/zabbix_manager/classes/actions.rb +6 -4
  11. data/lib/zabbix_manager/classes/applications.rb +4 -2
  12. data/lib/zabbix_manager/classes/configurations.rb +6 -4
  13. data/lib/zabbix_manager/classes/drules.rb +5 -3
  14. data/lib/zabbix_manager/classes/errors.rb +10 -9
  15. data/lib/zabbix_manager/classes/events.rb +4 -3
  16. data/lib/zabbix_manager/classes/graphs.rb +13 -11
  17. data/lib/zabbix_manager/classes/hostgroups.rb +13 -11
  18. data/lib/zabbix_manager/classes/hosts.rb +36 -34
  19. data/lib/zabbix_manager/classes/httptests.rb +4 -2
  20. data/lib/zabbix_manager/classes/items.rb +34 -28
  21. data/lib/zabbix_manager/classes/maintenance.rb +4 -2
  22. data/lib/zabbix_manager/classes/mediatypes.rb +15 -13
  23. data/lib/zabbix_manager/classes/problems.rb +31 -30
  24. data/lib/zabbix_manager/classes/proxies.rb +9 -7
  25. data/lib/zabbix_manager/classes/roles.rb +19 -17
  26. data/lib/zabbix_manager/classes/screens.rb +6 -4
  27. data/lib/zabbix_manager/classes/scripts.rb +6 -4
  28. data/lib/zabbix_manager/classes/server.rb +3 -1
  29. data/lib/zabbix_manager/classes/templates.rb +18 -16
  30. data/lib/zabbix_manager/classes/triggers.rb +35 -17
  31. data/lib/zabbix_manager/classes/unusable.rb +2 -0
  32. data/lib/zabbix_manager/classes/usergroups.rb +11 -9
  33. data/lib/zabbix_manager/classes/usermacros.rb +38 -37
  34. data/lib/zabbix_manager/classes/users.rb +11 -9
  35. data/lib/zabbix_manager/classes/valuemaps.rb +5 -3
  36. data/lib/zabbix_manager/client.rb +40 -30
  37. data/lib/zabbix_manager/version.rb +3 -1
  38. data/lib/zabbix_manager.rb +36 -35
  39. data/zabbix_manager.gemspec +43 -0
  40. metadata +143 -19
  41. data/.idea/workspace.xml +0 -69
  42. data/.rubocop.yml +0 -13
  43. data/CODE_OF_CONDUCT.md +0 -84
  44. data/Gemfile +0 -10
  45. data/Rakefile +0 -8
  46. data/bin/console +0 -15
  47. data/bin/setup +0 -8
  48. data/zabbix_manager-5.0.1.gem +0 -0
  49. data/zabbix_manager-5.0.2.gem +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2dd53cab35de67ecf4197410f9122102aaff9509c0860532fb751fb8d555d3f
4
- data.tar.gz: 9c3ffb719aabcbfc0db37dfd1f0932cb9478d59e7c5b100493894d090b26a897
3
+ metadata.gz: aa8b6fa1927ad9ff3012369cc92e302a7ef67c83e1dd8dcff2773257c10f9def
4
+ data.tar.gz: bcc2663a1d885d3e7632fc42eeb756de6aab34df75c289399f6464176fc6745b
5
5
  SHA512:
6
- metadata.gz: cfd62dce5eb71f75b04a2ffd6c115fa865185f29dd4a89b165beef09089be108c0d95e633457216e11d114e0b20cbd4e8cb5241c347adb78826a4f00047fe4c2
7
- data.tar.gz: dbc6eb6f1788dfb8e07c0f6816cea0ec535ee1a04b379c189dc5920b1b7d895ab7eb9ac9c4344a33b752516ec1778c7938336f7de9a12efe6d1eb088b3fc5b27
6
+ metadata.gz: 7fd2a1c9d746706fa2959a8734bd258b862b43db5c1c6e240a80b25e2ec16c0d09eb1b9ad60dbb8b4ddea4b9ae310d026ae9b8f2cd331abbc94b04244a63420d
7
+ data.tar.gz: 62f0b3557a65b5fdf1ff2e2ccd1c3406febc9190ad4ecca052b7f9dc4227d8ac98f371aacf36cb72f62e2af52506e3be9b74f40d15cf6665d88f712b073d4a7c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,3 @@
1
- ## [Unreleased]
1
+ #CHANGELOG
2
2
 
3
- ## [0.1.0] - 2021-12-17
4
-
5
- - Initial release
3
+ ### 5.0.7
data/LICENSE.txt CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2021 TODO: Write your name
3
+ Copyright (c) 2022 TODO: WENWU.YAN
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
data/README.md CHANGED
@@ -1,41 +1,68 @@
1
- # ZabbixManager
1
+ # Ruby Zabbix Api Module
2
2
 
3
- most codes are borrowed from zabbixapi, but changed some logic to fit everyday job wells. now support ZABBIX 4.0, 5.0, 6.0
3
+ [![Gem Version](http://img.shields.io/gem/v/zabbix_manager.svg)][gem]
4
4
 
5
- ## Installation
5
+ [gem]: https://rubygems.org/gems/zabbix_manager
6
+
7
+ Most codes borrowed from zabbixapi, but fit for my everyday works well!
8
+ Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Zabbix API][Zabbix API]
6
9
 
7
- Add this line to your application's Gemfile:
10
+ ## Installation
11
+ ```sh
12
+ # latest
13
+ gem install zabbix_manager
8
14
 
9
- ```ruby
10
- gem 'zabbix_manager'
15
+ # specific version
16
+ gem install zabbix_manager -v 4.2.0
11
17
  ```
12
18
 
13
- And then execute:
19
+ ## Documentation
20
+ [http://rdoc.info/gems/zabbix_manager][documentation]
14
21
 
15
- $ bundle install
22
+ [documentation]: http://rdoc.info/gems/zabbix_manager
16
23
 
17
- Or install it yourself as:
24
+ ## Examples
18
25
 
19
- $ gem install zabbix_manager
20
26
 
21
- ## Usage
27
+ ## Supported Ruby Versions
28
+ This library aims to support and is [tested against][github-ci] the following Ruby
29
+ versions:
22
30
 
23
- TODO: Write usage instructions here
31
+ * Ruby 2.5
32
+ * Ruby 2.6
33
+ * Ruby 2.7
34
+ * JRuby 9.2.10.0
24
35
 
25
- ## Development
36
+ If something doesn't work on one of these versions, it's a bug.
26
37
 
27
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+ This library may inadvertently work (or seem to work) on other Ruby versions,
39
+ however support will only be provided for the versions listed above.
28
40
 
29
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
41
+ If you would like this library to support another Ruby version or
42
+ implementation, you may volunteer to be a maintainer. Being a maintainer
43
+ entails making sure all tests run and pass on that implementation. When
44
+ something breaks on your implementation, you will be responsible for providing
45
+ patches in a timely fashion. If critical issues for a particular implementation
46
+ exist at the time of a major release, support for that Ruby version may be
47
+ dropped.
30
48
 
31
- ## Contributing
49
+ ## Dependencies
50
+
51
+ * net/http
52
+ * json
32
53
 
33
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/zabbix_manager. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/zabbix_manager/blob/master/CODE_OF_CONDUCT.md).
54
+ ## Contributing
34
55
 
35
- ## License
56
+ * Fork the project.
57
+ * Base your work on the master branch.
58
+ * Make your feature addition or bug fix, write tests, write documentation/examples.
59
+ * Commit, do not mess with rakefile, version.
60
+ * Make a pull request.
36
61
 
37
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+ ## Zabbix documentation
38
63
 
39
- ## Code of Conduct
64
+ * [Zabbix Project Homepage][Zabbix]
65
+ * [Zabbix API docs][Zabbix API]
40
66
 
41
- Everyone interacting in the ZabbixManager project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/ciscolive/zabbix_manager/blob/master/CODE_OF_CONDUCT.md).
67
+ [Zabbix]: https://www.zabbix.com
68
+ [Zabbix API]: https://www.zabbix.com/documentation/5.2/manual/api
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Basic
3
5
  # Get Zabbix object data from API by id
@@ -1,16 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Basic
3
5
  # 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
4
6
  def get_hostgroup_ids(data)
5
7
  result = @client.api_request(
6
- method: 'hostgroup.get',
8
+ method: "hostgroup.get",
7
9
  params: {
8
10
  output: "extend",
9
11
  filter: {
10
12
  name: [data].flatten
11
13
  }
12
14
  }
13
- ).map { |item| { groupid: item['groupid'] } }
15
+ ).map { |item| { groupid: item["groupid"] } }
14
16
 
15
17
  # 检查是是否存在
16
18
  result.empty? ? nil : result.flatten
@@ -20,19 +22,19 @@ class ZabbixManager
20
22
  def get_or_create_hostgroups(data)
21
23
  [data].flatten.each do |item|
22
24
  # 是否存在设备属组,不存在则新建
23
- begin
24
- result = get_hostgroup_ids(item)
25
+
26
+ result = get_hostgroup_ids(item)
27
+ if result.nil?
25
28
  @client.api_request(
26
- method: 'hostgroup.create',
29
+ method: "hostgroup.create",
27
30
  params: {
28
31
  name: item
29
32
  }
30
- ) if result.nil?
31
- rescue => e
32
- ap e
33
+ )
33
34
  end
35
+ rescue StandardError => e
36
+ ap e
34
37
  end
35
38
  end
36
-
37
39
  end
38
40
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Basic
3
5
  # Log messages to stdout when debugging
@@ -26,12 +28,13 @@ class ZabbixManager
26
28
  # @param object [Array, Hash]
27
29
  # @return [Array, Hash]
28
30
  def symbolize_keys(object)
29
- if object.is_a?(Array)
31
+ case object
32
+ when Array
30
33
  object.each_with_index do |val, index|
31
34
  object[index] = symbolize_keys(val)
32
35
  end
33
- elsif object.is_a?(Hash)
34
- object.keys.each do |key|
36
+ when Hash
37
+ object.each_key do |key|
35
38
  object[key.to_sym] = symbolize_keys(object.delete(key))
36
39
  end
37
40
  end
@@ -62,9 +65,10 @@ class ZabbixManager
62
65
  result = []
63
66
 
64
67
  array.each do |e|
65
- if e.is_a?(Array)
68
+ case e
69
+ when Array
66
70
  result.push(normalize_array(e))
67
- elsif e.is_a?(Hash)
71
+ when Hash
68
72
  result.push(normalize_hash(e))
69
73
  else
70
74
  result.push(e.to_s)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Basic
3
5
  # Initializes a new Basic object with ZabbixManager Client
@@ -12,7 +14,7 @@ class ZabbixManager
12
14
  #
13
15
  # @raise [ManagerError] Basic object does not directly support method_name
14
16
  def method_name
15
- raise ManagerError.new("Can't call method_name here")
17
+ raise ManagerError, "Can't call method_name here"
16
18
  end
17
19
 
18
20
  # Placeholder for inherited objects to provide default options
@@ -26,21 +28,21 @@ class ZabbixManager
26
28
  #
27
29
  # @return [String]
28
30
  def keys
29
- key + 's'
31
+ "#{key}s"
30
32
  end
31
33
 
32
34
  # Returns the object's id field name (identify) based on method_name + id
33
35
  #
34
36
  # @return [String]
35
37
  def key
36
- method_name + 'id'
38
+ "#{method_name}id"
37
39
  end
38
40
 
39
41
  # Placeholder for inherited objects to provide object-specific id field name
40
42
  #
41
43
  # @raise [ManagerError] Basic object does not directly support identify
42
44
  def identify
43
- raise ManagerError.new("Can't call identify here")
45
+ raise ManagerError, "Can't call identify here"
44
46
  end
45
47
  end
46
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Basic
3
5
  # Create new Zabbix object using API (with defaults)
@@ -96,7 +98,7 @@ class ZabbixManager
96
98
  filter: {
97
99
  identify.to_sym => data[identify.to_sym]
98
100
  },
99
- output: 'extend'
101
+ output: "extend"
100
102
  }
101
103
  )
102
104
  end
@@ -131,7 +133,7 @@ class ZabbixManager
131
133
  filter: {
132
134
  key.to_sym => data[key.to_sym]
133
135
  },
134
- output: 'extend'
136
+ output: "extend"
135
137
  }
136
138
  )
137
139
  end
@@ -145,7 +147,7 @@ class ZabbixManager
145
147
  result = {}
146
148
  @client.api_request(
147
149
  method: "#{method_name}.get",
148
- params: { output: 'extend' }
150
+ params: { output: "extend" }
149
151
  ).each do |item|
150
152
  result[item[identify]] = item[key]
151
153
  end
@@ -165,7 +167,7 @@ class ZabbixManager
165
167
  data = symbolize_keys(data) if data.key?(identify)
166
168
  # raise an error if identify name was not supplied
167
169
  name = data[identify.to_sym]
168
- raise ManagerError.new("#{identify} not supplied in call to get_id") if name.nil?
170
+ raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?
169
171
 
170
172
  result = @client.api_request(
171
173
  method: "#{method_name}.get",
@@ -174,7 +176,7 @@ class ZabbixManager
174
176
  output: [key, identify]
175
177
  }
176
178
  )
177
- id = nil
179
+ id = nil
178
180
  result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
179
181
  id
180
182
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Actions < Basic
3
5
  # The method name used for interacting with Actions via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'action'
9
+ "action"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Action objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
18
 
17
19
  # Get full/extended Action object data from API
@@ -29,11 +31,11 @@ class ZabbixManager
29
31
  filter: {
30
32
  identify.to_sym => data[identify.to_sym]
31
33
  },
32
- output: 'extend',
34
+ output: "extend",
33
35
  selectOperations: "extend",
34
36
  selectRecoveryOperations: "extend",
35
37
  selectAcknowledgeOperations: "extend",
36
- selectFilter: "extend",
38
+ selectFilter: "extend"
37
39
  }
38
40
  )
39
41
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Applications < Basic
3
5
  # The method name used for interacting with Applications via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'application'
9
+ "application"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Application objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
18
 
17
19
  # Get or Create Application object using Zabbix API
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Configurations < Basic
3
5
  # @return [Boolean]
@@ -9,14 +11,14 @@ class ZabbixManager
9
11
  #
10
12
  # @return [String]
11
13
  def method_name
12
- 'configuration'
14
+ "configuration"
13
15
  end
14
16
 
15
17
  # The id field name used for identifying specific Configuration objects via Zabbix API
16
18
  #
17
19
  # @return [String]
18
20
  def identify
19
- 'host'
21
+ "host"
20
22
  end
21
23
 
22
24
  # Export configuration data using Zabbix API
@@ -26,7 +28,7 @@ class ZabbixManager
26
28
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
27
29
  # @return [Hash]
28
30
  def export(data)
29
- @client.api_request(method: 'configuration.export', params: data)
31
+ @client.api_request(method: "configuration.export", params: data)
30
32
  end
31
33
 
32
34
  # Import configuration data using Zabbix API
@@ -36,7 +38,7 @@ class ZabbixManager
36
38
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
37
39
  # @return [Hash]
38
40
  def import(data)
39
- @client.api_request(method: 'configuration.import', params: data)
41
+ @client.api_request(method: "configuration.import", params: data)
40
42
  end
41
43
  end
42
44
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Drules < Basic
3
5
  # The method name used for interacting with Drules via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'drule'
9
+ "drule"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Drule objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
18
 
17
19
  # The default options used when creating Drule objects via Zabbix API
@@ -22,7 +24,7 @@ class ZabbixManager
22
24
  name: nil,
23
25
  iprange: nil,
24
26
  delay: 3600,
25
- status: 0,
27
+ status: 0
26
28
  }
27
29
  end
28
30
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class BaseError < RuntimeError
3
5
  attr_accessor :response, :error, :error_message
@@ -9,15 +11,14 @@ class ZabbixManager
9
11
  set_error! if @response
10
12
  end
11
13
 
12
- private
13
-
14
- def set_error!
15
- @error = @response['error']
16
- @error_message = "#{@error['message']}: #{@error['data']}"
17
- rescue StandardError
18
- @error = nil
19
- @error_message = nil
20
- end
14
+ private
15
+ def set_error!
16
+ @error = @response["error"]
17
+ @error_message = "#{@error["message"]}: #{@error["data"]}"
18
+ rescue StandardError
19
+ @error = nil
20
+ @error_message = nil
21
+ end
21
22
  end
22
23
 
23
24
  class ManagerError < BaseError
@@ -1,18 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Events < Basic
3
5
  # The method name used for interacting with Events via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'event'
9
+ "event"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Event objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
-
17
18
  end
18
19
  end
@@ -1,17 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class Graphs < Basic
3
5
  # The method name used for interacting with Graphs via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'graph'
9
+ "graph"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific Graph objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
18
 
17
19
  # Get full/extended Graph data from Zabbix API
@@ -29,7 +31,7 @@ class ZabbixManager
29
31
  search: {
30
32
  identify.to_sym => data[identify.to_sym]
31
33
  },
32
- output: 'extend'
34
+ output: "extend"
33
35
  }
34
36
  )
35
37
  end
@@ -42,22 +44,22 @@ class ZabbixManager
42
44
  # @return [Array] Returns array of Graph ids
43
45
  def get_ids_by_host(data)
44
46
  result = @client.api_request(
45
- method: 'graph.get',
47
+ method: "graph.get",
46
48
  params: {
47
49
  filter: {
48
50
  host: data[:host]
49
51
  },
50
- output: 'extend'
52
+ output: "extend"
51
53
  }
52
54
  )
53
55
 
54
- result.map do |graph|
55
- num = graph['graphid']
56
- name = graph['name']
56
+ result.filter_map do |graph|
57
+ num = graph["graphid"]
58
+ name = graph["name"]
57
59
  filter = data[:filter]
58
60
 
59
61
  num if filter.nil? || /#{filter}/ =~ name
60
- end.compact
62
+ end
61
63
  end
62
64
 
63
65
  # Get Graph Item object using Zabbix API
@@ -68,10 +70,10 @@ class ZabbixManager
68
70
  # @return [Hash]
69
71
  def get_items(data)
70
72
  @client.api_request(
71
- method: 'graphitem.get',
73
+ method: "graphitem.get",
72
74
  params: {
73
75
  graphids: [data],
74
- output: 'extend'
76
+ output: "extend"
75
77
  }
76
78
  )
77
79
  end
@@ -1,37 +1,39 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class ZabbixManager
2
4
  class HostGroups < Basic
3
5
  # The method name used for interacting with HostGroups via Zabbix API
4
6
  #
5
7
  # @return [String]
6
8
  def method_name
7
- 'hostgroup'
9
+ "hostgroup"
8
10
  end
9
11
 
10
12
  # The id field name used for identifying specific HostGroup objects via Zabbix API
11
13
  #
12
14
  # @return [String]
13
15
  def identify
14
- 'name'
16
+ "name"
15
17
  end
16
18
 
17
19
  # The key field name used for HostGroup objects via Zabbix API
18
20
  #
19
21
  # @return [String]
20
22
  def key
21
- 'groupid'
23
+ "groupid"
22
24
  end
23
25
 
24
26
  # 新增 get_hostgroup_ids 方法,使用列表 flatten 功能拉平属组对象
25
27
  def get_hostgroup_ids(data)
26
28
  result = @client.api_request(
27
- method: 'hostgroup.get',
29
+ method: "hostgroup.get",
28
30
  params: {
29
31
  output: "extend",
30
32
  filter: {
31
33
  name: [data].flatten
32
34
  }
33
35
  }
34
- ).map { |item| { groupid: item['groupid'] } }
36
+ ).map { |item| { groupid: item["groupid"] } }
35
37
 
36
38
  # 检查是是否存在
37
39
  result.empty? ? nil : result.flatten
@@ -41,17 +43,17 @@ class ZabbixManager
41
43
  def get_or_create_hostgroups(data)
42
44
  # 是否存在设备属组,不存在则新建
43
45
  [data].flatten.each do |item|
44
- begin
45
- result = get_hostgroup_ids(item)
46
+ result = get_hostgroup_ids(item)
47
+ if result.nil?
46
48
  @client.api_request(
47
- method: 'hostgroup.create',
49
+ method: "hostgroup.create",
48
50
  params: {
49
51
  name: item
50
52
  }
51
- ) if result.nil?
52
- rescue => e
53
- ap e
53
+ )
54
54
  end
55
+ rescue StandardError => e
56
+ ap e
55
57
  end
56
58
  end
57
59
  end