Class: ZabbixApi::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/zabbixapi/templates.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Templates

Returns a new instance of Templates.



4
5
6
7
# File 'lib/zabbixapi/templates.rb', line 4

def initialize(options = {})
  @client = Client.new(options)
  @options = options
end

Instance Method Details

#add(data) ⇒ Object

Add template Synonym create



22
23
24
# File 'lib/zabbixapi/templates.rb', line 22

def add(data)
  create(data)
end

#allObject

Return all templates

  • Returns :

    • Hash with => “templateid1”, “Template_Name2” => “templateid2”



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/zabbixapi/templates.rb', line 125

def all
  result = {}
  case @client.api_version
    when "1.2"
      @client.api_request(:method => "template.get", :params => {:output => "extend"}).values.each do |tmpl|
        result[tmpl['host']] = tmpl['hostid']
      end          
    else
      @client.api_request(:method => "template.get", :params => {:output => "extend"}).each do |tmpl|
        result[tmpl['host']] = tmpl['hostid']
      end
  end
  result
end

#create(data) ⇒ Object

Create template

  • Args :

    • data -> Hash with :host => “Template_Name” and :groups => array with hostgroup ids

  • Returns :

    • Nil or Integer



15
16
17
18
# File 'lib/zabbixapi/templates.rb', line 15

def create(data)
  result = @client.api_request(:method => "template.create", :params => [data])
  result.empty? ? nil : result['templateids'][0].to_i
end

#delete(data) ⇒ Object

Delete template

  • Args :

    • data -> Hash with :host => “Template_Name”

  • Returns :

    • Nil or Integer



32
33
34
35
# File 'lib/zabbixapi/templates.rb', line 32

def delete(data)
  result = @client.api_request(:method => "template.delete", :params => [:templateid => data])
  result.empty? ? nil : result['templateids'][0].to_i
end

#destroy(data) ⇒ Object

Destroy template Synonym delete



39
40
41
# File 'lib/zabbixapi/templates.rb', line 39

def destroy(data)
  delete(data)
end

#get_full_data(data) ⇒ Object

Return info about template

  • Args :

    • data -> Hash with :host => “Template name”

  • Returns :

    • Hash with template info



146
147
148
149
150
151
152
153
154
155
# File 'lib/zabbixapi/templates.rb', line 146

def get_full_data(data)
  case @client.api_version
    when "1.2"
      # in this version returned id=>{...}
      result = @client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
      result.empty? ? [] : result.values 
    else
      @client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
  end
end

#get_id(data) ⇒ Object

Return info about template

  • Args :

    • data -> Hash with :host => “Template name”

  • Returns :

    • Nil or Integer



163
164
165
166
167
# File 'lib/zabbixapi/templates.rb', line 163

def get_id(data)
  templateid = nil
  get_full_data(data).each { |template| templateid = template['templateid'].to_i if template['host'] == data[:host] }
  templateid
end

#get_ids_by_host(data) ⇒ Object

Return templateids linked with host

  • Args :

    • data -> Hash with :hostids => [hostid]

  • Returns :

    • Array with templateids



49
50
51
52
53
54
55
# File 'lib/zabbixapi/templates.rb', line 49

def get_ids_by_host(data)
  result = []
  @client.api_request(:method => "template.get", :params => data).each do |tmpl|
    result << tmpl['templateid']
  end
  result
end

#get_or_create(data) ⇒ Object

Return templateid

  • Args :

    • data -> Hash with :host => “Template_Name” and :groups => array with hostgroup ids

  • Returns :

    • Integer



63
64
65
66
67
68
# File 'lib/zabbixapi/templates.rb', line 63

def get_or_create(data)
  unless templateid = get_id(:host => data[:host])
    templateid = create(data)
  end
  templateid
end

#mass_add(data) ⇒ Object

Analog Zabbix api call massAdd

  • Args :

    • data -> Hash with :hosts_id => [hostid1, hostid2 …], and :templates_id => [templateid1, templateid2 …]

  • Returns :

    • True or False



93
94
95
96
97
98
99
100
101
102
# File 'lib/zabbixapi/templates.rb', line 93

def mass_add(data)
  result = @client.api_request(
    :method => "template.massAdd", 
    :params => {
      :hosts => data[:hosts_id].map { |t| {:hostid => t} },
      :templates => data[:templates_id].map { |t| {:templateid => t} }
    }
  )
  result.empty? ? false : true
end

#mass_remove(data) ⇒ Object

Analog Zabbix api call massRemove

  • Args :

    • data -> Hash with :hosts_id => [hostid1, hostid2 …], and :templates_id => [templateid1, templateid2 …]

  • Returns :

    • True or False



110
111
112
113
114
115
116
117
118
119
# File 'lib/zabbixapi/templates.rb', line 110

def mass_remove(data)
  result = @client.api_request(
    :method => "template.massRemove", 
    :params => {
      :hostids => data[:hosts_id],
      :templateids => data[:templates_id]
    }
  )
  result.empty? ? false : true      
end

#mass_update(data) ⇒ Object

Analog Zabbix api call massUpdate

  • Args :

    • data -> Hash with :hosts_id => [hostid1, hostid2 …], and :templates_id => [templateid1, templateid2 …]

  • Returns :

    • True or False



76
77
78
79
80
81
82
83
84
85
# File 'lib/zabbixapi/templates.rb', line 76

def mass_update(data)
  result = @client.api_request(
    :method => "template.massAdd", 
    :params => {
      :hosts => data[:hosts_id].map { |t| {:hostid => t} },
      :templates => data[:templates_id].map { |t| {:templateid => t} }
    }
  )
  result.empty? ? false : true
end