Class: ZabbixManager::Items
- Defined in:
- lib/zabbix_manager/classes/items.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{ delay: "1m", history: "1h", status: 0, value_type: 3 }.freeze
- ITEM_TYPES =
[0, 2, 3, 5, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22].freeze
- REQUIRED_INTERFACE_TYPES =
[0, 12, 16, 17, 20].freeze
- REQUIRED_PARAMS_TYPES =
[11, 13, 14, 15, 21, 22].freeze
Instance Method Summary collapse
-
#default_options ⇒ Hash
返回创建监控项时的克制默认值。.
-
#delete_many(hostid:, itemids:) ⇒ Array<Integer>
批量删除明确指定的监控项。.
-
#find_by_key(hostid:, key:) ⇒ Hash?
按主机和稳定 key 查询唯一监控项。.
-
#for_host(hostid, keys: nil, output: "extend", select_preprocessing: nil, select_tags: nil) ⇒ Array<Hash>
查询主机监控项,可按稳定 key 集合过滤并选择关联对象。.
-
#identify ⇒ String
使用名称作为通用 CRUD 的业务标识。.
-
#identity_filter(data) ⇒ Integer
使用主机和名称组成通用 CRUD 的查询边界。.
-
#method_name ⇒ String
返回监控项对应的 Zabbix API 模块名。.
-
#monitored_traffic_candidates(hostid) ⇒ Array<Hash>
返回主机中可能表示接口流量的已启用监控项。 方向和接口精确匹配由 Monitoring 业务层负责。.
-
#set_status(hostid:, itemids:, enabled:) ⇒ Array<Integer>
批量启用或停用监控项。.
-
#upsert_by_key(data) ⇒ Integer
按 hostid + key_ 幂等创建或更新监控项。.
-
#upsert_dns_item(hostid:, interfaceid:, dns_name:) ⇒ Integer
按主机接口幂等创建或更新 DNS 解析监控项,并返回监控项 ID。 DNS 名称会进入 item key,因此拒绝可能改变 key 参数结构的分隔符。.
-
#upsert_many(collection) ⇒ Array<Integer>
先校验整批数据,再按稳定 key 逐项幂等写入。.
Methods inherited from Basic
#add, #all, #create, #create_or_update, #create_raw, #delete, #delete_raw, #destroy, #dump_by_id, #get, #get_full_data, #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
#default_options ⇒ Hash
返回创建监控项时的克制默认值。
32 33 34 |
# File 'lib/zabbix_manager/classes/items.rb', line 32 def DEFAULT_OPTIONS.dup end |
#delete_many(hostid:, itemids:) ⇒ Array<Integer>
批量删除明确指定的监控项。
114 115 116 117 118 119 |
# File 'lib/zabbix_manager/classes/items.rb', line 114 def delete_many(hostid:, itemids:) ids = normalized_ids(itemids, "itemids") verify_host_ownership!(hostid, ids) result = @client.api_request(method: "item.delete", params: ids) Array(result.fetch("itemids")).map(&:to_i) end |
#find_by_key(hostid:, key:) ⇒ Hash?
按主机和稳定 key 查询唯一监控项。
61 62 63 64 65 66 |
# File 'lib/zabbix_manager/classes/items.rb', line 61 def find_by_key(hostid:, key:) result = for_host(hostid, keys: key, output: ["itemid", "key_", "name"]) raise Conflict, "multiple items use key #{key} on host #{hostid}" if result.length > 1 result.first end |
#for_host(hostid, keys: nil, output: "extend", select_preprocessing: nil, select_tags: nil) ⇒ Array<Hash>
查询主机监控项,可按稳定 key 集合过滤并选择关联对象。
49 50 51 52 53 54 55 56 57 |
# File 'lib/zabbix_manager/classes/items.rb', line 49 def for_host(hostid, keys: nil, output: "extend", select_preprocessing: nil, select_tags: nil) raise Invalid, "hostid is required" if hostid.blank? params = { hostids: hostid, output: output } params[:filter] = { key_: keys } if keys.present? params[:selectPreprocessing] = select_preprocessing if select_preprocessing params[:selectTags] = if @client.api_request(method: "item.get", params: params) end |
#identify ⇒ String
使用名称作为通用 CRUD 的业务标识。
25 26 27 |
# File 'lib/zabbix_manager/classes/items.rb', line 25 def identify "name" end |
#identity_filter(data) ⇒ Integer
使用主机和名称组成通用 CRUD 的查询边界。
42 43 44 45 |
# File 'lib/zabbix_manager/classes/items.rb', line 42 def identity_filter(data) attributes = data.deep_symbolize_keys { name: attributes.fetch(:name), hostid: attributes.fetch(:hostid) } end |
#method_name ⇒ String
返回监控项对应的 Zabbix API 模块名。
18 19 20 |
# File 'lib/zabbix_manager/classes/items.rb', line 18 def method_name "item" end |
#monitored_traffic_candidates(hostid) ⇒ Array<Hash>
返回主机中可能表示接口流量的已启用监控项。 方向和接口精确匹配由 Monitoring 业务层负责。
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/zabbix_manager/classes/items.rb', line 124 def monitored_traffic_candidates(hostid) @client.api_request( method: "item.get", params: { hostids: [hostid], monitored: true, output: %w[itemid hostid name key_ snmp_oid value_type status units], selectPreprocessing: "extend", sortfield: "name", sortorder: "ASC" } ) end |
#set_status(hostid:, itemids:, enabled:) ⇒ Array<Integer>
批量启用或停用监控项。
102 103 104 105 106 107 108 109 110 |
# File 'lib/zabbix_manager/classes/items.rb', line 102 def set_status(hostid:, itemids:, enabled:) ids = normalized_ids(itemids, "itemids") verify_host_ownership!(hostid, ids) result = @client.api_request( method: "item.update", params: ids.map { |itemid| { itemid: itemid, status: enabled ? 0 : 1 } } ) Array(result.fetch("itemids")).map(&:to_i) end |
#upsert_by_key(data) ⇒ Integer
按 hostid + key_ 幂等创建或更新监控项。
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/zabbix_manager/classes/items.rb', line 70 def upsert_by_key(data) attributes = validate_item!(data) current = find_by_key(hostid: attributes[:hostid], key: attributes[:key_]) if current itemid = current.fetch("itemid") @client.api_request( method: "item.update", params: attributes.except(:hostid).merge(itemid: itemid) ) itemid.to_i else create_attributes = .merge(attributes) validate_create_item!(create_attributes) result = @client.api_request(method: "item.create", params: create_attributes) result.fetch("itemids").first.to_i end end |
#upsert_dns_item(hostid:, interfaceid:, dns_name:) ⇒ Integer
按主机接口幂等创建或更新 DNS 解析监控项,并返回监控项 ID。 DNS 名称会进入 item key,因此拒绝可能改变 key 参数结构的分隔符。
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/zabbix_manager/classes/items.rb', line 141 def upsert_dns_item(hostid:, interfaceid:, dns_name:) name = dns_name.to_s.strip raise Invalid, "dns_name is required" if name.blank? raise Invalid, "dns_name contains unsupported item key delimiters" if name.match?(/[\[\],]/) itemid = upsert_by_key( hostid: hostid, interfaceid: interfaceid, name: "【DNS域名解析监控】#{name}", key_: "net.dns.record[,#{name},A,2,2]", type: 0, value_type: 1, delay: "1m", history: "90d", timeout: "3s" ) log "Ensured DNS monitoring item for #{name}" itemid rescue StandardError => e log "Failed to ensure DNS monitoring item: #{e.class}" raise end |
#upsert_many(collection) ⇒ Array<Integer>
先校验整批数据,再按稳定 key 逐项幂等写入。
91 92 93 94 95 96 97 98 |
# File 'lib/zabbix_manager/classes/items.rb', line 91 def upsert_many(collection) items = Array(collection).map { |data| validate_item!(data) } identities = items.map { |item| [item[:hostid].to_s, item[:key_].to_s] } duplicate = identities.tally.find { |_identity, count| count > 1 }&.first raise Invalid, "duplicate hostid + key_ identity #{duplicate.join(":")}" if duplicate items.map { |item| upsert_by_key(item) } end |