class NexposeThycotic::NxLogger

Constants

DEFAULT_LOG
ENDPOINT
KEY_FORMAT
KEY_RANGE
LOG_PATH
PRODUCT_FORMAT
PRODUCT_RANGE

Public Class Methods

new() click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 19
def initialize()
  create_calls
  @logger_file = get_log_path @product
  setup_logging(true, 'info')
end

Public Instance Methods

<<(value) click to toggle source

Used by net library for debugging

# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 166
def <<(value)
  log_debug_message(value)
end
create_calls() click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 62
def create_calls
  levels = [:info, :debug, :error, :warn]
  levels.each do |level|
    method_name = "log_#{level.to_s}_message"
    define_singleton_method(method_name) do |message|
      puts message if @stdout
      @log.send(level, message) unless !@enabled || @log.nil?
    end
    singleton_class.send(:alias_method, level, method_name.to_sym)
  end
end
generate_payload(statistic_value='') click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 121
def generate_payload(statistic_value='')
  product_name, separator, version = @product.to_s.rpartition('_')
  payload_value = {'version' => version}.to_json

  payload = {'statistic-key' => @statistic_key.to_s,
             'statistic-value' => payload_value,
             'product' => product_name}
  JSON.generate(payload)
end
get_log_path(product) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 81
def get_log_path(product)
  product.downcase! unless product.nil?
  File.join(File.dirname(__FILE__), LOG_PATH % (product || DEFAULT_LOG))
end
get_product(product, version) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 100
def get_product(product, version)
  return nil if ((product.nil? || product.empty?) ||
      (version.nil? || version.empty?))

  product.gsub!('-', '_')
  product.slice! product.rindex('_') until product.count('_') <= 1

  product.delete! "^A-Za-z0-9\_"
  version.delete! "^A-Za-z0-9\.\-"

  product = (PRODUCT_FORMAT % [product, version])[0...PRODUCT_RANGE.max]

  product.slice! product.rindex(/[A-Z0-9]/i)+1..-1

  if product.length < PRODUCT_RANGE.min
    log_stat_message("Product length below minimum <#{PRODUCT_RANGE.min}>.")
    return nil
  end
  product.downcase
end
get_statistic_key(vendor) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 86
def get_statistic_key(vendor)
  if vendor.nil? || vendor.length < KEY_RANGE.min
    log_stat_message("Vendor length is below minimum of <#{KEY_RANGE}>")
    return nil
  end

  vendor.gsub!('-', '_')
  vendor.slice! vendor.rindex('_') until vendor.count('_') <= 1

  vendor.delete! "^A-Za-z0-9\_"

  KEY_FORMAT % vendor[0...KEY_RANGE.max].downcase
end
log_message(message) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 74
def log_message(message)
  log_info_message message
end
log_stat_message(message) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 78
def log_stat_message(message)
end
on_connect(nexpose_address, nexpose_port, session_id, value) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 148
def on_connect(nexpose_address, nexpose_port, session_id, value)
  log_stat_message 'Sending statistics data to Nexpose'

  if @product.nil? || @statistic_key.nil?
    log_stat_message('Invalid product name and/or statistics key.')
    log_stat_message('Statistics collection not enabled.')
    return
  end

  begin
    payload = generate_payload value
    send(nexpose_address, nexpose_port, session_id, payload)
  rescue => e
    #Let the program continue
  end
end
send(nexpose_address, nexpose_port, session_id, payload) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 131
def send(nexpose_address, nexpose_port, session_id, payload)
  header = {'Content-Type' => 'application/json',
            'nexposeCCSessionID' => session_id,
            'Cookie' => "nexposeCCSessionID=#{session_id}"}
  req = Net::HTTP::Put.new(ENDPOINT, header)
  req.body = payload
  http_instance = Net::HTTP.new(nexpose_address, nexpose_port)
  http_instance.use_ssl = true
  http_instance.verify_mode = OpenSSL::SSL::VERIFY_NONE
  response = http_instance.start { |http| http.request(req) }
  log_stat_message "Received code #{response.code} from Nexpose console."
  log_stat_message "Received message #{response.msg} from Nexpose console."
  log_stat_message 'Finished sending statistics data to Nexpose.'

  response.code
end
setup_logging(enabled, log_level = 'info', stdout=false, rotation='weekly', log_size=nil) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 34
def setup_logging(enabled,
                  log_level = 'info',
                  stdout=false,
                  rotation='weekly',
                  log_size=nil)
  @stdout = stdout

  log_message('Logging disabled.') unless enabled || @log.nil?
  @enabled = enabled
  return unless @enabled

  @logger_file = get_log_path @product

  require 'logger'
  directory = File.dirname(@logger_file)
  FileUtils.mkdir_p(directory) unless File.directory?(directory)
  io = IO.for_fd(IO.sysopen(@logger_file, 'a'), 'a')
  io.autoclose = false
  io.sync = true
  @log = Logger.new(io, rotation, log_size)
  @log.level = if log_level.to_s.casecmp('info') == 0
                 Logger::INFO
               else
                 Logger::DEBUG
               end
  log_message("Logging enabled at level <#{log_level}>")
end
setup_statistics_collection(vendor, product_name, gem_version) click to toggle source
# File lib/nexpose_thycotic/utilities/nx_logger.rb, line 25
def setup_statistics_collection(vendor, product_name, gem_version)
  begin
    @statistic_key = get_statistic_key vendor
    @product = get_product product_name, gem_version
  rescue => e
    #Continue
  end
end