Class: NSCA::PacketV3

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

Direct Known Subclasses

PacketV3__2_9

Constant Summary collapse

NAGIOS_VERSION =
2.7
PACKET_VERSION =
3
END_OF_TRANSMISSION =
?\x0a
HOSTNAME_LENGTH =
64
SERVICE_LENGTH =
128
PLUGIN_OUTPUT_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> A#{HOSTNAME_LENGTH} A#{SERVICE_LENGTH} A#{PLUGIN_OUTPUT_LENGTH} xx"
PACK_LENGTH =
2+2+4+4+2+HOSTNAME_LENGTH+SERVICE_LENGTH+PLUGIN_OUTPUT_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, no_verification_checks = nil) ⇒ Object

Raises:



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nsca/packet.rb', line 73

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

Instance Method Details

#build(slf = nil) ⇒ Object

Builds a check-result-line for NSCA.

Will be terminated by end-of-terminate.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/nsca/packet.rb', line 56

def build slf = nil
  cl = (slf || self).class
  entry = [
    cl::PACKET_VERSION,
    0, # crc32 (unknown yet)
    (timestamp || Time.now).to_i,
    return_code.to_i,
    NSCA::str2cstr_rand_padding( hostname || `hostname -f`, cl::HOSTNAME_LENGTH),
    NSCA::str2cstr_rand_padding( service, cl::SERVICE_LENGTH),
    NSCA::str2cstr_rand_padding( status, cl::PLUGIN_OUTPUT_LENGTH) # incl perfdata
  ]
  # generate crc32 and put it at entry[2...6]
  entry[1] = NSCA::crc32 entry.pack( cl::PACK_STRING)
  entry = entry.pack cl::PACK_STRING
  entry
end