class SendNsca::NscaConnection

Constants

CRC_TABLE
PACKET_VERSION
PACK_STRING

Attributes

connected[R]

connection status and error if one found

crc[RW]

for sending to nsca

debug[RW]

debug

encryption_mode[RW]
error[R]
hostname[RW]

status data to send to nagios Currently all 4 are required, meaning we only support 'service' passive checkins. todo: add host checkins

nscahost[RW]

params for connecting to the nsca/nagios server

nscatimeout[RW]
port[RW]
return_code[RW]
service[RW]
status[RW]
timestamp[RW]
timestamp_hex[RW]
timestring[RW]
xor_key[RW]
xor_key_and_timestamp[RW]

read from the nsca/nagios server

xor_key_and_timestamp_str[RW]

converted from :xor_key_and_timestamp

xor_key_string[RW]

Public Class Methods

crc32(c) click to toggle source
# File lib/send_nsca/send_nsca.rb, line 151
def self.crc32(c)
    r = 0xFFFFFFFF
    c.each_byte do |b|
        r ^= b
        8.times do
          r = (r>>1) ^ (0xEDB88320 * (r & 1))
        end
    end
    r ^ 0xFFFFFFFF
end
new(args) click to toggle source
# File lib/send_nsca/send_nsca.rb, line 58
def initialize(args)
  # connecting to nagios
  @nscahost = args[:nscahost]
  @port = args[:port]
  @hostname = args[:hostname]
  @service = args[:service]
  @return_code = args[:return_code]
  @status = args[:status]
  @connected = false
  @password = args[:password] || ''
  @timeout = args[:nscatimeout] || 1
  @encryption_mode = args[:encryption_mode] || SendNsca::XOR
  @debug = args[:debug] || false
end
xor(iv, str, password='') click to toggle source
# File lib/send_nsca/send_nsca.rb, line 162
def self.xor(iv, str, password='')
  
  str_a = str.unpack("C*")
  iv_a = iv.unpack("C*")
  password_a = password.unpack("C*")
  result = []

  str_a.each_with_index do |c, i|
    result[i] = c ^ iv_a[i % iv_a.size]
    result[i] ^= password_a[i % password_a.size] unless password_a.empty?
  end

  ret_val = result.pack("C*")
end

Public Instance Methods

connect_and_get_keys() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 73
def connect_and_get_keys
  begin
    Timeout.timeout(@timeout) do # the server has @timeout second(s) to answer
      @tcp_client = TCPSocket.open(@nscahost, @port)
      @connected = true
      @xor_key_and_timestamp = @tcp_client.recv(132)
    end
  rescue
    @connected = false
    @error = "send_ncsa - error connecting to nsca/nagios: #{$!}"
    puts @error
    @tcp_client.try(:close) unless @tcp_client.nil?
    raise # re-raise same exception
  end
  timestamp_for_logging
end
convert_timestamp() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 90
def convert_timestamp
  # convert timestamp for use in comm to nagios
  @timestring = @xor_key_and_timestamp[@xor_key_and_timestamp.length-4,@xor_key_and_timestamp.length]
end
convert_xor_key() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 103
def convert_xor_key
  # strip off the last 4 characters which are the timestamp
  @xor_key = @xor_key_and_timestamp[0,@xor_key_and_timestamp.length-4]
end
intialize_nsca_connection() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 108
def intialize_nsca_connection
  connect_and_get_keys
  convert_timestamp
  convert_xor_key
end
send_nsca() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 114
    def send_nsca
      intialize_nsca_connection
      @crc = 0 
      nsca_params = [PACKET_VERSION, @crc, @timestring, @return_code, @hostname, @service, @status ]
      string_to_send_without_crc = nsca_params.pack(PACK_STRING)
      
      @crc = SendNsca::NscaConnection.crc32(string_to_send_without_crc)
      nsca_params = [PACKET_VERSION, @crc, @timestring, @return_code, @hostname, @service, @status ]
      string_to_send_with_crc = nsca_params.pack(PACK_STRING)

      if @debug
        puts %{DEBUG: PACKET_VERSION = #{PACKET_VERSION}
@crc = #{@crc}
@timestring = #{@timestring}
@timestamp_hex = #{Time.at(timestamp_hex)}
@return_code #{@return_code}
@hostname = #{@hostname}
@service = #{@service}
@status = #{@status}}
        puts "string_to_send_with_crc = #{string_to_send_with_crc.length}"
        puts "string_to_send_with_crc = #{string_to_send_with_crc.unpack('H*')}"
      end

      if @encryption_mode == SendNsca::NONE
        string_to_send = string_to_send_with_crc
      else
        string_to_send = SendNsca::NscaConnection.xor(@xor_key, string_to_send_with_crc, @password)
        if @debug
          puts "encrypted_string_to_send = #{string_to_send.length}"
          puts "encrypted_string_to_send = #{string_to_send.unpack('H*')}"
        end
      end

      @tcp_client.send(string_to_send, 0)
      @tcp_client.close
    end
timestamp_for_logging() click to toggle source
# File lib/send_nsca/send_nsca.rb, line 95
def timestamp_for_logging
  # convert timestamp in a format we can log
  @xor_key_and_timestamp_str = @xor_key_and_timestamp.unpack("H*")
  @timestring_for_log = @xor_key_and_timestamp_str[0][256,8]
  @timestamp_hex = @timestring_for_log.hex
  @timestamp = Time.at(@timestamp_hex)
end