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 = "      Usage: ultragrep [OPTIONS] [REGEXP ...]\n\n      Dates: all datetimes are in UTC whatever Ruby's Time.parse() accepts.\n      For example '2011-04-30 11:30:00'.\n\n      Options are:\n    BANNER\n    parser.on(\"--help\", \"-h\", \"This text\"){ puts parser; exit 0 }\n    parser.on(\"--version\", \"Show version\") do\n      require 'ultragrep/version'\n      puts \"Ultragrep version \#{Ultragrep::VERSION}\"\n      exit 0\n    end\n    parser.on(\"--config\", \"-c FILE\", String, \"Config file location (default: \#{Config::DEFAULT_LOCATIONS.join(\", \")})\") { |config| options[:config] = config }\n    parser.on(\"--progress\", \"-p\", \"show grep progress to STDERR\") { options[:verbose] = true }\n    parser.on(\"--verbose\", \"-v\", \"DEPRECATED\") do\n      $stderr.puts(\"The --verbose option is gone. please use -p or --progress instead\")\n      exit 0\n    end\n    parser.on(\"--not REGEXP\", \"the next given regular expression's match status should invert\") do |regexp|\n      options[:not_regexps] ||= []\n      options[:not_regexps] << regexp\n    end\n\n    parser.on(\"--tail\", \"-t\", \"Tail requests, show matching requests as they arrive\") do\n      options[:tail] = true\n      options[:range_end] = Time.now.to_i + 100 * DAY\n    end\n    parser.on(\"--type\", \"-l TYPE\", String, \"Search type of logs, specified in config\") { |type| options[:type] = type }\n    parser.on(\"--perf\", \"Output just performance information\") { options[:perf] = true }\n    parser.on(\"--day\", \"-d DATETIME\", String, \"Find requests that happened on this day\") do |date|\n      date = parse_time(date)\n      options[:range_start] = date\n      options[:range_end] = date + DAY - 1\n    end\n    parser.on(\"--daysback\", \"-b  COUNT\", Integer, \"Find requests from COUNT days ago to now\") do |back|\n      options[:range_start] = Time.now.to_i - (back * DAY)\n    end\n    parser.on(\"--hoursback\", \"-o COUNT\", Integer, \"Find requests  from COUNT hours ago to now\") do |back|\n      options[:range_start] = Time.now.to_i - (back * HOUR)\n    end\n    parser.on(\"--start\", \"-s DATETIME\", String, \"Find requests starting at this date\") do |date|\n      options[:range_start] = parse_time(date)\n    end\n    parser.on(\"--end\", \"-e DATETIME\", String, \"Find requests ending at this date\") do |date|\n      options[:range_end] = parse_time(date)\n    end\n    parser.on(\"--around DATETIME\", String, \"Find a request at about this time (10 seconds buffer on either side\") do |date|\n      options[:range_start] = parse_time(date) - 10\n      options[:range_end] = parse_time(date) + 10\n    end\n    parser.on(\"--host HOST\", String, \"Only find requests on this host\") do |host|\n      options[:host_filter] ||= []\n      options[:host_filter] << host\n    end\n  end\n  parser.parse!(argv)\n\n  if argv.empty?\n    puts parser\n    exit 1\n  else\n    options[:regexps] = argv\n  end\n\n  options[:printer] = if options.delete(:perf)\n    RequestPerformancePrinter.new(options[:verbose])\n  else\n    RequestPrinter.new(options[:verbose])\n  end\n\n  options[:config] = load_config(options[:config])\n\n  options\nend\n".gsub(/^ {6,}/, "")

.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