Class: ZabbixManager::Usermacros

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create_raw, #default_options, #delete_raw, #destroy, #dump_by_id, #get, #get_hostgroup_ids, #get_or_create_hostgroups, #get_raw, #hash_equals?, #initialize, #key, #keys, #log, #merge_params, #mojo_update, #normalize_array, #normalize_hash, #parse_keys, #symbolize_keys, #update_raw

Constructor Details

This class inherits a constructor from ZabbixManager::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:

  • (ManagerError)

    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.



90
91
92
# File 'lib/zabbix_manager/classes/usermacros.rb', line 90

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:

  • (ManagerError)

    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.



101
102
103
# File 'lib/zabbix_manager/classes/usermacros.rb', line 101

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:

  • (ManagerError)

    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.



189
190
191
192
# File 'lib/zabbix_manager/classes/usermacros.rb', line 189

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:

  • (ManagerError)

    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.



200
201
202
203
# File 'lib/zabbix_manager/classes/usermacros.rb', line 200

def create_or_update_global(data)
  globalmacroid = get_id_global(macro: data[:macro], hostid: data[:hostid])
  globalmacroid ? 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:

  • (ManagerError)

    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.



112
113
114
115
# File 'lib/zabbix_manager/classes/usermacros.rb', line 112

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:

  • (ManagerError)

    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.



124
125
126
127
# File 'lib/zabbix_manager/classes/usermacros.rb', line 124

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 (identify) and id value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    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.



65
66
67
68
69
# File 'lib/zabbix_manager/classes/usermacros.rb', line 65

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 (identify) and id value

Returns:

  • (Hash)

Raises:

  • (ManagerError)

    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.



77
78
79
80
81
# File 'lib/zabbix_manager/classes/usermacros.rb', line 77

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:

  • (ManagerError)

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

  • (HttpError)

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



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

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?(identify)
  # raise an error if identify name was not supplied
  name = data[identify.to_sym]
  raise ManagerError, "#{identify} 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:

  • (ManagerError)

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

  • (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
# File 'lib/zabbix_manager/classes/usermacros.rb', line 45

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?(identify)
  # raise an error if identify name was not supplied
  name = data[identify.to_sym]
  raise ManagerError, "#{identify} not supplied in call to get_id_global" 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:

  • (ManagerError)

    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.



159
160
161
162
163
164
165
166
# File 'lib/zabbix_manager/classes/usermacros.rb', line 159

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:

  • (ManagerError)

    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.



174
175
176
177
178
179
180
181
# File 'lib/zabbix_manager/classes/usermacros.rb', line 174

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

#identifyString

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

Returns:

  • (String)


8
9
10
# File 'lib/zabbix_manager/classes/usermacros.rb', line 8

def identify
  "macro"
end

#method_nameString

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

Returns:

  • (String)


15
16
17
# File 'lib/zabbix_manager/classes/usermacros.rb', line 15

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 (identify), 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:

  • (ManagerError)

    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.



137
138
139
# File 'lib/zabbix_manager/classes/usermacros.rb', line 137

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 (identify), 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:

  • (ManagerError)

    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.



149
150
151
# File 'lib/zabbix_manager/classes/usermacros.rb', line 149

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