class SmsLogparser::Cli
Constants
- STATUS
Public Instance Methods
clean_up(mode = "all")
click to toggle source
# File lib/sms-logparser/cli.rb, line 144 def clean_up(mode = "all") mysql = Mysql.new(options) case mode when "all" logger.info("Clean up parser table...") mysql.clean_up_parser_table logger.info("Clean up events table...") mysql.clean_up_events_table logger.info("All tables have been cleaned up.") when "events" logger.info("Clean up events table...") mysql.clean_up_events_table logger.info("Events table has been cleaned up.") when "parser" logger.info("Clean up parser table...") mysql.clean_up_parser_table logger.info("Parser table has been cleaned up.") else logger.warn("Mode '#{mode}' not allowed. Allowed modes are 'events', 'parser' or 'all'.") end SmsLogparser::Loggster.instance.close rescue => e logger.fatal e end
history()
click to toggle source
# File lib/sms-logparser/cli.rb, line 118 def history runs = Mysql.new(options).last_runs(options[:results]) if runs && runs.size > 0 table = [%w(id started_at count last_id status run_time)] runs.to_a.reverse.each do |run| table << [ run['id'], run['run_at'].strftime('%d.%d.%Y %T'), run['match_count'], run['last_event_id'], STATUS.key(run['status']).upcase, "#{run['run_time']}s" ] end options[:format] == 'csv' ? table_to_csv(table) : print_table(table) else say "No parser runs found in the database." end rescue => e logger.fatal(e.message) say(e.backtrace.join("\n"), :yellow) if options[:debug] end
log_parse_results(res)
click to toggle source
# File lib/sms-logparser/cli.rb, line 205 def log_parse_results(res) res = res.reject {|k,v| %w(:id :run_at).include? k} message = "Parser #{options[:simulate] ? 'simulation' : 'run'} ended." message += " (" + res.map {|k,v| "#{k}=\"#{v}\""}.join(' ') + ")" logger.info(message) end
logger()
click to toggle source
# File lib/sms-logparser/cli.rb, line 187 def logger SmsLogparser::Loggster.instance.set_severity options[:severity] SmsLogparser::Loggster.instance.set_log_device options[:logfile] end
options()
click to toggle source
Calls superclass method
# File lib/sms-logparser/cli.rb, line 212 def options original_options = super filename = original_options[:config] || File.join(Dir.home, '.sms-logparser.yml') return original_options unless File.exists?(filename) defaults = ::YAML::load_file(filename) || {} Thor::CoreExt::HashWithIndifferentAccess.new(defaults.merge(original_options)) end
parse()
click to toggle source
# File lib/sms-logparser/cli.rb, line 39 def parse start_message = "Parser started" start_message += options[:simulate] ? " in simulation mode." : "." logger.debug("Parser options: #{options.inspect}") logger.info(start_message) cache = DataCache.new if options[:accumulate] mysql = Mysql.new(options) if !options[:simulate] && mysql.parser_running? logger.warn("Exit. Another instance of the parser is already running.") SmsLogparser::Loggster.instance.close exit! end state = { last_event_id: mysql.get_last_parse_id, match_count: 0, status: STATUS[:running], started_at: Time.now, run_time: 0.0 } state[:id] = mysql.start_run(state) unless options[:simulate] api = Api.new(options) mysql.get_entries(last_id: state[:last_event_id], limit: options[:limit]) do |entries| logger.info { "Getting log messages from database..." } entries.each do |entry| Parser.extract_data_from_msg(entry['Message']) do |data| if data.size > 0 data.each do |data_entry| if options[:accumulate] cache.add(data_entry) logger.debug {"Cached data: #{data_entry}"} else url, status = api.send(data_entry) verbose_parser_output(entry['ID'], data_entry, url, status) end end state[:last_event_id] = entry['ID'] state[:match_count] += 1 end end end end if options[:accumulate] resp_stats = {} api.send_sets(cache.data_sets, options[:concurrency]) do |url, status| logger.debug { "POST #{url} (#{status})" } resp_stats[status] = resp_stats[status].to_i + 1 end logger.info { "Usage commited: #{resp_stats.map {|k,v| "#{v} x status #{k}" }.join(" : ")}" } end rescue SystemExit, Interrupt logger.error("Received an interrupt. Stopping the parser run.") state[:status] = STATUS[:interrupted] if state rescue => e logger.error "Aborting the parser run." logger.error e state[:status] = STATUS[:api_error] if state else state[:status] = STATUS[:ok] ensure begin if mysql && state state = STATUS[:unknown] if state[:status] == STATUS[:running] state[:run_time] = (Time.now - state[:started_at]).round(2) if state[:id] && !options[:simulate] mysql.write_parse_result(state) end log_parse_results(state) SmsLogparser::Loggster.instance.close end rescue => e logger.fatal e end end
setup()
click to toggle source
# File lib/sms-logparser/cli.rb, line 172 def setup case Mysql.new(options).create_parser_table(options[:force]) when 0 logger.info("Created database table.") when 1 logger.warn("Table already exists.") when 2 logger.info("Recreated database tables.") end SmsLogparser::Loggster.instance.close rescue => e logger.fatal e end
table_to_csv(table)
click to toggle source
# File lib/sms-logparser/cli.rb, line 199 def table_to_csv(table) table.each do |line| puts line.join(',') end end
verbose_parser_output(entry_id, data, url, status)
click to toggle source
# File lib/sms-logparser/cli.rb, line 192 def verbose_parser_output(entry_id, data, url, status) logger.debug { "Parsing data for #{entry_id} (#{data.map{|k,v| "#{k}=\"#{v || '-'}\""}.join(" ") || ''})" } logger.debug {"URL for entry #{entry_id}: #{url} (#{status})"} end
version()
click to toggle source
# File lib/sms-logparser/cli.rb, line 21 def version say "sms-logparser v#{SmsLogparser::VERSION}" end