Class: Rubix::Item

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

Constant Summary collapse

TRAPPER_TYPE =

The numeric code for a Zabbix item of type ‘Zabbix trapper’. The item must have this type in order for the Zabbix server to listen and accept data submitted by zabbix_sender.

2.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 =
{
  0 => :float,              # Numeric (float)
  1 => :character,          # Character
  2 => :log_line,           # Log
  3 => :unsigned_int,       # Numeric (unsigned)
  4 => :text                # Text
}.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::BelongsToHost

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

Methods inherited from Model

#before_update, #create, #destroy, find, find_or_create, #new_record?, #request, request, #resource_name, resource_name, #save, #update, #validate

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Item

Returns a new instance of Item.



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rubix/models/item.rb', line 53

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

  self.host            = properties[:host]
  self.host_id         = properties[:host_id]
  self.applications    = properties[:applications]
  self.application_ids = properties[:application_ids]
end

Instance Attribute Details

#descriptionObject

Returns the value of attribute description.



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

def description
  @description
end

#keyObject

Returns the value of attribute key.



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

def key
  @key
end

#value_typeObject

Returns the value of attribute value_type.



51
52
53
# File 'lib/rubix/models/item.rb', line 51

def value_type
  @value_type
end

Class Method Details

.build(item) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/rubix/models/item.rb', line 70

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

.find_request(options = {}) ⇒ Object



66
67
68
# File 'lib/rubix/models/item.rb', line 66

def self.find_request options={}
  request('item.get', 'hostids' => [options[:host_id]], 'filter' => {'key_' => options[:key], 'id' => options[:id]}, "select_applications" => "refer", "output" => "extend")
end

.id_fieldObject



81
82
83
# File 'lib/rubix/models/item.rb', line 81

def self.id_field
  'itemid'
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.



46
47
48
# File 'lib/rubix/models/item.rb', line 46

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.



35
36
37
38
39
40
41
42
# File 'lib/rubix/models/item.rb', line 35

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_requestObject



116
117
118
# File 'lib/rubix/models/item.rb', line 116

def create_request
  request('item.create', params)
end

#destroy_requestObject



124
125
126
# File 'lib/rubix/models/item.rb', line 124

def destroy_request
  request('item.delete', [id])
end

#paramsObject

CRUD ==



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rubix/models/item.rb', line 104

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

#update_requestObject



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

def update_request
  request('item.update', params.merge('itemid' => id))
end