Class: ZabbixManager::Items

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #delete, #destroy, #dump_by_id, #get, #get_full_data, #get_id, #get_ids, #get_ids_by_identify, #get_key_ids, #get_key_ids_by_identify, #get_or_create_keys, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_hashes, #normalize_array, #normalize_hash, #parse_keys, #request_raw, #symbolize_keys, #update

Constructor Details

This class inherits a constructor from ZabbixManager::Basic

Instance Method Details

#create_or_update(data) ⇒ Object

通过 Zabbix API 创建或更新 Item 对象



65
66
67
68
# File 'lib/zabbix_manager/classes/items.rb', line 65

def create_or_update(data)
  itemid = get_id(name: data[:name], hostid: data[:hostid])
  itemid ? update(data.merge(itemid: itemid)) : create(data)
end

#default_optionsObject

通过 Zabbix API 创建 Item 对象时使用的默认选项



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/zabbix_manager/classes/items.rb', line 16

def default_options
  {
    name:                  nil,
    key_:                  nil,
    hostid:                nil,
    delay:                 60,
    history:               3600,
    status:                0,
    type:                  7,
    snmp_community:        "",
    snmp_oid:              "",
    value_type:            3,
    data_type:             0,
    trapper_hosts:         "localhost",
    snmp_port:             161,
    units:                 "",
    multiplier:            0,
    delta:                 0,
    snmpv3_securityname:   "",
    snmpv3_securitylevel:  0,
    snmpv3_authpassphrase: "",
    snmpv3_privpassphrase: "",
    formula:               0,
    trends:                86_400,
    logtimefmt:            "",
    valuemapid:            0,
    delay_flex:            "",
    authtype:              0,
    username:              "",
    password:              "",
    publickey:             "",
    privatekey:            "",
    params:                "",
    ipmi_sensor:           ""
  }
end

#get_interface_items(hostid, name) ⇒ Object

通过 Zabbix API 获取特定主机和接口下的监控项



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/zabbix_manager/classes/items.rb', line 71

def get_interface_items(hostid, name)
  # 记录调试信息,标识方法调用以及传递的参数
  log "[DEBUG] 调用 #{method_name}.get_interface_items,参数:hostid #{hostid.inspect},name #{name.inspect}"

  # 自动剔除收尾空白字符,规范化接口名
  _name = name&.gsub(%r{[^/0-9]}, "")&.strip
  iface = "#{_name}("

  # 使用 Zabbix API 进行模糊查询,获取特定主机和接口下的监控项
  result = get_raw(
    {
      output:  "extend",
      hostids: [hostid],
      search:  { name: iface }
    }
  )

  # 过滤结果,只保留符合正则表达式的项
  filtered_result = result.select { |item| item["snmp_oid"].match?(/(1.3.6.1.2.1.31.1.1.1.(6|10|15)|1.3.6.1.2.1.2.2.1.8)./) }

  # 如果结果为空,则返回 nil;否则,返回排序后的结果
  filtered_result.empty? ? nil : filtered_result.sort_by { |item| item["key_"] }
end

#get_or_create(data) ⇒ Object

通过 Zabbix API 获取或创建 Item 对象



54
55
56
57
58
59
60
61
62
# File 'lib/zabbix_manager/classes/items.rb', line 54

def get_or_create(data)
  log "[DEBUG] 调用 #{method_name}.get_or_create 方法,参数: #{data.inspect}"

  if (id = get_id(name: data[:name], hostid: data[:hostid]))
    id
  else
    create(data)
  end
end

#identifyObject

用于通过 Zabbix API 标识特定 Item 对象的 id 字段名称



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

def identify
  "name"
end

#method_nameObject

用于通过 Zabbix API 与 Items 交互的方法名称



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

def method_name
  "item"
end