Module: Ultragrep

Defined in:
lib/ultragrep.rb,
lib/ultragrep/config.rb,
lib/ultragrep/version.rb,
lib/ultragrep/log_collector.rb

Defined Under Namespace

Classes: Config, LogCollector, RequestPerformancePrinter, RequestPrinter

Constant Summary collapse

HOUR =
60 * 60
DAY =
24 * HOUR
VERSION =
"0.10.0"

Class Method Summary collapse

Class Method Details

.parse_args(argv) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ultragrep.rb', line 91

def parse_args(argv)
  options = {
    :files => [],
    :range_start => Time.now.to_i - (Time.now.to_i % DAY),
    :range_end => Time.now.to_i,
  }
  key_value = []
  warn_about_missing_quotes_in_time_argument(argv)

  parser = OptionParser.new do |parser|
    parser.banner = <<-BANNER.gsub(/^ {6,}/, "")
      Usage: ultragrep [OPTIONS] [REGEXP ...]

      Dates: all datetimes are in UTC whatever Ruby's Time.parse() accepts.
      For example '2011-04-30 11:30:00'.

      Options are:
    BANNER
    parser.on("--help", "-h", "This text"){ puts parser; exit 0 }
    parser.on("--version", "Show version") do
      require 'ultragrep/version'
      puts "Ultragrep version #{Ultragrep::VERSION}"
      exit 0
    end
    parser.on("--config", "-c FILE", String, "Config file location (default: #{Config::DEFAULT_LOCATIONS.join(", ")})") { |config| options[:config] = config }
    parser.on("--progress", "-p", "show grep progress to STDERR") { options[:verbose] = true }
    parser.on("--verbose", "-v", "DEPRECATED") do
      $stderr.puts("The --verbose option is gone. please use -p or --progress instead")
      exit 0
    end
    parser.on("--not REGEXP", "the next given regular expression's match status should invert") do |regexp|
      options[:not_regexps] ||= []
      options[:not_regexps] << regexp
    end

    parser.on("--tail", "-t", "Tail requests, show matching requests as they arrive") do
      options[:tail] = true
      options[:range_end] = Time.now.to_i + 100 * DAY
    end
    parser.on("--type", "-l TYPE", String, "Search type of logs, specified in config") { |type| options[:type] = type }
    parser.on("--perf", "Output just performance information") { options[:perf] = true }
    parser.on("--day", "-d DATETIME", String, "Find requests that happened on this day") do |date|
      date = parse_time(date)
      options[:range_start] = date
      options[:range_end] = date + DAY - 1
    end
    parser.on("--daysback", "-b  COUNT", Integer, "Find requests from COUNT days ago to now") do |back|
      options[:range_start] = Time.now.to_i - (back * DAY)
    end
    parser.on("--hoursback", "-o COUNT", Integer, "Find requests  from COUNT hours ago to now") do |back|
      options[:range_start] = Time.now.to_i - (back * HOUR)
    end
    parser.on("--start", "-s DATETIME", String, "Find requests starting at this date") do |date|
      options[:range_start] = parse_time(date)
    end
    parser.on("--end", "-e DATETIME", String, "Find requests ending at this date") do |date|
      options[:range_end] = parse_time(date)
    end
    parser.on("--around DATETIME", String, "Find a request at about this time (10 seconds buffer on either side") do |date|
      options[:range_start] = parse_time(date) - 10
      options[:range_end] = parse_time(date) + 10
    end
    parser.on("--host HOST", String, "Only find requests on this host") do |host|
      options[:host_filter] ||= []
      options[:host_filter] << host
    end
  end
  parser.parse!(argv)

  if argv.empty?
    puts parser
    exit 1
  else
    options[:regexps] = argv
  end

  options[:printer] = if options.delete(:perf)
    RequestPerformancePrinter.new(options[:verbose])
  else
    RequestPrinter.new(options[:verbose])
  end

  options[:config] = load_config(options[:config])

  options
end

.ultragrep(options) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/ultragrep.rb', line 178

def ultragrep(options)
  lower_priority
  config = options.fetch(:config)
  file_type = options.fetch(:type, config.default_file_type)
  if !config.types[file_type]
    $stderr.puts("No such log type: #{file_type} -- available types are #{config.types.keys.join(',')}")
    exit 1
  end

  lua = config.types[file_type]["lua"]
  collector = Ultragrep::LogCollector.new(config.log_path_glob(file_type), options)
  file_lists = collector.collect_files

  request_printer = options.fetch(:printer)
  request_printer.run

  print_regex_info(options) if options[:verbose]

  regexps =  options[:regexps].map { |r| "+" + r }
  regexps += options[:not_regexps].map { |r| "!" + r } if options[:not_regexps]

  quoted_regexps = quote_shell_words(regexps)

  file_lists.each do |files|
    print_search_list(files) if options[:verbose]

    children_pipes = files.map do |file|
      [worker(file, lua, quoted_regexps, options), file]
    end

    children_pipes.each do |pipe, _|
      request_printer.set_read_up_to(pipe, 0)
    end

    # each thread here waits for child data and then pushes it to the printer thread.
    children_pipes.map do |pipe, filename|
      worker_reader(filename, pipe, request_printer, options)
    end.each(&:join)

    Process.waitall
  end

  request_printer.finish
end