Class: Zabbix::Sender::Batch

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

Overview

Batch instances hold all the data and discovery that you collect as your program does its thing with source data. Once you’ve done all your data collection, you can:

  • Send the batch instance to an instance of Pipe to have it transmitted to zabbix

  • puts mybatch.to_senderline to output the zabbix_sender input text

  • Use it to help feed data into zabbix by whatever other mechanism you might be using, e.g. a ruby implementation of the zabbix sender protocol

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp: Time.now, hostname: Zabbix::AgentConfiguration.zabbixHostname) ⇒ Batch

Both parameters are optional - reasonable defaults are provided.

Bear in mind that the hostname and timestamp values you provide here will be applied to all the ItemData and Discovery objects you add via the addItemData() and addDiscovery() methods by default (unless you override them when you add them)



364
365
366
367
368
# File 'lib/zabbix_sender_api/api.rb', line 364

def initialize(timestamp: Time.now, hostname: Zabbix::AgentConfiguration.zabbixHostname)
  @time = timestamp
  @hostname = hostname
  @data = Array.new
end

Instance Attribute Details

#dataObject (readonly)

This is an array of all the ItemData and Discovery instances that have been added to this discovery since instantiation.



356
357
358
# File 'lib/zabbix_sender_api/api.rb', line 356

def data
  @data
end

Instance Method Details

#addDiscovery(aDiscovery) ⇒ Object

Add a discovery object to this batch of data. The object will be added to the top of the item list.

If you did not specifically provide a hostname or a timestamp when you instantiated the Discovery, they’ll given the ones provided when this instance of Batch was constructed.



387
388
389
390
391
392
393
394
# File 'lib/zabbix_sender_api/api.rb', line 387

def addDiscovery(aDiscovery)
  # It doesn't matter right now really as zabbix has to digest the disco
  # and won't do it before it tries to process the data, but it makes logical
  # sense to put discos first.
  aDiscovery.timestamp = @time if not aDiscovery.timestamp
  aDiscovery.hostname = @hostname if not aDiscovery.hostname
  @data.unshift(aDiscovery)
end

#addItemData(key: nil, value: nil, timestamp: @time, hostname: @hostname) ⇒ Object

Create a new instance of ItemData and add that instance to the list of data that this batch contains. You must provide a key and a value. You can provide a timestamp and a hostname. If you do not provide a timestamp or hostname, they will be given the timestamp and hostname associated with the instance of Batch that you’re working with



375
376
377
# File 'lib/zabbix_sender_api/api.rb', line 375

def addItemData(key: nil,value: nil,timestamp: @time, hostname: @hostname)
  @data.push(ItemData.new(key: key,value: value,timestamp: timestamp,hostname: hostname))
end

#appendBatch(aBatch) ⇒ Object

Append another batch’s data into this one.



398
399
400
# File 'lib/zabbix_sender_api/api.rb', line 398

def appendBatch(aBatch)
  @data.append(*aBatch.data)
end

#to_senderlineObject

Render this batch of data as a sequence of lines of text appropriate for sending into zabbix_sender



406
407
408
# File 'lib/zabbix_sender_api/api.rb', line 406

def to_senderline
  @data.collect {|line| line.to_senderline}.join
end

#to_senderstructObject

Render this batch as a json object



412
413
414
415
416
417
418
# File 'lib/zabbix_sender_api/api.rb', line 412

def to_senderstruct
  return batch = {
    request: "sender data",
    data: @data.collect {|item| item.to_senderstruct},
    clock: @time.to_i
  }
end