class Fluent::NscaOutput

Fluentd output plugin to send service checks to an NSCA / Nagios monitoring servers.

Constants

CRITICAL

CRITICAL return code (2).

MAX_HOST_NAME_BYTES

The maximum bytes for host names.

MAX_PLUGIN_OUTPUT_BYTES

The maximum bytes for plugin outputs.

MAX_SERVICE_DESCRIPTION_BYTES

The maximum bytes for service descriptions.

OK

OK return code (0).

UNKNOWN

UNKNOWN return code (3).

VALID_RETURN_CODES

Mapping from the permitted return code representations to the normalized return codes.

WARNING

WARNING return code (1).

Public Class Methods

new() click to toggle source

Load SendNsca module.

Calls superclass method
# File lib/fluent/plugin/out_nsca.rb, line 96
def initialize
  super
  require 'socket'
  require 'send_nsca'
end

Public Instance Methods

configure(conf) click to toggle source

Read and validate the configuration.

Calls superclass method
# File lib/fluent/plugin/out_nsca.rb, line 104
def configure(conf)
  super
  @host_name ||= Socket.gethostname
  reject_host_name_option_exceeding_max_bytes
  reject_service_description_option_exceeding_max_bytes
  reject_plugin_output_option_exceeding_max_bytes
end
format(tag, time, record) click to toggle source

Pack the tuple (tag, time, record).

# File lib/fluent/plugin/out_nsca.rb, line 142
def format(tag, time, record)
  [tag, time, record].to_msgpack
end
write(chunk) click to toggle source

Send a service check.

# File lib/fluent/plugin/out_nsca.rb, line 148
def write(chunk)
  results = []
  chunk.msgpack_each { |(tag, time, record)|
    nsca_check = SendNsca::NscaConnection.new({
      :nscahost => @server,
      :port => @port,
      :password => @password,
      :hostname => determine_host_name(record),
      :service => determine_service_description(tag, record),
      :return_code => determine_return_code(record),
      :status => determine_plugin_output(record)
    })
    results.push(nsca_check.send_nsca)
  }

  # Returns the results of send_nsca for tests
  return results
end

Private Instance Methods

determine_host_name(record) click to toggle source

Determines the host name.

# File lib/fluent/plugin/out_nsca.rb, line 169
def determine_host_name(record)
  if @host_name_field and record[@host_name_field]
    host_name = record[@host_name_field].to_s
    warn_if_host_name_exceeds_max_bytes(host_name)
    return host_name
  else
    return @host_name
  end
end
determine_plugin_output(record) click to toggle source

Determines the plugin output.

# File lib/fluent/plugin/out_nsca.rb, line 212
def determine_plugin_output(record)
  if @plugin_output_field and record[@plugin_output_field]
    plugin_output = record[@plugin_output_field]
    warn_if_plugin_output_exceeds_max_bytes(plugin_output)
    return plugin_output
  elsif @plugin_output
    return @plugin_output
  else
    plugin_output = record.to_json
    warn_if_plugin_output_exceeds_max_bytes(plugin_output)
    return plugin_output
  end
end
determine_return_code(record) click to toggle source

Determines the return code.

# File lib/fluent/plugin/out_nsca.rb, line 196
def determine_return_code(record)
  if @return_code_field and record[@return_code_field]
    return_code =  VALID_RETURN_CODES[record[@return_code_field]]
    if return_code
      return return_code
    end
    log.warn('Invalid return code.',
             :return_code_field => @return_code_field,
             :value => record[@return_code_field],
             :fall_back_to => @return_code)
  end
  return @return_code
end
determine_service_description(tag, record) click to toggle source

Determines the service description.

# File lib/fluent/plugin/out_nsca.rb, line 181
def determine_service_description(tag, record)
  if @service_description_field and record[@service_description_field]
    service_description = record[@service_description_field]
    warn_if_service_description_exceeds_max_bytes(service_description)
    return service_description
  elsif @service_description
    return @service_description
  else
    warn_if_service_description_exceeds_max_bytes(tag)
    return tag
  end
end
host_name_exceeds_max_bytes?(host_name) click to toggle source

Returns true if host_name exceeds the max bytes

# File lib/fluent/plugin/out_nsca.rb, line 259
def host_name_exceeds_max_bytes?(host_name)
  return !! host_name && host_name.bytesize > MAX_HOST_NAME_BYTES
end
plugin_output_exceeds_max_bytes?(plugin_output) click to toggle source

Returns true if plugin_output exceeds the max bytes

# File lib/fluent/plugin/out_nsca.rb, line 272
def plugin_output_exceeds_max_bytes?(plugin_output)
  return !! plugin_output &&
    plugin_output.bytesize > MAX_PLUGIN_OUTPUT_BYTES
end
reject_host_name_option_exceeding_max_bytes() click to toggle source

Reject host_name option exceeding the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 114
def reject_host_name_option_exceeding_max_bytes
  if host_name_exceeds_max_bytes?(@host_name)
    raise ConfigError,
      "host_name must not exceed #{MAX_HOST_NAME_BYTES} bytes"
  end
end
reject_plugin_output_option_exceeding_max_bytes() click to toggle source

Reject plugin_output option exceeding the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 133
def reject_plugin_output_option_exceeding_max_bytes
  if plugin_output_exceeds_max_bytes?(@plugin_output)
    raise ConfigError,
      "plugin_output must not exceed #{MAX_PLUGIN_OUTPUT_BYTES} bytes"
  end
end
reject_service_description_option_exceeding_max_bytes() click to toggle source

Reject service_description option exceeding the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 123
def reject_service_description_option_exceeding_max_bytes
  if service_description_exceeds_max_bytes?(@service_description)
    raise ConfigError,
      "service_description must not exceed" +
      " #{MAX_SERVICE_DESCRIPTION_BYTES} bytes"
  end
end
service_description_exceeds_max_bytes?(service_description) click to toggle source

Returns true if service_description exceeds the max bytes

# File lib/fluent/plugin/out_nsca.rb, line 265
def service_description_exceeds_max_bytes?(service_description)
  return !! service_description &&
    service_description.bytesize > MAX_SERVICE_DESCRIPTION_BYTES
end
warn_if_host_name_exceeds_max_bytes(host_name) click to toggle source

Log a warning if the host name exceeds the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 228
def warn_if_host_name_exceeds_max_bytes(host_name)
  if host_name_exceeds_max_bytes?(host_name)
    log.warn("Host name exceeds the max bytes; it will be truncated",
             :max_host_name_bytes => MAX_HOST_NAME_BYTES,
             :host_name => host_name)
  end
end
warn_if_plugin_output_exceeds_max_bytes(plugin_output) click to toggle source

Log a warning if the plugin output exceeds the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 249
def warn_if_plugin_output_exceeds_max_bytes(plugin_output)
  if plugin_output_exceeds_max_bytes?(plugin_output)
    log.warn("Plugin output exceeds the max bytes; it will be truncated.",
            :max_plugin_output_bytes => MAX_PLUGIN_OUTPUT_BYTES,
            :plugin_output => plugin_output)
  end
end
warn_if_service_description_exceeds_max_bytes(service_description) click to toggle source

Log a warning if the service description exceeds the max bytes.

# File lib/fluent/plugin/out_nsca.rb, line 238
def warn_if_service_description_exceeds_max_bytes(service_description)
  if service_description_exceeds_max_bytes?(service_description)
    log.warn(
      "Service description exceeds the max bytes; it will be truncated.",
      :max_service_description_bytes => MAX_SERVICE_DESCRIPTION_BYTES,
      :service_description => service_description)
  end
end