Class: ZabbixApi::Usermacros

Inherits:
Basic
  • Object
show all
Defined in:
lib/zabbixapi/classes/usermacros.rb

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #default_options, #destroy, #dump_by_id, #get, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_params, #normalize_array, #normalize_hash, #parse_keys, #symbolize_keys

Constructor Details

This class inherits a constructor from ZabbixApi::Basic

Instance Method Details

#create(data) ⇒ Integer, Boolean

Create new User macro object using Zabbix API (with defaults)

Parameters:

  • data (Hash)

    Needs to include hostid, macro, and value to create User macro via Zabbix API

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.



88
89
90
# File 'lib/zabbixapi/classes/usermacros.rb', line 88

def create(data)
  request(data, 'usermacro.create', 'hostmacroids')
end

#create_global(data) ⇒ Integer, Boolean

Create new Global macro object using Zabbix API (with defaults)

Parameters:

  • data (Hash)

    Needs to include hostid, macro, and value to create Global macro via Zabbix API

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.



99
100
101
# File 'lib/zabbixapi/classes/usermacros.rb', line 99

def create_global(data)
  request(data, 'usermacro.createglobal', 'globalmacroids')
end

#create_or_update(data) ⇒ Integer

Create or update User macro object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include macro and hostid to properly identify User macros 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.



187
188
189
190
# File 'lib/zabbixapi/classes/usermacros.rb', line 187

def create_or_update(data)
  hostmacroid = get_id(:macro => data[:macro], :hostid => data[:hostid])
  hostmacroid ? update(data.merge(:hostmacroid => hostmacroid)) : create(data)
end

#create_or_update_global(data) ⇒ Integer

Create or update Global macro object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include macro and hostid to properly identify Global macros 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.



198
199
200
201
# File 'lib/zabbixapi/classes/usermacros.rb', line 198

def create_or_update_global(data)
  hostmacroid = get_id_global(:macro => data[:macro], :hostid => data[:hostid])
  hostmacroid ? update_global(data.merge(:globalmacroid => globalmacroid)) : create_global(data)
end

#delete(data) ⇒ Integer, Boolean

Delete User macro object using Zabbix API

Parameters:

  • data (Hash)

    Should include hostmacroid's of User macros to delete

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.



110
111
112
113
# File 'lib/zabbixapi/classes/usermacros.rb', line 110

def delete(data)
  data_delete = [data]
  request(data_delete, 'usermacro.delete', 'hostmacroids')
end

#delete_global(data) ⇒ Integer, Boolean

Delete Global macro object using Zabbix API

Parameters:

  • data (Hash)

    Should include hostmacroid's of Global macros to delete

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.



122
123
124
125
# File 'lib/zabbixapi/classes/usermacros.rb', line 122

def delete_global(data)
  data_delete = [data]
  request(data_delete, 'usermacro.deleteglobal', 'globalmacroids')
end

#get_full_data(data) ⇒ Hash

Get full/extended User macro data from Zabbix 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.



63
64
65
66
67
# File 'lib/zabbixapi/classes/usermacros.rb', line 63

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

  request(data, 'usermacro.get', 'hostmacroid')
end

#get_full_data_global(data) ⇒ Hash

Get full/extended Global macro data from Zabbix 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.



75
76
77
78
79
# File 'lib/zabbixapi/classes/usermacros.rb', line 75

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

  request(data, 'usermacro.get', 'globalmacroid')
end

#get_id(data) ⇒ Integer

Get User macro object id from Zabbix API based on provided data

Parameters:

  • data (Hash)

    Needs to include macro to properly identify user macros via Zabbix API

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.



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

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 = request(data, 'usermacro.get', 'hostmacroid')

  !result.empty? && result[0].key?('hostmacroid') ? result[0]['hostmacroid'].to_i : nil
end

#get_id_global(data) ⇒ Integer

Get Global macro object id from Zabbix API based on provided data

Parameters:

  • data (Hash)

    Needs to include macro to properly identify global macros via Zabbix API

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.



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

def get_id_global(data)
  log "[DEBUG] Call get_id_global 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 = request(data, 'usermacro.get', 'globalmacroid')

  !result.empty? && result[0].key?('globalmacroid') ? result[0]['globalmacroid'].to_i : nil
end

#get_or_create(data) ⇒ Integer

Get or Create User macro object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include macro and hostid to properly identify User macros 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.



157
158
159
160
161
162
163
164
# File 'lib/zabbixapi/classes/usermacros.rb', line 157

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

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

#get_or_create_global(data) ⇒ Integer

Get or Create Global macro object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include macro and hostid to properly identify Global macros 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.



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

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

  unless (id = get_id_global(:macro => data[:macro], :hostid => data[:hostid]))
    id = create_global(data)
  end
  id
end

#indentifyString

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

Returns:

  • (String)


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

def indentify
  'macro'
end

#method_nameString

The method name used for interacting with User macros via Zabbix API

Returns:

  • (String)


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

def method_name
  'usermacro'
end

#update(data) ⇒ Integer, Boolean

Update User macro object using Zabbix API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify), id value, and fields to update

  • force (Boolean)

    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.



135
136
137
# File 'lib/zabbixapi/classes/usermacros.rb', line 135

def update(data)
  request(data, 'usermacro.update', 'hostmacroids')
end

#update_global(data) ⇒ Integer, Boolean

Update Global macro object using Zabbix API

Parameters:

  • data (Hash)

    Should include object's id field name (indentify), id value, and fields to update

  • force (Boolean)

    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.



147
148
149
# File 'lib/zabbixapi/classes/usermacros.rb', line 147

def update_global(data)
  request(data, 'usermacro.updateglobal', 'globalmacroids')
end