Class: ZabbixManager::Monitoring

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbix_manager/monitoring.rb

Overview

封装网络设备、接口和线路监控的高频幂等业务流程。

Constant Summary collapse

DEFAULT_WINDOW =
"5m"
DEFAULT_PRIORITY =
3
SUPPORTED_FUNCTIONS =
%w[avg max min last].freeze
EXPRESSION_ITEM_KEY =
/\A[A-Za-z0-9_.-]+(?:\[[A-Za-z0-9_.:,\/\-\s{}\#\$"']*\])?\z/.freeze

Instance Method Summary collapse

Constructor Details

#initialize(manager) ⇒ Monitoring

使用同一个 Manager 复用 API 会话和资源模块。



14
15
16
# File 'lib/zabbix_manager/monitoring.rb', line 14

def initialize(manager)
  @manager = manager
end

Instance Method Details

#reconcile_device(data) ⇒ Integer

创建或更新受监控的网络设备。

Returns:

  • (Integer)

    Zabbix 主机 ID



21
22
23
# File 'lib/zabbix_manager/monitoring.rb', line 21

def reconcile_device(data)
  @manager.hosts.reconcile(data)
end

#reconcile_devices(collection, fail_fast: false) ⇒ Array<Hash>

先校验整批设备,再按技术主机名逐台幂等创建或更新。 默认保留逐台结果;fail_fast 为 true 时遇到首个运行期错误即停止。

Returns:

  • (Array<Hash>)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/zabbix_manager/monitoring.rb', line 28

def reconcile_devices(collection, fail_fast: false)
  devices = Array(collection).map { |device| @manager.hosts.validate(device) }
  validate_device_batch!(devices)
  devices.map do |device|
    hostid = reconcile_device(device)
    { status: :ok, device: safe_device_identity(device), hostid: hostid }
  rescue ApiError, TransportError, ArgumentError => e
    raise if fail_fast

    failure_result(:device, safe_device_identity(device), e)
  end
end

#reconcile_interface(data) ⇒ Hash

创建或更新一个设备接口的监控项和阈值触发器。

必需输入: host: { hostid: 10101, host: "router-01" } interface: { name: "GigabitEthernet1/0/1", interfaceid: 12 } items: { inbound_bps: { key_: "...", name: "...", type: 4, value_type: 3 }, ... }

阈值支持 bandwidth、errors 和 packet_loss,只增改不自动删除远端对象。

Returns:

  • (Hash)

    监控项和触发器 ID



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/zabbix_manager/monitoring.rb', line 86

def reconcile_interface(data)
  input = data.deep_symbolize_keys
  host = resolve_explicit_host(input.fetch(:host))
  interface = input.fetch(:interface)
  validate_interface!(interface)

  prepared_items, item_keys = prepare_items(host, interface, input.fetch(:items, {}))
  thresholds = input.fetch(:thresholds, {})
  validate_threshold_items!(thresholds, prepared_items)
  prepared_triggers = prepare_thresholds(
    host: host,
    interface: interface,
    item_keys: item_keys,
    thresholds: thresholds
  )
  itemids = reconcile_items(prepared_items)
  triggerids = reconcile_thresholds(prepared_triggers)

  {
    hostid: host[:hostid].to_i,
    interface: interface[:name],
    itemids: itemids,
    triggerids: triggerids
  }
end

#reconcile_line(data = nil, **attributes) ⇒ Hash

使用 Zabbix 已发现的流量项创建或更新线路阈值触发器。 兼容 add_line_monitors.rb 的历史字段名。

Returns:

  • (Hash)

    主机、接口、监控项和触发器结果



56
57
58
# File 'lib/zabbix_manager/monitoring.rb', line 56

def reconcile_line(data = nil, **attributes)
  reconcile_line_with_cache(data || attributes, nil)
end

#reconcile_lines(collection, fail_fast: false) ⇒ Array<Hash>

批量同步线路并复用主机和监控项查询缓存。 默认逐条记录成功或失败;fail_fast 为 true 时立即抛出错误。

Returns:

  • (Array<Hash>)

    每条线路的状态和结果



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

def reconcile_lines(collection, fail_fast: false)
  lines = validate_line_collection!(collection)
  cache = { items: {}, hosts: {} }
  lines.map do |line|
    { status: :ok, result: reconcile_line_with_cache(line, cache) }
  rescue ApiError, TransportError, ArgumentError => e
    raise if fail_fast

    failure_result(:line, safe_line_identity(line), e)
  end
end

#reconcile_network(devices:, lines:, fail_fast: false) ⇒ Hash

分设备和线路两个阶段执行一次网络监控批次,并返回稳定的汇总结果。 线路依赖模板或自动发现产生的流量项;尚未生成时会记录为线路失败,后续可安全重跑。

Returns:

  • (Hash)


44
45
46
47
48
49
50
# File 'lib/zabbix_manager/monitoring.rb', line 44

def reconcile_network(devices:, lines:, fail_fast: false)
  validate_line_collection!(lines)
  device_results = reconcile_devices(devices, fail_fast: fail_fast)
  line_results = reconcile_lines(lines, fail_fast: fail_fast)
  results = { devices: device_results, lines: line_results }
  results.merge(summary: batch_summary(results))
end