Class: ZabbixManager::HostGroups

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

Instance Method Summary collapse

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

#get_hostgroup_ids(data) ⇒ Array<Hash>?

批量解析主机群组名称并返回对应的群组 ID。

Parameters:

  • data (Array<String>, String)

    待查询的群组名称

Returns:

  • (Array<Hash>, nil)

    群组 ID 列表,未命中时返回 nil



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/zabbix_manager/classes/hostgroups.rb', line 29

def get_hostgroup_ids(data)
  names = normalized_names(data)
  return nil if names.empty?

  groups = @client.api_request(
    method: "hostgroup.get",
    params: {
      output: %w[groupid name],
      filter: { name: names }
    }
  )

  groups.empty? ? nil : groups.map { |group| { groupid: group.fetch("groupid") } }
end

#get_or_create_hostgroups(data) ⇒ Array<Hash>

批量查询并创建缺失的主机群组,避免逐名称重复查询。

Parameters:

  • data (Array<String>, String)

    待确保存在的群组名称

Returns:

  • (Array<Hash>)

    群组 ID 列表



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/zabbix_manager/classes/hostgroups.rb', line 47

def get_or_create_hostgroups(data)
  names = normalized_names(data)
  existing = @client.api_request(
    method: "hostgroup.get",
    params: { output: %w[groupid name], filter: { name: names } }
  ).index_by { |group| group.fetch("name") }

  names.map do |name|
    group = existing[name]
    next({ groupid: group.fetch("groupid") }) if group

    result = @client.api_request(method: "hostgroup.create", params: { name: name })
    { groupid: result.fetch("groupids").first }
  end
end

#identifyString

返回主机群组用于业务识别的字段名。

Returns:

  • (String)


15
16
17
# File 'lib/zabbix_manager/classes/hostgroups.rb', line 15

def identify
  "name"
end

#keyString

返回主机群组的 Zabbix ID 字段名。

Returns:

  • (String)


22
23
24
# File 'lib/zabbix_manager/classes/hostgroups.rb', line 22

def key
  "groupid"
end

#method_nameString

返回主机群组对应的 Zabbix API 方法前缀。

Returns:

  • (String)


8
9
10
# File 'lib/zabbix_manager/classes/hostgroups.rb', line 8

def method_name
  "hostgroup"
end