Class: NexposeTicketing::NxLogger
- Inherits:
-
Object
- Object
- NexposeTicketing::NxLogger
- Includes:
- Singleton
- Defined in:
- lib/nexpose_ticketing/utilities/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 =
4..30
- KEY_RANGE =
3..15
- ENDPOINT =
'/data/external/statistic/'
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Used by net library for debugging.
- #create_calls ⇒ Object
- #generate_payload(statistic_value = '') ⇒ Object
- #get_log_path(product) ⇒ Object
- #get_product(product, version) ⇒ Object
- #get_statistic_key(vendor) ⇒ Object
-
#initialize ⇒ NxLogger
constructor
A new instance of NxLogger.
- #log_message(message) ⇒ Object
- #log_stat_message(message) ⇒ Object
- #on_connect(nexpose_address, nexpose_port, session_id, value) ⇒ Object
- #send(nexpose_address, nexpose_port, session_id, payload) ⇒ Object
- #setup_logging(enabled, log_level = 'info', stdout = false) ⇒ Object
- #setup_statistics_collection(vendor, product_name, gem_version) ⇒ Object
Constructor Details
#initialize ⇒ NxLogger
Returns a new instance of NxLogger.
19 20 21 22 23 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 19 def initialize() create_calls @logger_file = get_log_path @product setup_logging(true, 'info') end |
Instance Method Details
#<<(value) ⇒ Object
Used by net library for debugging
161 162 163 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 161 def <<(value) (value) end |
#create_calls ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 58 def create_calls levels = [:info, :debug, :error, :warn] levels.each do |level| method_name = define_singleton_method("log_#{level.to_s}_message") do || puts if @stdout @log.send(level, ) unless !@enabled || @log.nil? end end end |
#generate_payload(statistic_value = '') ⇒ Object
116 117 118 119 120 121 122 123 124 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 116 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) ⇒ Object
76 77 78 79 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 76 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 95 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 ("Product length below minimum <#{PRODUCT_RANGE.min}>.") return nil end product.downcase end |
#get_statistic_key(vendor) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 81 def get_statistic_key(vendor) if vendor.nil? || vendor.length < KEY_RANGE.min ("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) ⇒ Object
69 70 71 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 69 def () end |
#log_stat_message(message) ⇒ Object
73 74 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 73 def () end |
#on_connect(nexpose_address, nexpose_port, session_id, value) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 143 def on_connect(nexpose_address, nexpose_port, session_id, value) 'Sending statistics data to Nexpose' if @product.nil? || @statistic_key.nil? ('Invalid product name and/or statistics key.') ('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
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 126 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) } "Received code #{response.code} from Nexpose console." "Received message #{response.msg} from Nexpose console." 'Finished sending statistics data to Nexpose.' response.code end |
#setup_logging(enabled, log_level = 'info', stdout = false) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/nexpose_ticketing/utilities/nx_logger.rb', line 34 def setup_logging(enabled, log_level = 'info', stdout=false) @stdout = stdout ('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, 'weekly') @log.level = if log_level.to_s.casecmp('info') == 0 Logger::INFO else Logger::DEBUG end ("Logging enabled at level <#{log_level}>") end |
#setup_statistics_collection(vendor, product_name, gem_version) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/nexpose_ticketing/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 |