Class: ZabbixApi::Basic

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbixapi/basic/basic_func.rb,
lib/zabbixapi/basic/basic_init.rb,
lib/zabbixapi/basic/basic_alias.rb,
lib/zabbixapi/basic/basic_logic.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ ZabbixApi::Client

Initializes a new Basic object with ZabbixApi Client

Parameters:


7
8
9
# File 'lib/zabbixapi/basic/basic_init.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#add(data) ⇒ Integer, Boolean

Add new Zabbix object using API create

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

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.


20
21
22
# File 'lib/zabbixapi/basic/basic_alias.rb', line 20

def add(data)
  create(data)
end

#allArray<Hash>

Get full/extended Zabbix data for all objects of type/class from API

Returns:

  • (Array<Hash>)

    Array of matching objects

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.


132
133
134
135
136
137
138
# File 'lib/zabbixapi/basic/basic_logic.rb', line 132

def all
  result = {}
  @client.api_request(method: "#{method_name}.get", params: { output: 'extend' }).each do |item|
    result[item[indentify]] = item[key]
  end
  result
end

#create(data) ⇒ Integer, Boolean

Create new Zabbix object using API (with defaults)

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

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.


10
11
12
13
14
15
16
17
# File 'lib/zabbixapi/basic/basic_logic.rb', line 10

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

  data_with_default = default_options.empty? ? data : merge_params(default_options, data)
  data_create = [data_with_default]
  result = @client.api_request(method: "#{method_name}.create", params: data_create)
  parse_keys result
end

#create_or_update(data) ⇒ Integer, Boolean

Create or update Zabbix object using API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id value

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

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.


41
42
43
44
45
46
# File 'lib/zabbixapi/basic/basic_logic.rb', line 41

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

  id = get_id(indentify.to_sym => data[indentify.to_sym])
  id ? update(data.merge(key.to_sym => id.to_s)) : create(data)
end

#default_optionsHash

Placeholder for inherited objects to provide default options

Returns:

  • (Hash)

21
22
23
# File 'lib/zabbixapi/basic/basic_init.rb', line 21

def default_options
  {}
end

#delete(data) ⇒ Integer, Boolean

Delete Zabbix object using API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id value

Returns:

  • (Integer)

    The object id if a single object is deleted

  • (Boolean)

    True/False if multiple objects are deleted

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.


26
27
28
29
30
31
32
# File 'lib/zabbixapi/basic/basic_logic.rb', line 26

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

  data_delete = [data]
  result = @client.api_request(method: "#{method_name}.delete", params: data_delete)
  parse_keys result
end

#destroy(data) ⇒ Integer, Boolean

Destroy Zabbix object using API delete

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id value

Returns:

  • (Integer)

    The object id if a single object is deleted

  • (Boolean)

    True/False if multiple objects are deleted

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.


31
32
33
# File 'lib/zabbixapi/basic/basic_alias.rb', line 31

def destroy(data)
  delete(data)
end

#dump_by_id(data) ⇒ Hash

Dump Zabbix object data by key from 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.


113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/zabbixapi/basic/basic_logic.rb', line 113

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

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

#get(data) ⇒ Hash

Get Zabbix object data from API by id

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id 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.


9
10
11
# File 'lib/zabbixapi/basic/basic_alias.rb', line 9

def get(data)
  get_full_data(data)
end

#get_full_data(data) ⇒ Hash

Get full/extended Zabbix object data from API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id 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.


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/zabbixapi/basic/basic_logic.rb', line 78

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

  @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: {
        indentify.to_sym => data[indentify.to_sym]
      },
      output: 'extend'
    }
  )
end

#get_id(data) ⇒ Integer

Get Zabbix object id from API based on provided data

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    Zabbix object id

Raises:

  • (ApiError)

    Error returned when there is a problem with the Zabbix API call or missing object's id field name (indentify).

  • (HttpError)

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


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/zabbixapi/basic/basic_logic.rb', line 146

def get_id(data)
  log "[DEBUG] Call get_id with parameters: #{data.inspect}"
  # symbolize keys if the user used string keys instead of symbols
  data = symbolize_keys(data) if data.key?(indentify)
  # raise an error if indentify name was not supplied
  name = data[indentify.to_sym]
  raise ApiError.new("#{indentify} not supplied in call to get_id") if name.nil?

  result = @client.api_request(
    method: "#{method_name}.get",
    params: {
      filter: data,
      output: [key, indentify]
    }
  )
  id = nil
  result.each { |item| id = item[key].to_i if item[indentify] == data[indentify.to_sym] }
  id
end

#get_or_create(data) ⇒ Integer

Get or Create Zabbix object using API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id value

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.


172
173
174
175
176
177
178
179
# File 'lib/zabbixapi/basic/basic_logic.rb', line 172

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

  unless (id = get_id(indentify.to_sym => data[indentify.to_sym]))
    id = create(data)
  end
  id
end

#get_raw(data) ⇒ Hash

Get raw Zabbix object data from API

Parameters:

  • data (Hash)

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.


98
99
100
101
102
103
104
105
# File 'lib/zabbixapi/basic/basic_logic.rb', line 98

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

  @client.api_request(
    method: "#{method_name}.get",
    params: data
  )
end

#hash_equals?(first_hash, second_hash) ⇒ Boolean

Compare two hashes for equality

Parameters:

  • first_hash (Hash)
  • second_hash (Hash)

Returns:

  • (Boolean)

15
16
17
18
19
20
21
22
# File 'lib/zabbixapi/basic/basic_func.rb', line 15

def hash_equals?(first_hash, second_hash)
  normalized_first_hash = normalize_hash(first_hash)
  normalized_second_hash = normalize_hash(second_hash)

  hash1 = normalized_first_hash.merge(normalized_second_hash)
  hash2 = normalized_second_hash.merge(normalized_first_hash)
  hash1 == hash2
end

#indentifyObject

Placeholder for inherited objects to provide object-specific id field name

Raises:

  • (ApiError)

    Basic object does not directly support indentify


42
43
44
# File 'lib/zabbixapi/basic/basic_init.rb', line 42

def indentify
  raise ApiError.new("Can't call indentify here")
end

#keyString

Returns the object's id field name (indentify) based on method_name + id

Returns:

  • (String)

35
36
37
# File 'lib/zabbixapi/basic/basic_init.rb', line 35

def key
  method_name + 'id'
end

#keysString

Returns the object's plural id field name (indentify) based on key

Returns:

  • (String)

28
29
30
# File 'lib/zabbixapi/basic/basic_init.rb', line 28

def keys
  key + 's'
end

#log(message) ⇒ Object

Log messages to stdout when debugging

Parameters:

  • message (String)

6
7
8
# File 'lib/zabbixapi/basic/basic_func.rb', line 6

def log(message)
  puts message.to_s if @client.options[:debug]
end

#merge_params(first_hash, second_hash) ⇒ Hash

Merge two hashes into a single new hash

Parameters:

  • first_hash (Hash)
  • second_hash (Hash)

Returns:

  • (Hash)

98
99
100
101
# File 'lib/zabbixapi/basic/basic_func.rb', line 98

def merge_params(first_hash, second_hash)
  new = first_hash.dup
  new.merge(second_hash)
end

#method_nameObject

Placeholder for inherited objects to provide object-specific method name

Raises:

  • (ApiError)

    Basic object does not directly support method_name


14
15
16
# File 'lib/zabbixapi/basic/basic_init.rb', line 14

def method_name
  raise ApiError.new("Can't call method_name here")
end

#normalize_array(array) ⇒ Array

Normalize all array values to strings

Parameters:

  • array (Array)

Returns:

  • (Array)

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zabbixapi/basic/basic_func.rb', line 61

def normalize_array(array)
  result = []

  array.each do |e|
    if e.is_a?(Array)
      result.push(normalize_array(e))
    elsif e.is_a?(Hash)
      result.push(normalize_hash(e))
    else
      result.push(e.to_s)
    end
  end

  result
end

#normalize_hash(hash) ⇒ Hash

Normalize all hash values to strings

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

45
46
47
48
49
50
51
52
53
54
55
# File 'lib/zabbixapi/basic/basic_func.rb', line 45

def normalize_hash(hash)
  result = hash.dup

  result.delete(:hostid) # TODO: remove to logig. TemplateID and HostID has different id

  result.each do |key, value|
    result[key] = value.is_a?(Array) ? normalize_array(value) : value.to_s
  end

  result
end

#parse_keys(data) ⇒ Integer, Boolean

Parse a data hash for id key or boolean to return

Parameters:

  • data (Hash)

Returns:

  • (Integer)

    The object id if a single object hash is provided with key

  • (Boolean)

    True/False if multiple class object hash is provided


82
83
84
85
86
87
88
89
90
91
# File 'lib/zabbixapi/basic/basic_func.rb', line 82

def parse_keys(data)
  case data
  when Hash
    data.empty? ? nil : data[keys][0].to_i
  when TrueClass
    true
  when FalseClass
    false
  end
end

#symbolize_keys(object) ⇒ Array, Hash

Convert all hash/array keys to symbols

Parameters:

  • object (Array, Hash)

Returns:

  • (Array, Hash)

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

def symbolize_keys(object)
  if object.is_a?(Array)
    object.each_with_index do |val, index|
      object[index] = symbolize_keys(val)
    end
  elsif object.is_a?(Hash)
    object.keys.each do |key|
      object[key.to_sym] = symbolize_keys(object.delete(key))
    end
  end
  object
end

#update(data, force = false) ⇒ Integer, Boolean

Update Zabbix object using API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify) and id value

  • force (Boolean) (defaults to: false)

    Whether to force an object update even if provided data matches Zabbix

Returns:

  • (Integer)

    The object id if a single object is created

  • (Boolean)

    True/False if multiple objects are created

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.


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zabbixapi/basic/basic_logic.rb', line 56

def update(data, force = false)
  log "[DEBUG] Call update with parameters: #{data.inspect}"
  dump = {}
  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
  if hash_equals?(dump, data) && !force
    log "[DEBUG] Equal keys #{dump} and #{data}, skip update"
    data[key.to_sym].to_i
  else
    data_update = [data]
    result = @client.api_request(method: "#{method_name}.update", params: data_update)
    parse_keys result
  end
end