Class: ZabbixApi::Hosts

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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Hosts

Returns a new instance of Hosts.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/zabbixapi/hosts.rb', line 4

def initialize(options = {})
  @client = Client.new(options)
  @options = options
  @host_default_options = {
    :host => nil,
    :port => 10050,
    :status => 1,
    :useip => 1,
    :dns => '',
    :ip => '0.0.0.0',
    :proxy_hostid => 0,
    :groups => [],
    :useipmi => 0,
    :ipmi_ip => '',
    :ipmi_port => 623,
    :ipmi_authtype => 0,
    :ipmi_privilege => 0,
    :ipmi_username => '',
    :ipmi_password => ''
  }
end

Instance Method Details

#add(data) ⇒ Object



36
37
38
# File 'lib/zabbixapi/hosts.rb', line 36

def add(data)
  create(data)
end

#create(data) ⇒ Object



31
32
33
34
# File 'lib/zabbixapi/hosts.rb', line 31

def create(data)
  result = @client.api_request(:method => "host.create", :params => [merge_params(data)])
  result.empty? ? nil : result['hostids'][0].to_i
end

#create_or_update(data) ⇒ Object



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

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

#delete(data) ⇒ Object



56
57
58
59
# File 'lib/zabbixapi/hosts.rb', line 56

def delete(data)
  result = @client.api_request(:method => "host.delete", :params => [:hostid => data])
  result.empty? ? nil : result['hostids'][0].to_i
end

#destroy(data) ⇒ Object



61
62
63
# File 'lib/zabbixapi/hosts.rb', line 61

def destroy(data)
  delete(data)
end

#get_full_data(data) ⇒ Object



70
71
72
# File 'lib/zabbixapi/hosts.rb', line 70

def get_full_data(data)
  @client.api_request(:method => "host.get", :params => {:filter => data, :output => "extend"})
end

#get_id(data) ⇒ Object



79
80
81
82
83
84
# File 'lib/zabbixapi/hosts.rb', line 79

def get_id(data)
  result = get_full_data(data)
  hostid = nil
  result.each { |host| hostid = host['hostid'].to_i if host['host'] == data[:host] }
  hostid
end

#merge_params(params) ⇒ Object



26
27
28
29
# File 'lib/zabbixapi/hosts.rb', line 26

def merge_params(params)
  result = JSON.generate(@host_default_options).to_s + "," + JSON.generate(params).to_s
  JSON.parse(result.gsub('},{', ','))
end


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zabbixapi/hosts.rb', line 40

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

#update(data) ⇒ Object



65
66
67
68
# File 'lib/zabbixapi/hosts.rb', line 65

def update(data)
  result = @client.api_request(:method => "host.update", :params => data)
  result.empty? ? nil : result['hostids'][0].to_i
end