zabbix_manager 5.1.0 → 5.1.1.pre.alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +10 -2
  3. data/{LICENSE.txt → LICENSE} +6 -6
  4. data/README.md +3 -2
  5. data/lib/zabbix_manager/basic/basic_alias.rb +6 -8
  6. data/lib/zabbix_manager/basic/basic_func.rb +17 -18
  7. data/lib/zabbix_manager/basic/basic_init.rb +21 -18
  8. data/lib/zabbix_manager/basic/basic_logic.rb +143 -139
  9. data/lib/zabbix_manager/classes/actions.rb +1 -1
  10. data/lib/zabbix_manager/classes/applications.rb +8 -7
  11. data/lib/zabbix_manager/classes/configurations.rb +2 -2
  12. data/lib/zabbix_manager/classes/drules.rb +6 -6
  13. data/lib/zabbix_manager/classes/errors.rb +1 -1
  14. data/lib/zabbix_manager/classes/graphs.rb +6 -6
  15. data/lib/zabbix_manager/classes/hostgroups.rb +0 -34
  16. data/lib/zabbix_manager/classes/hostinterfaces.rb +26 -0
  17. data/lib/zabbix_manager/classes/hosts.rb +35 -153
  18. data/lib/zabbix_manager/classes/httptests.rb +3 -3
  19. data/lib/zabbix_manager/classes/items.rb +51 -49
  20. data/lib/zabbix_manager/classes/mediatypes.rb +5 -5
  21. data/lib/zabbix_manager/classes/problems.rb +43 -61
  22. data/lib/zabbix_manager/classes/proxies.rb +4 -24
  23. data/lib/zabbix_manager/classes/roles.rb +7 -7
  24. data/lib/zabbix_manager/classes/screens.rb +3 -3
  25. data/lib/zabbix_manager/classes/templates.rb +17 -33
  26. data/lib/zabbix_manager/classes/triggers.rb +31 -73
  27. data/lib/zabbix_manager/classes/usergroups.rb +6 -6
  28. data/lib/zabbix_manager/classes/usermacros.rb +38 -37
  29. data/lib/zabbix_manager/classes/users.rb +4 -4
  30. data/lib/zabbix_manager/classes/valuemaps.rb +8 -7
  31. data/lib/zabbix_manager/client.rb +70 -45
  32. data/lib/zabbix_manager/version.rb +2 -1
  33. data/lib/zabbix_manager.rb +18 -38
  34. data/zabbix_manager.gemspec +7 -11
  35. metadata +22 -66
  36. data/CODE_OF_CONDUCT.md +0 -84
  37. data/lib/zabbix_manager/basic/basic_extend.rb +0 -40
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b6d365a03157b9a76cfb41caeaacef8a27e030a358b5b21e4fc65156de9f5e0
4
- data.tar.gz: 72ca3a28bfa6833545670193d6f4898a49ccdf040cdc9c61ff1c283164ce23e8
3
+ metadata.gz: 7f399a8e709f4dd599a96f7262fd9db1fe45df50517e1dbe6c03fb800802c842
4
+ data.tar.gz: 31bed21203fb1f34d2f334454a8e4bc4eeaba82f0cd15c2991b0b3a1c44bf6b2
5
5
  SHA512:
6
- metadata.gz: f4bee7468445d37efbb38a5988eb11c833295e12574bd6b91d7c310d1e159292e932907eb2c2ba01a68a221507f3380c380ed981c39d41c189b609fd51873a69
7
- data.tar.gz: 8470d3bdb4b3d1dfca93f67cc973972c1389139d93727285fe6f479f82d71c4f5d6ca62d56e3e73b96399b2b2c360665abfa2c51d97031a2792d4cfa6af05272
6
+ metadata.gz: 0bc102949b9b885ef3b66b29af279bab13c81fd3356cccb2a6e3bbd72db107f9bb6edd7fbc30dc9d157bae70de67076678a8f98f053040a7feb6fe81720ab9ad
7
+ data.tar.gz: 6ea4885896f47adace6df1b8c3ce8538a2af28d3cf7fe32a485bc2b285fe20f3e06a72fae6ac56503d59ad694b0861b6eb2cff90c109dfb8a03104c261e833c9
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
- #CHANGELOG
1
+ # CHANGELOG
2
2
 
3
- ### 5.0.7
3
+ ## 5.1.1
4
+ * 新增基类查询方法:
5
+ * get_key_ids_by_identify:基于监控对象索引键(#{identify})查询 { "#{key}": id };
6
+ * get_key_ids:基于监控对象索引建"#{identify}"查询 { "#{key}": id };
7
+ * get_or_create_keys:批量创建或更新监控对象并返回 [{ "#{key}": id }];
8
+ * 优化 Client 对象实例化逻辑:
9
+ * 增加入参检查:必须提供 url、user和password,以及 @id 缓存;
10
+ * 优化 debug 模式,接口请求入参和出参打印,均使用 JSON.pretty_unparse(data) 美化;
11
+ * 完善项目注释,项目注释覆盖率90%;
@@ -1,6 +1,6 @@
1
- The MIT License (MIT)
1
+ MIT License
2
2
 
3
- Copyright (c) 2022 TODO: WENWU.YAN
3
+ Copyright (c) 2023 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
@@ -9,13 +9,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
9
  copies of the Software, and to permit persons to whom the Software is
10
10
  furnished to do so, subject to the following conditions:
11
11
 
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
14
 
15
15
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
16
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
17
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  [gem]: https://rubygems.org/gems/zabbix_manager
6
6
 
7
- Most codes borrowed from zabbixapi, but fit for my everyday works well!
7
+ Most of the code in the project is based on rewriting the ZabbixApi
8
8
  Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Zabbix API][Zabbix API]
9
9
 
10
10
  ## Installation
@@ -13,7 +13,7 @@ Simple and lightweight ruby module for working with [Zabbix][Zabbix] via the [Za
13
13
  gem install zabbix_manager
14
14
 
15
15
  # specific version
16
- gem install zabbix_manager -v 4.2.0
16
+ gem install zabbix_manager -v 5.1.1
17
17
  ```
18
18
 
19
19
  ## Documentation
@@ -50,6 +50,7 @@ dropped.
50
50
 
51
51
  * net/http
52
52
  * json
53
+ * activesupport
53
54
 
54
55
  ## Contributing
55
56
 
@@ -3,9 +3,9 @@
3
3
  class ZabbixManager
4
4
  class Basic
5
5
  # Get Zabbix object data from API by id
6
- #
6
+ # @note 通用的查询方法
7
7
  # @param data [Hash] Should include object's id field name (identify) and id value
8
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
8
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
9
9
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
10
10
  # @return [Hash]
11
11
  def get(data)
@@ -13,9 +13,9 @@ class ZabbixManager
13
13
  end
14
14
 
15
15
  # Add new Zabbix object using API create
16
- #
16
+ # @note 通用的新增方法
17
17
  # @param data [Hash]
18
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
18
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
19
19
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
20
20
  # @return [Integer] The object id if a single object is created
21
21
  # @return [Boolean] True/False if multiple objects are created
@@ -24,16 +24,14 @@ class ZabbixManager
24
24
  end
25
25
 
26
26
  # Destroy Zabbix object using API delete
27
- #
27
+ # @note 通用的删除方法
28
28
  # @param data [Hash] Should include object's id field name (identify) and id value
29
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
29
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
30
30
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
31
31
  # @return [Integer] The object id if a single object is deleted
32
32
  # @return [Boolean] True/False if multiple objects are deleted
33
33
  def destroy(data)
34
34
  delete(data)
35
35
  end
36
-
37
- def method_name; end
38
36
  end
39
37
  end
@@ -3,19 +3,19 @@
3
3
  class ZabbixManager
4
4
  class Basic
5
5
  # Log messages to stdout when debugging
6
- #
6
+ # @note 开启debug模式,打印日志
7
7
  # @param message [String]
8
8
  def log(message)
9
- puts message.to_s if @client.options[:debug]
9
+ puts message if @client.options[:debug]
10
10
  end
11
11
 
12
12
  # Compare two hashes for equality
13
- #
13
+ # @note 比较两个哈希是否相等
14
14
  # @param first_hash [Hash]
15
15
  # @param second_hash [Hash]
16
16
  # @return [Boolean]
17
17
  def hash_equals?(first_hash, second_hash)
18
- normalized_first_hash = normalize_hash(first_hash)
18
+ normalized_first_hash = normalize_hash(first_hash)
19
19
  normalized_second_hash = normalize_hash(second_hash)
20
20
 
21
21
  hash1 = normalized_first_hash.merge(normalized_second_hash)
@@ -24,25 +24,22 @@ class ZabbixManager
24
24
  end
25
25
 
26
26
  # Convert all hash/array keys to symbols
27
- #
27
+ # @@note 将数组或哈希序列化
28
28
  # @param object [Array, Hash]
29
29
  # @return [Array, Hash]
30
30
  def symbolize_keys(object)
31
31
  case object
32
32
  when Array
33
- object.each_with_index do |val, index|
34
- object[index] = symbolize_keys(val)
35
- end
33
+ object.map(&method(:symbolize_keys))
36
34
  when Hash
37
- object.each_key do |key|
38
- object[key.to_sym] = symbolize_keys(object.delete(key))
39
- end
35
+ object.transform_keys(&:to_sym).transform_values(&method(:symbolize_keys))
36
+ else
37
+ object
40
38
  end
41
- object
42
39
  end
43
40
 
44
41
  # Normalize all hash values to strings
45
- #
42
+ # @note 将哈希元素序列化成字符串
46
43
  # @param hash [Hash]
47
44
  # @return [Hash]
48
45
  def normalize_hash(hash)
@@ -51,14 +48,14 @@ class ZabbixManager
51
48
  result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id
52
49
 
53
50
  result.each do |key, value|
54
- result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
51
+ result[key.to_sym] = value.is_a?(Array) ? normalize_array(value) : value.to_s
55
52
  end
56
53
 
57
54
  result
58
55
  end
59
56
 
60
57
  # Normalize all array values to strings
61
- #
58
+ # @note 将数组元素序列化成字符串
62
59
  # @param array [Array]
63
60
  # @return [Array]
64
61
  def normalize_array(array)
@@ -79,18 +76,20 @@ class ZabbixManager
79
76
  end
80
77
 
81
78
  # Parse a data hash for id key or boolean to return
82
- #
83
- # @param data [Hash]
79
+ # @note 提取 zabbix 监控对象 CRUD key 或者 keys
80
+ # @param data
84
81
  # @return [Integer] The object id if a single object hash is provided with key
85
82
  # @return [Boolean] True/False if multiple class object hash is provided
86
83
  def parse_keys(data)
87
84
  case data
88
85
  when Hash
89
- data.empty? ? nil : data[keys][0].to_i
86
+ data.empty? ? false : data[keys][0].to_i
90
87
  when TrueClass
91
88
  true
92
89
  when FalseClass
93
90
  false
91
+ else
92
+ false
94
93
  end
95
94
  end
96
95
 
@@ -1,9 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
+ # @author: WENWU YAN
5
+ # @email: 968828@gmail.com
6
+ # @date: 2023/3/25下午4:47
4
7
  class Basic
5
8
  # Initializes a new Basic object with ZabbixManager Client
6
- #
9
+ # @note zabbix api 基类
7
10
  # @param client [ZabbixManager::Client]
8
11
  # @return [ZabbixManager::Client]
9
12
  def initialize(client)
@@ -11,38 +14,38 @@ class ZabbixManager
11
14
  end
12
15
 
13
16
  # Placeholder for inherited objects to provide object-specific method name
14
- #
15
- # @raise [ManagerError] Basic object does not directly support method_name
17
+ # @note 子类必须实现的方法 method_name
18
+ # @raise [ZbxError] Basic object does not directly support method_name
16
19
  def method_name
17
- raise ManagerError, "Can't call method_name here"
20
+ raise ZbxError, "Can't call method_name here"
21
+ end
22
+
23
+ # Placeholder for inherited objects to provide object-specific id field name
24
+ # @note 子类必须实现的方法
25
+ # @raise [ZbxError] Basic object does not directly support identify
26
+ def identify
27
+ raise ZbxError, "Can't call identify here"
18
28
  end
19
29
 
20
30
  # Placeholder for inherited objects to provide default options
21
- #
31
+ # @note 子类缺省的 options
22
32
  # @return [Hash]
23
33
  def default_options
24
34
  {}
25
35
  end
26
36
 
27
- # Returns the object's plural id field name (identify) based on key
28
- #
29
- # @return [String]
30
- def keys
31
- "#{key}s"
32
- end
33
-
34
37
  # Returns the object's id field name (identify) based on method_name + id
35
- #
38
+ # @note 默认 key 为 "#{method_name}id"
36
39
  # @return [String]
37
40
  def key
38
41
  "#{method_name}id"
39
42
  end
40
43
 
41
- # Placeholder for inherited objects to provide object-specific id field name
42
- #
43
- # @raise [ManagerError] Basic object does not directly support identify
44
- def identify
45
- raise ManagerError, "Can't call identify here"
44
+ # Returns the object's plural id field name (identify) based on key
45
+ # @note 默认 key 为 "#{method_name}ids"
46
+ # @return [String]
47
+ def keys
48
+ "#{key}s"
46
49
  end
47
50
  end
48
51
  end
@@ -3,64 +3,31 @@
3
3
  class ZabbixManager
4
4
  class Basic
5
5
  # Create new Zabbix object using API (with defaults)
6
- #
6
+ # @note 新增 zabbix 监控对象,返回监控对象 id
7
7
  # @param data [Hash]
8
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
8
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
9
9
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
10
10
  # @return [Integer] The object id if a single object is created
11
11
  # @return [Boolean] True/False if multiple objects are created
12
12
  def create(data)
13
- log "[DEBUG] Call create with parameters: #{data.inspect}"
13
+ log "[DEBUG] Call #{method_name}.create with parameters: #{data.inspect}"
14
14
 
15
- # 判断是否绑定默认选项并重新生成配置
16
- data_with_default = default_options.empty? ? data : merge_params(default_options, data)
17
- data_create = [data_with_default]
18
-
19
- # 调用实例方法
20
- result = @client.api_request(method: "#{method_name}.create", params: data_create)
21
- # 判断是否执行成功并返回结果
15
+ params = default_options.merge(data)
16
+ result = @client.api_request(method: "#{method_name}.create", params: params)
22
17
  parse_keys result
23
18
  end
24
19
 
25
- # Delete Zabbix object using API
26
- #
27
- # @param data [Hash] Should include object's id field name (identify) and id value
28
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
29
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
30
- # @return [Integer] The object id if a single object is deleted
31
- # @return [Boolean] True/False if multiple objects are deleted
32
- def delete(data)
33
- log "[DEBUG] Call delete with parameters: #{data.inspect}"
34
-
35
- data_delete = [data]
36
- result = @client.api_request(method: "#{method_name}.delete", params: data_delete)
37
- parse_keys result
38
- end
39
-
40
- # Create or update Zabbix object using API
41
- #
42
- # @param data [Hash] Should include object's id field name (identify) and id value
43
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
44
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
45
- # @return [Integer] The object id if a single object is created
46
- # @return [Boolean] True/False if multiple objects are created
47
- def create_or_update(data)
48
- log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"
49
-
50
- id = get_id(identify.to_sym => data[identify.to_sym])
51
- id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
52
- end
53
-
54
20
  # Update Zabbix object using API
55
- #
21
+ # @note 更新 zabbix 监控对象,返回监控对象 id
56
22
  # @param data [Hash] Should include object's id field name (identify) and id value
57
23
  # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
58
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
24
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
59
25
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
60
26
  # @return [Integer] The object id if a single object is created
61
27
  # @return [Boolean] True/False if multiple objects are created
62
28
  def update(data, force = false)
63
- log "[DEBUG] Call update with parameters: #{data.inspect}"
29
+ log "[DEBUG] Call #{method_name}.update with parameters: #{data.inspect}"
30
+
64
31
  dump = {}
65
32
  dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
66
33
  dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
@@ -69,67 +36,85 @@ class ZabbixManager
69
36
  log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
70
37
  data[key.to_sym].to_i
71
38
  else
72
- data_update = [data]
73
- result = @client.api_request(method: "#{method_name}.update", params: data_update)
39
+ result = @client.api_request(method: "#{method_name}.update", params: data)
74
40
  parse_keys result
75
41
  end
76
42
  end
77
43
 
78
- # mojo_update 请求补丁函数
79
- def mojo_update(method, data)
80
- log "[DEBUG] Call update with parameters: #{data.inspect}"
81
- @client.api_request(method: method, params: data)
82
- end
44
+ # Delete Zabbix object using API
45
+ # @note 删除 zabbix 监控对象,返回监控对象 id
46
+ # @param data [Hash] Should include object's id field name (identify) and id value
47
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
48
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
49
+ # @return [Integer] The object id if a single object is deleted
50
+ # @return [Boolean] True/False if multiple objects are deleted
51
+ def delete(data)
52
+ log "[DEBUG] Call #{method_name}.delete with parameters: #{data.inspect}"
83
53
 
84
- # MOJO_UPDATE
54
+ result = @client.api_request(method: "#{method_name}.delete", params: data)
55
+ parse_keys result
56
+ end
85
57
 
86
- # Get full/extended Zabbix object data from API
87
- #
58
+ # Get or Create Zabbix object using API
59
+ # @note 创建或更新 zabbix 监控对象,返回监控对象 id
88
60
  # @param data [Hash] Should include object's id field name (identify) and id value
89
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
61
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
90
62
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
91
- # @return [Hash]
92
- def get_full_data(data)
93
- log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
63
+ # @return [Integer, Array, TrueClass, FalseClass] Zabbix object id
64
+ def get_or_create(data)
65
+ log "[DEBUG] Call #{method_name}.get_or_create with parameters: #{data.inspect}"
94
66
 
95
- @client.api_request(
96
- method: "#{method_name}.get",
97
- params: {
98
- filter: {
99
- identify.to_sym => data[identify.to_sym]
100
- },
101
- output: "extend"
102
- }
103
- )
67
+ # Call get_id return object id if exists
68
+ if (id = get_id(identify.to_sym => data[identify.to_sym]))
69
+ id
70
+ else
71
+ create(data)
72
+ end
104
73
  end
105
74
 
106
- # Get raw Zabbix object data from API
107
- #
108
- # @param data [Hash]
109
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
75
+ # Get or Create Zabbix object [{ key: id }] using API
76
+ # @note 批量创建或更新监控对象并返回 [ { "#{key}": id } ]
77
+ # @param data [Hash, Array] Should include object's id field name (identify) and id value
78
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
110
79
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
111
- # @return [Hash]
112
- def get_raw(data)
113
- log "[DEBUG] Call get_raw with parameters: #{data.inspect}"
80
+ # @return [Array] Zabbix object [{keys: id}]
81
+ def get_or_create_keys(data)
82
+ log "[DEBUG] Call #{method_name}.get_or_create_keys with parameters: #{data.inspect}"
83
+
84
+ ids = []
85
+ [data].flatten.each do |item|
86
+ if (id = get_or_create(item))
87
+ ids << { "#{key}": id }
88
+ end
89
+ end
90
+ ids
91
+ end
114
92
 
115
- @client.api_request(
116
- method: "#{method_name}.get",
117
- params: data
118
- )
93
+ # Create or update Zabbix object using API
94
+ # @note 创建或更新 zabbix 监控对象,入参为基于符号的哈希对象,返回监控对象 id
95
+ # @param data [Hash] Should include object's id field name (identify) and id value
96
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
97
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
98
+ # @return [Integer] The object id if a single object is created
99
+ # @return [Boolean] True/False if multiple objects are created
100
+ def create_or_update(data)
101
+ log "[DEBUG] Call #{method_name}.create_or_update with parameters: #{data.inspect}"
102
+
103
+ id = get_id(identify.to_sym => data[identify.to_sym])
104
+ id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
119
105
  end
120
106
 
121
107
  # Dump Zabbix object data by key from API
122
- #
108
+ # @note 基于 "#{key}" 打印 zabbix 监控对象详情
123
109
  # @param data [Hash] Should include desired object's key and value
124
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
110
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
125
111
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
126
112
  # @return [Hash]
127
113
  def dump_by_id(data)
128
- log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
114
+ log "[DEBUG] Call #{method_name}.dump_by_id with parameters: #{data.inspect}"
129
115
 
130
- @client.api_request(
131
- method: "#{method_name}.get",
132
- params: {
116
+ get_raw(
117
+ {
133
118
  filter: {
134
119
  key.to_sym => data[key.to_sym]
135
120
  },
@@ -138,90 +123,109 @@ class ZabbixManager
138
123
  )
139
124
  end
140
125
 
141
- # Get full/extended Zabbix data for all objects of type/class from API
142
- #
143
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
144
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
145
- # @return [Array<Hash>] Array of matching objects
146
- def all
147
- result = {}
148
- @client.api_request(
149
- method: "#{method_name}.get",
150
- params: { output: "extend" }
151
- ).each do |item|
152
- result[item[identify]] = item[key]
153
- end
154
- result
155
- end
156
-
157
126
  # Get Zabbix object id from API based on provided data
158
- #
127
+ # @note 基于 zabbix "#{identify}" 查询监控对象的 "#{key}"
159
128
  # @param data [Hash]
160
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
129
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
161
130
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
162
- # @return [Integer] Zabbix object id
131
+ # @return [Integer, NilClass] Zabbix object id
163
132
  def get_id(data)
164
- log "[DEBUG] Call get_id with parameters: #{data.inspect}"
133
+ log "[DEBUG] Call #{method_name}.get_id with parameters: #{data.inspect}"
165
134
 
166
135
  # symbolize keys if the user used string keys instead of symbols
167
136
  data = symbolize_keys(data) if data.key?(identify)
168
137
  # raise an error if identify name was not supplied
169
138
  name = data[identify.to_sym]
170
- raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?
139
+ raise ZbxError, "#{identify} not supplied in call to get_id" if name.nil?
171
140
 
172
- result = @client.api_request(
173
- method: "#{method_name}.get",
174
- params: {
141
+ result = get_raw(
142
+ {
175
143
  filter: data,
176
144
  output: [key, identify]
177
145
  }
178
146
  )
179
- id = nil
180
- result.each { |item| id = item[key].to_i if item[identify] == data[identify.to_sym] }
181
- id
147
+
148
+ result.find { |item| item[identify] == data[identify.to_sym] }&.[](key)&.to_i
182
149
  end
183
150
 
184
- # Get or Create Zabbix object using API
185
- #
186
- # @param data [Hash] Should include object's id field name (identify) and id value
187
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
151
+ # Get Zabbix object [{ key: id }] from API based on provided data
152
+ # @note 基于监控对象索引建"#{identify}"查询 { "#{key}": id }
153
+ # @param data [Hash, Array]
154
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
188
155
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
189
- # @return [Integer] Zabbix object id
190
- def get_or_create(data)
191
- log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
192
-
193
- unless (id = get_id(identify.to_sym => data[identify.to_sym]))
194
- id = create(data)
156
+ # @return [Array] Zabbix object id
157
+ def get_key_ids(data)
158
+ log "[DEBUG] Call #{method_name}.get_key_ids with parameters: #{data.inspect}"
159
+
160
+ ids = []
161
+ [data].flatten.each do |item|
162
+ if (id = get_id(item))
163
+ ids << { "#{key}": id }
164
+ end
195
165
  end
196
- id
166
+ ids
197
167
  end
198
168
 
199
- # 根据数据自动创建
200
- def create_raw(data)
201
- log "[DEBUG] Call create_raw with parameters: #{data.inspect}"
202
- # 请求创建数据
203
- @client.api_request(
204
- method: "#{method_name}.create",
205
- params: data
169
+ # Get Zabbix object ID by name using Zabbix API
170
+ # @note 基于监控对象索引键(#{identify})查询 #{key}
171
+ # @param data [String, Array] Needs to include host to properly identify Zabbix Objects via Zabbix API
172
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
173
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
174
+ # @return [Array, NilClass] Zabbix object id
175
+ def get_key_ids_by_identify(data)
176
+ log "[DEBUG] Call #{method_name}.get_ids_by_name with parameters: #{data.inspect}"
177
+
178
+ result = get_raw(
179
+ {
180
+ filter: { "#{identify}": [data].flatten },
181
+ output: ["#{key}"]
182
+ }
206
183
  )
184
+ result.empty? ? nil : result.map { |i| { "#{key}": i[key] } }
207
185
  end
208
186
 
209
- # 根据数据自动更新数据
210
- def update_raw(data)
211
- log "[DEBUG] Call update_raw with parameters: #{data.inspect}"
212
- # 请求创建数据
213
- @client.api_request(
214
- method: "#{method_name}.update",
215
- params: data
187
+ # Get full/extended Zabbix data for all objects of type/class from API
188
+ # @note 查询 zabbix 监控对象所有的 "#{identify}: #{key}"
189
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
190
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
191
+ # @return [Hash] Array of matching objects
192
+ def all
193
+ result = {}
194
+ get_raw({ output: "extend" }).each do |item|
195
+ result[item[identify]] = item[key]
196
+ end
197
+ result
198
+ end
199
+
200
+ # Get full/extended Zabbix object data from API
201
+ # @note 基于 "#{identify}" 过滤 zabbix 监控对象,输出详情
202
+ # @param data [Hash] Should include object's id field name (identify) and id value
203
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
204
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
205
+ # @return [Hash]
206
+ def get_full_data(data)
207
+ log "[DEBUG] Call #{method_name}.get_full_data with parameters: #{data.inspect}"
208
+
209
+ get_raw(
210
+ {
211
+ filter: {
212
+ identify.to_sym => data[identify.to_sym]
213
+ },
214
+ output: "extend"
215
+ }
216
216
  )
217
217
  end
218
218
 
219
- # 根据数据自动删除数据
220
- def delete_raw(data)
221
- log "[DEBUG] Call delete_raw with parameters: #{data.inspect}"
222
- # 请求创建数据
219
+ # Get raw Zabbix object data from API
220
+ # @note zabbix 监控对象低阶查询方法,静默方法 debug 打印信息
221
+ # @param data [Hash]
222
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
223
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
224
+ # @return [Hash]
225
+ def get_raw(data)
226
+ # log "[DEBUG] Call #{method_name}.get_raw with parameters: #{data.inspect}"
223
227
  @client.api_request(
224
- method: "#{method_name}.delete",
228
+ method: "#{method_name}.get",
225
229
  params: data
226
230
  )
227
231
  end