Class: ZabbixManager::HostInterfaces
- Defined in:
- lib/zabbix_manager/classes/hostinterfaces.rb
Overview
封装高频使用的 Zabbix 主机接口查询与幂等更新。
Instance Method Summary collapse
-
#delete_many(hostid:, interfaceids:) ⇒ Array<Integer>
删除明确指定的主机接口;关联监控项由 Zabbix 按官方规则处理。.
-
#for_host(hostid, select_items: nil) ⇒ Array<Hash>
查询指定主机的全部接口,可选择同时返回关联监控项。.
-
#identify ⇒ String
使用接口 ID 作为通用查询标识。.
-
#key ⇒ String
返回接口对象的主键字段。.
-
#method_name ⇒ String
返回对应的 Zabbix API 模块名。.
-
#reconcile_for_host(hostid:, interfaces:) ⇒ Array<Integer>
幂等创建或更新一个主机的接口集合。.
-
#validate(interfaces) ⇒ Array<Hash>
规范化并校验接口定义,不执行远端查询或写入。.
-
#validate_for_create(interfaces) ⇒ Array<Hash>
校验 host.create 中全部接口的创建必填合同。.
Methods inherited from Basic
#add, #all, #create, #create_or_update, #create_raw, #default_options, #delete, #delete_raw, #destroy, #dump_by_id, #get, #get_full_data, #get_id, #get_or_create, #get_raw, #hash_equals?, #identity_filter, #initialize, #keys, #log, #normalize_hash, #parse_keys, #update, #update_raw
Constructor Details
This class inherits a constructor from ZabbixManager::Basic
Instance Method Details
#delete_many(hostid:, interfaceids:) ⇒ Array<Integer>
删除明确指定的主机接口;关联监控项由 Zabbix 按官方规则处理。
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 59 def delete_many(hostid:, interfaceids:) ids = Array(interfaceids).filter_map { |value| value.to_s.strip.presence }.uniq raise Invalid, "interfaceids are required" if ids.empty? owned_ids = for_host(hostid).map { |interface| interface.fetch("interfaceid").to_s } foreign_ids = ids - owned_ids raise Conflict, "interfaces do not belong to host #{hostid}: #{foreign_ids.join(", ")}" if foreign_ids.any? result = @client.api_request(method: "hostinterface.delete", params: ids) Array(result.fetch("interfaceids")).map(&:to_i) end |
#for_host(hostid, select_items: nil) ⇒ Array<Hash>
查询指定主机的全部接口,可选择同时返回关联监控项。
26 27 28 29 30 31 32 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 26 def for_host(hostid, select_items: nil) raise Invalid, "hostid is required" if hostid.blank? params = { hostids: hostid, output: "extend" } params[:selectItems] = select_items if select_items @client.api_request(method: "hostinterface.get", params: params) end |
#identify ⇒ String
使用接口 ID 作为通用查询标识。
20 21 22 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 20 def identify "interfaceid" end |
#key ⇒ String
返回接口对象的主键字段。
14 15 16 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 14 def key "interfaceid" end |
#method_name ⇒ String
返回对应的 Zabbix API 模块名。
8 9 10 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 8 def method_name "hostinterface" end |
#reconcile_for_host(hostid:, interfaces:) ⇒ Array<Integer>
幂等创建或更新一个主机的接口集合。
50 51 52 53 54 55 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 50 def reconcile_for_host(hostid:, interfaces:) @client.with_upsert_lock("host-interfaces:#{hostid}") do plan = plan_for_host(hostid: hostid, interfaces: interfaces) apply_plan(hostid: hostid, plan: plan) end end |
#validate(interfaces) ⇒ Array<Hash>
规范化并校验接口定义,不执行远端查询或写入。
36 37 38 39 40 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 36 def validate(interfaces) desired = Array.wrap(interfaces).map { |interface| normalize_definition(interface) } validate_definitions!(desired) desired end |
#validate_for_create(interfaces) ⇒ Array<Hash>
校验 host.create 中全部接口的创建必填合同。
44 45 46 |
# File 'lib/zabbix_manager/classes/hostinterfaces.rb', line 44 def validate_for_create(interfaces) validate(interfaces).each { |attributes| validate_create_definition!(attributes) } end |