zabbix_manager 5.0.4 → 5.0.7

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 +76 -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 +52 -50
  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 +41 -37
  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: 034a6a763df4c8337459dafae939113d20d243bb3a03edca7c53cb8f7e4735f5
4
- data.tar.gz: 5cc9e8342583fff642f3e8190b4f92329e52076b94cc182e899bdea36ca1dda4
3
+ metadata.gz: a06ad87ced47aa4288d704ff5343ebeda605d34f7d6944459cecf4f69711d38d
4
+ data.tar.gz: 2e5e50ddf70f312b4c679d590d6a8aecbc6031764faf3f89e475c36958040772
5
5
  SHA512:
6
- metadata.gz: c70af5c6c152c2a6673fd4580038180525c88d0eb40589bf4708894fc0cb7a73e9b59daf8de101823739cceceb799cea82f87e6bc6f787457e80196e68486a28
7
- data.tar.gz: 2ea559104053fd3b3bd5b9e6c7026062e80108dbf5861ff17ac4c0a316655be22850f082683a8292992b63afbf036ab5aa224dbd8bb85b6057bd324a140475d9
6
+ metadata.gz: c6801af1c5372993075c7791ad45167c5b8a2f15a639861c7418e66d1ada84aee0e7c5bb7cb87369fcb662e93089d5bacee30623f7c110fee06f4bd95108ad48
7
+ data.tar.gz: 55c96cd371fa3a682ce39a421683606e60f444a1d2e1db60c3525e86bce946614757fad310dacaa9e21631e85524346e9328eec5a82f65779386b5ea8dfc9a63
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,43 +1,98 @@
1
- # ZabbixManager
1
+ # Ruby Zabbix Api Module
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/zabbix_manager`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](http://img.shields.io/gem/v/zabbix_manager.svg)][gem]
4
+ [![Build Status](https://github.com/anapsix/zabbix_manager/workflows/CI/badge.svg)][github-ci]
4
5
 
5
- TODO: Delete this and the text above, and describe your gem
6
+ [gem]: https://rubygems.org/gems/zabbix_manager
7
+ [github-ci]: https://github.com/express42/zabbix_manager/actions?query=workflow%3ACI
6
8
 
7
- ## Installation
9
+ Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Zabbix API][Zabbix API]
8
10
 
9
- Add this line to your application's Gemfile:
11
+ ## Installation
12
+ ```sh
13
+ # latest
14
+ gem install zabbix_manager
10
15
 
11
- ```ruby
12
- gem 'zabbix_manager'
16
+ # specific version
17
+ gem install zabbix_manager -v 4.2.0
13
18
  ```
14
19
 
15
- And then execute:
20
+ ## Documentation
21
+ [http://rdoc.info/gems/zabbix_manager][documentation]
22
+
23
+ [documentation]: http://rdoc.info/gems/zabbix_manager
24
+
25
+ ## Examples
26
+ [https://github.com/express42/zabbix_manager/tree/master/examples][examples]
27
+
28
+ [examples]: https://github.com/express42/zabbix_manager/tree/master/examples
29
+
30
+ ## Version Policy
16
31
 
17
- $ bundle install
32
+ **NOTE:** `master` branch is used for ongoing development on Zabbix API 5.x (5.0 and 5.2).
18
33
 
19
- Or install it yourself as:
34
+ We support only two last versions of zabbix (5.0 and 5.2), so you should consider all previous versions deprecated.
20
35
 
21
- $ gem install zabbix_manager
36
+ * Zabbix 1.8.2 (api version 1.2) | zabbix_manager 0.6.x | [branch zabbix1.8](https://github.com/express42/zabbix_manager/tree/zabbix1.8)
37
+ * Zabbix 1.8.9 (api version 1.3) | zabbix_manager 0.6.x | [branch zabbix1.8](https://github.com/express42/zabbix_manager/tree/zabbix1.8)
38
+ * Zabbix 2.0.x (api version 1.4 -> 2.0.10) | zabbix_manager 2.0.x | [branch zabbix2.0](https://github.com/express42/zabbix_manager/tree/zabbix2.0)
39
+ * Zabbix 2.2.x (api version 2.2.x) | zabbix_manager 2.2.x | [branch zabbix2.2](https://github.com/express42/zabbix_manager/tree/zabbix2.2)
40
+ * Zabbix 2.4.x (api version 2.2.x) | zabbix_manager 2.4.x | [branch zabbix2.4](https://github.com/express42/zabbix_manager/tree/zabbix2.4)
41
+ * Zabbix 3.0.x (api version 3.0.x) | zabbix_manager 3.0.x | [branch zabbix3.0](https://github.com/express42/zabbix_manager/tree/zabbix3.0)
42
+ * Zabbix 3.2.x (api version 3.2.x) | zabbix_manager 3.2.x | [branch zabbix3.2](https://github.com/express42/zabbix_manager/tree/zabbix3.2)
43
+ * Zabbix 4.0.x (api version 4.0.x) | zabbix_manager 4.1.x | [branch zabbix4.0](https://github.com/express42/zabbix_manager/tree/zabbix4.0)
44
+ * Zabbix 4.2.x (api version 4.2.x) | zabbix_manager 4.1.x | [branch zabbix4.0](https://github.com/express42/zabbix_manager/tree/zabbix4.0)
45
+ * Zabbix 4.4.x (api version 4.4.x) | zabbix_manager 4.2.x | [branch zabbix4.2](https://github.com/express42/zabbix_manager/tree/zabbix4.2)
22
46
 
23
- ## Usage
47
+ ## Supported Ruby Versions
48
+ This library aims to support and is [tested against][github-ci] the following Ruby
49
+ versions:
24
50
 
25
- TODO: Write usage instructions here
51
+ * Ruby 2.5
52
+ * Ruby 2.6
53
+ * Ruby 2.7
54
+ * JRuby 9.2.10.0
26
55
 
27
- ## Development
56
+ If something doesn't work on one of these versions, it's a bug.
28
57
 
29
- 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.
58
+ This library may inadvertently work (or seem to work) on other Ruby versions,
59
+ however support will only be provided for the versions listed above.
30
60
 
31
- 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).
61
+ If you would like this library to support another Ruby version or
62
+ implementation, you may volunteer to be a maintainer. Being a maintainer
63
+ entails making sure all tests run and pass on that implementation. When
64
+ something breaks on your implementation, you will be responsible for providing
65
+ patches in a timely fashion. If critical issues for a particular implementation
66
+ exist at the time of a major release, support for that Ruby version may be
67
+ dropped.
68
+
69
+ ## Dependencies
70
+
71
+ * net/http
72
+ * json
32
73
 
33
74
  ## Contributing
34
75
 
35
- 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).
76
+ * Fork the project.
77
+ * Base your work on the master branch.
78
+ * Make your feature addition or bug fix, write tests, write documentation/examples.
79
+ * Commit, do not mess with rakefile, version.
80
+ * Make a pull request.
81
+
82
+ ## Zabbix documentation
83
+
84
+ * [Zabbix Project Homepage][Zabbix]
85
+ * [Zabbix API docs][Zabbix API]
86
+
87
+ [Zabbix]: https://www.zabbix.com
88
+ [Zabbix API]: https://www.zabbix.com/documentation/5.2/manual/api
36
89
 
37
- ## License
90
+ ## Copyright
38
91
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
92
+ - Copyright (c) 2021 [contributors]
93
+ - Copyright (c) 2015-2018 Express 42 and [contributors]
40
94
 
41
- ## Code of Conduct
95
+ See [LICENSE] for details.
42
96
 
43
- 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/[USERNAME]/zabbix_manager/blob/master/CODE_OF_CONDUCT.md).
97
+ [LICENSE]: LICENSE.md
98
+ [contributors]: https://github.com/express42/zabbix_manager/graphs/contributors
@@ -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