Class: NexposeTicketing::NxLogger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nexpose_ticketing/nx_logger.rb

Constant Summary collapse

LOG_PATH =
"./logs/rapid7_%s.log"
KEY_FORMAT =
"external.integration.%s"
PRODUCT_FORMAT =
"%s_%s"
DEFAULT_LOG =
'integration'
PRODUCT_RANGE =
3..30
KEY_RANGE =
3..15
ENDPOINT =
'/data/external/statistic/'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNxLogger

Returns a new instance of NxLogger.



20
21
22
23
# File 'lib/nexpose_ticketing/nx_logger.rb', line 20

def initialize()
  @logger_file = get_log_path product
  setup_logging(true, 'info')
end

Instance Attribute Details

#logger_fileObject

Returns the value of attribute logger_file.



9
10
11
# File 'lib/nexpose_ticketing/nx_logger.rb', line 9

def logger_file
  @logger_file
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/nexpose_ticketing/nx_logger.rb', line 9

def options
  @options
end

#productObject

Returns the value of attribute product.



9
10
11
# File 'lib/nexpose_ticketing/nx_logger.rb', line 9

def product
  @product
end

#statistic_keyObject

Returns the value of attribute statistic_key.



9
10
11
# File 'lib/nexpose_ticketing/nx_logger.rb', line 9

def statistic_key
  @statistic_key
end

Instance Method Details

#<<(value) ⇒ Object

Used by net library for debugging



150
151
152
# File 'lib/nexpose_ticketing/nx_logger.rb', line 150

def <<(value)
  log_debug_message(value)
end

#generate_payload(statistic_value = '') ⇒ Object



109
110
111
112
113
114
# File 'lib/nexpose_ticketing/nx_logger.rb', line 109

def generate_payload(statistic_value='')
  payload = {'statistic-key' => @statistic_key,
             'statistic-value' => statistic_value,
             'product' => @product}
  JSON.generate(payload)
end

#get_log_path(product) ⇒ Object



84
85
86
87
# File 'lib/nexpose_ticketing/nx_logger.rb', line 84

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) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/nexpose_ticketing/nx_logger.rb', line 98

def get_product(product, version)
  return nil if (product.nil? || version.nil?)
  product = (PRODUCT_FORMAT % [product, version])[0...PRODUCT_RANGE.max]

  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) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/nexpose_ticketing/nx_logger.rb', line 89

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

  KEY_FORMAT % vendor[0...KEY_RANGE.max].downcase
end

#log_debug_message(message) ⇒ Object

Logs a debug message



67
68
69
# File 'lib/nexpose_ticketing/nx_logger.rb', line 67

def log_debug_message(message)
  @log.debug(message) unless @log.nil?
end

#log_error_message(message) ⇒ Object

Logs an error message



72
73
74
# File 'lib/nexpose_ticketing/nx_logger.rb', line 72

def log_error_message(message)
  @log.error(message) unless @log.nil?
end

#log_message(message) ⇒ Object

Logs an info message



62
63
64
# File 'lib/nexpose_ticketing/nx_logger.rb', line 62

def log_message(message)
  @log.info(message) unless @log.nil?
end

#log_stat_message(message) ⇒ Object



81
82
# File 'lib/nexpose_ticketing/nx_logger.rb', line 81

def log_stat_message(message)
end

#log_warn_message(message) ⇒ Object

Logs a warn message



77
78
79
# File 'lib/nexpose_ticketing/nx_logger.rb', line 77

def log_warn_message(message)
  @log.warn(message) unless @log.nil?
end

#on_connect(nexpose_address, nexpose_port, session_id, value) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/nexpose_ticketing/nx_logger.rb', line 132

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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/nexpose_ticketing/nx_logger.rb', line 116

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') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/nexpose_ticketing/nx_logger.rb', line 38

def setup_logging(enabled, log_level = 'info')
  unless enabled || @log.nil?
    log_message('Logging disabled.')
    return
  end

  @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, 'weekly')
  @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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/nexpose_ticketing/nx_logger.rb', line 25

def setup_statistics_collection(vendor, product_name, gem_version)
  #Remove illegal characters
  vendor.to_s.gsub!('-', '_')
  product_name.to_s.gsub!('-', '_')

  begin
    @statistic_key = get_statistic_key vendor
    @product = get_product product_name, gem_version
  rescue => e
    #Continue
  end
end