Class: Rubix::Model

Inherits:
Object
  • Object
show all
Extended by:
Logs
Includes:
Logs
Defined in:
lib/rubix/models/model.rb

Overview

A base class for all Zabbix models to subclass.

It might be worth using ActiveModel – but maybe not. The goal is to keep dependencies low while still retaining expressiveness.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logs

debug, error, fatal, info, warn

Constructor Details

#initialize(properties = {}) ⇒ Model

Create a new model instance. This may represent a new or existing Zabbix resource.

Parameters:

  • properties (Hash) (defaults to: {})

Options Hash (properties):

  • id (Fixnum)

    the ID of the resource in Zabbix (typically blank for a new resource)



71
72
73
74
# File 'lib/rubix/models/model.rb', line 71

def initialize properties={}
  @properties = properties
  @id         = properties[:id]
end

Instance Attribute Details

#idFixnum?

Returns the ID of this model.

Returns:

  • (Fixnum, nil)

    the ID of this model



13
14
15
# File 'lib/rubix/models/model.rb', line 13

def id
  @id
end

#propertiesHash

Returns ] the properties this model was initialized with.

Returns:

  • (Hash)

    ] the properties this model was initialized with



10
11
12
# File 'lib/rubix/models/model.rb', line 10

def properties
  @properties
end

Class Method Details

.all(options = {}) ⇒ Array<Rubix::Model>

List all objects of this resource’s type.

Parameters:

  • options (Hash) (defaults to: {})

    options for filtering the list of all resources.

Returns:



294
295
296
297
298
299
300
301
302
# File 'lib/rubix/models/model.rb', line 294

def self.all options={} 
  response = all_request(options)
  if response.has_data?
    response.result.map { |obj_data| build(obj_data) }
  else
    error("Error listing all Zabbix #{resource_name}s: #{response.error_message}") unless response.success?
    []
  end
end

.all_params(options = {}) ⇒ Hash

Parameters to list all the objects of this resource’s type.

Parameters:

  • options (Hash) (defaults to: {})

    options for filtering the list of all resources.

Returns:

  • (Hash)


278
279
280
# File 'lib/rubix/models/model.rb', line 278

def self.all_params options={}
  get_params.merge(options)
end

.all_request(options = {}) ⇒ Rubix::Response

Send a request to list all objects of this resource’s type.

Parameters:

  • options (Hash) (defaults to: {})

    options for filtering the list of all resources.

Returns:



286
287
288
# File 'lib/rubix/models/model.rb', line 286

def self.all_request options={}
  request("#{zabbix_name}.get", all_params(options))
end

.each(options = {}, &block) ⇒ Array<Rubix::Model>

Execute block once for each element of the result set.

Parameters:

  • options (Hash) (defaults to: {})

    options for filtering the list of all resources.

Returns:



308
309
310
# File 'lib/rubix/models/model.rb', line 308

def self.each options={}, &block
  all(options).each(&block)
end

.find(options = {}) ⇒ Rubix::Model?

Find a resource using the given options or return nil if none is found.

Parameters:

  • options (Hash) (defaults to: {})

    specify properties about the object to find

Returns:



337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/rubix/models/model.rb', line 337

def self.find options={}
  response = find_request(options)
  case
  when response.has_data?
    build(response.result.first)
  when response.success?
    # a successful but empty response means it wasn't found
  else
    error("Error finding Zabbix #{resource_name} using #{options.inspect}: #{response.error_message}")
    nil
  end
end

.find_or_create(options = {}) ⇒ Rubix::Model, false

Find a resource using the given options or create one if none can be found. Will return false if the object cannot be found and cannot be created.

Parameters:

  • options (Hash) (defaults to: {})

    specify properties about the object to find

Returns:



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rubix/models/model.rb', line 356

def self.find_or_create options={}
  response = find_request(options)
  case
  when response.has_data?
    build(response.result.first)
  when response.success?
    # doesn't exist
    obj = new(options)
    if obj.save
      obj
    else
      false
    end
  else
    error("Error creating Zabbix #{resource_name} using #{options.inspect}: #{response.error_message}")
    false
  end
end

.find_params(options = {}) ⇒ Hash

Parameters for finding a specific resource.

Parameters:

  • options (Hash) (defaults to: {})

    specify properties about the object to find

Returns:

  • (Hash)


320
321
322
# File 'lib/rubix/models/model.rb', line 320

def self.find_params options={}
  get_params.merge(options)
end

.find_request(options = {}) ⇒ Rubix::Response

Send a find request for a specific resource.

Parameters:

  • options (Hash) (defaults to: {})

    specify properties about the object to find

Returns:



328
329
330
# File 'lib/rubix/models/model.rb', line 328

def self.find_request options={}
  request("#{zabbix_name}.get", find_params(options))
end

.get_paramsHash

Parameters for ‘get’-type requests for this resource’s type.

Returns:

  • (Hash)


270
271
272
# File 'lib/rubix/models/model.rb', line 270

def self.get_params
  { :output => :extend }
end

.id_fieldString

This is the name of the id field returned in Zabbix responses – hostid, groupid, hostmacroid, &c.

Returns:

  • (String)


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

def self.id_field
  "#{zabbix_name}id"
end

.list(ids) ⇒ Object



375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'lib/rubix/models/model.rb', line 375

def self.list ids
  return [] if ids.nil? || ids.empty?
  response = request("#{zabbix_name}.get", get_params.merge((id_field + 's') => ids))
  case
  when response.has_data?
    response.result.map do |obj|
      build(obj)
    end
  when response.success?
    []
  else
    error("Error listing Zabbix #{resource_name}s: #{response.error_message}")
  end
end

.request(method, params) ⇒ Rubix::Response

Send a request to the Zabbix API. This is just a convenience method for Rubix::Connection#request.

Parameters:

  • method (String)
  • params (Hash, Array)

Returns:



92
93
94
# File 'lib/rubix/models/model.rb', line 92

def self.request method, params
  Rubix.connection && Rubix.connection.request(method, params)
end

.resource_nameString

This is the name of the resource as used inside Rubix – Host, HostGroup, UserMacro, &c.

Returns:

  • (String)


26
27
28
# File 'lib/rubix/models/model.rb', line 26

def self.resource_name
  self.to_s.split('::').last
end

.web_request(verb, path, data = {}) ⇒ Object

Send a web request to the Zabbix web application. This is just a convenience method for Rubix::Connection#web_request.

Parameters:

  • verb (String)

    one of “GET” or “POST”

  • path (String)

    the path to send the request to

  • data (Hash) (defaults to: {})

    the data to include with the request



102
103
104
# File 'lib/rubix/models/model.rb', line 102

def self.web_request verb, path, data={}
  Rubix.connection && Rubix.connection.web_request(verb, path, data)
end

.zabbix_nameString

This is the name of the resource as used by Zabbix – host, hostgroup, usermacro, &c.

Returns:

  • (String)


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

def self.zabbix_name
  resource_name.downcase
end

Instance Method Details

#before_destroytrue, false

A hook that will be run before this resource is destroyed.

Override this in a subclass to implement any desired before-destroy functionality. Must return true or false.

Returns:

  • (true, false)


259
260
261
# File 'lib/rubix/models/model.rb', line 259

def before_destroy
  true
end

#before_updatetrue, false

A hook that will be run before this resource is updated.

Override this in a subclass to implement any desired before-update functionality. Must return true or false.

Returns:

  • (true, false)


211
212
213
# File 'lib/rubix/models/model.rb', line 211

def before_update
  true
end

#createtrue, false

Create this resource.

Returns:

  • (true, false)


153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/rubix/models/model.rb', line 153

def create
  return false unless validate
  response = create_request
  if response.has_data?
    @id = response.result[id_field + 's'].first.to_i
    info("Created Zabbix #{resource_name}")
    true
  else
    error("Error creating Zabbix #{resource_name}: #{response.error_message}")
    return false
  end
end

#create_paramsHash

Parameters for creating a new resource of this type.

Returns:

  • (Hash)


139
140
141
# File 'lib/rubix/models/model.rb', line 139

def create_params
  {}
end

#create_requestRubix::Response

Send a request to create this resource.

Returns:



146
147
148
# File 'lib/rubix/models/model.rb', line 146

def create_request
  request("#{self.class.zabbix_name}.create", create_params)
end

#destroytrue, false

Destroy this resource.

Returns:

  • (true, false)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/rubix/models/model.rb', line 236

def destroy
  return true if new_record?
  return false unless before_destroy
  response = destroy_request
  case
  when response.has_data? && response.result.values.first.first.to_i == id
    info("Destroyed Zabbix #{resource_name}")
    true
  when response.zabbix_error? && response.error_message =~ /does not exist/i
    # was never there
    true
  else
    error("Could not destroy Zabbix #{resource_name}: #{response.error_message}")
    false
  end
end

#destroy_paramsArray<Fixnum>

Parameters for destroying this resource.

Returns:

  • (Array<Fixnum>)


222
223
224
# File 'lib/rubix/models/model.rb', line 222

def destroy_params
  [id]
end

#destroy_requestRubix::Response

Send a request to destroy this resource.

Returns:



229
230
231
# File 'lib/rubix/models/model.rb', line 229

def destroy_request
  request("#{self.class.zabbix_name}.delete", destroy_params)
end

#id_fieldString

This is the name of the id field returned in Zabbix responses – hostid, groupid, hostmacroid, &c.

Returns:

  • (String)


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

def id_field
  self.class.id_field
end

#new_record?true, false

Is this a new record? We can tell because the ID must be blank.

Returns:

  • (true, false)


109
110
111
# File 'lib/rubix/models/model.rb', line 109

def new_record?
  @id.nil?
end

#request(method, params) ⇒ Rubix::Response

Send a request to the Zabbix API. This is just a convenience method for Rubix::Connection#request.

Parameters:

  • method (String)
  • params (Hash, Array)

Returns:



82
83
84
# File 'lib/rubix/models/model.rb', line 82

def request method, params
  self.class.request(method, params)
end

#resource_nameString

This is the name of this resource instance, using this object’s ‘name’ property if possible.

Returns:

  • (String)


34
35
36
# File 'lib/rubix/models/model.rb', line 34

def resource_name
  "#{self.class.resource_name} #{respond_to?(:name) ? self.name : self.id}"
end

#savetrue, false

Save this record.

Will create new records and update old ones.

Returns:

  • (true, false)


118
119
120
# File 'lib/rubix/models/model.rb', line 118

def save
  new_record? ? create : update
end

#updatetrue, false

Update this resource.

Returns:

  • (true, false)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/rubix/models/model.rb', line 187

def update
  return false unless validate
  return create if new_record?
  return false unless before_update
  response = update_request
  case
  when response.has_data? && response.result.values.first.map(&:to_i).include?(id)
    info("Updated Zabbix #{resource_name}")
    true
  when response.has_data?
    error("No error, but failed to update Zabbix #{resource_name}")
    false
  else
    error("Error updating Zabbix #{resource_name}: #{response.error_message}")
    false
  end
end

#update_paramsHash

Parameters for updating a resource of this type.

Returns:

  • (Hash)


173
174
175
# File 'lib/rubix/models/model.rb', line 173

def update_params
  create_params.merge({id_field => id})
end

#update_requestRubix::Response

Send a request to update this resource.

Returns:



180
181
182
# File 'lib/rubix/models/model.rb', line 180

def update_request
  request("#{self.class.zabbix_name}.update", update_params)
end

#validatetrue, false

Validate this record.

Override this method in a subclass and have it raise a Rubix::ValidationError if validation fails.

Returns:

  • (true, false)


128
129
130
# File 'lib/rubix/models/model.rb', line 128

def validate
  true
end