#!/usr/bin/env ruby ENV['RAILS_ENV'] = ENV['RAILS_ENV'] ? ENV['RAILS_ENV'] : 'production' require 'syslogger' require 'oneacct_exporter' require 'oneacct_exporter/log' require 'settings' require 'fileutils' require 'oneacct_opts' options = OneacctOpts.parse(ARGV) log = Logger.new(STDOUT) if Settings['logging'] && Settings['logging']['log_type'] == 'file' begin log_file = File.open(Settings['logging']['log_file'], File::WRONLY | File::CREAT | File::APPEND) log = Logger.new(log_file) rescue => e OneacctExporter::Log.setup_logging(log) log.warn("Unable to create log file #{Settings['logging']['log_file']}: #{e.message}.\ Falling back to STDOUT.") end elsif Settings['logging'] && Settings['logging']['log_type'] == 'syslog' log = Syslogger.new('oneacct-export') end OneacctExporter::Log.setup_log_level(log) range = {} range[:from] = options.records_from range[:to] = options.records_to groups = {} groups[:include] = options.include_groups if options.include_groups groups[:exclude] = options.exclude_groups if options.exclude_groups if options.groups_file log.debug('Reading groups from file...') if File.exist?(options.groups_file) && File.readable?(options.groups_file) file = File.open(options.groups_file, 'r') file.each_line do |line| groups[groups.keys.first] << line end file.close else log.error("File contaning groups: #{options.groups_file} doesn't exists or cannot be read. "\ 'Skipping groups restriction...') groups[groups.keys.first] = [] end end begin FileUtils.mkdir_p Settings.output['output_dir'] rescue SystemCallError => e puts "Cannot create an output directory: #{e.message}. Quitting." exit end log.debug('Creating OneacctExporter...') opts = {} opts[:range] = range opts[:groups] = groups opts[:blocking] = options.blocking opts[:timeout] = options.timeout opts[:compatibility] = options.compatibility log.debug(opts) oneacct_exporter = OneacctExporter.new(opts, log) oneacct_exporter.export