Class: NSCA::PacketV3

Inherits:
Packet
  • Object
show all
Defined in:
lib/nsca/packet.rb

Constant Summary collapse

NAGIOS_VERSION =
2.7
PACKET_VERSION =
3
END_OF_TRANSMISSION =
?\x0a
HOSTNAME_LENGTH =
64
SERVICE_LENGTH =
128
STATUS_LENGTH =
512
PACK_STRING =

these line describes the data package: typedef struct data_packet_struct

int16_t   packet_version;
/* two padding bytes (because aligning): xx */
u_int32_t crc32_value;
u_int32_t timestamp;
int16_t   return_code;
char      host_name[MAX_HOSTNAME_LENGTH];
char      svc_description[MAX_DESCRIPTION_LENGTH];
char      plugin_output[MAX_PLUGINOUTPUT_LENGTH];
/* two extra padding-xx, too. */

data_packet;

"s> xx L> L> s> Z#{HOSTNAME_LENGTH} Z#{SERVICE_LENGTH} Z#{STATUS_LENGTH} xx"
PACK_LENGTH =
2+2+4+4+2+HOSTNAME_LENGTH+SERVICE_LENGTH+STATUS_LENGTH+2

Instance Attribute Summary

Attributes inherited from Packet

#hostname, #return_code, #service, #status, #timestamp

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Packet

#initialize, register_version, versions

Constructor Details

This class inherits a constructor from NSCA::Packet

Class Method Details

.parse(entry, key = nil, password = nil, no_verification_checks = nil) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nsca/packet.rb', line 99

def self.parse entry, key = nil, password = nil, no_verification_checks = nil
  entry = NSCA::xor key, entry  if key
  entry = NSCA::xor password, entry  if password
  ver, crc32sum, *x = entry.unpack( PACK_STRING)
  raise VersionCheckFailed, "Packet version 3 expected. (recv: #{ver})" \
    unless no_verification_checks or 3 == ver
  entry[4..7] = ?\x00*4
  raise CSC32CheckFailed, "crc32-check failed. packet seems to be broken." \
    unless no_verification_checks or crc32sum == NSCA::crc32( entry)
  new *x
end

Instance Method Details

#build(key = nil, password = nil) ⇒ Object

Builds a check-result-line for NSCA.

Will be terminated by end-of-terminate.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/nsca/packet.rb', line 81

def build key = nil, password = nil
  entry = [
    PACKET_VERSION,
    0, # crc32 (unknown yet)
    (timestamp || Time.now).to_i,
    return_code.to_i,
    NSCA::str2cstr( hostname || `hostname -f`, HOSTNAME_LENGTH),
    NSCA::str2cstr( service, SERVICE_LENGTH),
    NSCA::str2cstr( status, STATUS_LENGTH) # incl perfdata
  ]
  # generate crc32 and put it at entry[2...6]
  entry[1] = NSCA::crc32 entry.pack( PACK_STRING)
  entry = entry.pack PACK_STRING
  entry = NSCA::xor key, entry  if key
  entry = NSCA::xor password, entry  if password
  entry
end