Class: ZabbixApi::Hosts

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

Instance Method Summary collapse

Methods inherited from Basic

#add, #all, #create, #delete, #destroy, #get, #get_full_data, #get_id, #get_or_create, #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 Host object using Zabbix API

Parameters:

  • data (Hash)

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



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

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

#default_optionsHash

The default options used when creating Host objects via Zabbix API

Returns:

  • (Hash)


41
42
43
44
45
46
47
48
49
50
# File 'lib/zabbixapi/classes/hosts.rb', line 41

def default_options
  {
    :host => nil,
    :interfaces => [],
    :status => 0,
    :available => 1,
    :groups => [],
    :proxy_hostid => nil,
  }
end

#dump_by_id(data) ⇒ Hash

Dump Host 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
# File 'lib/zabbixapi/classes/hosts.rb', line 23

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

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

#indentifyString

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

Returns:

  • (String)


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

def indentify
  'host'
end

#method_nameString

The method name used for interacting with Hosts via Zabbix API

Returns:

  • (String)


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

def method_name
  'host'
end

Unlink/Remove Templates from 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.



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

def unlink_templates(data)
  result = @client.api_request(
    :method => 'host.massRemove',
    :params => {
      :hostids => data[:hosts_id],
      :templates => data[:templates_id],
    }
  )
  result.empty? ? false : true
end