Exemplo n.º 1
0
def main(_, options):

  if options.version:
    sys.stdout.write("%s\n" % __version__)
    sys.exit(0)

  config = SnifferConfig(options.iface)
  config.track_replies = True
  config.zookeeper_port = options.zookeeper_port
  config.max_queued_requests = options.max_queued_requests
  config.client_port = options.client_port if options.client_port != 0 else config.client_port

  if options.excluded_hosts and options.included_hosts:
    sys.stderr.write("The flags --include-host and --exclude-host can't be mixed.\n")
    sys.exit(1)

  if options.excluded_hosts:
    config.excluded_ips += expand_hosts(options.excluded_hosts)
  elif options.included_hosts:
    config.included_ips += expand_hosts(options.included_hosts)

  config.update_filter()

  if options.include_pings:
    config.include_pings()

  config.dump_bad_packet = options.dump_bad_packet

  loopback = options.iface in ["lo", "lo0"]

  if options.count_requests > 0 and options.measure_latency > 0:
    sys.stderr.write("The flags --count-requests and --measure-latency can't be mixed.\n")
    sys.exit(1)

  if options.count_requests > 0:
    validate_group_by(options.group_by)
    validate_aggregation_depth(options.aggregation_depth)
    p = CountPrinter(options.count_requests, options.group_by, loopback, options.aggregation_depth)
  elif options.measure_latency > 0:
    validate_group_by(options.group_by)
    validate_aggregation_depth(options.aggregation_depth)
    validate_sort_by(options.sort_by)
    p = LatencyPrinter(
      options.measure_latency, options.group_by, loopback, options.aggregation_depth,
      options.sort_by)
  elif options.unpaired:
    p = UnpairedPrinter(options.colors, loopback)
  else:
    p = DefaultPrinter(options.colors, loopback)
  p.start()

  sniffer = Sniffer(
    config,
    p.request_handler,
    p.reply_handler,
    p.event_handler,
    error_to_stderr=True
  )
  sniffer.start()

  try:
    while p.isAlive():
      time.sleep(0.5)
  except (KeyboardInterrupt, SystemExit):
    p.cancel()

  # shutdown sniffer
  sniffer.stop()
  while sniffer.isAlive():
    time.sleep(0.001)

  try:
    sys.stdout.write("\033[0m")
    sys.stdout.flush()
  except IOError: pass
Exemplo n.º 2
0
Arquivo: zk.py Projeto: XXXu/zktraffic
def main(_, options):

    if options.version:
        sys.stdout.write("%s\n" % __version__)
        sys.exit(0)

    config = SnifferConfig(options.iface)
    config.track_replies = True
    config.zookeeper_port = options.zookeeper_port
    config.max_queued_requests = options.max_queued_requests
    config.client_port = options.client_port if options.client_port != 0 else config.client_port

    if options.excluded_hosts and options.included_hosts:
        sys.stderr.write(
            "The flags --include-host and --exclude-host can't be mixed.\n")
        sys.exit(1)

    if options.excluded_hosts:
        config.excluded_ips += expand_hosts(options.excluded_hosts)
    elif options.included_hosts:
        config.included_ips += expand_hosts(options.included_hosts)

    config.update_filter()

    if options.include_pings:
        config.include_pings()

    config.dump_bad_packet = options.dump_bad_packet

    loopback = options.iface in ["lo", "lo0"]

    if options.count_requests > 0 and options.measure_latency > 0:
        sys.stderr.write(
            "The flags --count-requests and --measure-latency can't be mixed.\n"
        )
        sys.exit(1)

    if options.count_requests > 0:
        validate_group_by(options.group_by)
        validate_aggregation_depth(options.aggregation_depth)
        p = CountPrinter(options.count_requests, options.group_by, loopback,
                         options.aggregation_depth)
    elif options.measure_latency > 0:
        validate_group_by(options.group_by)
        validate_aggregation_depth(options.aggregation_depth)
        validate_sort_by(options.sort_by)
        p = LatencyPrinter(options.measure_latency, options.group_by, loopback,
                           options.aggregation_depth, options.sort_by)
    elif options.unpaired:
        p = UnpairedPrinter(options.colors, loopback)
    else:
        p = DefaultPrinter(options.colors, loopback)
    p.start()

    sniffer = Sniffer(config,
                      p.request_handler,
                      p.reply_handler,
                      p.event_handler,
                      error_to_stderr=True)
    sniffer.start()

    try:
        while p.isAlive():
            time.sleep(0.5)
    except (KeyboardInterrupt, SystemExit):
        p.cancel()

    # shutdown sniffer
    sniffer.stop()
    while sniffer.isAlive():
        time.sleep(0.001)

    try:
        sys.stdout.write("\033[0m")
        sys.stdout.flush()
    except IOError:
        pass