class Nrpeclient::NrpePacket

Constants

MAX_PACKETBUFFER_LENGTH
MAX_PACKET_SIZE
NRPE_PACKET_VERSION_1
NRPE_PACKET_VERSION_2
NRPE_PACKET_VERSION_3
QUERY_PACKET
RESPONSE_PACKET

Attributes

buffer[RW]
crc32[RW]
packet_version[RW]
perfdata[RW]
result_code[RW]

Public Class Methods

new(unpacked=nil) click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 18
def initialize(unpacked=nil)
  @packet_version = NRPE_PACKET_VERSION_2
  @random = 1

  if unpacked
    @packet_version = unpacked[0]
    @packet_type    = unpacked[1]
    @crc32          = unpacked[2]
    @result_code    = unpacked[3]
    @buffer         = unpacked[4]
    @random         = unpacked[5]
    # Parse perfdata and return as a hash
    if @buffer.include? "|" then
      @perfdata = Hash.new
      @buffer.split("|")[1].split(",").each do |metric|
        key = metric.split("=")[0].strip
        value = metric.split("=")[1].strip
        @perfdata[:"#{key}"] = value
      end
    end
  end
end
read(io, validate_crc32=true) click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 73
def self.read(io, validate_crc32=true)
  bytes = io.read(MAX_PACKET_SIZE)
  values = bytes.unpack("nnNnA#{MAX_PACKETBUFFER_LENGTH}n")
  packet = self.new(values)
  packet.validate_crc32 if validate_crc32
  packet
end

Public Instance Methods

calculate_crc32() click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 57
def calculate_crc32
  Zlib::crc32(self.to_bytes(0))
end
packet_type() click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 41
def packet_type
  case @packet_type
  when QUERY_PACKET    then :query
  when RESPONSE_PACKET then :response
  end
end
packet_type=(type) click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 48
def packet_type=(type)
  case type
  when :query    then @packet_type = QUERY_PACKET
  when :response then @packet_type = RESPONSE_PACKET
  else
    raise "Invalid packet type"
  end
end
strip_buffer() click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 65
def strip_buffer
  self.buffer = self.buffer.lstrip.rstrip
end
to_bytes(use_crc32=self.calculate_crc32) click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 69
def to_bytes(use_crc32=self.calculate_crc32)
  [ @packet_version, @packet_type, use_crc32, @result_code, @buffer, @random].pack("nnNna#{MAX_PACKETBUFFER_LENGTH}n")
end
validate_crc32() click to toggle source
# File lib/nrpeclient/nrpe_packet.rb, line 61
def validate_crc32
  raise 'Invalid CRC32' unless @crc32 == self.calculate_crc32
end