zabbix_manager 5.1.3 → 5.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/zabbix_manager/basic/basic_alias.rb +3 -20
  3. data/lib/zabbix_manager/basic/basic_func.rb +24 -46
  4. data/lib/zabbix_manager/basic/basic_init.rb +8 -24
  5. data/lib/zabbix_manager/basic/basic_logic.rb +66 -104
  6. data/lib/zabbix_manager/classes/actions.rb +11 -21
  7. data/lib/zabbix_manager/classes/applications.rb +5 -19
  8. data/lib/zabbix_manager/classes/configurations.rb +5 -19
  9. data/lib/zabbix_manager/classes/drules.rb +6 -22
  10. data/lib/zabbix_manager/classes/errors.rb +19 -9
  11. data/lib/zabbix_manager/classes/events.rb +2 -6
  12. data/lib/zabbix_manager/classes/graphs.rb +16 -47
  13. data/lib/zabbix_manager/classes/hostgroups.rb +3 -9
  14. data/lib/zabbix_manager/classes/hostinterfaces.rb +7 -15
  15. data/lib/zabbix_manager/classes/hosts.rb +42 -76
  16. data/lib/zabbix_manager/classes/httptests.rb +7 -23
  17. data/lib/zabbix_manager/classes/items.rb +12 -68
  18. data/lib/zabbix_manager/classes/maintenance.rb +2 -6
  19. data/lib/zabbix_manager/classes/mediatypes.rb +20 -52
  20. data/lib/zabbix_manager/classes/problems.rb +38 -66
  21. data/lib/zabbix_manager/classes/proxies.rb +7 -26
  22. data/lib/zabbix_manager/classes/roles.rb +18 -57
  23. data/lib/zabbix_manager/classes/screens.rb +7 -21
  24. data/lib/zabbix_manager/classes/scripts.rb +7 -10
  25. data/lib/zabbix_manager/classes/server.rb +1 -6
  26. data/lib/zabbix_manager/classes/templates.rb +15 -49
  27. data/lib/zabbix_manager/classes/triggers.rb +15 -49
  28. data/lib/zabbix_manager/classes/unusable.rb +2 -1
  29. data/lib/zabbix_manager/classes/usergroups.rb +6 -28
  30. data/lib/zabbix_manager/classes/usermacros.rb +19 -113
  31. data/lib/zabbix_manager/classes/users.rb +7 -34
  32. data/lib/zabbix_manager/classes/valuemaps.rb +5 -26
  33. data/lib/zabbix_manager/client.rb +65 -69
  34. data/lib/zabbix_manager/version.rb +1 -2
  35. data/lib/zabbix_manager.rb +33 -46
  36. metadata +3 -3
@@ -2,23 +2,17 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Drules < Basic
5
- # The method name used for interacting with Drules via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API Drules 交互的方法名称
8
6
  def method_name
9
7
  "drule"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific Drule objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 Drule 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
18
14
 
19
- # The default options used when creating Drule objects via Zabbix API
20
- #
21
- # @return [Hash]
15
+ # 创建 Drule 对象时使用的默认选项
22
16
  def default_options
23
17
  {
24
18
  name: nil,
@@ -28,14 +22,9 @@ class ZabbixManager
28
22
  }
29
23
  end
30
24
 
31
- # Get or Create Drule object using Zabbix API
32
- #
33
- # @param data [Hash] Needs to include name to properly identify Drule via Zabbix API
34
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
35
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
36
- # @return [Integer] Zabbix object id
25
+ # 通过 Zabbix API 获取或创建 Drule 对象
37
26
  def get_or_create(data)
38
- log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
27
+ log "[DEBUG] 调用 get_or_create,参数为:#{data.inspect}"
39
28
 
40
29
  unless (id = get_id(name: data[:name]))
41
30
  id = create(data)
@@ -43,12 +32,7 @@ class ZabbixManager
43
32
  id
44
33
  end
45
34
 
46
- # Create or update Drule object using Zabbix API
47
- #
48
- # @param data [Hash] Needs to include name to properly identify Drules via Zabbix API
49
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
50
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
51
- # @return [Integer, TrueClass, FalseClass] Zabbix object id
35
+ # 通过 Zabbix API 创建或更新 Drule 对象
52
36
  def create_or_update(data)
53
37
  druleid = get_id(name: data[:name])
54
38
  druleid ? update(data.merge(druleid: druleid)) : create(data)
@@ -1,29 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
+ # BaseError 是 ZabbixManager 模块中处理异常的基类
4
5
  class BaseError < RuntimeError
5
- attr_accessor :response, :error, :error_message
6
+ # 用于存储响应对象
7
+ attr_accessor :response
8
+ # 用于存储 Zabbix API 错误信息
9
+ attr_accessor :error
10
+ # 用于存储格式化后的错误消息
11
+ attr_accessor :error_message
6
12
 
13
+ # 初始化异常对象
7
14
  def initialize(message, response = nil)
8
15
  super(message)
9
16
  @response = response
10
-
11
17
  set_error! if @response
12
18
  end
13
19
 
14
20
  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
+ # 从响应中提取 Zabbix API 错误信息
23
+ def set_error!
24
+ @error = @response["error"]
25
+ @error_message = "#{@error["message"]}: #{@error["data"]}"
26
+ rescue StandardError
27
+ @error = nil
28
+ @error_message = nil
29
+ end
22
30
  end
23
31
 
32
+ # ZbxError 是 ZabbixManager 模块中处理 Zabbix API 异常的类
24
33
  class ZbxError < BaseError
25
34
  end
26
35
 
36
+ # HttpError 是 ZabbixManager 模块中处理 HTTP 异常的类
27
37
  class HttpError < BaseError
28
38
  end
29
39
  end
@@ -2,16 +2,12 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Events < Basic
5
- # The method name used for interacting with Events via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API Events 交互的方法名称
8
6
  def method_name
9
7
  "event"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific Event objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 Event 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
@@ -2,32 +2,22 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Graphs < Basic
5
- # The method name used for interacting with Graphs via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API Graphs 交互的方法名称
8
6
  def method_name
9
7
  "graph"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific Graph objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 Graph 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
18
14
 
19
- # Get full/extended Graph data from Zabbix API
20
- #
21
- # @param data [Hash] Should include object's id field name (identify) and id value
22
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
- # @return [Hash]
15
+ # Zabbix API 获取完整/扩展的 Graph 数据
25
16
  def get_full_data(data)
26
- log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"
17
+ log "[DEBUG] 使用参数调用 get_full_data: #{data.inspect}"
27
18
 
28
- @client.api_request(
29
- method: "#{method_name}.get",
30
- params: {
19
+ get_raw(
20
+ {
31
21
  search: {
32
22
  identify.to_sym => data[identify.to_sym]
33
23
  },
@@ -36,16 +26,10 @@ class ZabbixManager
36
26
  )
37
27
  end
38
28
 
39
- # Get Graph ids for Host from Zabbix API
40
- #
41
- # @param data [Hash] Should include host value to query for matching graphs
42
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
43
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
44
- # @return [Array] Returns array of Graph ids
29
+ # Zabbix API 获取主机的 Graph ids
45
30
  def get_ids_by_host(data)
46
- result = @client.api_request(
47
- method: "graph.get",
48
- params: {
31
+ result = get_raw(
32
+ {
49
33
  filter: {
50
34
  host: data[:host]
51
35
  },
@@ -54,38 +38,28 @@ class ZabbixManager
54
38
  )
55
39
 
56
40
  result.filter_map do |graph|
57
- num = graph["graphid"]
58
- name = graph["name"]
41
+ num = graph["graphid"]
42
+ name = graph["name"]
59
43
  filter = data[:filter]
60
44
 
61
45
  num if filter.nil? || /#{filter}/ =~ name
62
46
  end
63
47
  end
64
48
 
65
- # Get Graph Item object using Zabbix API
66
- #
67
- # @param data [Hash] Needs to include graphids to properly identify Graph Items via Zabbix API
68
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
69
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
70
- # @return [Hash]
49
+ # 通过 Zabbix API 获取 Graph Item 对象
71
50
  def get_items(data)
72
51
  @client.api_request(
73
52
  method: "graphitem.get",
74
53
  params: {
75
54
  graphids: [data],
76
- output: "extend"
55
+ output: "extend"
77
56
  }
78
57
  )
79
58
  end
80
59
 
81
- # Get or Create Graph object using Zabbix API
82
- #
83
- # @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
84
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
85
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
86
- # @return [Integer] Zabbix object id
60
+ # 通过 Zabbix API 获取或创建 Graph 对象
87
61
  def get_or_create(data)
88
- log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
62
+ log "[DEBUG] 使用参数调用 get_or_create: #{data.inspect}"
89
63
 
90
64
  unless (id = get_id(name: data[:name], templateid: data[:templateid]))
91
65
  id = create(data)
@@ -94,12 +68,7 @@ class ZabbixManager
94
68
  id
95
69
  end
96
70
 
97
- # Create or update Graph object using Zabbix API
98
- #
99
- # @param data [Hash] Needs to include name and templateid to properly identify Graphs via Zabbix API
100
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
101
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
102
- # @return [Integer, TrueClass, FalseClass] Zabbix object id
71
+ # 通过 Zabbix API 创建或更新 Graph 对象
103
72
  def create_or_update(data)
104
73
  graphid = get_id(name: data[:name], templateid: data[:templateid])
105
74
  graphid ? _update(data.merge(graphid: graphid)) : create(data)
@@ -2,23 +2,17 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class HostGroups < Basic
5
- # The method name used for interacting with HostGroups via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API HostGroups 交互的方法名称
8
6
  def method_name
9
7
  "hostgroup"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific HostGroup objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 HostGroup 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
18
14
 
19
- # The key field name used for HostGroup objects via Zabbix API
20
- #
21
- # @return [String]
15
+ # HostGroup 对象通过 Zabbix API 使用的 key 字段名称
22
16
  def key
23
17
  "groupid"
24
18
  end
@@ -2,35 +2,27 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class HostInterfaces < Basic
5
- # The method name used for interacting with HostInterfaces via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API HostInterfaces 交互的方法名称
8
6
  def method_name
9
7
  "hostinterface"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific HostInterface objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 HostInterface 对象的 id 字段名称
15
11
  def identify
16
12
  "hostids"
17
13
  end
18
14
 
19
- # The key field name used for HostInterface objects via Zabbix API
20
- #
21
- # @return [String]
15
+ # HostInterface 对象通过 Zabbix API 使用的 key 字段名称
22
16
  def key
23
17
  "interfaceid"
24
18
  end
25
19
 
26
- # Get Zabbix interface id from API based on provided hostid
20
+ # 通过 Zabbix API 基于提供的 hostid 获取 Zabbix interface id
27
21
  # @note 基于 hostid 查询关联的 Interfaceid
28
- # @param hostid [Integer, String]
29
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
30
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
31
- # @return [Integer, NilClass] Zabbix object id
32
22
  def get_interfaceid(hostid)
33
- result = get_raw({ output: key, "#{identify}": hostid })
23
+ result = get_raw({ output: key, identify.to_sym => hostid })
24
+
25
+ # 如果查询结果不为空,且第一个元素不为空,则继续获取 {interfaceid} 并转换为整数
34
26
  result&.[](0)&.[](key)&.to_i
35
27
  end
36
28
  end
@@ -2,44 +2,17 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Hosts < Basic
5
- # The method name used for interacting with Hosts via Zabbix API
6
- #
7
- # @return [String]
5
+ # Zabbix API 交互的主机方法名称
8
6
  def method_name
9
7
  "host"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific Host objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定主机对象的 id 字段名称
15
11
  def identify
16
12
  "host"
17
13
  end
18
14
 
19
- # Dump Host object data by key from Zabbix API
20
- #
21
- # @param data [Hash] Should include desired object's key and value
22
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
23
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
- # @return [Hash]
25
- def dump_by_id(data)
26
- log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
27
-
28
- @client.api_request(
29
- method: "host.get",
30
- params: {
31
- filter: {
32
- key.to_sym => data[key.to_sym]
33
- },
34
- output: "extend",
35
- selectHosts: "shorten"
36
- }
37
- )
38
- end
39
-
40
- # The default options used when creating Host objects via Zabbix API
41
- # @note Create Host 缺省的配置参数
42
- # @return [Hash]
15
+ # 在通过 Zabbix API 创建主机对象时使用的默认选项
43
16
  def default_options
44
17
  {
45
18
  host: nil,
@@ -63,76 +36,69 @@ class ZabbixManager
63
36
  }
64
37
  end
65
38
 
66
- # Unlink/Remove Templates from Hosts using Zabbix API
67
- #
68
- # @param data [Hash] Should include hosts_id array and templates_id array
69
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
70
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
71
- # @return [Boolean]
72
- def unlink_templates(data)
73
- result = @client.api_request(
74
- method: "host.massRemove",
75
- params: {
76
- hostids: data[:hosts_id],
77
- templates: data[:templates_id]
78
- }
39
+ # 通过 Zabbix API 从指定的 key 值获取主机对象数据
40
+ def dump_by_id(data)
41
+ log "[DEBUG] 调用 dump_by_id 方法,参数: #{data.inspect}"
42
+
43
+ get_raw({
44
+ filter: {
45
+ key.to_sym => data[key.to_sym]
46
+ },
47
+ output: "extend",
48
+ selectHosts: "shorten"
49
+ }
79
50
  )
80
- result.empty? ? false : true
81
51
  end
82
52
 
83
- # Get Zabbix object ID by name using Zabbix API
84
- # @note 基于监控对象索引键(#{identify})查询 #{key}
85
- # @param data [String, Array] Needs to include host to properly identify Zabbix Objects via Zabbix API
86
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
87
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
88
- # @return [Array, NilClass] Zabbix object id
53
+ # 通过 Zabbix API 根据名称获取 Zabbix 对象的 ID
89
54
  def get_ids_by_name(data)
90
- log "[DEBUG] Call #{method_name}.get_ids_by_name with parameters: #{data.inspect}"
55
+ log "[DEBUG] 调用 #{method_name}.get_ids_by_name 方法,参数: #{data.inspect}"
91
56
 
92
- result = get_raw(
93
- {
94
- filter: { name: [data].flatten },
95
- output: ["#{key}"]
96
- }
57
+ result = get_raw({
58
+ filter: { name: [data].flatten },
59
+ output: [key]
60
+ }
97
61
  )
98
- result.empty? ? nil : result.map { |i| { "#{key}": i[key] } }
62
+ result.empty? ? nil : result.map { |i| { key.to_sym => i[key] } }
99
63
  end
100
64
 
101
- # Create or update Zabbix object using API
102
- # @note 创建或更新 Host 监控对象,入参为基于符号的哈希对象,返回监控对象 id
103
- # @note 重写基类方法 create_or_update,特殊处理 interfaces 对象
104
- # @param data [Hash] Should include object's id field name (identify) and id value
105
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
106
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
107
- # @return [Integer] The object id if a single object is created
108
- # @return [Boolean] True/False if multiple objects are created
65
+ # 使用 Zabbix API 创建或更新 Zabbix 对象
109
66
  def create_or_update(data)
110
- log "[DEBUG] Call #{method_name}.create_or_update with parameters: #{data.inspect}"
67
+ log "[DEBUG] 调用 #{method_name}.create_or_update 方法,参数: #{data.inspect}"
111
68
 
112
69
  hostid = get_id(identify.to_sym => data[identify.to_sym])
113
70
  return create(data) unless hostid
114
71
 
115
72
  interface_id = get_interfaceid(hostid)
116
- raise ZbxError, "interfaceid not found in call to get_interfaceid" unless interface_id
73
+ raise ZbxError, "#{data.inspect} get_interfaceid 查无记录" unless interface_id
117
74
 
118
- interfaces = data.delete(:interfaces)&.merge(interfaceid: interface_id)
119
- params = data.merge(interfaces: interfaces).merge(key.to_sym => hostid.to_s)
75
+ dump_data = data.dup
76
+ interfaces = dump_data.delete(:interfaces)&.merge(interfaceid: interface_id)
77
+ params = dump_data.merge(interfaces: interfaces).merge(key.to_sym => hostid.to_s)
120
78
  update(params)
121
79
  end
122
80
 
123
- # Get Zabbix interface id from API based on provided hostid
124
- # @note 基于 hostid 查询关联的 Interfaceid
125
- # @param hostid [Integer, String]
126
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
127
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
128
- # @return [Integer, NilClass] Zabbix object id
81
+ # API 中基于提供的 hostid 获取 Zabbix interface id
129
82
  def get_interfaceid(hostid)
83
+ log "[DEBUG] 调用 #{method_name}.get_interfaceid 方法,参数: #{hostid.inspect}"
84
+
130
85
  result = @client.api_request(
131
86
  method: "hostinterface.get",
132
87
  params: { output: "interfaceid", hostids: hostid }
133
88
  )
134
89
  result&.[](0)&.[]("interfaceid")&.to_i
135
90
  end
91
+
92
+ # 在 Hosts 中使用 Zabbix API 取消关联/移除模板
93
+ def unlink_templates(data)
94
+ result = @client.api_request(
95
+ method: "host.massRemove",
96
+ params: {
97
+ hostids: data[:hosts_id],
98
+ templates: data[:templates_id]
99
+ }
100
+ )
101
+ result.empty? ? false : true
102
+ end
136
103
  end
137
104
  end
138
-
@@ -2,37 +2,26 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class HttpTests < Basic
5
- # The method name used for interacting with HttpTests via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API HttpTests 交互的方法名称
8
6
  def method_name
9
7
  "httptest"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific HttpTest objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 HttpTest 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
18
14
 
19
- # The default options used when creating HttpTest objects via Zabbix API
20
- #
21
- # @return [Hash]
15
+ # 通过 Zabbix API 创建 HttpTest 对象时使用的默认选项
22
16
  def default_options
23
17
  {
24
18
  hostid: nil,
25
- name: nil,
26
- steps: []
19
+ name: nil,
20
+ steps: []
27
21
  }
28
22
  end
29
23
 
30
- # Get or Create HttpTest object using Zabbix API
31
- #
32
- # @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
33
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
34
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
35
- # @return [Integer] Zabbix object id
24
+ # 通过 Zabbix API 获取或创建 HttpTest 对象
36
25
  def get_or_create(data)
37
26
  log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
38
27
 
@@ -42,12 +31,7 @@ class ZabbixManager
42
31
  id
43
32
  end
44
33
 
45
- # Create or update HttpTest object using Zabbix API
46
- #
47
- # @param data [Hash] Needs to include name and hostid to properly identify HttpTests via Zabbix API
48
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
49
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
50
- # @return [Integer, TrueClass, FalseClass] Zabbix object id
34
+ # 通过 Zabbix API 创建或更新 HttpTest 对象
51
35
  def create_or_update(data)
52
36
  httptestid = get_id(name: data[:name], hostid: data[:hostid])
53
37
  httptestid ? update(data.merge(httptestid: httptestid)) : create(data)
@@ -2,23 +2,17 @@
2
2
 
3
3
  class ZabbixManager
4
4
  class Items < Basic
5
- # The method name used for interacting with Items via Zabbix API
6
- #
7
- # @return [String]
5
+ # 用于通过 Zabbix API Items 交互的方法名称
8
6
  def method_name
9
7
  "item"
10
8
  end
11
9
 
12
- # The id field name used for identifying specific Item objects via Zabbix API
13
- #
14
- # @return [String]
10
+ # 用于通过 Zabbix API 标识特定 Item 对象的 id 字段名称
15
11
  def identify
16
12
  "name"
17
13
  end
18
14
 
19
- # The default options used when creating Item objects via Zabbix API
20
- #
21
- # @return [Hash]
15
+ # 通过 Zabbix API 创建 Item 对象时使用的默认选项
22
16
  def default_options
23
17
  {
24
18
  name: nil,
@@ -56,12 +50,7 @@ class ZabbixManager
56
50
  }
57
51
  end
58
52
 
59
- # Get or Create Item object using Zabbix API
60
- #
61
- # @param data [Hash] Needs to include name and hostid to properly identify Items via Zabbix API
62
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
63
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
64
- # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
53
+ # 通过 Zabbix API 获取或创建 Item 对象
65
54
  def get_or_create(data)
66
55
  log "[DEBUG] Call #{method_name}.get_or_create with parameters: #{data.inspect}"
67
56
 
@@ -72,36 +61,22 @@ class ZabbixManager
72
61
  end
73
62
  end
74
63
 
75
- # Create or update Item object using Zabbix API
76
- #
77
- # @param data [Hash] Needs to include name and hostid to properly identify Items via Zabbix API
78
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
79
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
80
- # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
64
+ # 通过 Zabbix API 创建或更新 Item 对象
81
65
  def create_or_update(data)
82
66
  itemid = get_id(name: data[:name], hostid: data[:hostid])
83
67
  itemid ? update(data.merge(itemid: itemid)) : create(data)
84
68
  end
85
69
 
86
- # Create Dns Monitor object using Zabbix API
87
- # @note 根据设备名称和接口名字查询监控项:入参 GigabitEthernet1/0/12,出参 15809
88
- # @example Interface Et0/0(TO_CLOUD): Bits sent, snmpoid 1.3.6.1.2.1.31.1.1.1.10.1
89
- # @example Interface Et0/0(TO_CLOUD): Operational status, snmpoid 1.3.6.1.2.1.2.2.1.8.1
90
- # @example Interface Et0/0(TO_CLOUD): Speed, snmpoid 1.3.6.1.2.1.31.1.1.1.15.1
91
- # @example Interface Et0/0(TO_CLOUD): Bits received, snmpoid 1.3.6.1.2.1.31.1.1.1.6.1
92
- # @param hostid [String, Integer] Needs to include name and hostid to properly identify Items via Zabbix API
93
- # @param name [String] Needs to include name and hostid to properly identify Items via Zabbix API
94
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
95
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
96
- # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
70
+ # 通过 Zabbix API 获取特定主机和接口下的监控项
97
71
  def get_interface_items(hostid, name)
98
- log "[DEBUG] Call #{method_name}.get_interface_items with: hostid #{hostid.inspect} name #{name.inspect}"
72
+ # 记录调试信息,标识方法调用以及传递的参数
73
+ log "[DEBUG] 调用 #{method_name}.get_interface_items,参数:hostid #{hostid.inspect},name #{name.inspect}"
99
74
 
100
- # 自动剔除收尾空白字串
101
- _name = name&.gsub(%r{[^/0-9]}, "").strip
75
+ # 自动剔除收尾空白字符,规范化接口名
76
+ _name = name&.gsub(%r{[^/0-9]}, "")&.strip
102
77
  iface = "#{_name}("
103
78
 
104
- # 模糊查询接口下所有监控项,同时过滤出特定的 snmp_oid
79
+ # 使用 Zabbix API 进行模糊查询,获取特定主机和接口下的监控项
105
80
  result = get_raw(
106
81
  {
107
82
  output: "extend",
@@ -115,39 +90,8 @@ class ZabbixManager
115
90
  }.sort_by {
116
91
  |item| item["key_"]
117
92
  }
118
- result.empty? ? nil : result
119
- end
120
-
121
- # The default options used when creating DNS Monitor objects via Zabbix API
122
- # @note 缺省的 dns 参数
123
- # @return [Hash]
124
- def default_dns_params
125
- {
126
- type: 0, # 代表 zabbix_agent
127
- value_type: 1, # 代表字符串
128
- delay: "1m",
129
- history: "90d",
130
- lifetime: "30d",
131
- timeout: "15s"
132
- }
133
- end
134
-
135
- # Create Dns Monitor object using Zabbix API
136
- #
137
- # @param hostid [Hash] Needs to include dns_name and hostid to properly identify Items via Zabbix API
138
- # @param dns_name [Hash] Needs to include dns_name and hostid to properly identify Items via Zabbix API
139
- # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
140
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
141
- # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
142
- def create_dns_item(hostid, dns_name)
143
- name = "【DNS域名解析监控】#{dns_name}"
144
- key = "net.dns.record[,#{dns_name},A,2,2]"
145
93
 
146
- params = default_dns_params.merge(hostid: hostid, name: name, key: key)
147
- result = @client.api_request(
148
- method: "item.create",
149
- params: params
150
- )
94
+ # 如果结果为空,则返回 nil;否则,返回排序后的结果
151
95
  result.empty? ? nil : result
152
96
  end
153
97
  end