def _add_cpu_table(self, cpu_info): rows = [] if cpu_info.get('disabled', False): self.page.text("CPU is disabled") else: rows.append(self._get_value_for_table(cpu_info, "vendor")) if self._get_value_for_table(cpu_info, "product") is not None: rows.append(self._get_value_for_table(cpu_info, "product")) elif self._get_value_for_table(cpu_info, "version") is not None: rows.append(self._get_value_for_table(cpu_info, "version")) if self._get_value_for_table(cpu_info, "clock") is not None: rows.append((self._get_value_for_table(cpu_info, "clock")[0], bytes2human(int(self._get_value_for_table(cpu_info, "clock")[1]), ndigits=2)+"Hz")) elif self._get_value_for_table(cpu_info, "capacity") is not None: rows.append((self._get_value_for_table(cpu_info, "capacity")[0], bytes2human(int(self._get_value_for_table(cpu_info, "capacity")[1]), ndigits=2)+"Hz")) cores_str = ("enabled: {}/{}, threads {}".format(cpu_info['configuration']['enabledcores'], cpu_info['configuration']['cores'], cpu_info['configuration']['threads'] ) ) rows.append(("Cores", cores_str)) capabilities = [] for cap, val in cpu_info["capabilities"].items(): if val is True: capabilities.append(cap) else: capabilities.append(val) rows.append(("Capabilities", ", ".join(sorted(capabilities)))) self.page.ll_table(rows)
def bytes_to_human(bytes): '''Turns an integer value of nbytes into a human readable format''' try: b = int(bytes) except: b = 0 return strutils.bytes2human(b)
def main(): parser = argparse.ArgumentParser(description='King Phisher Server', conflict_handler='resolve') utilities.argp_add_args(parser) startup.argp_add_server(parser) arguments = parser.parse_args() # basic runtime checks if sys.version_info < (3, 4): color.print_error('the Python version is too old (minimum required is 3.4)') return 0 console_log_handler = utilities.configure_stream_logger(arguments.logger, arguments.loglvl) del parser if os.getuid(): color.print_error('the server must be started as root, configure the') color.print_error('\'server.setuid_username\' option in the config file to drop privileges') return os.EX_NOPERM # configure environment variables and load the config find.init_data_path('server') config = configuration.ex_load_config(arguments.config_file) if arguments.verify_config: color.print_good('configuration verification passed') color.print_good('all required settings are present') return os.EX_OK if config.has_option('server.data_path'): find.data_path_append(config.get('server.data_path')) if arguments.update_geoip_db: color.print_status('downloading a new geoip database') try: size = geoip.download_geolite2_city_db(config.get('server.geoip.database')) except errors.KingPhisherResourceError as error: color.print_error(error.message) return os.EX_UNAVAILABLE color.print_good("download complete, file size: {0}".format(strutils.bytes2human(size))) return os.EX_OK # setup logging based on the configuration if config.has_section('logging'): log_file = _ex_config_logging(arguments, config, console_log_handler) logger.debug("king phisher version: {0} python version: {1}.{2}.{3}".format(version.version, sys.version_info[0], sys.version_info[1], sys.version_info[2])) # initialize the plugin manager try: plugin_manager = plugins.ServerPluginManager(config) except errors.KingPhisherError as error: if isinstance(error, errors.KingPhisherPluginError): color.print_error("plugin error: {0} ({1})".format(error.plugin_name, error.message)) else: color.print_error(error.message) return os.EX_SOFTWARE status_code = build_and_run(arguments, config, plugin_manager, log_file) plugin_manager.shutdown() logging.shutdown() return status_code
def format_dict(pd): ret = {} ret['pid'] = pd['pid'] ret['time'] = '' if pd['cpu_times'] is not None: ret['time'] = format_cpu_time(sum(pd['cpu_times'])) ret['user'] = pd['username'][:12] ret['ni'] = pd['nice'] mem_info = pd['memory_info'] ret['virt'] = bytes2human(getattr(mem_info, 'vms', 0)) ret['res'] = bytes2human(getattr(mem_info, 'rss', 0)) ret['cpu'] = '' if pd['cpu_percent'] is None else pd['cpu_percent'] ret['mem'] = '' if pd['memory_percent'] is not None: ret['mem'] = round(pd['memory_percent'], 1) ret['name'] = pd['name'] or '' return ret
def get_rusage_dict(children=False): # TODO: # number of child processes? # page size # difference between a page out and a major fault? # NOTE: values commented with an * are untracked on Linux if not resource: return {} who = resource.RUSAGE_SELF if children: who = resource.RUSAGE_CHILDREN rr = resource.getrusage(who) if sys.platform == 'darwin': rss_bytes = rr.ru_maxrss # darwin breaks posix else: rss_bytes = rr.ru_maxrss * 1024 max_rss_human = bytes2human(rss_bytes, ndigits=1) ret = { 'cpu_times': { 'user_time': rr.ru_utime, 'sys_time': rr.ru_stime }, 'memory': { 'max_rss_human': max_rss_human, 'max_rss': rr.ru_maxrss, 'shared_rss': rr.ru_ixrss, # * 'unshared_rss': rr.ru_idrss, # * 'stack_rss': rr.ru_isrss }, # * 'page_faults': { 'minor_faults': rr.ru_minflt, 'major_faults': rr.ru_majflt, 'page_outs': rr.ru_nswap }, # * 'blocking_io': { 'input_ops': rr.ru_inblock, 'output_ops': rr.ru_oublock }, 'messages': { 'sent': rr.ru_msgsnd, # * 'received': rr.ru_msgrcv }, # * 'signals': { 'received': rr.ru_nsignals }, # * 'ctx_switches': { 'voluntary': rr.ru_nvcsw, 'involuntary': rr.ru_nivcsw } } ret['rlimit'] = get_rlimit_dict() return ret
def self_build(args, reqs): # note that ^ this is parsed args, not argv import virtualenv cur_info = _args2sky_metadata(args, reqs) build_dir = args.output_dir or (reqs.sky_path + '/self-build/') build_venv = build_dir + 'build-env' wheelhouse = build_dir + 'wheelhouse' fileutils.mkdir_p(build_dir) virtualenv.create_environment(build_venv) in_virtualenv = reqs.executor.in_virtualenv( build_venv).patch_env(ALL_PROXY='') def build_wheels(*pkgs): in_virtualenv.pip.wheel( *pkgs, wheel_dir=wheelhouse, **wheel_kwargs).interactive() wheel_kwargs = {} if cur_info.pypier_repo: pypier_dir = reqs.cache.pull_project_git('pypier', cur_info.pypier_repo) wheel_kwargs['find_links'] = pypier_dir + '/packages/index.html' # build all dependencies + opensky itself build_wheels(cur_info.opensky_package, *cur_info.packages) all_packages = fnmatch.filter(os.listdir(wheelhouse), '*.whl') # create metadata package cur_info = attr.evolve( cur_info, # TODO: sys.executable sucks inside a pex command=[sys.executable] + sys.argv, all_packages=all_packages) sky_metadata_pkg_path = generate_package(cur_info, build_dir) # build meta-data package build_wheels(sky_metadata_pkg_path) top_level_pkgs = ['opensky', 'sky_metadata'] + list(cur_info.packages) in_virtualenv.pip.install('pex').batch() artifact_path = build_dir + '/sky-' + cur_info.version in_virtualenv.command( ['pex', '--python-shebang=/usr/bin/env python2.7', '--repo', wheelhouse, '--no-index', '--pre', '--output-file=' + artifact_path, '--disable-cache', '--entry-point', 'opensky'] + top_level_pkgs ).redirect() sky_size = os.path.getsize(artifact_path) sky_human_size = strutils.bytes2human(sky_size, ndigits=2) message = ('sky %s (%s) executable successfully saved to: %s' % (cur_info.version, sky_human_size, artifact_path)) return (0, message)
def urlretrieve(self, url, dest, print_progress=True): resp = self._session.get(url, stream=True) total_size, total_chunks = 0, 0 with fileutils.atomic_save(dest) as f: content_iter = resp.iter_content(1024) while 1: size_str = bytes2human(total_size, 1).rjust(7) msg = ('%s downloaded from %s\r' % (size_str, url)) if print_progress and (total_chunks % 20) == 0: sys.stdout.write(msg) sys.stdout.flush() try: chunk = next(content_iter) except StopIteration: if print_progress: sys.stdout.write('\n') break total_size += len(chunk) total_chunks += 1 f.write(chunk) return
def get_rusage_dict(children=False): # TODO: # number of child processes? # page size # difference between a page out and a major fault? # NOTE: values commented with an * are untracked on Linux if not resource: return {} who = resource.RUSAGE_SELF if children: who = resource.RUSAGE_CHILDREN rr = resource.getrusage(who) if sys.platform == 'darwin': rss_bytes = rr.ru_maxrss # darwin breaks posix else: rss_bytes = rr.ru_maxrss * 1024 max_rss_human = bytes2human(rss_bytes, ndigits=1) ret = {'cpu_times': {'user_time': rr.ru_utime, 'sys_time': rr.ru_stime}, 'memory': {'max_rss_human': max_rss_human, 'max_rss': rr.ru_maxrss, 'shared_rss': rr.ru_ixrss, # * 'unshared_rss': rr.ru_idrss, # * 'stack_rss': rr.ru_isrss}, # * 'page_faults': {'minor_faults': rr.ru_minflt, 'major_faults': rr.ru_majflt, 'page_outs': rr.ru_nswap}, # * 'blocking_io': {'input_ops': rr.ru_inblock, 'output_ops': rr.ru_oublock}, 'messages': {'sent': rr.ru_msgsnd, # * 'received': rr.ru_msgrcv}, # * 'signals': {'received': rr.ru_nsignals}, # * 'ctx_switches': {'voluntary': rr.ru_nvcsw, 'involuntary': rr.ru_nivcsw}} ret['rlimit'] = get_rlimit_dict() return ret