Class: Nrpeclient::NrpePacket

Inherits:
Object
  • Object
show all
Defined in:
lib/nrpeclient/nrpe_packet.rb

Constant Summary collapse

NRPE_PACKET_VERSION_3 =
3
NRPE_PACKET_VERSION_2 =
2
NRPE_PACKET_VERSION_1 =
1
QUERY_PACKET =
1
RESPONSE_PACKET =
2
MAX_PACKETBUFFER_LENGTH =
1024
MAX_PACKET_SIZE =
12 + 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unpacked = nil) ⇒ NrpePacket

Returns a new instance of NrpePacket.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



16
17
18
# File 'lib/nrpeclient/nrpe_packet.rb', line 16

def buffer
  @buffer
end

#crc32Object

Returns the value of attribute crc32.



16
17
18
# File 'lib/nrpeclient/nrpe_packet.rb', line 16

def crc32
  @crc32
end

#packet_versionObject

Returns the value of attribute packet_version.



16
17
18
# File 'lib/nrpeclient/nrpe_packet.rb', line 16

def packet_version
  @packet_version
end

#perfdataObject

Returns the value of attribute perfdata.



16
17
18
# File 'lib/nrpeclient/nrpe_packet.rb', line 16

def perfdata
  @perfdata
end

#result_codeObject

Returns the value of attribute result_code.



16
17
18
# File 'lib/nrpeclient/nrpe_packet.rb', line 16

def result_code
  @result_code
end

Class Method Details

.read(io, validate_crc32 = true) ⇒ Object



73
74
75
76
77
78
79
# 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

Instance Method Details

#calculate_crc32Object



57
58
59
# File 'lib/nrpeclient/nrpe_packet.rb', line 57

def calculate_crc32
  Zlib::crc32(self.to_bytes(0))
end

#packet_typeObject



41
42
43
44
45
46
# 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) ⇒ Object



48
49
50
51
52
53
54
55
# 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_bufferObject



65
66
67
# 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) ⇒ Object



69
70
71
# 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_crc32Object



61
62
63
# File 'lib/nrpeclient/nrpe_packet.rb', line 61

def validate_crc32
  raise 'Invalid CRC32' unless @crc32 == self.calculate_crc32
end