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 ‘zabbix’.

{
  :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
DATA_CODES =

The numeric codes for the data types of a Zabbix item.

The default will be :decimal

{
  :decimal     => 0,
  :octal       => 1,
  :hexadecimal => 2
}.freeze
DATA_NAMES =
DATA_CODES.invert.freeze
STATUS_CODES =

The numeric codes for the status of a Zabbix item.

The default will be :active

{
  :active        => 0,
  :disabled      => 1,
  :not_supported => 3
}.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::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, list, #new_record?, request, #request, resource_name, #save, #update, #update_params, #update_request, #validate, web_request, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Item

Returns a new instance of Item.



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

def initialize properties={}
  super(properties)
  self.key             = properties[:key]
  self.description     = properties[:description]
  self.type            = properties[:type]
  self.units           = properties[:units]
  self.multiply_by     = properties[:multiply_by]
  self.value_type      = properties[:value_type]
  self.data_type       = properties[:data_type]
  self.history         = properties[:history]
  self.trends          = properties[:trends]
  self.status          = properties[:status]
  self.frequency       = properties[:frequency]

  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

#data_typeObject

Returns the value of attribute data_type.



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

def data_type
  @data_type
end

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#frequencyObject

Returns the value of attribute frequency.



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

def frequency
  @frequency
end

#historyObject

Returns the value of attribute history.



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

def history
  @history
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#multiply_byObject

Returns the value of attribute multiply_by.



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

def multiply_by
  @multiply_by
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Returns the value of attribute trends.



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

def trends
  @trends
end

#typeObject



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

def type
  @type ||= :zabbix
end

#unitsObject

Returns the value of attribute units.



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

def units
  @units
end

#value_typeObject



114
115
116
# File 'lib/rubix/models/item.rb', line 114

def value_type
  @value_type ||= :character
end

Class Method Details

.build(item) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/rubix/models/item.rb', line 176

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],
        :data_type       => DATA_NAMES[item['data_type'].to_i],
        :history         => item['history'].to_i,
        :trends          => item['trends'].to_i,
        :status          => STATUS_NAMES[item['status'].to_i],
        :application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
        :key             => item['key_'],
        :units           => item['units'],
        :frequency       => item['delay'].to_i,
        :multiply_by     => ((item['multiplier'].to_i == 1 && item['formula'].to_f != 0.0) ? item['formula'].to_f : nil)
      })
end

.find_params(options = {}) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubix/models/item.rb', line 164

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



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

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.



79
80
81
# File 'lib/rubix/models/item.rb', line 79

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.



68
69
70
71
72
73
74
75
# File 'lib/rubix/models/item.rb', line 68

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



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

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
    p[:data_type]    = self.class::DATA_CODES[data_type] if data_type
    p[:history]      = history.to_i if history
    p[:trends]       = trends.to_i if trends
    p[:status]       = self.class::STATUS_CODES[status] if status
    p[:delay]        = frequency if frequency
    if multiply_by && multiply_by.to_f != 0.0
      p[:multiplier] = 1
      p[:formula]    = multiply_by.to_f
    end
  end
end

#resource_nameObject



110
111
112
# File 'lib/rubix/models/item.rb', line 110

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

#time_series(options = {}) ⇒ Object



195
196
197
# File 'lib/rubix/models/item.rb', line 195

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