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

all, all_params, all_request, #create, #create_request, #destroy, #destroy_request, each, find, find_or_create, find_request, id_field, #id_field, #new_record?, request, #request, #resource_name, resource_name, #save, #update, #update_request, zabbix_name

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



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

def self.build host
  new({
        :id             => host[id_field].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[id_field]).to_i },
        :user_macros    => host['macros'].map { |um| UserMacro.new(:host_id => um[id_field].to_i, :id => um['hostmacroid'], :value => um['value'], :macro => um['macro']) },
        :profile        => host['profile'],
        :port           => host['port']
      })
end

.find_params(options = {}) ⇒ Object



121
122
123
# File 'lib/rubix/models/host.rb', line 121

def self.find_params options={}
  get_params.merge(:filter => {:host => options[:name], id_field => options[:id]})
end

.get_paramsObject



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

def self.get_params
  super().merge({:select_groups => :refer, :selectParentTemplates => :refer, :select_profile => :refer, :select_macros => :extend})
end

Instance Method Details

#before_destroyObject



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

def before_destroy
  return true if user_macros.nil? || user_macros.empty?
  user_macros.map { |um| um.destroy }.all?
end

#before_updateObject



86
87
88
89
90
91
92
93
94
# File 'lib/rubix/models/host.rb', line 86

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

#create_paramsObject

Requests ==



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubix/models/host.rb', line 57

def create_params
  {
    :host      => name,
    :groups    => host_group_params,
    :templates => template_params,
    :macros    => user_macro_params
  }.tap do |hp|
    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

#destroy_paramsObject



96
97
98
# File 'lib/rubix/models/host.rb', line 96

def destroy_params
  [{id_field => id}]
end

#update_paramsObject



77
78
79
80
81
82
83
84
# File 'lib/rubix/models/host.rb', line 77

def update_params
  create_params.tap do |cp|
    cp.delete(:groups)
    cp.delete(:templates)
    cp.delete(:macros)
    cp[id_field] = id
  end
end

#validateObject

Validation ==

Raises:



48
49
50
51
# File 'lib/rubix/models/host.rb', line 48

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