Class: ZabbixApi::Triggers

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #default_options, #delete, #destroy, #get, #get_full_data, #get_id, #get_or_create, #hash_equals?, #initialize, #key, #keys, #log, #merge_params, #normalize_hash, #parse_keys, #symbolize_keys, #update

Constructor Details

This class inherits a constructor from ZabbixApi::Basic

Instance Method Details

#create_or_update(data) ⇒ Object



4
5
6
7
# File 'lib/zabbixapi/classes/unusable.rb', line 4

def create_or_update(data)
  log "[DEBUG] Call create_or_update with parametrs: #{data.inspect}"
  get_or_create(data)
end

#dump_by_id(data) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/zabbixapi/classes/triggers.rb', line 12

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

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

#indentifyObject



8
9
10
# File 'lib/zabbixapi/classes/triggers.rb', line 8

def indentify
  "description"
end

#method_nameObject



4
5
6
# File 'lib/zabbixapi/classes/triggers.rb', line 4

def method_name
  "trigger"
end

#safe_update(data) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/zabbixapi/classes/triggers.rb', line 28

def safe_update(data)
  log "[DEBUG] Call update with parametrs: #{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] Equal keys #{dump} and #{data}, skip 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