Class: Rubix::Host

Inherits:
Model
  • Object
show all
Includes:
Associations::HasManyHostGroups, Associations::HasManyTemplates, Associations::HasManyUserMacros
Defined in:
lib/rubix/models/host.rb

Constant Summary collapse

BLANK_IP =

The IP for a Host that not supposed to be polled by the Zabbix server.

'0.0.0.0'
DEFAULT_PORT =

The default port.

10050

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::HasManyUserMacros

#user_macro_ids, #user_macro_ids=, #user_macro_params, #user_macros, #user_macros=

Methods included from Associations::HasManyTemplates

#template_ids, #template_ids=, #template_params, #templates, #templates=

Methods included from Associations::HasManyHostGroups

#host_group_ids, #host_group_ids=, #host_group_params, #host_groups, #host_groups=

Methods inherited from Model

#create, #destroy, find, find_or_create, log_name, model_name, #new_record?, #request, request, #save, #update

Methods included from Logs

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(properties = {}) ⇒ Host

Returns a new instance of Host.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubix/models/host.rb', line 18

def initialize properties={}
  super(properties)
  @name        = properties[:name]
  @ip          = properties[:ip]
  @port        = properties[:port]
  @profile     = properties[:profile]
  @status      = properties[:status]

  self.host_group_ids = properties[:host_group_ids]
  self.host_groups    = properties[:host_groups]

  self.template_ids   = properties[:template_ids]
  self.templates      = properties[:templates]

  self.user_macro_ids = properties[:user_macro_ids]
  self.user_macros    = properties[:user_macros]
end

Instance Attribute Details

#ipObject

Returns the value of attribute ip.



16
17
18
# File 'lib/rubix/models/host.rb', line 16

def ip
  @ip
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/rubix/models/host.rb', line 16

def name
  @name
end

#portObject

Returns the value of attribute port.



16
17
18
# File 'lib/rubix/models/host.rb', line 16

def port
  @port
end

#profileObject

Returns the value of attribute profile.



16
17
18
# File 'lib/rubix/models/host.rb', line 16

def profile
  @profile
end

#statusObject

Returns the value of attribute status.



16
17
18
# File 'lib/rubix/models/host.rb', line 16

def status
  @status
end

Class Method Details

.build(host) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubix/models/host.rb', line 40

def self.build host
  new({
        :id             => host['hostid'].to_i,
        :name           => host['host'],
        :host_group_ids => host['groups'].map { |group| group['groupid'].to_i },
        :template_ids   => host['parentTemplates'].map { |template| (template['templateid'] || template['hostid']).to_i },
        :user_macros    => host['macros'].map { |um| UserMacro.new(:host_id => um['hostid'].to_i, :id => um['hostmacroid'], :value => um['value'], :macro => um['macro']) },
        :profile        => host['profile'],
        :port           => host['port']
      })
end

.find_request(options = {}) ⇒ Object



36
37
38
# File 'lib/rubix/models/host.rb', line 36

def self.find_request options={}
  request('host.get', 'filter' => {'host' => options[:name], 'hostid' => options[:id]}, 'select_groups' => 'refer', 'selectParentTemplates' => 'refer', 'select_profile' => 'refer', 'select_macros' => 'extend', 'output' => 'extend')
end

.id_fieldObject



56
57
58
# File 'lib/rubix/models/host.rb', line 56

def self.id_field
  'hostid'
end

Instance Method Details

#after_updateObject



107
108
109
110
111
112
113
114
115
116
# File 'lib/rubix/models/host.rb', line 107

def after_update
  response = request('host.massUpdate', { 'groups' => host_group_params, 'templates' => template_params, 'macros' => user_macro_params, 'hosts' => [{'hostid' => id}]})
  if response.has_data?
    info("Updated templates, host groups, & macros")
    true
  else
    error("Could not update all templates, host groups, and/or macros: #{response.error_message}")
    false
  end
end

#create_requestObject



99
100
101
# File 'lib/rubix/models/host.rb', line 99

def create_request
  request('host.create', params.merge('groups' => host_group_params, 'templates' => template_params, 'macros' => user_macro_params))
end

#destroy_requestObject



118
119
120
# File 'lib/rubix/models/host.rb', line 118

def destroy_request
  request('host.delete', [{'hostid' => id}])
end

#log_nameObject



52
53
54
# File 'lib/rubix/models/host.rb', line 52

def log_name
  "HOST #{name || id}"
end

#paramsObject

CRUD ==



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rubix/models/host.rb', line 81

def params
  {}.tap do |hp|
    hp['host']    = name
    
    hp['profile'] = profile if profile
    hp['status']  = status  if status
    
    if ip
      hp['ip']      = ip
      hp['useip']   = true
      hp['port']    = port || self.class::DEFAULT_PORT
    else
      hp['ip'] = self.class::BLANK_IP
    end
    
  end
end

#update_requestObject



103
104
105
# File 'lib/rubix/models/host.rb', line 103

def update_request
  request('host.update', params.merge('hostid' => id))
end

#validateObject

Validation ==

Raises:



72
73
74
75
# File 'lib/rubix/models/host.rb', line 72

def validate
  raise ValidationError.new("A host must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
  true
end