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

The numeric codes for the various status types.

{
  :monitored     => 0,
  :not_monitored => 1,
  :unreachable   => 2,
  :template      => 3,
  :deleted       => 4,
  :proxy_active  => 5,
  :proxy_passive => 6
}.freeze
STATUS_NAMES =
STATUS_CODES.invert.freeze

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_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Host

Returns a new instance of Host.



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

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rubix/models/host.rb', line 135

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'],
        :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]
      })
end

.find_params(options = {}) ⇒ Object



160
161
162
# File 'lib/rubix/models/host.rb', line 160

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

.get_paramsObject



156
157
158
# File 'lib/rubix/models/host.rb', line 156

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

Instance Method Details

#before_destroyObject



130
131
132
133
# File 'lib/rubix/models/host.rb', line 130

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

#before_updateObject



116
117
118
119
120
121
122
123
124
# File 'lib/rubix/models/host.rb', line 116

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rubix/models/host.rb', line 81

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]  = (monitored ? 0 : 1) unless monitored.nil?
    
    case
    when use_ip == true && (!ip.nil?) && (!ip.empty?)
      hp[:useip] = 1
      hp[:ip]    = ip
      hp[:port]  = port || self.class::DEFAULT_PORT
    when (!dns.nil?) && (!dns.empty?)
      hp[:useip] = 0
      hp[:dns]   = dns
      hp[:port]  = port || self.class::DEFAULT_PORT
    else
      hp[:ip] = self.class::BLANK_IP
      hp[:useip] = 1
    end
  end
end

#destroy_paramsObject



126
127
128
# File 'lib/rubix/models/host.rb', line 126

def destroy_params
  [{id_field => id}]
end

#monitoredObject



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

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

#update_paramsObject



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

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



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

def use_ip
  return @use_ip if (!@use_ip.nil?)
  @use_ip = true
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