Ejemplo n.º 1
0
    def get_lab_info(self, recursive, lab_hash=None, machine_name=None, all_users=False):
        user_name = utils.get_current_user_name() if not all_users else None
        if not recursive:
            machines = self.docker_machine.get_machines_by_filters(lab_hash=lab_hash,
                                                                   machine_name=machine_name,
                                                                   user=user_name
                                                                   )
        else:
            machines = self.docker_machine.get_machines_by_filters_rec(lab_hash=lab_hash,
                                                                       machine_name=machine_name,
                                                                       user=user_name
                                                                       )
        if not machines:
            if not lab_hash:
                raise Exception("No machines running.")
            else:
                raise Exception("Lab is not started.")

        machines = sorted(machines, key=lambda x: x.name)

        machine_streams = {}

        for machine in machines:
            machine_streams[machine] = machine.stats(stream=True, decode=True)

        table_header = ["LAB HASH", "USER", "MACHINE NAME", "STATUS", "CPU %", "MEM USAGE / LIMIT", "MEM %", "NET I/O"]
        stats_table = DoubleTable([])
        stats_table.inner_row_border = True

        while True:
            machines_data = [
                table_header
            ]

            for (machine, machine_stats) in machine_streams.items():
                real_name = machine.labels['name']
                if recursive:
                    path = machine.exec_run('hostname')[1].decode('utf-8')
                    real_name_split = path.split('.')
                    real_name = ('.'.join(real_name_split[:-1]))
                try:
                    result = next(machine_stats)
                except StopIteration:
                    continue

                stats = self._get_aggregate_machine_info(result)

                machines_data.append([machine.labels['lab_hash'],
                                      machine.labels['user'],
                                      real_name,
                                      machine.status,
                                      stats["cpu_usage"],
                                      stats["mem_usage"],
                                      stats["mem_percent"],
                                      stats["net_usage"]
                                      ])

            stats_table.table_data = machines_data

            yield "TIMESTAMP: %s" % datetime.now() + "\n\n" + stats_table.table
Ejemplo n.º 2
0
    def get_lab_info(self, lab_hash=None, machine_name=None, all_users=False):
        if all_users:
            raise NotSupportedError("Cannot use `--all` flag.")

        if lab_hash:
            lab_hash = lab_hash.lower()

        table_header = ["LAB HASH", "DEVICE NAME", "STATUS", "ASSIGNED NODE"]
        stats_table = DoubleTable([])
        stats_table.inner_row_border = True

        while True:
            machines_stats = self.k8s_machine.get_machines_info(lab_hash=lab_hash, machine_filter=machine_name)

            machines_data = [
                table_header
            ]
            for machine_stats in machines_stats:
                machines_data.append([machine_stats["real_lab_hash"],
                                      machine_stats["name"],
                                      machine_stats["status"],
                                      machine_stats["assigned_node"]
                                      ])

            stats_table.table_data = machines_data

            yield "TIMESTAMP: %s" % datetime.now() + "\n\n" + stats_table.table
Ejemplo n.º 3
0
    def get_lab_info(self, lab_hash=None, machine_name=None, all_users=False):
        user_name = utils.get_current_user_name() if not all_users else None

        machine_streams = self.docker_machine.get_machines_info(
            lab_hash, machine_filter=machine_name, user=user_name)

        table_header = [
            "LAB HASH", "USER", "DEVICE NAME", "STATUS", "CPU %",
            "MEM USAGE / LIMIT", "MEM %", "NET I/O"
        ]
        stats_table = DoubleTable([])
        stats_table.inner_row_border = True

        while True:
            machines_data = [table_header]

            try:
                result = next(machine_streams)
            except StopIteration:
                return

            if not result:
                return

            for machine_stats in result:
                machines_data.append([
                    machine_stats['real_lab_hash'], machine_stats['user'],
                    machine_stats['name'], machine_stats['status'],
                    machine_stats['cpu_usage'], machine_stats['mem_usage'],
                    machine_stats['mem_percent'], machine_stats['net_usage']
                ])

            stats_table.table_data = machines_data

            yield "TIMESTAMP: %s" % datetime.now() + "\n\n" + stats_table.table
Ejemplo n.º 4
0
    def get_formatted_lab_info(self,
                               lab_hash: str = None,
                               machine_name: str = None,
                               all_users: bool = False) -> str:
        """Return a formatted string with the information about the running devices.

        Args:
            lab_hash (str): If not None, return information of the corresponding network scenario.
            machine_name (str): If not None, return information of the specified device.
            all_users (bool): If True, return information about the device of all users.

        Returns:
             str: String containing devices info
        """
        table_header = [
            "LAB HASH", "USER", "DEVICE NAME", "STATUS", "CPU %",
            "MEM USAGE / LIMIT", "MEM %", "NET I/O"
        ]
        stats_table = DoubleTable([])
        stats_table.inner_row_border = True

        lab_info = self.get_lab_info(lab_hash, machine_name, all_users)

        while True:
            machines_data = [table_header]

            try:
                result = next(lab_info)
            except StopIteration:
                return

            if not result:
                return

            for machine_stats in result:
                machines_data.append([
                    machine_stats['real_lab_hash'], machine_stats['user'],
                    machine_stats['name'], machine_stats['status'],
                    machine_stats['cpu_usage'], machine_stats['mem_usage'],
                    machine_stats['mem_percent'], machine_stats['net_usage']
                ])

            stats_table.table_data = machines_data

            yield "TIMESTAMP: %s" % datetime.now() + "\n\n" + stats_table.table
Ejemplo n.º 5
0
    def get_formatted_lab_info(self,
                               lab_hash: str = None,
                               machine_name: str = None,
                               all_users: bool = False) -> str:
        """Return a formatted string with the information about the running devices.

        Args:
            lab_hash (str): If not None, return information of the corresponding network scenario.
            machine_name (str): If not None, return information of the specified device.
            all_users (bool): If True, return information about the device of all users.

        Returns:
             str: String containing devices info
        """
        if all_users:
            raise NotSupportedError("Cannot use `--all` flag on Megalos.")

        table_header = ["LAB HASH", "DEVICE NAME", "STATUS", "ASSIGNED NODE"]
        stats_table = DoubleTable([])
        stats_table.inner_row_border = True

        lab_info = self.get_lab_info(lab_hash=lab_hash,
                                     machine_name=machine_name)

        while True:
            machines_data = [table_header]

            try:
                result = next(lab_info)
            except StopIteration:
                return

            if not result:
                return

            for machine_stats in result:
                machines_data.append([
                    machine_stats["real_lab_hash"], machine_stats["name"],
                    machine_stats["status"], machine_stats["assigned_node"]
                ])

            stats_table.table_data = machines_data

            yield "TIMESTAMP: %s" % datetime.now() + "\n\n" + stats_table.table