Class: ZabbixApi::Templates

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #create_or_update, #default_options, #destroy, #dump_by_id, #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

#delete(data) ⇒ Integer

Delete Template object using Zabbix API

Parameters:

  • data (Array)

    Should include array of templateid's

Returns:

  • (Integer)

    The Template object id that was 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.


23
24
25
26
# File 'lib/zabbixapi/classes/templates.rb', line 23

def delete(data)
  result = @client.api_request(method: 'template.delete', params: [data])
  result.empty? ? nil : result['templateids'][0].to_i
end

#get_ids_by_host(data) ⇒ Array

Get Template ids for Host from Zabbix API

Parameters:

  • data (Hash)

    Should include host value to query for matching templates

Returns:

  • (Array)

    Returns array of Template ids

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.


34
35
36
37
38
# File 'lib/zabbixapi/classes/templates.rb', line 34

def get_ids_by_host(data)
  @client.api_request(method: 'template.get', params: data).map do |tmpl|
    tmpl['templateid']
  end
end

#get_or_create(data) ⇒ Integer

Get or Create Template object using Zabbix API

Parameters:

  • data (Hash)

    Needs to include host to properly identify Templates 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.


46
47
48
49
50
51
# File 'lib/zabbixapi/classes/templates.rb', line 46

def get_or_create(data)
  unless (templateid = get_id(host: data[:host]))
    templateid = create(data)
  end
  templateid
end

#indentifyString

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

Returns:

  • (String)

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

def indentify
  'host'
end

#mass_add(data) ⇒ Boolean

Mass add Templates to Hosts using Zabbix API

Parameters:

  • data (Hash)

    Should include hosts_id array and templates_id array

Returns:

  • (Boolean)

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.


76
77
78
79
80
81
82
83
84
85
# File 'lib/zabbixapi/classes/templates.rb', line 76

def mass_add(data)
  result = @client.api_request(
    method: 'template.massAdd',
    params: {
      hosts: data[:hosts_id].map { |t| { hostid: t } },
      templates: data[:templates_id].map { |t| { templateid: t } }
    }
  )
  result.empty? ? false : true
end

#mass_remove(data) ⇒ Boolean

Mass remove Templates to Hosts using Zabbix API

Parameters:

  • data (Hash)

    Should include hosts_id array and templates_id array

Returns:

  • (Boolean)

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.


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/zabbixapi/classes/templates.rb', line 93

def mass_remove(data)
  result = @client.api_request(
    method: 'template.massRemove',
    params: {
      hostids: data[:hosts_id],
      templateids: data[:templates_id],
      groupids: data[:group_id],
      force: 1
    }
  )
  result.empty? ? false : true
end

#mass_update(data) ⇒ Boolean

Mass update Templates for Hosts using Zabbix API

Parameters:

  • data (Hash)

    Should include hosts_id array and templates_id array

Returns:

  • (Boolean)

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.


59
60
61
62
63
64
65
66
67
68
# File 'lib/zabbixapi/classes/templates.rb', line 59

def mass_update(data)
  result = @client.api_request(
    method: 'template.massUpdate',
    params: {
      hosts: data[:hosts_id].map { |t| { hostid: t } },
      templates: data[:templates_id].map { |t| { templateid: t } }
    }
  )
  result.empty? ? false : true
end

#method_nameString

The method name used for interacting with Templates via Zabbix API

Returns:

  • (String)

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

def method_name
  'template'
end