Class: ZabbixManager::Screens

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbix_manager/classes/screens.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 (String, Array)

    要删除的聚合图形 ID

Returns:

  • (Integer, nil)

    已删除聚合图形的 ID,无结果时返回 nil

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

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



44
45
46
47
# File 'lib/zabbix_manager/classes/screens.rb', line 44

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

#get_or_create_for_host(data) ⇒ Integer

按名称获取聚合图形,不存在时按图形 ID 网格化创建。

Parameters:

  • data (Hash)

    包含 screen_name、graphids 及可选布局参数

Returns:

  • (Integer)

    聚合图形 ID

Raises:

  • (ApiError)

    Zabbix API 返回业务错误时抛出

  • (TransportError)

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



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/zabbix_manager/classes/screens.rb', line 55

def get_or_create_for_host(data)
  screen_name = data[:screen_name]
  graphids = Array(data[:graphids])
  raise Invalid, "screen_name is required" if screen_name.blank?
  raise Invalid, "graphids must contain at least one graph" if graphids.empty?

  hsize = positive_hsize(data.fetch(:hsize, 3))

  valign = data[:valign] || 2
  halign = data[:halign] || 2
  rowspan = data[:rowspan] || 1
  colspan = data[:colspan] || 1
  height = data[:height] || 320
  width = data[:width] || 200
  vsize = data[:vsize] || graphids.length.fdiv(hsize).ceil
  screenid = get_id(name: screen_name)

  unless screenid
    screenitems = graphids.each_with_index.map do |graphid, index|
      {
        resourcetype: 0,
        resourceid: graphid,
        x: index % hsize,
        y: index / hsize,
        valign: valign,
        halign: halign,
        rowspan: rowspan,
        colspan: colspan,
        height: height,
        width: width
      }
    end

    screenid = create(
      name: screen_name,
      hsize: hsize,
      vsize: vsize,
      screenitems: screenitems
    )
  end
  screenid
end

#identifyString

返回用于唯一识别聚合图形的业务字段名。

Returns:

  • (String)

    聚合图形的业务标识字段名



34
35
36
# File 'lib/zabbix_manager/classes/screens.rb', line 34

def identify
  "name"
end

#method_nameString

返回 Zabbix API 中聚合图形对象的方法名前缀。

Returns:

  • (String)

    聚合图形对象的方法名前缀



27
28
29
# File 'lib/zabbix_manager/classes/screens.rb', line 27

def method_name
  "screen"
end