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 on the Host at which Zabbix agent is listening.

10050

Instance Attribute Summary

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

#after_create, all, all_params, all_request, #create, #create_request, #destroy, #destroy_request, each, find, find_or_create, find_request, id_field, #id_field, list, #new_record?, properties, request, #request, #resource_name, resource_name, #save, #to_hash, #update, #update_request, web_request, zabbix_attr, zabbix_define, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Host

Returns a new instance of Host.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rubix/models/host.rb', line 63

def initialize properties={}
  super(properties)

  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

Class Method Details

.build(host) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rubix/models/host.rb', line 176

def self.build host
  host['profile'].delete('hostid') if host.is_a?(Hash) && host['profile'].is_a?(Hash) && host['profile']['hostid']
  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'],
        :ip             => host['ip'],
        :dns            => host['dns'],
        :use_ip         => (host['useip'].to_i  == 1),

        # If the status is '1' then this is an unmonitored host.
        # Otherwise it's either '0' for monitored and ok or
        # something else for monitored and *not* ok.
        :monitored      => (host['status'].to_i == 1 ? false : true),
        :status         => self::STATUS_NAMES[host['status'].to_i],
        :use_ipmi       => (host['useipmi'].to_i == 1),
        :ipmi_port      => host['ipmi_port'].to_i,
        :ipmi_username  => host['ipmi_username'],
        :ipmi_password  => host['ipmi_password'],
        :ipmi_ip        => host['ipmi_ip'],
        :ipmi_authtype  => self::IPMI_AUTH_NAMES[host['ipmi_authtype'].to_i],
        :ipmi_privilege => self::IPMI_PRIVILEGE_NAMES[host['ipmi_privilege'].to_i]
      })
end

.find_params(options = {}) ⇒ Object



209
210
211
# File 'lib/rubix/models/host.rb', line 209

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

.get_paramsObject



205
206
207
# File 'lib/rubix/models/host.rb', line 205

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

Instance Method Details

#before_destroyObject



171
172
173
174
# File 'lib/rubix/models/host.rb', line 171

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

#before_updateObject



157
158
159
160
161
162
163
164
165
# File 'lib/rubix/models/host.rb', line 157

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 ==



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rubix/models/host.rb', line 117

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[:profile].delete("hostid") if hp[:profile] && hp[:profile]["hostid"]
    hp[:status]  = (monitored ? 0 : 1) unless monitored.nil?
    
    # Check to see if use_ip is set, otherwise we will use dns
    hp[:useip]          = (use_ip == true ? 1 : 0)
    
    # if we have an IP then use it, otherwise use 0.0.0.0, same goes for the port
    hp[:ip]             = ip   || self.class::BLANK_IP
    hp[:port]           = port || self.class::DEFAULT_PORT
    
    # Always allow for a DNS record to exist even if we dont use it to monitor.
    hp[:dns]            = dns           if dns
    
    hp[:useipmi]        = (use_ipmi == true ? 1 : 0)
    hp[:ipmi_port]      = ipmi_port     if ipmi_port
    hp[:ipmi_username]  = ipmi_username if ipmi_username
    hp[:ipmi_password]  = ipmi_password if ipmi_password
    hp[:ipmi_ip]        = ipmi_ip       if ipmi_ip
    hp[:ipmi_authtype]  = self.class::IPMI_AUTH_CODES[ipmi_authtype]       if ipmi_authtype
    hp[:ipmi_privilege] = self.class::IPMI_PRIVILEGE_CODES[ipmi_privilege] if ipmi_privilege
  end
end

#destroy_paramsObject



167
168
169
# File 'lib/rubix/models/host.rb', line 167

def destroy_params
  [{id_field => id}]
end

#monitoredObject



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

def monitored
  return @monitored if (!@monitored.nil?)
  @monitored = true
end

#update_paramsObject



148
149
150
151
152
153
154
155
# File 'lib/rubix/models/host.rb', line 148

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

#use_ipObject



76
77
78
79
# File 'lib/rubix/models/host.rb', line 76

def use_ip
  return @use_ip if (!@use_ip.nil?)
  @use_ip = true
end

#use_ipmiObject



86
87
88
89
# File 'lib/rubix/models/host.rb', line 86

def use_ipmi
  return @use_ipmi if (!@use_ipmi.nil?)
  @use_ipmi = false
end

#validateObject

Validation ==

Raises:



103
104
105
106
107
108
109
110
111
# File 'lib/rubix/models/host.rb', line 103

def validate
  raise ValidationError.new("A host must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
  raise ValidationError.new("A host must have a valid ip address if use_ip is set.") if use_ip && ip == self.class::BLANK_IP
  raise ValidationError.new("A host must have an ip address if use_ip is set.") if use_ip && (ip.nil? || ip.empty?)
  raise ValidationError.new("A host must have a dns name if use_ip is false.") if !use_ip && dns.nil?
  raise ValidationError.new("A host must have a ipmi_privilege defined as one of: " + self.class::IPMI_PRIVILEGE_CODES.keys.to_s) if use_ipmi && self.class::IPMI_PRIVILEGE_CODES[ipmi_privilege].nil?
  raise ValidationError.new("A host must have a ipmi_authtype defined as one of: " + self.class::IPMI_AUTH_CODES.keys.to_s) if use_ipmi && self.class::IPMI_AUTH_CODES[ipmi_authtype].nil?
  true
end