Class: Rubix::Trigger

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

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, #save, #to_hash, #update, #update_params, #update_request, #validate, web_request, zabbix_attr, zabbix_define, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Trigger

Returns a new instance of Trigger.



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

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.



43
44
45
# File 'lib/rubix/models/trigger.rb', line 43

def expression
  @expression
end

Class Method Details

.build(trigger) ⇒ Object



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

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_NAMES[trigger['status'].to_i],
        :priority        => PRIORITY_NAMES[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



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

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



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

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

.host_or_template_params_from_id(id) ⇒ Object



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

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



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

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

#resource_nameObject



60
61
62
# File 'lib/rubix/models/trigger.rb', line 60

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