Class: ZabbixManager::Triggers

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

Constant Summary collapse

DEFAULT_UNCERTAIN_WRITE_DELAYS =
[0, 0.25, 1, 2].freeze

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #create_or_update, #create_raw, #default_options, #delete, #delete_raw, #destroy, #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

#add_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false) ⇒ Integer

在保留现有依赖的前提下,为触发器追加一个或多个依赖。 读改写过程按触发器 ID 加锁,跨进程可通过 Client 的 upsert_lock 协调。

Returns:

  • (Integer)

Raises:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/zabbix_manager/classes/triggers.rb', line 152

def add_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
  id = normalized_ids([triggerid]).first
  additions = normalized_ids(depends_on)
  raise Invalid, "trigger cannot depend on itself" if additions.include?(id)

  verify_host_ownership!(hostid, [id])

  @client.with_upsert_lock("trigger-dependencies:#{id}") do
    current = find_by_id(id, select_dependencies: ["triggerid"])
    raise ApiError, "Zabbix trigger #{id} was not found" unless current

    existing = Array(current["dependencies"]).filter_map { |dependency| dependency["triggerid"] }
    dependency_ids = (existing + additions).uniq
    unless allow_cross_host_dependencies
      verify_host_ownership!(hostid, dependency_ids)
    end
    dependencies = dependency_ids.map { |value| { triggerid: value } }
    update_dependencies(id, dependencies)
  end
end

#delete_many(hostid:, triggerids:) ⇒ Array<Integer>

批量删除明确指定的触发器。

Returns:

  • (Array<Integer>)


128
129
130
131
132
133
# File 'lib/zabbix_manager/classes/triggers.rb', line 128

def delete_many(hostid:, triggerids:)
  ids = normalized_ids(triggerids)
  verify_host_ownership!(hostid, ids)
  result = @client.api_request(method: "trigger.delete", params: ids)
  Array(result.fetch("triggerids")).map(&:to_i)
end

#dump_by_id(data) ⇒ Hash

按触发器 ID 查询完整对象及关联项和函数。

Parameters:

  • data (Hash)

    Should include desired object's key and value

Returns:

  • (Hash)

Raises:

  • (ApiError)

    Error returned when there is a problem with the Zabbix API call.

  • (TransportError)

    Error raised when HTTP status from Zabbix Server response is not a 200 OK.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zabbix_manager/classes/triggers.rb', line 26

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

  @client.api_request(
    method: "trigger.get",
    params: {
      filter: {
        key.to_sym => data[key.to_sym]
      },
      output: "extend",
      selectItems: "extend",
      selectFunctions: "extend"
    }
  )
end

#find_by_id(triggerid, select_dependencies: nil) ⇒ Hash?

按触发器 ID 查询唯一对象,并可返回当前依赖集合。

Returns:

  • (Hash, nil)

Raises:



95
96
97
98
99
100
101
102
103
# File 'lib/zabbix_manager/classes/triggers.rb', line 95

def find_by_id(triggerid, select_dependencies: nil)
  id = normalized_ids([triggerid]).first
  params = { triggerids: id, output: "extend" }
  params[:selectDependencies] = select_dependencies if select_dependencies
  result = @client.api_request(method: "trigger.get", params: params)
  raise Conflict, "multiple triggers use triggerid #{id}" if result.length > 1

  result.first
end

#find_for_host(hostid:, description:, tags: nil) ⇒ Hash?

按主机、描述和可选标签查询唯一触发器。

Returns:

  • (Hash, nil)

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zabbix_manager/classes/triggers.rb', line 59

def find_for_host(hostid:, description:, tags: nil)
  params = {
    hostids: hostid,
    filter: { description: description },
    output: ["triggerid", "description"],
    selectTags: "extend"
  }
  params[:tags] = tags if tags.present?
  result = @client.api_request(
    method: "trigger.get",
    params: params
  )
  raise Conflict, "multiple triggers match #{description} on host #{hostid}" if result.length > 1

  result.first
end

#find_managed_for_host(hostid:, managed_key:) ⇒ Hash?

按管理标签查询唯一触发器。

Returns:

  • (Hash, nil)

Raises:



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/zabbix_manager/classes/triggers.rb', line 78

def find_managed_for_host(hostid:, managed_key:)
  result = @client.api_request(
    method: "trigger.get",
    params: {
      hostids: hostid,
      tags: [{ tag: "zabbix_manager_id", value: managed_key, operator: 1 }],
      output: ["triggerid", "description"],
      selectTags: "extend"
    }
  )
  raise Conflict, "multiple triggers use managed key #{managed_key}" if result.length > 1

  result.first
end

#for_host(hostid, output: "extend", select_items: nil, select_functions: nil, select_dependencies: nil, select_tags: nil, filter: nil) ⇒ Array<Hash>

查询主机触发器,并按需返回监控项、函数、依赖和标签。

Returns:

  • (Array<Hash>)

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zabbix_manager/classes/triggers.rb', line 44

def for_host(hostid, output: "extend", select_items: nil, select_functions: nil,
             select_dependencies: nil, select_tags: nil, filter: nil)
  raise Invalid, "hostid is required" if hostid.blank?

  params = { hostids: hostid, output: output }
  params[:selectItems] = select_items if select_items
  params[:selectFunctions] = select_functions if select_functions
  params[:selectDependencies] = select_dependencies if select_dependencies
  params[:selectTags] = select_tags if select_tags
  params[:filter] = filter if filter.present?
  @client.api_request(method: "trigger.get", params: params)
end

#identifyString

使用描述作为通用 CRUD 的业务标识。

Returns:

  • (String)


16
17
18
# File 'lib/zabbix_manager/classes/triggers.rb', line 16

def identify
  "description"
end

#method_nameString

返回触发器对应的 Zabbix API 模块名。

Returns:

  • (String)


9
10
11
# File 'lib/zabbix_manager/classes/triggers.rb', line 9

def method_name
  "trigger"
end

#replace_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false) ⇒ Integer

使用 trigger.update 完整替换一个触发器的依赖集合。 传入空集合可清除依赖,返回被更新的触发器 ID。

Returns:

  • (Integer)

Raises:



138
139
140
141
142
143
144
145
146
147
# File 'lib/zabbix_manager/classes/triggers.rb', line 138

def replace_dependencies(hostid:, triggerid:, depends_on:, allow_cross_host_dependencies: false)
  id = normalized_ids([triggerid]).first
  dependency_ids = normalized_ids(depends_on, allow_empty: true)
  dependencies = dependency_ids.map { |value| { triggerid: value } }
  raise Invalid, "trigger cannot depend on itself" if dependencies.any? { |item| item[:triggerid] == id }

  verify_host_ownership!(hostid, [id])
  verify_host_ownership!(hostid, dependency_ids) if dependency_ids.any? && !allow_cross_host_dependencies
  update_dependencies(id, dependencies)
end

#set_status(hostid:, triggerids:, enabled:) ⇒ Array<Integer>

批量启用或停用触发器。

Returns:

  • (Array<Integer>)


116
117
118
119
120
121
122
123
124
# File 'lib/zabbix_manager/classes/triggers.rb', line 116

def set_status(hostid:, triggerids:, enabled:)
  ids = normalized_ids(triggerids)
  verify_host_ownership!(hostid, ids)
  result = @client.api_request(
    method: "trigger.update",
    params: ids.map { |triggerid| { triggerid: triggerid, status: enabled ? 0 : 1 } }
  )
  Array(result.fetch("triggerids")).map(&:to_i)
end

#upsert_for_host(data) ⇒ Integer

按稳定管理键串行创建或更新主机触发器。 可通过 Client 的 upsert_lock 接入跨进程锁。

Returns:

  • (Integer)


108
109
110
111
112
# File 'lib/zabbix_manager/classes/triggers.rb', line 108

def upsert_for_host(data)
  attributes = data.deep_symbolize_keys
  lock_key = "trigger:#{attributes[:hostid]}:#{attributes[:managed_key] || attributes[:description]}"
  @client.with_upsert_lock(lock_key) { perform_upsert_for_host(attributes) }
end