Class: ZabbixManager::Triggers

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #delete, #destroy, #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

#add_trigger_dependency(triggerid, depend_on_trigger_id) ⇒ Object

使用 Zabbix API 添加触发器依赖关系



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

def add_trigger_dependency(triggerid, depend_on_trigger_id)
  log "[DEBUG] 调用 add_trigger_dependency 方法,参数: #{triggerid} #{depend_on_trigger_id}"

  result = @client.api_request(
    method: "trigger.adddependencies",
    params: {
      triggerid:          triggerid,
      dependsOnTriggerid: depend_on_trigger_id
    }
  )
  result.empty? ? nil : result["triggerids"]
end

#create_or_update(data) ⇒ Object

使用 Zabbix API 创建或更新触发器对象



87
88
89
90
# File 'lib/zabbix_manager/classes/triggers.rb', line 87

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

#default_optionsObject

创建 Zabbix 触发器的默认选项



16
17
18
19
20
21
22
# File 'lib/zabbix_manager/classes/triggers.rb', line 16

def default_options
  {
    recovery_mode: 1,
    type:          0,
    manual_close:  1
  }
end

#dump_by_id(data) ⇒ Object

通过键值对从 Zabbix API 转储触发器对象数据



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

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

  get_raw(
    {
      triggerids:       data[keys.to_sym],
      output:           "extend",
      select_items:     "extend",
      select_functions: "extend"
    }
  )
end

#get_or_create(data) ⇒ Object

通过 Zabbix API 获取或创建触发器对象



76
77
78
79
80
81
82
83
84
# File 'lib/zabbix_manager/classes/triggers.rb', line 76

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

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

#identifyObject

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



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

def identify
  "description"
end

#method_nameObject

与 Zabbix API 交互的触发器方法名称



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

def method_name
  "trigger"
end

#safe_update(data) ⇒ Object

安全地使用 Zabbix API 更新触发器对象,通过删除和替换触发器



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/zabbix_manager/classes/triggers.rb', line 39

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

  dump    = {}
  item_id = data[key.to_sym].to_i
  dump_by_id(key.to_sym => data[key.to_sym]).each do |item|
    dump = symbolize_keys(item) if item[key].to_i == data[key.to_sym].to_i
  end

  expression        = "#{dump[:items][0][:key_]}.#{dump[:functions][0][:function]}(#{dump[:functions][0][:parameter]})"
  dump[:expression] = dump[:expression].gsub(/\{(\d*)\}/, "{#{expression}}") # TODO: ugly regexp
  dump.delete(:functions)
  dump.delete(:items)

  old_expression    = data[:expression]
  data[:expression] = data[:expression].gsub(/\{.*:/, "{") # TODO: ugly regexp
  data.delete(:templateid)

  log "[DEBUG] expression: #{dump[:expression]}\n data: #{data[:expression]}"

  if hash_equals?(dump, data)
    log "[DEBUG] 键 #{dump}#{data} 相等,跳过 safe_update"
    item_id
  else
    data[:expression] = old_expression
    # disable old trigger
    log "[DEBUG] disable :" + @client.api_request(method: "#{method_name}.update",
                                                  params: [{
                                                             triggerid: data[:triggerid], status: "1"
                                                           }]).inspect
    # create new trigger
    data.delete(:triggerid)
    create(data)
  end
end