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_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_params, #normalize_array, #normalize_hash, #parse_keys, #symbolize_keys, #update

Constructor Details

This class inherits a constructor from ZabbixApi::Basic

Instance Method Details

#create_or_update(data) ⇒ Integer

Create or update Trigger object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include description and hostid to properly identify Triggers via Zabbix API

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ApiError)

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

  • (HttpError)

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


99
100
101
102
103
# File 'lib/zabbixapi/classes/triggers.rb', line 99

def create_or_update(data)
  triggerid = get_id(description: data[:description], hostid: data[:hostid])

  triggerid ? update(data.merge(triggerid: triggerid)) : create(data)
end

#dump_by_id(data) ⇒ Hash

Dump Trigger object data by key from Zabbix API

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.

  • (HttpError)

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


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

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

#get_or_create(data) ⇒ Integer

Get or Create Trigger object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include description and hostid to properly identify Triggers via Zabbix API

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ApiError)

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

  • (HttpError)

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


84
85
86
87
88
89
90
91
# File 'lib/zabbixapi/classes/triggers.rb', line 84

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

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

#indentifyString

The id field name used for identifying specific Trigger objects via Zabbix API

Returns:

  • (String)

13
14
15
# File 'lib/zabbixapi/classes/triggers.rb', line 13

def indentify
  'description'
end

#method_nameString

The method name used for interacting with Triggers via Zabbix API

Returns:

  • (String)

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

def method_name
  'trigger'
end

#safe_update(data) ⇒ Integer

Safely update Trigger object using Zabbix API by deleting and replacing trigger

Parameters:

  • data (Hash)

    Needs to include description and hostid to properly identify Triggers via Zabbix API

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ApiError)

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

  • (HttpError)

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


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
74
75
76
# File 'lib/zabbixapi/classes/triggers.rb', line 45

def safe_update(data)
  log "[DEBUG] Call safe_update with parameters: #{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 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