Class: ZabbixManager::Templates

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #create_or_update, #create_raw, #default_options, #delete_raw, #destroy, #dump_by_id, #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

#delete(data) ⇒ Integer?

删除指定模板并返回首个已删除模板的 ID。

Parameters:

  • data (Array)

    要删除的 templateid 数组

Returns:

  • (Integer, nil)

    已删除模板的 ID,无结果时返回 nil

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

    Zabbix 服务返回非成功 HTTP 状态时抛出



25
26
27
28
# File 'lib/zabbix_manager/classes/templates.rb', line 25

def delete(data)
  result = @client.api_request(method: "template.delete", params: [data])
  result.empty? ? nil : result["templateids"][0].to_i
end

#get_ids_by_host(data) ⇒ Array<String>

根据查询条件获取模板 ID 列表。

Parameters:

  • data (Hash)

    Zabbix template.get 查询参数

Returns:

  • (Array<String>)

    模板 ID 数组

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

    Zabbix 服务返回非成功 HTTP 状态时抛出



36
37
38
39
40
# File 'lib/zabbix_manager/classes/templates.rb', line 36

def get_ids_by_host(data)
  @client.api_request(method: "template.get", params: data).map do |tmpl|
    tmpl["templateid"]
  end
end

#get_template_ids(data) ⇒ Array<Hash>?

按模板技术名称查询,并返回可直接用于主机或模板更新的引用对象。

Parameters:

  • data (String, Array<String>)

    一个或多个模板技术名称

Returns:

  • (Array<Hash>, nil)

    templateid 引用数组,未找到时返回 nil



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/zabbix_manager/classes/templates.rb', line 99

def get_template_ids(data)
  result = @client.api_request(
    method: "template.get",
    params: {
      output: "extend",
      filter: {
        host: Array(data)
      }
    }
  ).map { |template| { templateid: template.fetch("templateid") } }

  result.presence
end

#identifyString

返回用于唯一识别模板的业务字段名。

Returns:

  • (String)

    模板对象的业务标识字段名



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

def identify
  "host"
end

#mass_add(data) ⇒ Boolean

批量为主机添加模板关联。

Parameters:

  • data (Hash)

    包含 hosts_id 和 templates_id 数组

Returns:

  • (Boolean)

    API 是否返回有效结果

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

    Zabbix 服务返回非成功 HTTP 状态时抛出



65
66
67
68
69
70
71
72
73
74
# File 'lib/zabbix_manager/classes/templates.rb', line 65

def mass_add(data)
  result = @client.api_request(
    method: "template.massAdd",
    params: {
      hosts: data[:hosts_id].map { |t| { hostid: t } },
      templates: data[:templates_id].map { |t| { templateid: t } }
    }
  )
  result.empty? ? false : true
end

#mass_remove(data) ⇒ Boolean

批量移除主机的模板或主机组关联。

Parameters:

  • data (Hash)

    包含 hosts_id、templates_id 及可选 group_id

Returns:

  • (Boolean)

    API 是否返回有效结果

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

    Zabbix 服务返回非成功 HTTP 状态时抛出



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zabbix_manager/classes/templates.rb', line 82

def mass_remove(data)
  result = @client.api_request(
    method: "template.massRemove",
    params: {
      hostids: data[:hosts_id],
      templateids: data[:templates_id],
      groupids: data[:group_id],
      force: 1
    }
  )
  result.empty? ? false : true
end

#mass_update(data) ⇒ Boolean

批量更新主机与模板的关联关系。

Parameters:

  • data (Hash)

    包含 hosts_id 和 templates_id 数组

Returns:

  • (Boolean)

    API 是否返回有效结果

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

    Zabbix 服务返回非成功 HTTP 状态时抛出



48
49
50
51
52
53
54
55
56
57
# File 'lib/zabbix_manager/classes/templates.rb', line 48

def mass_update(data)
  result = @client.api_request(
    method: "template.massUpdate",
    params: {
      hosts: data[:hosts_id].map { |t| { hostid: t } },
      templates: data[:templates_id].map { |t| { templateid: t } }
    }
  )
  result.empty? ? false : true
end

#method_nameString

返回 Zabbix API 中模板对象的方法名前缀。

Returns:

  • (String)

    模板对象的方法名前缀



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

def method_name
  "template"
end