Class: Rubix::Trigger

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

Constant Summary collapse

PRIORITY_NAMES =

Properties & Finding ==

{
  :not_classified => 0,
  :information    => 1,
  :warning        => 2,
  :average        => 3,
  :high           => 4,
  :disaster       => 5
}.freeze
PRIORITY_CODES =
PRIORITY_NAMES.invert.freeze
STATUS_NAMES =
{
  :enabled  => 0,
  :disabled => 1
}.freeze
STATUS_CODES =
STATUS_NAMES.invert.freeze

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::HasManyItems

#item_ids, #item_ids=, #items, #items=

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

#after_create, 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?, properties, request, #request, #resource_name, resource_name, #save, #to_hash, #update, #update_params, #update_request, #validate, web_request, zabbix_attr, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Trigger

Returns a new instance of Trigger.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rubix/models/trigger.rb', line 31

def initialize properties={}
  super(properties)
  self.expression = properties[:expression]
  
  self.host     = properties[:host]
  self.template = properties[:template]
  
  self.template_id = properties[:template_id]
  self.host_id = properties[:host_id]
  
  self.items    = properties[:items]
  self.item_ids = properties[:item_ids]
end

Instance Attribute Details

#expressionObject

Returns the value of attribute expression.



45
46
47
# File 'lib/rubix/models/trigger.rb', line 45

def expression
  @expression
end

Class Method Details

.build(trigger) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rubix/models/trigger.rb', line 106

def self.build trigger
  new({
        :id              => trigger[id_field].to_i,
        :description     => trigger['description'],
        :expression      => trigger['expression'],
        :comments        => trigger['comments'],
        :url             => trigger['url'],
        :status          => STATUS_CODES[trigger['status'].to_i],
        :priority        => PRIORITY_CODES[trigger['priority'].to_i],
        :item_ids        => (trigger['items'] || []).map { |item| item['itemid'].to_i }
      }.merge(host_or_template_params_from_id(trigger['templateid'].to_i)))
end

.find_params(options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubix/models/trigger.rb', line 90

def self.find_params options={}
  fp = {
    :filter => {
      :description => options[:description]
    }
  }.tap do |fp|
    case
    when options[:template_id]
      fp[:templateids] = [options[:template_id]]
    when options[:host_id]
      fp[:hostids] = [options[:host_id]]
    end
  end
  super().merge(fp)
end

.get_paramsObject



86
87
88
# File 'lib/rubix/models/trigger.rb', line 86

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

.host_or_template_params_from_id(id) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rubix/models/trigger.rb', line 119

def self.host_or_template_params_from_id id
  template_or_host = Template.find(:id => id) || Host.find(:id => id)
  case template_or_host
  when Template
    { :template => template_or_host }
  when Host
    { :host     => template_or_host }
  else
    {}
  end
end

Instance Method Details

#create_paramsObject

Requests ==



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubix/models/trigger.rb', line 74

def create_params
  {
    :templateid   => (template_id || host_id),
    :description  => (description || 'Unknown'),
    :expression   => expression,
    :priority     => self.class::PRIORITY_NAMES[priority],
    :status       => self.class::STATUS_NAMES[status],
    :comments     => comments,
    :url          => url
  }
end