Class: Rubix::Template

Inherits:
Model
  • Object
show all
Includes:
Associations::HasManyHostGroups, Associations::HasManyHosts
Defined in:
lib/rubix/models/template.rb

Constant Summary collapse

IMPORT_OPTIONS =

Options which control the template import process and the Zabbix keys they need to be mapped to.

{
  :update_hosts     => 'rules[host][exist]',
  :add_hosts        => 'rules[host][missed]',
  :update_items     => 'rules[item][exist]',
  :add_items        => 'rules[item][missed]',
  :update_triggers  => 'rules[trigger][exist]',
  :add_triggers     => 'rules[trigger][missed]',
  :update_graphs    => 'rules[graph][exist]',
  :add_graphs       => 'rules[graph][missed]',
  :update_templates => 'rules[template][exist]'
}.freeze

Instance Attribute Summary

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::HasManyHostGroups

#host_group_ids, #host_group_ids=, #host_group_params, #host_groups, #host_groups=

Methods included from Associations::HasManyHosts

#host_ids, #host_ids=, #hosts, #hosts=

Methods inherited from Model

#after_create, all, all_params, all_request, #before_destroy, #before_update, #create, #create_request, #destroy, #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_request, web_request, zabbix_attr, zabbix_define, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Template


11
12
13
14
15
16
17
18
19
# File 'lib/rubix/models/template.rb', line 11

def initialize properties={}
  super(properties)
  
  self.host_ids = properties[:host_ids]
  self.hosts    = properties[:hosts]

  self.host_group_ids = properties[:host_group_ids]
  self.host_groups    = properties[:host_groups]
end

Class Method Details

.build(template) ⇒ Object


62
63
64
65
66
67
68
69
# File 'lib/rubix/models/template.rb', line 62

def self.build template
  new({
        :id       => (template[id_field] || template['hostid']).to_i,
        :name     => template['host'],
        :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
        :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
      })
end

.find_params(options = {}) ⇒ Object


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

def self.find_params options={}
  get_params.merge(:filter => {:host => options[:name], :hostid => options[:id]})
end

.get_paramsObject


54
55
56
# File 'lib/rubix/models/template.rb', line 54

def self.get_params
  super().merge(:select_groups => :refer, :select_hosts => :refer)
end

.import(fh, options = {}) ⇒ Object

Import/update a template from XML contained in an open file handle fh.

By default all hosts, items, triggers, and graphs the XML defines will be both added and updated. (Linked templates will also be updated.) This behavior matches the default behavior of the web interface in Zabbix 1.8.8.

This can be controlled with options like :update_hosts or :add_graphs, all of which default to true. (Linked templates are controlled with :update_templates.)


100
101
102
103
# File 'lib/rubix/models/template.rb', line 100

def self.import fh, options={}
  response = web_request("POST", "/templates.php", import_options(options).merge(:import_file => fh))
  File.open('/tmp/output.html', 'w') { |f| f.puts(response.body) }
end

.import_options(options) ⇒ Object


105
106
107
108
109
110
111
# File 'lib/rubix/models/template.rb', line 105

def self.import_options options
  {}.tap do |o|
    self::IMPORT_OPTIONS.each_pair do |name, zabbix_name|
      o[zabbix_name] = 'yes' unless options[name] == false
    end
  end
end

Instance Method Details

#create_paramsObject

CRUD ==


42
43
44
# File 'lib/rubix/models/template.rb', line 42

def create_params
  {:host => name, :groups => host_group_params}
end

#destroy_paramsObject


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

def destroy_params
  [{id_field => id}]
end

#update_paramsObject


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

def update_params
  [create_params.merge(id_field => id)]
end

#validateObject

Validation ==

Raises:


25
26
27
28
29
# File 'lib/rubix/models/template.rb', line 25

def validate
  super()
  raise ValidationError.new("A template must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
  true
end