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 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, list, #new_record?, request, #request, #resource_name, resource_name, #save, #update, #update_request, web_request, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Host

Returns a new instance of Host.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rubix/models/host.rb', line 31

def initialize properties={}
  super(properties)
  @name        = properties[:name]
  @ip          = properties[:ip]
  @port        = properties[:port]
  @profile     = properties[:profile]
  @monitored   = properties[:monitored]
  @dns         = properties[:dns]
  @use_ip      = properties[:use_ip]
  @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

#dnsObject

Returns the value of attribute dns.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def dns
  @dns
end

#ipObject

Returns the value of attribute ip.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def ip
  @ip
end

#monitoredObject



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

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

#nameObject

Returns the value of attribute name.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def name
  @name
end

#portObject

Returns the value of attribute port.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def port
  @port
end

#profileObject

Returns the value of attribute profile.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def profile
  @profile
end

#statusObject

Returns the value of attribute status.



28
29
30
# File 'lib/rubix/models/host.rb', line 28

def status
  @status
end

#use_ipObject



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

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

Class Method Details

.build(host) ⇒ Object



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

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



162
163
164
# File 'lib/rubix/models/host.rb', line 162

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

.get_paramsObject



158
159
160
# File 'lib/rubix/models/host.rb', line 158

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

Instance Method Details

#before_destroyObject



132
133
134
135
# File 'lib/rubix/models/host.rb', line 132

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

#before_updateObject



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

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



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

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



128
129
130
# File 'lib/rubix/models/host.rb', line 128

def destroy_params
  [{id_field => id}]
end

#update_paramsObject



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

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:



74
75
76
77
# File 'lib/rubix/models/host.rb', line 74

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