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

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.



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

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



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

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



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

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

.get_paramsObject



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

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

Instance Method Details

#before_destroyObject



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

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

#before_updateObject



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

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



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

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



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

def destroy_params
  [{id_field => id}]
end

#monitoredObject



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

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

#update_paramsObject



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

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



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

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

#validateObject

Validation ==

Raises:



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

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