Class: Zabbix::Sender::ItemData

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

Overview

ItemData instances hold the k-v pair of a value and its timestamp along with the hostname to which the data belongs. It handles formatting that data appropriately as input to zabbix_sender

Direct Known Subclasses

Discovery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

All values must be provided.



255
256
257
258
259
260
# File 'lib/zabbix_sender_api/api.rb', line 255

def initialize(key: nil,value: nil, timestamp:  nil, hostname: nil)
  @key = key
  @value = value
  @timestamp = timestamp
  @hostname = hostname
end

Instance Attribute Details

#hostnameObject

The name of the zabbix host that owns the item key



248
249
250
# File 'lib/zabbix_sender_api/api.rb', line 248

def hostname
  @hostname
end

#keyObject

The item key that’ll get the new value



242
243
244
# File 'lib/zabbix_sender_api/api.rb', line 242

def key
  @key
end

#timestampObject

The timestamp for this datapoint



251
252
253
# File 'lib/zabbix_sender_api/api.rb', line 251

def timestamp
  @timestamp
end

#valueObject

The value that the item will get



245
246
247
# File 'lib/zabbix_sender_api/api.rb', line 245

def value
  @value
end

Instance Method Details

#to_senderlineObject

Render the ItemData instance as a line of text that can be piped into zabbix_sender



264
265
266
267
268
269
270
# File 'lib/zabbix_sender_api/api.rb', line 264

def to_senderline
  if @timestamp.to_i == 0
    puts %Q("#{@hostname}" #{@key} #{@timestamp.to_i} #{@value}\n)
    abort("Attempt was made to render a timestamp of zero.  You DO NOT want this - it can kill db performance. Fix it.")
  end
  return %Q("#{@hostname}" #{@key} #{@timestamp.to_i} #{@value}\n)
end

#to_senderstructObject

Render the ItemData instance as an object suitable for conversion to json, for socket transmission



273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/zabbix_sender_api/api.rb', line 273

def to_senderstruct
  if @timestamp.to_i == 0
    puts %Q("#{@hostname}" #{@key} #{@timestamp.to_i} #{@value}\n)
    abort("Attempt was made to render a timestamp of zero.  You DO NOT want this - it can kill db performance. Fix it.")
  else
    return item = {
      host: @hostname,
      key: @key,
      value: @value,
      clock: @timestamp.to_i
    }
  end
end