Пример #1
0
    def _calc_resource_stats(self, interval):
        """
        Get local resource stats

        :return: dict
        """
        result = {}

        if 'mem' in self.metrics:
            result['mem'] = self.__get_mem_info()

        if 'disk-space' in self.metrics:
            result['disk-space'] = psutil.disk_usage(self.engine.artifacts_dir).percent

        if 'engine-loop' in self.metrics:
            result['engine-loop'] = self.engine.engine_loop_utilization

        if 'conn-all' in self.metrics:
            # take all connections without address resolution
            output = subprocess.check_output(['netstat', '-an'])
            output_lines = stream_decode(output).split('\n')  # in py3 stream has 'bytes' type
            est_lines = [line for line in output_lines if line.find('EST') != -1]
            result['conn-all'] = len(est_lines)

        if 'cpu' in self.metrics:
            result['cpu'] = psutil.cpu_percent()

        if 'bytes-recv' in self.metrics or 'bytes-sent' in self.metrics:
            net = psutil.net_io_counters()
            if net is not None:
                tx_bytes = int((net.bytes_sent - self._net_counters.bytes_sent) / float(interval))
                rx_bytes = int((net.bytes_recv - self._net_counters.bytes_recv) / float(interval))
                self._net_counters = net
            else:
                rx_bytes = 0.0
                tx_bytes = 0.0

            if 'bytes-recv' in self.metrics:
                result['bytes-recv'] = rx_bytes
            if 'bytes-sent' in self.metrics:
                result['bytes-sent'] = tx_bytes

        if 'disk-read' in self.metrics or 'disk-write' in self.metrics:
            disk = self.__get_disk_counters()
            if disk is not None:
                dru = int((disk.read_bytes - self._disk_counters.read_bytes) / float(interval))
                dwu = int((disk.write_bytes - self._disk_counters.write_bytes) / float(interval))
                self._disk_counters = disk
            else:
                dru = 0.0
                dwu = 0.0

            if 'disk-read' in self.metrics:
                result['disk-read'] = dru
            if 'disk-write' in self.metrics:
                result['disk-write'] = dwu

        return result
Пример #2
0
def sync_run(args, env=None):
    output = check_output(args, env=env, stderr=STDOUT)
    return stream_decode(output).rstrip()
Пример #3
0
    def _calc_resource_stats(self, interval):
        """
        Get local resource stats

        :return: dict
        """
        result = {}

        if 'mem' in self.metrics:
            result['mem'] = self.__get_mem_info()

        if 'disk-space' in self.metrics:
            result['disk-space'] = psutil.disk_usage(self.engine.artifacts_dir).percent

        if 'engine-loop' in self.metrics:
            result['engine-loop'] = self.engine.engine_loop_utilization

        if 'conn-all' in self.metrics:
            try:
                # take all connections without address resolution
                output = subprocess.check_output(['netstat', '-an'])
                output_lines = stream_decode(output).split('\n')  # in py3 stream has 'bytes' type
                est_lines = [line for line in output_lines if line.find('EST') != -1]
                result['conn-all'] = len(est_lines)
            except BaseException as exc:
                self.log.debug("Failed to get connections info: %s", exc)
                result['conn-all'] = 0

        if 'cpu' in self.metrics:
            result['cpu'] = psutil.cpu_percent()

        if 'bytes-recv' in self.metrics or 'bytes-sent' in self.metrics:
            net = psutil.net_io_counters()
            if net is not None:
                tx_bytes = int((net.bytes_sent - self._net_counters.bytes_sent) / float(interval))
                rx_bytes = int((net.bytes_recv - self._net_counters.bytes_recv) / float(interval))
                self._net_counters = net
            else:
                rx_bytes = 0.0
                tx_bytes = 0.0

            if 'bytes-recv' in self.metrics:
                result['bytes-recv'] = rx_bytes
            if 'bytes-sent' in self.metrics:
                result['bytes-sent'] = tx_bytes

        if 'disk-read' in self.metrics or 'disk-write' in self.metrics:
            disk = self.__get_disk_counters()
            if disk is not None:
                dru = int((disk.read_bytes - self._disk_counters.read_bytes) / float(interval))
                dwu = int((disk.write_bytes - self._disk_counters.write_bytes) / float(interval))
                self._disk_counters = disk
            else:
                dru = 0.0
                dwu = 0.0

            if 'disk-read' in self.metrics:
                result['disk-read'] = dru
            if 'disk-write' in self.metrics:
                result['disk-write'] = dwu

        return result