Class: Zabbix::Sender::Discovery

Inherits:
ItemData
  • Object
show all
Defined in:
lib/zabbix_sender_api/api.rb

Overview

Discovery instances are a special type of ItemData that you will typically create and hang on to as you accumulate (discover) related entities. You then pass the discover instance into a Batch via addDiscovery(), which includes it in the batch of data just like an ordinary ItemData instance.

Instance Attribute Summary collapse

Attributes inherited from ItemData

#hostname, #key, #timestamp, #value

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, value: nil, timestamp: nil, hostname: nil) ⇒ Discovery

The only required parameter is key:, which is the discovery rule key.



298
299
300
301
# File 'lib/zabbix_sender_api/api.rb', line 298

def initialize(key: nil,value: nil, timestamp:  nil, hostname: nil)
  super
  @entities = Set.new
end

Instance Attribute Details

#entitiesObject (readonly)

Returns the value of attribute entities.



294
295
296
# File 'lib/zabbix_sender_api/api.rb', line 294

def entities
  @entities
end

Instance Method Details

#add_entity(aHash) ⇒ Object

This is how you pass data to zabbix that you use to construct items from item templates. Pass in as many key-value pairs as you need. You’ll reference these in the item prototype like #MYKEY

Note that the keys (which you can pass as symbols if you want) are forced to uppercase. This is here because the author once spent way too much time trying to figure out why discovery wasn’t working right one day. All caps seems to fix the issue.



310
311
312
313
314
315
316
317
# File 'lib/zabbix_sender_api/api.rb', line 310

def add_entity(aHash)
  # just send in key value pairs - these will be the variables you can use in the discovery item prototypes
  zabbified = Hash.new
  aHash.each_pair { |key,value|
    zabbified[%Q({##{key.to_s.upcase}})] = value
  }
  @entities.add(zabbified)
end

#to_discodataObject

Render this discovery as the structure an external discovery script should return. You can use this if you’re writing custom external discovery logic



322
323
324
325
326
# File 'lib/zabbix_sender_api/api.rb', line 322

def to_discodata
  disco = { 'data'=>Array.new }
  disco['data'] = @entities.to_a
  return disco.to_json
end

#to_senderlineObject

Render this discovery instance as a zabbix_sender line.



330
331
332
333
# File 'lib/zabbix_sender_api/api.rb', line 330

def to_senderline
  @value = self.to_discodata.to_json
  super
end

#to_senderstructObject

Render this discovery instance as an object suitable for conversion to json for socket transmission



336
337
338
339
# File 'lib/zabbix_sender_api/api.rb', line 336

def to_senderstruct
  @value = self.to_discodata
  super
end