コード例 #1
0
ファイル: log.py プロジェクト: traumschule/nyx
    def _draw(self, subwindow, is_correction=False):
        scroll = self._scroller.location(self._last_content_height,
                                         subwindow.height - 1)

        event_filter = self._filter.clone()
        event_types = list(self._event_types)
        last_content_height = self._last_content_height
        show_duplicates = self._show_duplicates

        event_log = self._event_log_paused if nyx_interface().is_paused(
        ) else self._event_log
        event_log = list(
            filter(lambda entry: event_filter.match(entry.display_message),
                   event_log))
        event_log = list(
            filter(lambda entry: not entry.is_duplicate or show_duplicates,
                   event_log))

        is_scrollbar_visible = last_content_height > subwindow.height - 1

        if is_scrollbar_visible:
            subwindow.scrollbar(1, scroll, last_content_height)

        x, y = 2 if is_scrollbar_visible else 0, 1 - scroll
        y = _draw_entries(subwindow, x, y, event_log, show_duplicates)

        # drawing the title after the content, so we'll clear content from the top line

        _draw_title(subwindow, event_types, event_filter)

        # redraw the display if...
        # - last_content_height was off by too much
        # - we're off the bottom of the page

        new_content_height = y + scroll - 1
        content_height_delta = abs(last_content_height - new_content_height)
        force_redraw, force_redraw_reason = True, ''

        if content_height_delta >= CONTENT_HEIGHT_REDRAW_THRESHOLD:
            force_redraw_reason = 'estimate was off by %i' % content_height_delta
        elif new_content_height > subwindow.height and scroll + subwindow.height - 1 > new_content_height:
            force_redraw_reason = 'scrolled off the bottom of the page'
        elif not is_scrollbar_visible and new_content_height > subwindow.height - 1:
            force_redraw_reason = "scroll bar wasn't previously visible"
        elif is_scrollbar_visible and new_content_height <= subwindow.height - 1:
            force_redraw_reason = "scroll bar shouldn't be visible"
        else:
            force_redraw = False

        self._last_content_height = new_content_height
        self._has_new_event = False

        if force_redraw and not is_correction:
            log.debug(
                'redrawing the log panel with the corrected content height (%s)'
                % force_redraw_reason)
            self._draw(subwindow, True)
コード例 #2
0
ファイル: tracker.py プロジェクト: hectormartin42/nyx
  def _task(self, process_pid, process_name):
    try:
      resolver = _resources_via_proc if self._use_proc else _resources_via_ps
      total_cpu_time, uptime, memory_in_bytes, memory_in_percent = resolver(process_pid)

      if self._resources:
        cpu_sample = (total_cpu_time - self._resources.cpu_total) / self._resources.cpu_total
      else:
        cpu_sample = 0.0  # we need a prior datapoint to give a sampling

      self._resources = Resources(
        cpu_sample = cpu_sample,
        cpu_average = total_cpu_time / uptime,
        cpu_total = total_cpu_time,
        memory_bytes = memory_in_bytes,
        memory_percent = memory_in_percent,
        timestamp = time.time(),
      )

      self._failure_count = 0
      return True
    except IOError as exc:
      self._failure_count += 1

      if self._use_proc:
        if self._failure_count >= 3:
          # We've failed three times resolving via proc. Warn, and fall back
          # to ps resolutions.

          self._use_proc = False
          self._failure_count = 0

          log.info(
            'tracker.abort_getting_resources',
            resolver = 'proc',
            response = 'falling back to ps',
            exc = exc,
          )
        else:
          log.debug('tracker.unable_to_get_resources', resolver = 'proc', exc = exc)
      else:
        if self._failure_count >= 3:
          # Give up on further attempts.

          log.info(
            'tracker.abort_getting_resources',
            resolver = 'ps',
            response = 'giving up on getting resource usage information',
            exc = exc,
          )

          self.stop()
        else:
          log.debug('tracker.unable_to_get_resources', resolver = 'ps', exc = exc)

      return False
コード例 #3
0
ファイル: log.py プロジェクト: sammyshj/nyx
  def _draw(self, subwindow):
    scroll = self._scroller.location(self._last_content_height, subwindow.height - 1)

    nyx_controller = nyx.controller.get_controller()
    event_filter = self._filter.clone()
    event_types = list(self._event_types)
    last_content_height = self._last_content_height
    show_duplicates = self._show_duplicates

    event_log = self._event_log_paused if nyx_controller.is_paused() else self._event_log
    event_log = filter(lambda entry: event_filter.match(entry.display_message), event_log)
    event_log = filter(lambda entry: not entry.is_duplicate or show_duplicates, event_log)

    is_scrollbar_visible = last_content_height > subwindow.height - 1

    if is_scrollbar_visible:
      subwindow.scrollbar(1, scroll, last_content_height - 1)

    x, y = 3 if is_scrollbar_visible else 1, 1 - scroll
    y = _draw_entries(subwindow, x, y, event_log, show_duplicates)

    # drawing the title after the content, so we'll clear content from the top line

    _draw_title(subwindow, event_types, event_filter)

    # redraw the display if...
    # - last_content_height was off by too much
    # - we're off the bottom of the page

    new_content_height = y + scroll - 1
    content_height_delta = abs(last_content_height - new_content_height)
    force_redraw, force_redraw_reason = True, ''

    if content_height_delta >= CONTENT_HEIGHT_REDRAW_THRESHOLD:
      force_redraw_reason = 'estimate was off by %i' % content_height_delta
    elif new_content_height > subwindow.height and scroll + subwindow.height - 1 > new_content_height:
      force_redraw_reason = 'scrolled off the bottom of the page'
    elif not is_scrollbar_visible and new_content_height > subwindow.height - 1:
      force_redraw_reason = "scroll bar wasn't previously visible"
    elif is_scrollbar_visible and new_content_height <= subwindow.height - 1:
      force_redraw_reason = "scroll bar shouldn't be visible"
    else:
      force_redraw = False

    self._last_content_height = new_content_height
    self._has_new_event = False

    if force_redraw:
      log.debug('redrawing the log panel with the corrected content height (%s)' % force_redraw_reason)
      self.redraw()
コード例 #4
0
ファイル: tracker.py プロジェクト: hectormartin42/nyx
  def _task(self, process_pid, process_name):
    local_ports = self._last_requested_local_ports
    remote_ports = self._last_requested_remote_ports

    if not local_ports and not remote_ports:
      return True

    result = {}

    # Use cached results from our last lookup if available.

    for port, process in self._processes_for_ports.items():
      if port in local_ports:
        result[port] = process
        local_ports.remove(port)
      elif port in remote_ports:
        result[port] = process
        remote_ports.remove(port)

    try:
      if local_ports or remote_ports:
        result.update(_process_for_ports(local_ports, remote_ports))

      self._processes_for_ports = result
      self._failure_count = 0
      return True
    except IOError as exc:
      self._failure_count += 1

      if self._failure_count >= 3:
        log.info('tracker.abort_getting_port_usage', exc = exc)
        self.stop()
      else:
        log.debug('tracker.unable_to_get_port_usages', exc = exc)

      return False
コード例 #5
0
ファイル: log.py プロジェクト: patacca/nyx
  def draw(self, width, height):
    scroll = self._scroller.location(self._last_content_height, height)

    event_log = list(self._event_log_paused if self.is_paused() else self._event_log)
    event_filter = self._filter.clone()
    event_types = list(self._event_types)
    last_content_height = self._last_content_height
    show_duplicates = self._show_duplicates

    is_scrollbar_visible = last_content_height > height - 1

    if is_scrollbar_visible:
      self.add_scroll_bar(scroll, scroll + height - 1, last_content_height, 1)

    x, y = 3 if is_scrollbar_visible else 1, 1 - scroll

    # group entries by date, filtering out those that aren't visible

    day_to_entries, today = {}, nyx.log.day_count(time.time())

    for entry in event_log:
      if entry.is_duplicate and not show_duplicates:
        continue  # deduplicated message
      elif not event_filter.match(entry.display_message):
        continue  # filter doesn't match log message

      day_to_entries.setdefault(entry.day_count(), []).append(entry)

    for day in sorted(day_to_entries.keys(), reverse = True):
      if day == today:
        for entry in day_to_entries[day]:
          y = self._draw_entry(x, y, width, entry, show_duplicates)
      else:
        original_y, y = y, y + 1

        for entry in day_to_entries[day]:
          y = self._draw_entry(x, y, width, entry, show_duplicates)

        self.draw_box(original_y, x - 1, width - x + 1, y - original_y + 1, YELLOW, BOLD)
        time_label = time.strftime(' %B %d, %Y ', time.localtime(day_to_entries[day][0].timestamp))
        self.addstr(original_y, x + 1, time_label, YELLOW, BOLD)

        y += 1

    # drawing the title after the content, so we'll clear content from the top line

    self._draw_title(width, event_types, event_filter)

    # redraw the display if...
    # - last_content_height was off by too much
    # - we're off the bottom of the page

    new_content_height = y + scroll - 1
    content_height_delta = abs(last_content_height - new_content_height)
    force_redraw, force_redraw_reason = True, ''

    if content_height_delta >= CONTENT_HEIGHT_REDRAW_THRESHOLD:
      force_redraw_reason = 'estimate was off by %i' % content_height_delta
    elif new_content_height > height and scroll + height - 1 > new_content_height:
      force_redraw_reason = 'scrolled off the bottom of the page'
    elif not is_scrollbar_visible and new_content_height > height - 1:
      force_redraw_reason = "scroll bar wasn't previously visible"
    elif is_scrollbar_visible and new_content_height <= height - 1:
      force_redraw_reason = "scroll bar shouldn't be visible"
    else:
      force_redraw = False

    self._last_content_height = new_content_height
    self._has_new_event = False

    if force_redraw:
      log.debug('redrawing the log panel with the corrected content height (%s)' % force_redraw_reason)
      self.redraw(True)
コード例 #6
0
ファイル: tracker.py プロジェクト: hectormartin42/nyx
  def _task(self, process_pid, process_name):
    if self._custom_resolver:
      resolver = self._custom_resolver
      is_default_resolver = False
    elif self._resolvers:
      resolver = self._resolvers[0]
      is_default_resolver = True
    else:
      return False  # nothing to resolve with

    try:
      start_time = time.time()
      new_connections, new_start_times = [], {}

      if resolver == CustomResolver.INFERENCE:
        # provide connections going to a relay or one of our tor ports

        connections = []
        controller = tor_controller()
        consensus_tracker = get_consensus_tracker()

        for conn in proc.connections(user = controller.get_user(None)):
          if conn.remote_port in consensus_tracker.get_relay_fingerprints(conn.remote_address):
            connections.append(conn)  # outbound to another relay
          elif conn.local_port in controller.get_ports(stem.control.Listener.OR, []):
            connections.append(conn)  # inbound to our ORPort
          elif conn.local_port in controller.get_ports(stem.control.Listener.DIR, []):
            connections.append(conn)  # inbound to our DirPort
          elif conn.local_port in controller.get_ports(stem.control.Listener.CONTROL, []):
            connections.append(conn)  # controller connection
      else:
        connections = connection.get_connections(resolver, process_pid = process_pid, process_name = process_name)

      for conn in connections:
        conn_start_time, is_legacy = self._start_times.get(conn, (start_time, self._is_first_run))
        new_start_times[conn] = (conn_start_time, is_legacy)
        new_connections.append(Connection(conn_start_time, is_legacy, *conn))

      self._connections = new_connections
      self._start_times = new_start_times
      self._is_first_run = False

      runtime = time.time() - start_time

      if is_default_resolver:
        self._failure_count = 0

      # Reduce our rate if connection resolution is taking a long time. This is
      # most often an issue for extremely busy relays.

      min_rate = 100 * runtime

      if self.get_rate() < min_rate:
        self._rate_too_low_count += 1

        if self._rate_too_low_count >= 3:
          min_rate += 1  # little extra padding so we don't frequently update this
          self.set_rate(min_rate)
          self._rate_too_low_count = 0
          log.debug('tracker.lookup_rate_increased', seconds = "%0.1f" % min_rate)
      else:
        self._rate_too_low_count = 0

      return True
    except IOError as exc:
      log.info('wrap', text = exc)

      # Fail over to another resolver if we've repeatedly been unable to use
      # this one.

      if is_default_resolver:
        self._failure_count += 1

        if self._failure_count >= 3:
          self._resolvers.pop(0)
          self._failure_count = 0

          if self._resolvers:
            log.notice(
              'tracker.unable_to_use_resolver',
              old_resolver = resolver,
              new_resolver = self._resolvers[0],
            )
          else:
            log.notice('tracker.unable_to_use_all_resolvers')

      return False