Class: ZabbixManager::Graphs

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/graphs.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_id, #get_or_create, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #normalize_hash, #parse_keys, #update, #update_raw

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#get_full_data(data) ⇒ Hash

按名称搜索并获取图形的完整数据。

Parameters:

  • data (Hash)

    包含图形识别字段及其值的查询条件

Returns:

  • (Hash)

    匹配的图形完整数据

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

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



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

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

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

#get_ids_by_host(data) ⇒ Array

获取指定主机的图形 ID,并可按名称片段进一步过滤。

Parameters:

  • data (Hash)

    包含 host,且可包含 filter 的查询条件

Returns:

  • (Array)

    匹配的图形 ID 列表

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

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



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

def get_ids_by_host(data)
  result = @client.api_request(
    method: "graph.get",
    params: {
      filter: {
        host: data[:host]
      },
      output: "extend"
    }
  )

  result.filter_map do |graph|
    num = graph["graphid"]
    name = graph["name"]
    filter = data[:filter]

    num if filter.nil? || name.include?(filter.to_s)
  end
end

#get_items(data) ⇒ Hash

获取指定图形包含的图形监控项。

Parameters:

  • data (Hash, String, Integer)

    图形 ID

Returns:

  • (Hash)

    图形监控项数据

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

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



71
72
73
74
75
76
77
78
79
# File 'lib/zabbix_manager/classes/graphs.rb', line 71

def get_items(data)
  @client.api_request(
    method: "graphitem.get",
    params: {
      graphids: [data],
      output: "extend"
    }
  )
end

#identifyString

返回图形对象用于业务识别的字段名。

Returns:

  • (String)


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

def identify
  "name"
end

#identity_filter(data) ⇒ Hash

生成由图形名称和所属模板构成的稳定查询条件。

Parameters:

  • data (Hash)

    包含 name 和 templateid 的图形属性

Returns:

  • (Hash)

    图形唯一查询条件



84
85
86
87
# File 'lib/zabbix_manager/classes/graphs.rb', line 84

def identity_filter(data)
  attributes = data.deep_symbolize_keys
  { name: attributes.fetch(:name), templateid: attributes.fetch(:templateid) }
end

#method_nameString

返回图形对象对应的 Zabbix API 方法前缀。

Returns:

  • (String)


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

def method_name
  "graph"
end