Class: Metis::NrpePacket

Inherits:
Object
  • Object
show all
Defined in:
lib/metis/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
# File 'lib/metis/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]
  end
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#crc32Object

Returns the value of attribute crc32.



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

def crc32
  @crc32
end

#packet_versionObject

Returns the value of attribute packet_version.



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

def packet_version
  @packet_version
end

#result_codeObject

Returns the value of attribute result_code.



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

def result_code
  @result_code
end

Class Method Details

.read(io) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/metis/nrpe_packet.rb', line 60

def self.read(io)
  bytes = io.read(MAX_PACKET_SIZE)
  values = bytes.unpack("nnNnA#{MAX_PACKETBUFFER_LENGTH}n")
  packet = self.new(values)
  packet.validate_crc32
  packet
end

Instance Method Details

#calculate_crc32Object



48
49
50
# File 'lib/metis/nrpe_packet.rb', line 48

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

#packet_typeObject



32
33
34
35
36
37
# File 'lib/metis/nrpe_packet.rb', line 32

def packet_type
  case @packet_type
  when QUERY_PACKET    then :query
  when RESPONSE_PACKET then :response
  end
end

#packet_type=(type) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/metis/nrpe_packet.rb', line 39

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

#to_bytes(use_crc32 = self.calculate_crc32) ⇒ Object



56
57
58
# File 'lib/metis/nrpe_packet.rb', line 56

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



52
53
54
# File 'lib/metis/nrpe_packet.rb', line 52

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