Beispiel #1
0
    def do_host_status(self):
        boot_time = datetime.datetime.fromtimestamp(psutil.boot_time())

        insert_id = Host_Stats.insert({
            Host_Stats.boot_time:
            str(boot_time),
            Host_Stats.cpu_usage:
            round(psutil.cpu_percent(interval=0.5) / psutil.cpu_count(), 2),
            Host_Stats.cpu_cores:
            psutil.cpu_count(),
            Host_Stats.cpu_cur_freq:
            round(psutil.cpu_freq()[0], 2),
            Host_Stats.cpu_max_freq:
            psutil.cpu_freq()[2],
            Host_Stats.mem_percent:
            psutil.virtual_memory()[2],
            Host_Stats.mem_usage:
            helper.human_readable_file_size(psutil.virtual_memory()[3]),
            Host_Stats.mem_total:
            helper.human_readable_file_size(psutil.virtual_memory()[0]),
            Host_Stats.disk_percent:
            psutil.disk_usage('/')[3],
            Host_Stats.disk_usage:
            helper.human_readable_file_size(psutil.disk_usage('/')[1]),
            Host_Stats.disk_total:
            helper.human_readable_file_size(psutil.disk_usage('/')[0]),
        }).execute()

        # make sure we only have 1 record/row
        Host_Stats.delete().where(Host_Stats.id < int(insert_id)).execute()
    def get_world_info(self):
        world = self.get_world_name()

        if world:
            total_size = 0

            # do a scan of the directories in the server path.
            for root, dirs, files in os.walk(self.server_path, topdown=False):

                # for each directory we find
                for name in dirs:

                    # if the directory name is "region"
                    if name == "region":
                        # log it!
                        logger.debug(
                            "Path %s is called region. Getting directory size",
                            os.path.join(root, name))

                        # get this directory size, and add it to the total we have running.
                        total_size += self.get_dir_size(
                            os.path.join(root, name))

            level_total_size = helper.human_readable_file_size(total_size)

            return {'world_name': world, 'world_size': level_total_size}
        else:
            logger.warning("Unable to find world disk data")
            return {
                'world_name': 'Unable to find world name',
                'world_size': 'Unable to find world size'
            }
    def list_backups(self):
        backup_folder = "{}-{}".format(self.server_id, self.name)
        backup_list = Backups.get(Backups.server_id == int(self.server_id))
        backup_path = os.path.join(backup_list.storage_location, backup_folder)
        #helper.ensure_dir_exists(backup_path)

        results = []

        for dirpath, dirnames, filenames in os.walk(backup_path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                # skip if it is symbolic link
                if not os.path.islink(fp):
                    size = helper.human_readable_file_size(os.path.getsize(fp))
                    results.append({'path': f, 'size': size})

        return results
    def get_mc_process_stats(self):

        world_data = self.get_world_info()
        server_settings = MC_settings.get(self.server_id)
        server_settings_dict = model_to_dict(server_settings)

        if self.check_running():
            p = psutil.Process(self.PID)

            # call it first so we can be more accurate per the docs
            # https://giamptest.readthedocs.io/en/latest/#psutil.Process.cpu_percent

            dummy = p.cpu_percent()
            real_cpu = round(
                p.cpu_percent(interval=0.5) / psutil.cpu_count(), 2)

            # this is a faster way of getting data for a process
            with p.oneshot():
                server_stats = {
                    'server_start_time':
                    self.get_start_time(),
                    'server_running':
                    self.check_running(),
                    'cpu_usage':
                    real_cpu,
                    'memory_usage':
                    helper.human_readable_file_size(p.memory_info()[0]),
                    'world_name':
                    world_data['world_name'],
                    'world_size':
                    world_data['world_size'],
                    'server_ip':
                    server_settings_dict['server_ip'],
                    'server_port':
                    server_settings_dict['server_port']
                }
        else:
            server_stats = {
                'server_start_time': "Not Started",
                'server_running': False,
                'cpu_usage': 0,
                'memory_usage': "0 MB",
                'world_name': world_data['world_name'],
                'world_size': world_data['world_size'],
                'server_ip': server_settings_dict['server_ip'],
                'server_port': server_settings_dict['server_port']
            }

        # are we pingable?
        try:
            server_ping = self.ping_server()
        except:
            server_ping = False
            pass

        if server_ping:
            online_stats = json.loads(server_ping.players)
            server_stats.update({'online': online_stats.get('online', 0)})
            server_stats.update({'max': online_stats.get('max', 0)})
            server_stats.update({'players': online_stats.get('players', 0)})
            server_stats.update(
                {'server_description': server_ping.description})
            server_stats.update({'server_version': server_ping.version})

        else:
            server_stats.update({'online': 0})
            server_stats.update({'max': 0})
            server_stats.update({'players': []})
            server_stats.update({'server_description': "Unable to connect"})
            server_stats.update({'server_version': "Unable to connect"})

        return server_stats