Class: ZabbixManager::Hosts

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/hosts.rb

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create_or_update, #create_raw, #delete, #delete_raw, #destroy, #get, #get_full_data, #get_id, #get_or_create, #get_raw, #hash_equals?, #identity_filter, #initialize, #key, #keys, #log, #normalize_hash, #parse_keys, #update, #update_raw

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#create(data) ⇒ Object

使用显式群组和接口创建主机,不注入隐藏凭据。



51
52
53
54
55
56
# File 'lib/zabbix_manager/classes/hosts.rb', line 51

def create(data)
  attributes = default_options.merge(data.deep_symbolize_keys)
  validate_create_attributes!(attributes)
  result = @client.api_request(method: "host.create", params: attributes)
  first_id(result, "hostids")
end

#default_optionsObject

返回创建主机时的克制默认值。



16
17
18
19
20
21
# File 'lib/zabbix_manager/classes/hosts.rb', line 16

def default_options
  {
    status: 0,
    inventory_mode: 1
  }
end

#dump_by_id(data) ⇒ Object

按主机 ID 查询完整主机及接口、群组和模板。



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zabbix_manager/classes/hosts.rb', line 24

def dump_by_id(data)
  log "[DEBUG] Call dump_by_id with parameters: #{data.inspect}"
  @client.api_request(
    method: "host.get",
    params: {
      filter: { key.to_sym => data[key.to_sym] },
      output: "extend",
      selectGroups: "extend",
      selectInterfaces: "extend",
      selectParentTemplates: ["templateid", "host", "name"]
    }
  )
end

#find_by_candidates(candidates) ⇒ Object

用技术名、可见名或接口 IP/DNS 解析唯一主机。



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/zabbix_manager/classes/hosts.rb', line 121

def find_by_candidates(candidates)
  matches = Array(candidates).filter_map { |candidate| candidate.to_s.strip.presence }.uniq.flat_map do |candidate|
    hosts = [find_host_by_filter(host: candidate), find_host_by_filter(name: candidate)].compact
    interface = find_interface_by_endpoint(candidate)
    hosts << find_host_by_filter(hostid: interface["hostid"]) if interface
    hosts
  end
  matches.uniq! { |host| host.fetch("hostid") }
  if matches.length > 1
    raise Conflict, "host candidates resolve to multiple hosts; provide an explicit host object"
  end

  matches.first
end

#find_by_id(hostid) ⇒ Hash?

按主机 ID 查询唯一主机,并返回表达式需要的技术名称。

Returns:

  • (Hash, nil)


138
139
140
# File 'lib/zabbix_manager/classes/hosts.rb', line 138

def find_by_id(hostid)
  find_host_by_filter(hostid: hostid)
end

#get_host_id(name) ⇒ Object

按技术主机名查询主机 ID。



111
112
113
# File 'lib/zabbix_manager/classes/hosts.rb', line 111

def get_host_id(name)
  find_host_id(host: name)
end

#get_hostid_by_name(name) ⇒ Object

按可见名称查询主机 ID。



116
117
118
# File 'lib/zabbix_manager/classes/hosts.rb', line 116

def get_hostid_by_name(name)
  find_host_id(name: name)
end

#get_interface_id(hostid) ⇒ Object

返回主机的第一个接口 ID;高频业务应优先使用 hostinterfaces。



106
107
108
# File 'lib/zabbix_manager/classes/hosts.rb', line 106

def get_interface_id(hostid)
  interfaces_for(hostid).first&.fetch("interfaceid", nil)
end

#identifyObject

使用技术主机名作为通用 CRUD 标识。



11
12
13
# File 'lib/zabbix_manager/classes/hosts.rb', line 11

def identify
  "host"
end

#interfaces_for(hostid) ⇒ Object

查询指定主机的全部接口。



101
102
103
# File 'lib/zabbix_manager/classes/hosts.rb', line 101

def interfaces_for(hostid)
  hostinterfaces.for_host(hostid)
end

#method_nameObject

返回主机对应的 Zabbix API 模块名。



6
7
8
# File 'lib/zabbix_manager/classes/hosts.rb', line 6

def method_name
  "host"
end

#reconcile(data) ⇒ Object

幂等同步主机元数据和显式接口,调用方保持业务字段权威。



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/zabbix_manager/classes/hosts.rb', line 70

def reconcile(data)
  attributes = validate(data)
  host = attributes.fetch(:host)

  hostid = get_id(host: host)
  unless hostid
    outcome, hostid = @client.with_upsert_lock("host-create:#{host}") do
      current_id = get_id(host: host)
      current_id ? [:existing, current_id] : [:created, create(attributes)]
    end
    return hostid.to_i if outcome == :created
  end

  update_existing(hostid, attributes)
end

#set_status(hostids, enabled:) ⇒ Array<Integer>

批量启用或停用主机。

Returns:

  • (Array<Integer>)

Raises:



144
145
146
147
148
149
150
151
# File 'lib/zabbix_manager/classes/hosts.rb', line 144

def set_status(hostids, enabled:)
  ids = Array(hostids).filter_map { |value| value.to_s.strip.presence }.uniq
  raise Invalid, "hostids are required" if ids.empty?

  params = ids.map { |hostid| { hostid: hostid, status: enabled ? 0 : 1 } }
  result = @client.api_request(method: "host.update", params: params)
  Array(result.fetch("hostids")).map(&:to_i)
end

从指定主机批量解除模板关联。



39
40
41
42
43
44
45
46
47
48
# File 'lib/zabbix_manager/classes/hosts.rb', line 39

def unlink_templates(data)
  result = @client.api_request(
    method: "host.massRemove",
    params: {
      hostids: data[:hosts_id],
      templateids: data[:templates_id]
    }
  )
  !result.empty?
end

#update_host_to_serial(data) ⇒ Object

把可见名称对应的主机更新为稳定技术标识。



154
155
156
157
158
159
160
161
162
# File 'lib/zabbix_manager/classes/hosts.rb', line 154

def update_host_to_serial(data)
  attributes = data.deep_symbolize_keys
  hostid = get_hostid_by_name(attributes[:name])
  return nil unless hostid

  attributes.delete(:templates)
  @client.api_request(method: "host.update", params: attributes.merge(hostid: hostid))
  hostid.to_i
end

#validate(data) ⇒ Hash

规范化并校验设备定义,不执行远端查询或写入。 已存在设备允许只更新部分字段,新设备的群组和接口由 create 再次严格校验。

Returns:

  • (Hash)

Raises:



61
62
63
64
65
66
67
# File 'lib/zabbix_manager/classes/hosts.rb', line 61

def validate(data)
  attributes = data.deep_symbolize_keys
  raise Invalid, "host is required" if attributes[:host].blank?

  hostinterfaces.validate(attributes[:interfaces]) if attributes.key?(:interfaces)
  attributes
end