Created
October 24, 2013 15:04
-
-
Save microwaves/6c323a1616a5add39e70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'json' | |
| require 'logger' | |
| class MatchesChecker | |
| attr_accessor :events_file, :fingerprints_file, :data | |
| def initialize | |
| @data = {} | |
| @events_file = './logs/events.log' | |
| @fingerprints_file = './logs/fingerprints.log' | |
| @total_matched = 0 | |
| @current_line = 0 | |
| @lines_logger = Logger.new('./output/lines.log') | |
| @matches_logger = Logger.new('./output/matches.log') | |
| @json_output = './output/matches_checker.json' | |
| end | |
| def analyze | |
| load_log_files | |
| analyze_events | |
| write_to_file(JSON.pretty_generate(@data)) | |
| end | |
| private | |
| def load_log_files | |
| @events = File.open(@events_file).read | |
| @fingerprints = File.open(@fingerprints_file).read | |
| end | |
| def analyze_events | |
| @events.lines.each do |line| | |
| process_line(line) | |
| @current_line += 1 | |
| @lines_logger.info("Processed #{@current_line} lines of #{@events_file}" \ | |
| " so far.") | |
| end | |
| end | |
| def process_line(line) | |
| if line.include?('install: SOURCE:') | |
| check_install(line.split) | |
| end | |
| end | |
| def check_install(splitted_line) | |
| fingerprint = splitted_line.last | |
| matched_fingerprint = find_matching_fingerprint(fingerprint) | |
| unless matched_fingerprint.empty? | |
| matches = find_matches(fingerprint, matched_fingerprint.first.split( | |
| 'PROPERTIES: ').last) | |
| save_matches(fingerprint, matches) | |
| end | |
| end | |
| def find_matching_fingerprint(fingerprint) | |
| @fingerprints.lines.select do |line| | |
| line =~ /FINGERPRINT: #{fingerprint}/ | |
| end | |
| end | |
| def find_matches(fingerprint, properties) | |
| matched_properties = @fingerprints.lines.select do |line| | |
| line =~ /#{Regexp.escape(properties)}/ | |
| end | |
| matched_properties.each_with_object([]) do |matched_property, array| | |
| property_fingerprint = matched_property.split[7] | |
| array << property_fingerprint unless property_fingerprint == fingerprint | |
| end | |
| end | |
| def save_matches(fingerprint, matches) | |
| unless matches.empty? | |
| @data[fingerprint] = matches | |
| @matches_logger.info("#{fingerprint} matches with #{matches}.") | |
| end | |
| end | |
| def write_to_file(data) | |
| file = File.open(@json_output, 'w') | |
| file << data | |
| file.close | |
| end | |
| end | |
| MatchesChecker.new.analyze |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment