Example #1
0
    def update(self):
        """Update the ports list."""

        if self.input_method == 'local':
            # Only refresh:
            # * if there is not other scanning thread
            # * every refresh seconds (define in the configuration file)
            if self._thread is None:
                thread_is_running = False
            else:
                thread_is_running = self._thread.isAlive()
            if self.timer_ports.finished() and not thread_is_running:
                # Run ports scanner
                self._thread = ThreadScanner(self.stats)
                self._thread.start()
                # Restart timer
                if len(self.stats) > 0:
                    self.timer_ports = Timer(self.stats[0]['refresh'])
                else:
                    self.timer_ports = Timer(0)
        else:
            # Not available in SNMP mode
            pass

        return self.stats
Example #2
0
    def __init__(self, cached_time=1):
        self.cpu_percent = 0
        self.percpu_percent = []

        # cached_time is the minimum time interval between stats updates
        # since last update is passed (will retrieve old cached info instead)
        self.timer_cpu = Timer(0)
        self.timer_percpu = Timer(0)
        self.cached_time = cached_time
Example #3
0
    def __init__(self, args=None):
        """Init GlancesActions class."""
        # Dict with the criticity status
        # - key: stat_name
        # - value: criticity
        # Goal: avoid to execute the same command twice
        self.status = {}

        # Add a timer to avoid any trigger when Glances is started (issue#732)
        # Action can be triggered after refresh * 2 seconds
        if hasattr(args, 'time'):
            self.start_timer = Timer(args.time * 2)
        else:
            self.start_timer = Timer(3)
Example #4
0
 def __get_percpu(self):
     """Update and/or return the per CPU list using the psutil library."""
     # Never update more than 1 time per cached_time
     if self.timer_percpu.finished():
         self.percpu_percent = []
         for cpu_number, cputimes in enumerate(
                 psutil.cpu_times_percent(interval=0.0, percpu=True)):
             cpu = {
                 'key': self.get_key(),
                 'cpu_number': cpu_number,
                 'total': round(100 - cputimes.idle, 1),
                 'user': cputimes.user,
                 'system': cputimes.system,
                 'idle': cputimes.idle
             }
             # The following stats are for API purposes only
             if hasattr(cputimes, 'nice'):
                 cpu['nice'] = cputimes.nice
             if hasattr(cputimes, 'iowait'):
                 cpu['iowait'] = cputimes.iowait
             if hasattr(cputimes, 'irq'):
                 cpu['irq'] = cputimes.irq
             if hasattr(cputimes, 'softirq'):
                 cpu['softirq'] = cputimes.softirq
             if hasattr(cputimes, 'steal'):
                 cpu['steal'] = cputimes.steal
             if hasattr(cputimes, 'guest'):
                 cpu['guest'] = cputimes.guest
             if hasattr(cputimes, 'guest_nice'):
                 cpu['guest_nice'] = cputimes.guest_nice
             # Append new CPU to the list
             self.percpu_percent.append(cpu)
             # Reset timer for cache
             self.timer_percpu = Timer(self.cached_time)
     return self.percpu_percent
Example #5
0
 def __get_cpu(self):
     """Update and/or return the CPU using the psutil library."""
     # Never update more than 1 time per cached_time
     if self.timer_cpu.finished():
         self.cpu_percent = psutil.cpu_percent(interval=0.0)
         # Reset timer for cache
         self.timer_cpu = Timer(self.cached_time)
     return self.cpu_percent
Example #6
0
    def get(self):
        """Get the first public IP address returned by one of the online services"""
        q = queue.Queue()

        for u, j, k in urls:
            t = threading.Thread(target=self._get_ip_public, args=(q, u, j, k))
            t.daemon = True
            t.start()

        t = Timer(self.timeout)
        ip = None
        while not t.finished() and ip is None:
            if q.qsize() > 0:
                ip = q.get()

        return ip
Example #7
0
    def __init__(self, cache_timeout=60):
        """Init the class to collect stats about processes."""
        # Add internals caches because PSUtil do not cache all the stats
        # See: https://code.google.com/p/psutil/issues/detail?id=462
        logger.debug("[GlancesProcesses.__init__]")
        self.username_cache = {}
        self.cmdline_cache = {}

        # The internals caches will be cleaned each 'cache_timeout' seconds
        self.cache_timeout = cache_timeout
        self.cache_timer = Timer(self.cache_timeout)

        # Init the io dict
        # key = pid
        # value = [ read_bytes_old, write_bytes_old ]
        self.io_old = {}

        # Wether or not to enable process tree
        self._enable_tree = False
        self.process_tree = None

        # Init stats
        self.auto_sort = True
        self._sort_key = 'cpu_percent'
        self.allprocesslist = []
        self.processlist = []
        self.processcount = {'total': 0, 'running': 0, 'sleeping': 0, 'thread': 0}

        # Tag to enable/disable the processes stats (to reduce the Glances CPU consumption)
        # Default is to enable the processes stats
        self.disable_tag = False

        # Extended stats for top process is enable by default
        self.disable_extended_tag = False

        # Maximum number of processes showed in the UI (None if no limit)
        self._max_processes = None

        # Process filter is a regular expression
        self._filter = GlancesFilter()

        # Whether or not to hide kernel threads
        self.no_kernel_threads = False
Example #8
0
    def __init__(self, args=None, config=None):
        """Init the plugin."""
        super(Plugin, self).__init__(args=args)
        logger.debug("[gl_ports Plugin.__init__]")
        self.args = args
        self.config = config

        # We want to display the stat in the curse interface
        self.display_curse = True

        # Init stats
        self.stats = GlancesPortsList(config=config,
                                      args=args).get_ports_list()

        # Init global Timer
        self.timer_ports = Timer(0)

        # Global Thread running all the scans
        self._thread = None
Example #9
0
    def update(self, stats, cs_status=None, return_to_browser=False):
        """Update the screen.

        Wait for __refresh_time sec / catch key every 100 ms.

        INPUT
        stats: Stats database to display
        cs_status:
            "None": standalone or server mode
            "Connected": Client is connected to the server
            "Disconnected": Client is disconnected from the server
        return_to_browser:
            True: Do not exist, return to the browser list
            False: Exit and return to the shell

        OUPUT
        True: Exit key has been pressed
        False: Others cases...
        """
        # Flush display
        self.flush(stats, cs_status=cs_status)

        # Wait
        exitkey = False
        countdown = Timer(self.__refresh_time)
        while not countdown.finished() and not exitkey:
            # Getkey
            pressedkey = self.__catch_key(return_to_browser=return_to_browser)
            # Is it an exit key ?
            exitkey = (pressedkey == ord('\x1b') or pressedkey == ord('q'))
            if not exitkey and pressedkey > -1:
                # Redraw display
                self.flush(stats, cs_status=cs_status)
            # Wait 100ms...
            self.wait()

        return exitkey