Class: Rubix::Item

Inherits:
Model
  • Object
show all
Includes:
Associations::BelongsToHost, Associations::BelongsToTemplate, Associations::HasManyApplications
Defined in:
lib/rubix/models/item.rb

Constant Summary collapse

TYPE_CODES =

The numeric codes for the various item types.

Items without a type will be set to ‘trapper’ so they can be easily written to manually.

{
  :zabbix     => 0,
  :snmpv1     => 1,
  :trapper    => 2,
  :simple     => 3,
  :snmpv2c    => 4,
  :internal   => 5,
  :snmpv3     => 6,
  :active     => 7,
  :aggregate  => 8,
  :httptest   => 9,
  :external   => 10,
  :db_monitor => 11,
  :ipmi       => 12,
  :ssh        => 13,
  :telnet     => 14,
  :calculated => 15
}.freeze
TYPE_NAMES =
TYPE_CODES.invert.freeze
VALUE_CODES =

The numeric codes for the value types of a Zabbix item. This Hash is used by ZabbixPipe#value_code_from_value to dynamically set the type of a value when creating a new Zabbix item.

{
  :float        => 0,         # Numeric (float)
  :character    => 1,         # Character
  :log_line     => 2,         # Log
  :unsigned_int => 3,         # Numeric (unsigned)
  :text         => 4          # Text
}.freeze
VALUE_NAMES =

Text

VALUE_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::HasManyApplications

#application_ids, #application_ids=, #applications, #applications=

Methods included from Associations::BelongsToTemplate

#template, #template=, #template_id, #template_id=

Methods included from Associations::BelongsToHost

#host, #host=, #host_id, #host_id=

Methods inherited from Model

all, all_params, all_request, #before_destroy, #before_update, #create, #create_request, #destroy, #destroy_params, #destroy_request, each, find, find_or_create, find_request, #id_field, id_field, #new_record?, request, #request, resource_name, #save, #update, #update_params, #update_request, #validate, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Item

Returns a new instance of Item.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rubix/models/item.rb', line 65

def initialize properties={}
  super(properties)
  @key            = properties[:key]
  @description    = properties[:description]
  @type           = properties[:type]
  @units          = properties[:units]
  
  self.value_type = properties[:value_type]

  self.host            = properties[:host]
  self.host_id         = properties[:host_id]

  self.template        = properties[:template]
  self.template_id     = properties[:template_id]
  
  self.applications    = properties[:applications]
  self.application_ids = properties[:application_ids]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



62
63
64
# File 'lib/rubix/models/item.rb', line 62

def description
  @description
end

#keyObject

Returns the value of attribute key.



62
63
64
# File 'lib/rubix/models/item.rb', line 62

def key
  @key
end

#typeObject



92
93
94
# File 'lib/rubix/models/item.rb', line 92

def type
  @type ||= :trapper
end

#unitsObject

Returns the value of attribute units.



62
63
64
# File 'lib/rubix/models/item.rb', line 62

def units
  @units
end

#value_typeObject



88
89
90
# File 'lib/rubix/models/item.rb', line 88

def value_type
  @value_type ||= :character
end

Class Method Details

.build(item) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/rubix/models/item.rb', line 137

def self.build item
  new({
        :id              => item[id_field].to_i,
        :host_id         => item['hostid'].to_i,
        :description     => item['description'],
        :type            => TYPE_NAMES[item['type'].to_i],
        :value_type      => VALUE_NAMES[item['value_type'].to_i],
        :application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
        :key             => item['key_'],
        :units           => item['units']
      })
end

.find_params(options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/rubix/models/item.rb', line 125

def self.find_params options={}
  super().merge({
                  :filter => {
                    :key_ => options[:key],
                    :id   => options[:id]
                  }
                }.tap do |o|
                  o[:hostids] = [options[:host_id]] if options[:host_id]
                end
                )
end

.get_paramsObject



121
122
123
# File 'lib/rubix/models/item.rb', line 121

def self.get_params
  super().merge(:select_applications => :refer)
end

.value_code_from_value(value) ⇒ Object

Return the value_type‘s numeric code for a Zabbix item’s value type by examining the given value.



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

def self.value_code_from_value value
  self::VALUE_CODES[value_type_from_value(value)]
end

.value_type_from_value(value) ⇒ Object

Return the value_type name (:float, :text, &c.) for a Zabbix item’s value type by examining the given value.



47
48
49
50
51
52
53
54
# File 'lib/rubix/models/item.rb', line 47

def self.value_type_from_value value
  case
  when value =~ /\d+/       then :unsigned_int
  when value =~ /-?[\d\.]+/ then :float
  when value.include?("\n") then :text
  else :character
  end
end

Instance Method Details

#create_paramsObject

Requests ==



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rubix/models/item.rb', line 108

def create_params
  {
    :hostid       => host_id,
    :description  => (description || 'Unknown'),
    :type         => self.class::TYPE_CODES[type],
    :key_         => key,
    :value_type   => self.class::VALUE_CODES[value_type]
  }.tap do |p|
    p[:applications] = application_ids if application_ids
    p[:units]        = units           if units
  end
end

#resource_nameObject



84
85
86
# File 'lib/rubix/models/item.rb', line 84

def resource_name
  "#{self.class.resource_name} #{self.key || self.id}"
end

#time_series(options = {}) ⇒ Object



150
151
152
# File 'lib/rubix/models/item.rb', line 150

def time_series options={}
  TimeSeries.find(options.merge(:item_id => self.id, :item => self))
end