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, itemclass: ItemData) ⇒ Batch

All 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)

If you want the items in this batch to be sent to zabbix with their nanosecond component, you must pass NanosecondItemData as the itemclass. In addition, if you intend to send the batch via a pipe (rather than a socket) you must ensure that you’ve set nanos to true when you construct the pipe.



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

def initialize(timestamp: Time.now, hostname: Zabbix::AgentConfiguration.zabbixHostname, itemclass: ItemData)
  @itemclass = itemclass
  @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.



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

def data
  @data
end

#itemclassObject (readonly)

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



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

def itemclass
  @itemclass
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.



435
436
437
438
439
440
441
442
# File 'lib/zabbix_sender_api/api.rb', line 435

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



423
424
425
# File 'lib/zabbix_sender_api/api.rb', line 423

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

#appendBatch(aBatch) ⇒ Object

Append another batch’s data into this one.



446
447
448
# File 'lib/zabbix_sender_api/api.rb', line 446

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



454
455
456
# File 'lib/zabbix_sender_api/api.rb', line 454

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

#to_senderstructObject

Render this batch as a json object



460
461
462
463
464
465
466
# File 'lib/zabbix_sender_api/api.rb', line 460

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