Class: ZabbixManager::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_manager/basic/basic_func.rb,
lib/zabbix_manager/basic/basic_init.rb,
lib/zabbix_manager/basic/basic_alias.rb,
lib/zabbix_manager/basic/basic_logic.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ZabbixManager::Basic

使用 ZabbixManager 客户端初始化基础资源对象。

Parameters:



9
10
11
# File 'lib/zabbix_manager/basic/basic_init.rb', line 9

def initialize(client)
  @client = client
end

Instance Method Details

#add(data) ⇒ Integer, Boolean

通过 Zabbix API 创建对象。

Parameters:

  • data (Hash)

    待创建的对象属性

Returns:

  • (Integer)

    创建单个对象时返回对象 ID

  • (Boolean)

    创建多个对象时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



22
23
24
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 22

def add(data)
  create(data)
end

#allHash

获取当前资源的全部对象,并返回业务标识到 ID 的映射。

Returns:

  • (Hash)

    业务标识到对象 ID 的映射

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



138
139
140
141
142
143
144
145
146
147
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 138

def all
  result = {}
  @client.api_request(
    method: "#{method_name}.get",
    params: { output: "extend" }
  ).each do |item|
    result[item[identify]] = item[key]
  end
  result
end

#create(data) ⇒ Integer, Boolean

合并默认选项后通过 Zabbix API 创建对象。

Parameters:

  • data (Hash)

    待创建的对象属性

Returns:

  • (Integer)

    创建单个对象时返回对象 ID

  • (Boolean)

    创建多个对象时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 12

def create(data)
  log "[DEBUG] Call create with parameters: #{data.inspect}"

  # 判断是否绑定默认选项并重新生成配置
  data_with_default = default_options.empty? ? data : default_options.merge(data)
  data_create       = [data_with_default]

  # 调用实例方法
  result = @client.api_request(method: "#{method_name}.create", params: data_create)
  # 判断是否执行成功并返回结果
  parse_keys result
end

#create_or_update(data) ⇒ Integer, Boolean

按业务标识查找对象,存在则更新,否则创建。

Parameters:

  • data (Hash)

    包含对象业务标识及待写入属性

Returns:

  • (Integer)

    创建或更新后的对象 ID

  • (Boolean)

    批量操作时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



47
48
49
50
51
52
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 47

def create_or_update(data)
  log "[DEBUG] Call create_or_update with parameters: #{data.inspect}"

  id = get_id(**identity_filter(data))
  id ? update(data.merge(key.to_sym => id)) : create(data)
end

#create_raw(data) ⇒ Object

使用原始参数直接创建 Zabbix 对象。

Parameters:

  • data (Hash, Array)

    原始创建参数

Returns:

  • (Object)

    API 返回结果



204
205
206
207
208
209
210
211
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 204

def create_raw(data)
  log "[DEBUG] Call create_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.create",
    params: data
  )
end

#default_optionsHash

返回资源创建时使用的默认选项,子类可按需覆盖。

Returns:

  • (Hash)

    默认选项



24
25
26
# File 'lib/zabbix_manager/basic/basic_init.rb', line 24

def default_options
  {}
end

#delete(data) ⇒ Integer, Boolean

通过 Zabbix API 删除对象。

Parameters:

  • data (Hash)

    包含待删除对象的 ID 字段和值

Returns:

  • (Integer)

    删除单个对象时返回对象 ID

  • (Boolean)

    删除多个对象时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



32
33
34
35
36
37
38
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 32

def delete(data)
  log "[DEBUG] Call delete with parameters: #{data.inspect}"

  data_delete = [data]
  result      = @client.api_request(method: "#{method_name}.delete", params: data_delete)
  parse_keys result
end

#delete_raw(data) ⇒ Object

使用原始参数直接删除 Zabbix 对象。

Parameters:

  • data (Hash, Array)

    原始删除参数

Returns:

  • (Object)

    API 返回结果



230
231
232
233
234
235
236
237
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 230

def delete_raw(data)
  log "[DEBUG] Call delete_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.delete",
    params: data
  )
end

#destroy(data) ⇒ Integer, Boolean

通过 Zabbix API 删除对象。

Parameters:

  • data (Hash)

    包含对象标识字段及其值

Returns:

  • (Integer)

    删除单个对象时返回对象 ID

  • (Boolean)

    删除多个对象时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



33
34
35
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 33

def destroy(data)
  delete(data)
end

#dump_by_id(data) ⇒ Hash

按对象 ID 字段从 Zabbix API 获取扩展数据。

Parameters:

  • data (Hash)

    包含对象 ID 字段及其值

Returns:

  • (Hash)

    对象扩展数据

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 119

def dump_by_id(data)
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        key.to_sym => data[key.to_sym]
      },
      output: "extend"
    }
  )
end

#get(data) ⇒ Hash

按标识字段从 Zabbix API 获取对象完整数据。

Parameters:

  • data (Hash)

    包含对象标识字段及其值

Returns:

  • (Hash)

    对象完整数据

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



11
12
13
# File 'lib/zabbix_manager/basic/basic_alias.rb', line 11

def get(data)
  get_full_data(data)
end

#get_full_data(data) ⇒ Hash

按业务标识从 Zabbix API 获取对象扩展数据。

Parameters:

  • data (Hash)

    包含对象业务标识字段及其值

Returns:

  • (Hash)

    对象扩展数据

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 84

def get_full_data(data)
  log "[DEBUG] Call get_full_data with parameters: #{data.inspect}"

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        identify.to_sym => data[identify.to_sym]
      },
      output: "extend"
    }
  )
end

#get_id(data) ⇒ Integer?

按业务标识精确查询对象 ID,并拒绝歧义结果。

Parameters:

  • data (Hash)

    业务标识过滤条件

Returns:

  • (Integer, nil)

    Zabbix 对象 ID,未找到时返回空值

Raises:

  • (ApiError)

    API 调用失败、缺少标识字段或结果不唯一时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 155

def get_id(data)
  log "[DEBUG] Call get_id with parameters: #{data.inspect}"

  data = data.deep_symbolize_keys
  # 缺少业务标识字段时立即终止查询
  name = data[identify.to_sym]
  raise Invalid, "#{identify} not supplied in call to get_id" if name.nil?

  result = @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: data,
      output: [key, identify]
    }
  )
  matches = result.select { |item| item[identify].to_s == name.to_s }
  raise Conflict, "multiple #{method_name} objects match #{identify}=#{name}" if matches.length > 1

  matches.first&.fetch(key, nil)&.to_i
end

#get_or_create(data) ⇒ Integer

按业务标识获取对象,不存在时创建。

Parameters:

  • data (Hash)

    包含对象业务标识及待创建属性

Returns:

  • (Integer)

    Zabbix 对象 ID

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



182
183
184
185
186
187
188
189
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 182

def get_or_create(data)
  log "[DEBUG] Call get_or_create with parameters: #{data.inspect}"

  unless (id = get_id(**identity_filter(data)))
    id = create(data)
  end
  id
end

#get_raw(data) ⇒ Hash

将调用方参数直接传给对应的 Zabbix get 方法。

Parameters:

  • data (Hash)

    原始查询参数

Returns:

  • (Hash)

    API 返回数据

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



104
105
106
107
108
109
110
111
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 104

def get_raw(data)
  log "[DEBUG] Call get_raw with parameters: #{data.inspect}"

  @client.api_request(
    method: "#{method_name}.get",
    params: data
  )
end

#hash_equals?(first_hash, second_hash) ⇒ Boolean

比较实际哈希是否包含期望哈希中的全部键值。

Parameters:

  • first_hash (Hash)

    实际数据

  • second_hash (Hash)

    期望数据

Returns:

  • (Boolean)

    是否匹配



26
27
28
29
30
# File 'lib/zabbix_manager/basic/basic_func.rb', line 26

def hash_equals?(first_hash, second_hash)
  actual = normalize_hash(first_hash)
  expected = normalize_hash(second_hash)
  actual.slice(*expected.keys) == expected
end

#identifyString

定义资源业务标识字段占位,要求子类覆盖。

Returns:

  • (String)

    业务标识字段名

Raises:

  • (ApiError)

    基础类不能直接提供标识字段时抛出



46
47
48
# File 'lib/zabbix_manager/basic/basic_init.rb', line 46

def identify
  raise Invalid, "Can't call identify here"
end

#identity_filter(data) ⇒ Hash

提取通用获取或写入操作使用的远端业务标识。

Parameters:

  • data (Hash)

    对象属性

Returns:

  • (Hash)

    业务标识过滤条件



195
196
197
198
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 195

def identity_filter(data)
  attributes = data.deep_symbolize_keys
  { identify.to_sym => attributes.fetch(identify.to_sym) }
end

#keyString

根据 API 方法名生成对象 ID 字段名。

Returns:

  • (String)

    对象 ID 字段名



38
39
40
# File 'lib/zabbix_manager/basic/basic_init.rb', line 38

def key
  "#{method_name}id"
end

#keysString

根据单数 ID 字段名生成 API 返回结果中的复数字段名。

Returns:

  • (String)

    复数 ID 字段名



31
32
33
# File 'lib/zabbix_manager/basic/basic_init.rb', line 31

def keys
  "#{key}s"
end

#log(message) ⇒ void

This method returns an undefined value.

将调试信息交给客户端结构化日志,并移除可能包含请求参数的片段。

Parameters:

  • message (String)

    待记录的调试信息



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/zabbix_manager/basic/basic_func.rb', line 9

def log(message)
  return unless @client.options[:debug]

  raw_message = message.to_s
  safe_message = if raw_message.start_with?("[DEBUG]")
                   raw_message[/\A\[DEBUG\]\s+Call\s+[a-z_]+/i] || "[DEBUG] domain operation"
                 else
                   raw_message
                 end
  @client.log(:debug, "domain.operation", message: safe_message)
end

#method_nameString?

返回子类对应的 Zabbix API 方法名;由具体资源类实现。

Returns:

  • (String, nil)

    API 方法名



17
18
19
# File 'lib/zabbix_manager/basic/basic_init.rb', line 17

def method_name
  raise Invalid, "Can't call method_name here"
end

#normalize_hash(hash) ⇒ Hash

将哈希值递归规范为字符串,并忽略 hostid。

Parameters:

  • hash (Hash)

    待规范化的哈希

Returns:

  • (Hash)

    规范化后的副本



36
37
38
# File 'lib/zabbix_manager/basic/basic_func.rb', line 36

def normalize_hash(hash)
  hash.deep_symbolize_keys.except(:hostid).deep_transform_values(&:to_s)
end

#parse_keys(data) ⇒ Integer, ...

从 API 结果中解析单个对象 ID,或透传布尔结果。

Parameters:

  • data (Hash, Boolean)

    API 返回结果

Returns:

  • (Integer, Boolean, nil)

    对象 ID、布尔结果或空值



44
45
46
47
48
49
50
51
52
53
# File 'lib/zabbix_manager/basic/basic_func.rb', line 44

def parse_keys(data)
  case data
  when Hash
    data.empty? ? nil : data[keys][0].to_i
  when TrueClass
    true
  when FalseClass
    false
  end
end

#update(data, force = false) ⇒ Integer, Boolean

在属性发生变化或强制更新时通过 Zabbix API 更新对象。

Parameters:

  • data (Hash)

    包含对象 ID 及待更新属性

  • force (Boolean) (defaults to: false)

    属性一致时是否仍强制更新

Returns:

  • (Integer)

    更新后的对象 ID

  • (Boolean)

    批量操作时返回操作结果

Raises:

  • (ApiError)

    Zabbix API 调用失败时抛出

  • (TransportError)

    Zabbix 服务端返回非 200 状态时抛出



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 62

def update(data, force = false)
  log "[DEBUG] Call update with parameters: #{data.inspect}"
  dump = {}
  dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
    dump = item.deep_symbolize_keys if item[key].to_i == data[key.to_sym].to_i
  end
  if hash_equals?(dump, data) && !force
    log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
    data[key.to_sym].to_i
  else
    data_update = [data]
    result      = @client.api_request(method: "#{method_name}.update", params: data_update)
    parse_keys result
  end
end

#update_raw(data) ⇒ Object

使用原始参数直接更新 Zabbix 对象。

Parameters:

  • data (Hash, Array)

    原始更新参数

Returns:

  • (Object)

    API 返回结果



217
218
219
220
221
222
223
224
# File 'lib/zabbix_manager/basic/basic_logic.rb', line 217

def update_raw(data)
  log "[DEBUG] Call update_raw with parameters: #{data.inspect}"
  # 请求创建数据
  @client.api_request(
    method: "#{method_name}.update",
    params: data
  )
end