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
@@ -19,7 +19,7 @@ class ZabbixManager
19
19
  # Get User macro object id from Zabbix API based on provided data
20
20
  #
21
21
  # @param data [Hash] Needs to include macro to properly identify user macros via Zabbix API
22
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
22
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
23
23
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
24
24
  # @return [Integer] Zabbix object id
25
25
  def get_id(data)
@@ -29,7 +29,7 @@ class ZabbixManager
29
29
  data = symbolize_keys(data) if data.key?(identify)
30
30
  # raise an error if identify name was not supplied
31
31
  name = data[identify.to_sym]
32
- raise ManagerError, "#{identify} not supplied in call to get_id" if name.nil?
32
+ raise ZbxError, "#{identify} not supplied in call to get_id" if name.nil?
33
33
 
34
34
  result = request(data, "usermacro.get", "hostmacroid")
35
35
 
@@ -39,7 +39,7 @@ class ZabbixManager
39
39
  # Get Global macro object id from Zabbix API based on provided data
40
40
  #
41
41
  # @param data [Hash] Needs to include macro to properly identify global macros via Zabbix API
42
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
42
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call or missing object's id field name (identify).
43
43
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
44
44
  # @return [Integer] Zabbix object id
45
45
  def get_id_global(data)
@@ -49,7 +49,7 @@ class ZabbixManager
49
49
  data = symbolize_keys(data) if data.key?(identify)
50
50
  # raise an error if identify name was not supplied
51
51
  name = data[identify.to_sym]
52
- raise ManagerError, "#{identify} not supplied in call to get_id_global" if name.nil?
52
+ raise ZbxError, "#{identify} not supplied in call to get_id_global" if name.nil?
53
53
 
54
54
  result = request(data, "usermacro.get", "globalmacroid")
55
55
 
@@ -59,7 +59,7 @@ class ZabbixManager
59
59
  # Get full/extended User macro data from Zabbix API
60
60
  #
61
61
  # @param data [Hash] Should include object's id field name (identify) and id value
62
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
62
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
63
63
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
64
64
  # @return [Hash]
65
65
  def get_full_data(data)
@@ -71,7 +71,7 @@ class ZabbixManager
71
71
  # Get full/extended Global macro data from Zabbix API
72
72
  #
73
73
  # @param data [Hash] Should include object's id field name (identify) and id value
74
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
74
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
75
75
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
76
76
  # @return [Hash]
77
77
  def get_full_data_global(data)
@@ -83,7 +83,7 @@ class ZabbixManager
83
83
  # Create new User macro object using Zabbix API (with defaults)
84
84
  #
85
85
  # @param data [Hash] Needs to include hostid, macro, and value to create User macro via Zabbix API
86
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
86
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
87
87
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
88
88
  # @return [Integer] The object id if a single object is created
89
89
  # @return [Boolean] True/False if multiple objects are created
@@ -94,7 +94,7 @@ class ZabbixManager
94
94
  # Create new Global macro object using Zabbix API (with defaults)
95
95
  #
96
96
  # @param data [Hash] Needs to include hostid, macro, and value to create Global macro via Zabbix API
97
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
97
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
98
98
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
99
99
  # @return [Integer] The object id if a single object is created
100
100
  # @return [Boolean] True/False if multiple objects are created
@@ -105,7 +105,7 @@ class ZabbixManager
105
105
  # Delete User macro object using Zabbix API
106
106
  #
107
107
  # @param data [Hash] Should include hostmacroid's of User macros to delete
108
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
108
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
109
109
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
110
110
  # @return [Integer] The object id if a single object is deleted
111
111
  # @return [Boolean] True/False if multiple objects are deleted
@@ -117,7 +117,7 @@ class ZabbixManager
117
117
  # Delete Global macro object using Zabbix API
118
118
  #
119
119
  # @param data [Hash] Should include hostmacroid's of Global macros to delete
120
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
120
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
121
121
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
122
122
  # @return [Integer] The object id if a single object is deleted
123
123
  # @return [Boolean] True/False if multiple objects are deleted
@@ -130,7 +130,7 @@ class ZabbixManager
130
130
  #
131
131
  # @param data [Hash] Should include object's id field name (identify), id value, and fields to update
132
132
  # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
133
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
133
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
134
134
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
135
135
  # @return [Integer] The object id if a single object is created
136
136
  # @return [Boolean] True/False if multiple objects are created
@@ -142,7 +142,7 @@ class ZabbixManager
142
142
  #
143
143
  # @param data [Hash] Should include object's id field name (identify), id value, and fields to update
144
144
  # @param force [Boolean] Whether to force an object update even if provided data matches Zabbix
145
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
145
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
146
146
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
147
147
  # @return [Integer] The object id if a single object is created
148
148
  # @return [Boolean] True/False if multiple objects are created
@@ -153,7 +153,7 @@ class ZabbixManager
153
153
  # Get or Create User macro object using Zabbix API
154
154
  #
155
155
  # @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
156
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
156
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
157
157
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
158
158
  # @return [Integer] Zabbix object id
159
159
  def get_or_create(data)
@@ -168,7 +168,7 @@ class ZabbixManager
168
168
  # Get or Create Global macro object using Zabbix API
169
169
  #
170
170
  # @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
171
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
171
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
172
172
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
173
173
  # @return [Integer] Zabbix object id
174
174
  def get_or_create_global(data)
@@ -183,9 +183,9 @@ class ZabbixManager
183
183
  # Create or update User macro object using Zabbix API
184
184
  #
185
185
  # @param data [Hash] Needs to include macro and hostid to properly identify User macros via Zabbix API
186
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
186
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
187
187
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
188
- # @return [Integer] Zabbix object id
188
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
189
189
  def create_or_update(data)
190
190
  hostmacroid = get_id(macro: data[:macro], hostid: data[:hostid])
191
191
  hostmacroid ? update(data.merge(hostmacroid: hostmacroid)) : create(data)
@@ -194,36 +194,37 @@ class ZabbixManager
194
194
  # Create or update Global macro object using Zabbix API
195
195
  #
196
196
  # @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
197
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
197
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
198
198
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
199
- # @return [Integer] Zabbix object id
199
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
200
200
  def create_or_update_global(data)
201
201
  globalmacroid = get_id_global(macro: data[:macro], hostid: data[:hostid])
202
202
  globalmacroid ? update_global(data.merge(globalmacroid: globalmacroid)) : create_global(data)
203
203
  end
204
204
 
205
205
  private
206
- # Custom request method to handle both User and Global macros in one
207
- #
208
- # @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
209
- # @param method [String] Zabbix API method to use for the request
210
- # @param result_key [String] Which key to use for parsing results based on User vs Global macros
211
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
212
- # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
213
- # @return [Integer] Zabbix object id
214
- def request(data, method, result_key)
215
- # Zabbix has different result formats for gets vs updates
216
- if method.include?(".get")
217
- if result_key.include?("global")
218
- @client.api_request(method: method, params: { globalmacro: true, filter: data })
219
- else
220
- @client.api_request(method: method, params: { filter: data })
221
- end
222
- else
223
- result = @client.api_request(method: method, params: data)
224
206
 
225
- result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
207
+ # Custom request method to handle both User and Global macros in one
208
+ #
209
+ # @param data [Hash] Needs to include macro and hostid to properly identify Global macros via Zabbix API
210
+ # @param method [String] Zabbix API method to use for the request
211
+ # @param result_key [String] Which key to use for parsing results based on User vs Global macros
212
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
213
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
214
+ # @return [Integer, TrueClass, FalseClass, NilClass] Zabbix object id
215
+ def request(data, method, result_key)
216
+ # Zabbix has different result formats for gets vs updates
217
+ if method.include?(".get")
218
+ if result_key.include?("global")
219
+ @client.api_request(method: method, params: { globalmacro: true, filter: data })
220
+ else
221
+ @client.api_request(method: method, params: { filter: data })
226
222
  end
223
+ else
224
+ result = @client.api_request(method: method, params: data)
225
+
226
+ result.key?(result_key) && !result[result_key].empty? ? result[result_key][0].to_i : nil
227
227
  end
228
+ end
228
229
  end
229
230
  end
@@ -46,9 +46,9 @@ class ZabbixManager
46
46
  # Add media to users using Zabbix API
47
47
  #
48
48
  # @param data [Hash] Needs to include userids and media to mass add media to users
49
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
49
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
50
50
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
51
- # @return [Integer] Zabbix object id (media)
51
+ # @return [Integer, NilClass] Zabbix object id (media)
52
52
  def add_medias(data)
53
53
  medias_helper(data, "update")
54
54
  end
@@ -56,9 +56,9 @@ class ZabbixManager
56
56
  # Update media for users using Zabbix API
57
57
  #
58
58
  # @param data [Hash] Needs to include userids and media to mass update media for users
59
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
59
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
60
60
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
61
- # @return [Integer] Zabbix object id (user)
61
+ # @return [Integer, NilClass] Zabbix object id (user)
62
62
  def update_medias(data)
63
63
  medias_helper(data, "update")
64
64
  end
@@ -26,24 +26,25 @@ class ZabbixManager
26
26
  # Get or Create ValueMap object using Zabbix API
27
27
  #
28
28
  # @param data [Hash] Needs to include valuemapids [List] to properly identify ValueMaps via Zabbix API
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
- # @return [Integer] Zabbix object id
31
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
32
32
  def get_or_create(data)
33
33
  log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"
34
34
 
35
- unless (id = get_id(valuemapids: data[:valuemapids]))
36
- id = create(data)
35
+ if (id = get_id(valuemapids: data[:valuemapids]))
36
+ id
37
+ else
38
+ create(data)
37
39
  end
38
- id
39
40
  end
40
41
 
41
42
  # Create or update Item object using Zabbix API
42
43
  #
43
44
  # @param data [Hash] Needs to include valuemapids to properly identify ValueMaps via Zabbix API
44
- # @raise [ManagerError] Error returned when there is a problem with the Zabbix API call.
45
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
45
46
  # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
46
- # @return [Integer] Zabbix object id
47
+ # @return [Integer, TrueClass, FalseClass] Zabbix object id
47
48
  def create_or_update(data)
48
49
  valuemapid = get_id(name: data[:name])
49
50
  valuemapid ? update(data.merge(valuemapids: [:valuemapid])) : create(data)
@@ -4,57 +4,74 @@ require "net/http"
4
4
  require "json"
5
5
  require "openssl"
6
6
 
7
+ # @author: WENWU YAN
8
+ # @email: 968828@gmail.com
9
+ # @date: 2023/3/25下午4:47
7
10
  class ZabbixManager
8
11
  class Client
9
- # @param (see ZabbixManager::Client#initialize)
12
+ # @param
10
13
  # @return [Hash]
11
14
  attr_reader :options
12
15
 
13
16
  # @return [Integer]
14
17
  def id
15
- rand 10_000
18
+ @id ||= rand 100_000
16
19
  end
17
20
 
18
21
  # Returns the API version from the Zabbix Server
19
- #
22
+ # @note 获取 zabbix api 版本
20
23
  # @return [String, Hash]
21
24
  def api_version
22
25
  api_request(method: "apiinfo.version", params: {})
23
26
  end
24
27
 
28
+ # ZabbixManager::Basic.log does not like @client.options[:debug]
29
+ # @note 是否启用 debug 模式
30
+ # @return [boolean]
31
+ def debug?
32
+ !@options || @options[:debug]
33
+ end
34
+
35
+ # Log messages to stdout when debugging
36
+ # @note debug 打印信息
37
+ # @param message [String]
38
+ def log(message)
39
+ puts message if debug?
40
+ end
41
+
25
42
  # Log in to the Zabbix Server and generate an auth token using the API
26
- #
43
+ # @note 用户鉴权,登录成功即可获取令牌
44
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
27
45
  # @return [Hash, String]
28
46
  def auth
29
47
  api_request(
30
48
  method: "user.login",
31
49
  params: {
32
- user: @options[:user],
50
+ user: @options[:user],
33
51
  password: @options[:password]
34
52
  }
35
53
  )
36
54
  end
37
55
 
38
- # ZabbixManager::Basic.log does not like @client.options[:debug]
39
- #
40
- # @return [boolean]
41
- def debug?
42
- !@options || @options[:debug]
43
- end
44
-
45
56
  # Initializes a new Client object
46
- #
57
+ # @see 实例化 HTTP 客户端,同时进行用户权限签证
47
58
  # @param options [Hash]
48
- # @option opts [String] :url The url of zabbix_manager(example: 'http://localhost/zabbix/api_jsonrpc.php')
49
- # @option opts [String] :user
50
- # @option opts [String] :password
59
+ # @option opts [String] :url The url of zabbix
60
+ # @example 'http://zabbix.com/api_jsonrpc.php'
61
+ # @see 'https://www.zabbix.com/documentation/current/en/manual/api/reference'
62
+ # @option opts [String] :user A user for api auth.(required)
63
+ # @option opts [String] :password A password for api auth.(required)
51
64
  # @option opts [String] :http_user A user for basic auth.(optional)
52
65
  # @option opts [String] :http_password A password for basic auth.(optional)
53
66
  # @option opts [Integer] :timeout Set timeout for requests in seconds.(default: 60)
54
- #
67
+ # @raise [ZbxError] Error raised when api_version not support.
55
68
  # @return [ZabbixManager::Client]
56
69
  def initialize(options = {})
57
70
  @options = options
71
+ raise ZabbixManager::ZbxError, "Missing required argument: :url" unless @options.key?(:url)
72
+ raise ZabbixManager::ZbxError, "Missing required argument: :user" unless @options.key?(:user)
73
+ raise ZabbixManager::ZbxError, "Missing required argument: :password" unless @options.key?(:password)
74
+
58
75
  if !ENV["http_proxy"].nil? && options[:no_proxy] != true
59
76
  @proxy_uri = URI.parse(ENV["http_proxy"])
60
77
  @proxy_host = @proxy_uri.host
@@ -65,42 +82,45 @@ class ZabbixManager
65
82
  if api_version.match?(/^7\.\d+\.\d+$/)
66
83
  message = "Zabbix API version: #{api_version} is not supported by this version of zabbix_manager"
67
84
  if @options[:ignore_version]
68
- puts "[WARNING] #{message}" if @options[:debug]
85
+ log "[WARNING] #{message}"
69
86
  else
70
- raise ZabbixManager::ManagerError, message
87
+ raise ZabbixManager::ZbxError, message
71
88
  end
72
89
  end
73
90
 
74
91
  @auth_hash = auth
75
- puts "[DEBUG] Auth token: #{@auth_hash}" if @options[:debug]
92
+ log "[DEBUG] Auth token: #{@auth_hash}"
76
93
  end
77
94
 
78
- # Convert message body to JSON string for the Zabbix API
79
- #
95
+ # Convert payload body to JSON string for the Zabbix API
96
+ # @note 将 ruby 数据结构转换为 json 字符串,除用户登录和获取API版本外均添加令牌
80
97
  # @param body [Hash]
81
98
  # @return [String]
82
- def message_json(body)
83
- message = {
84
- method: body[:method],
85
- params: body[:params],
86
- id: id,
99
+ def json_payload(body)
100
+ payload = {
101
+ method: body[:method],
102
+ params: body[:params],
103
+ id: id,
87
104
  jsonrpc: "2.0"
88
105
  }
89
106
 
90
- # 除登录认证和获取版本后之外,都需要携带TOKEN查询
91
- message[:auth] = @auth_hash unless body[:method] == "apiinfo.version" || body[:method] == "user.login"
107
+ payload[:auth] = @auth_hash unless body[:method] == "apiinfo.version" || body[:method] == "user.login"
92
108
 
93
- JSON(message)
109
+ JSON(payload)
94
110
  end
95
111
 
112
+ # HTTP Request with the Zabbix API
113
+ # @note 实例化 Net::HTTP 对象,并发起 api 请求
96
114
  # @param body [String]
115
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
116
+ # @raise [HttpError] Error raised when HTTP status from Zabbix Server response is not a 200 OK.
97
117
  # @return [String]
98
118
  def http_request(body)
99
119
  uri = URI.parse(@options[:url])
100
120
 
101
121
  # set the time out the default (60) or to what the user passed
102
122
  timeout = @options[:timeout].nil? ? 60 : @options[:timeout]
103
- puts "[DEBUG] Timeout for request set to #{timeout} seconds" if @options[:debug]
123
+ log "[DEBUG] Timeout for request set to #{timeout} seconds"
104
124
 
105
125
  http =
106
126
  if @proxy_uri
@@ -121,48 +141,53 @@ class ZabbixManager
121
141
  request.basic_auth @options[:http_user], @options[:http_password] if @options[:http_user]
122
142
  request.add_field("Content-Type", "application/json-rpc")
123
143
  request.body = body
124
- puts "[DEBUG] HTTP request params: #{request.body}" if @options[:debug]
144
+ log "[DEBUG] HTTP request params: #{pretty_body(request.body)}"
125
145
 
126
146
  response = http.request(request)
127
147
  raise HttpError.new("HTTP Error: #{response.code} on #{@options[:url]}", response) unless response.code == "200"
128
148
 
129
- puts "[DEBUG] HTTP response answer: #{response.body}" if @options[:debug]
149
+ log "[DEBUG] HTTP response answer: #{pretty_body(response.body)}"
130
150
  response.body
131
151
  end
132
152
 
153
+ # Check the result returned by zabbix api is ok or not
154
+ # @note 发起 api 调用并对响应结果进行解析,如接口报错则抛出异常
155
+ # @@note 返回的数据为基于字符串索引,需要自行适配为符号索引
133
156
  # @param body [String]
157
+ # @raise [ZbxError] Error returned when there is a problem with the Zabbix API call.
134
158
  # @return [Hash, String]
135
159
  def _request(body)
136
160
  result = JSON.parse(http_request(body))
137
- # 异常信息抛出
138
161
  if result["error"]
139
- raise ManagerError.new(
162
+ raise ZbxError.new(
140
163
  "Server answer API error\n #{JSON.pretty_unparse(result["error"])}\n on request:\n #{pretty_body(body)}", result
141
164
  )
165
+ else
166
+ result["result"]
142
167
  end
143
-
144
- result["result"]
145
168
  end
146
169
 
170
+ # Pretty print json body
171
+ # @note 将 ruby 数据结构美化成 json 字符串后打印输出
172
+ # @param body [String]
173
+ # @return [Hash, String]
147
174
  def pretty_body(body)
148
- parsed_body = JSON.parse(body)
175
+ data = JSON.parse(body)
149
176
 
150
177
  # If password is in body hide it
151
- if parsed_body["params"].is_a?(Hash) && parsed_body["params"].key?("password")
152
- parsed_body["params"]["password"] =
153
- "***"
178
+ if data["params"].is_a?(Hash) && data["params"].key?("password")
179
+ data["params"]["password"] = "***"
154
180
  end
155
181
 
156
- JSON.pretty_unparse(parsed_body)
182
+ JSON.pretty_unparse(data)
157
183
  end
158
184
 
159
185
  # Execute Zabbix API requests and return response
160
- #
186
+ # @note 通用的 api 调用发起方法
161
187
  # @param body [Hash]
162
188
  # @return [Hash, String]
163
189
  def api_request(body)
164
- # ap body
165
- _request message_json(body)
190
+ _request json_payload(body)
166
191
  end
167
192
  end
168
193
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ZabbixManager
4
- VERSION = "5.1.0"
4
+ # @note find ./* -type f -exec sed -i 's/\[X\]/\[Y\]/g' {} +
5
+ VERSION = "5.1.1.pre.alpha1"
5
6
  end
@@ -1,40 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "zabbix_manager/version"
4
- require "zabbix_manager/client"
5
-
6
- require "zabbix_manager/basic/basic_alias"
7
- require "zabbix_manager/basic/basic_func"
8
- require "zabbix_manager/basic/basic_init"
9
- require "zabbix_manager/basic/basic_logic"
10
-
11
- require "zabbix_manager/classes/actions"
12
- require "zabbix_manager/classes/applications"
13
- require "zabbix_manager/classes/configurations"
14
- require "zabbix_manager/classes/errors"
15
- require "zabbix_manager/classes/events"
16
- require "zabbix_manager/classes/graphs"
17
- require "zabbix_manager/classes/hostgroups"
18
- require "zabbix_manager/classes/hosts"
19
- require "zabbix_manager/classes/httptests"
20
- require "zabbix_manager/classes/items"
21
- require "zabbix_manager/classes/maintenance"
22
- require "zabbix_manager/classes/mediatypes"
23
- require "zabbix_manager/classes/proxies"
24
- require "zabbix_manager/classes/problems"
25
- require "zabbix_manager/classes/roles"
26
- require "zabbix_manager/classes/screens"
27
- require "zabbix_manager/classes/scripts"
28
- require "zabbix_manager/classes/server"
29
- require "zabbix_manager/classes/templates"
30
- require "zabbix_manager/classes/triggers"
31
- require "zabbix_manager/classes/unusable"
32
- require "zabbix_manager/classes/usergroups"
33
- require "zabbix_manager/classes/usermacros"
34
- require "zabbix_manager/classes/users"
35
- require "zabbix_manager/classes/valuemaps"
36
- require "zabbix_manager/classes/drules"
3
+ # Dynamically load Ruby modules
4
+ Dir.glob(File.join(__dir__, "zabbix_manager", "**", "*.rb")).sort.each do |module_rb|
5
+ require module_rb
6
+ end
37
7
 
8
+ # @author: WENWU YAN
9
+ # @email: 968828@gmail.com
10
+ # @date: 2023/3/25下午4:47
38
11
  class ZabbixManager
39
12
  # @return [ZabbixManager::Client]
40
13
  attr_reader :client
@@ -47,27 +20,29 @@ class ZabbixManager
47
20
  new(options)
48
21
  end
49
22
 
23
+ # @note TODO
50
24
  # @return [ZabbixManager]
51
25
  def self.current
52
26
  @current ||= ZabbixManager.new
53
27
  end
54
28
 
55
29
  # Executes an API request directly using a custom query
56
- #
30
+ # @note 通用的查询方法,自行提供 method 和 params
57
31
  # @param data [Hash]
58
- # @return [Hash]
32
+ # @return [Hash, String]
59
33
  def query(data)
60
- @client.manager_request(method: data[:method], params: data[:params])
34
+ @client.api_request(method: data[:method], params: data[:params])
61
35
  end
62
36
 
63
37
  # Invalidate current authentication token
38
+ # @note TODO
64
39
  # @return [Boolean]
65
40
  def logout
66
41
  @client.logout
67
42
  end
68
43
 
69
44
  # Initializes a new ZabbixManager object
70
- #
45
+ # @note 实例化 ZabbixManager 对象,Client.new 进行参数检查
71
46
  # @param options [Hash]
72
47
  # @return [ZabbixManager::Client]
73
48
  def initialize(options = {})
@@ -104,6 +79,11 @@ class ZabbixManager
104
79
  @hostgroups ||= HostGroups.new(@client)
105
80
  end
106
81
 
82
+ # @return [ZabbixManager::HostInterfaces]
83
+ def hostinterfaces
84
+ @hostinterfaces ||= HostInterfaces.new(@client)
85
+ end
86
+
107
87
  # @return [ZabbixManager::Hosts]
108
88
  def hosts
109
89
  @hosts ||= Hosts.new(@client)
@@ -8,31 +8,27 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
8
8
  require "zabbix_manager/version"
9
9
 
10
10
  Gem::Specification.new do |spec|
11
- spec.add_dependency "http", ">= 4.0.0"
12
- spec.add_dependency "json", ">= 2.0"
13
- spec.add_development_dependency "bundler", ">= 2.0"
14
-
15
- spec.add_development_dependency "rspec", "~> 3.11"
16
- spec.add_development_dependency "yard", "~> 0.9.27"
17
- spec.add_development_dependency "yardstick", ">= 0.9.9"
11
+ spec.add_dependency "http", "5.1.1"
12
+ spec.add_dependency "json", "2.6.3"
13
+ spec.add_dependency "activesupport", "~> 7.0.4"
18
14
 
19
15
  spec.name = "zabbix_manager"
20
16
  spec.version = ZabbixManager::VERSION
21
17
  spec.authors = ["WENWU YAN"]
22
18
  spec.email = ["careline@foxmail.com"]
23
19
 
24
- spec.summary = "Simple and lightweight ruby module for working with the Zabbix API, support 4.0 5.0 6.0"
25
- spec.description = "Most codes borrowed from ZabbixApi, But changed some logic for everyday jobs. Support Zabbix 4.0 5.0 6.0"
20
+ spec.summary = "Common code for Zabbix API clients"
21
+ spec.description = "A Ruby interface to the Zabbix API"
26
22
  spec.homepage = "https://github.com/snmpd/zabbix_manager"
27
23
  spec.licenses = "MIT"
28
24
 
29
- # spec.metadata["allowed_push_host"] = "https://rubygems.org"
25
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
30
26
 
31
27
  spec.metadata["homepage_uri"] = spec.homepage
32
28
  spec.metadata["source_code_uri"] = "https://github.com/snmpd/zabbix_manager"
33
29
  spec.metadata["changelog_uri"] = "https://github.com/snmpd/zabbix_manager"
34
30
 
35
- spec.files = %w[CHANGELOG.md LICENSE.txt CODE_OF_CONDUCT.md README.md zabbix_manager.gemspec] + Dir["lib/**/*.rb"]
31
+ spec.files = %w[CHANGELOG.md LICENSE README.md zabbix_manager.gemspec] + Dir["lib/**/*.rb"]
36
32
  spec.require_paths = "lib"
37
33
  spec.required_ruby_version = ">= 2.7.0"
38
34
  end