lib/rubix/models/host.rb in rubix-0.5.4 vs lib/rubix/models/host.rb in rubix-0.5.5

- old
+ new

@@ -8,11 +8,11 @@ # The IP for a Host that not supposed to be polled by the Zabbix # server. BLANK_IP = '0.0.0.0' - # The default port. + # The default port on the Host at which Zabbix agent is listening. DEFAULT_PORT = 10050 # The numeric codes for the various status types. zabbix_define :STATUS, { :monitored => 0, @@ -21,19 +21,46 @@ :template => 3, :deleted => 4, :proxy_active => 5, :proxy_passive => 6 } + + # The numeric codes for IPMI authentication algorithms. + zabbix_define :IPMI_AUTH, { + :default => -1, + :none => 0, + :md2 => 1, + :md5 => 2, + :straight => 4, + :oem => 5, + :rmcp_plus => 6 + } + # The numeric codes for IPMI priviledge levels. + zabbix_define :IPMI_PRIVILEGE, { + :callback => 1, + :user => 2, + :operator => 3, + :admin => 4, + :oem => 5 + } + zabbix_attr :name zabbix_attr :ip zabbix_attr :port zabbix_attr :profile zabbix_attr :dns zabbix_attr :status zabbix_attr :use_ip, :default => true zabbix_attr :monitored, :default => true + zabbix_attr :use_ipmi, :default => false + zabbix_attr :ipmi_port, :default => 623 + zabbix_attr :ipmi_username + zabbix_attr :ipmi_password + zabbix_attr :ipmi_ip + zabbix_attr :ipmi_authtype + zabbix_attr :ipmi_privilege, :default => :user def initialize properties={} super(properties) self.host_group_ids = properties[:host_group_ids] @@ -54,10 +81,15 @@ def monitored return @monitored if (!@monitored.nil?) @monitored = true end + def use_ipmi + return @use_ipmi if (!@use_ipmi.nil?) + @use_ipmi = false + end + # # == Associations == # include Associations::HasManyHostGroups @@ -68,10 +100,15 @@ # == Validation == # 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 # # == Requests == @@ -83,25 +120,30 @@ :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? - 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 + # 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 def update_params create_params.tap do |cp| @@ -130,26 +172,34 @@ return true if user_macros.nil? || user_macros.empty? user_macros.map { |um| um.destroy }.all? end 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'), + :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] + :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 def self.get_params super().merge({:select_groups => :refer, :selectParentTemplates => :refer, :select_profile => :refer, :select_macros => :extend})