def test_basic_docker_conf(self, backend, remote_host=None, remote_docker_port=None, use_tls=None): """ Test if the configuration given for connecting to Docker works""" try: if backend == "remote": if isinstance(use_tls, basestring): tls_config = docker.tls.TLSConfig( client_cert=(use_tls + '/cert.pem', use_tls + '/key.pem'), verify=use_tls + '/ca.pem' ) protocol = "https" elif use_tls is True: tls_config = True protocol = "https" else: tls_config = False protocol = "http" docker_connection = docker.Client(base_url=protocol + "://" + remote_host + ":" + str(remote_docker_port), tls=tls_config) elif backend == "local": docker_connection = docker.Client(**kwargs_from_env()) else: self._display_warning("- The setup tool does not support remote manual agents. The configuration will not be checked.") return True except Exception as e: self._display_error("- Unable to connect to Docker. Error was %s" % str(e)) return False try: self._display_info("- Asking Docker some info") if docker.utils.compare_version('1.19', docker_connection.version()['ApiVersion']) < 0: self._display_error("- Docker version >= 1.7.0 is required.") return False except Exception as e: self._display_error("- Unable to contact Docker. Error was %s" % str(e)) return False self._display_info("- Successfully got info from Docker. Docker connection works.") if backend == "local": self._display_info("- Verifying access to cgroups") try: from cgutils import cgroup except: self._display_error("- Cannot import cgroup-utils. Is the package installed?") return False try: cgroup.scan_cgroups("memory") except: self._display_error("- Cannot find cgroup. Are you sure the Docker instance is local?") return False return True
def run(self, args): root_cgroup = cgroup.scan_cgroups(self.options.target_subsystem) def collect_configs(_cgroup, store): if self.options.debug: print(_cgroup) if self.options.hide_empty and _cgroup.n_procs == 0: return if self.options.show_default: if self.options.json: store[_cgroup.path] = _cgroup.get_configs() else: # To calculate rates, default values are required store[_cgroup.path] = (_cgroup.get_configs(), _cgroup.get_default_configs()) return configs = self._collect_changed_configs(_cgroup) if configs: if self.options.json: store[_cgroup.path] = configs else: # To calculate rates, default values are required store[_cgroup.path] = (configs, _cgroup.get_default_configs()) cgroups = {} cgroup.walk_cgroups(root_cgroup, collect_configs, cgroups) if self.options.json: import json json.dump(cgroups, sys.stdout, indent=4) else: for name, (configs, defaults) in cgroups.iteritems(): print(name) self._print_configs(configs, defaults)
def _update_cgroups(self): def collect_by_name(cg, store): if cg.fullname not in store: store[cg.fullname] = [] store[cg.fullname].append(cg) # Collect cgroups by group name (path) cgroups = {} for name in self.SUBSYSTEMS: try: root_cgroup = cgroup.scan_cgroups(name, self.FILTERS[name]) cgroup.walk_cgroups(root_cgroup, collect_by_name, cgroups) except EnvironmentError as e: # Don't annoy users by showing error messages pass except cgroup.NoSuchSubsystemError as e: # Some systems don't support all subsystems, for example # Parallels Cloud Server and OpenVZ may not have memory subsystem. if name not in self.nosubsys_warning_showed: print(e) time.sleep(1) self.nosubsys_warning_showed[name] = True self.cgroups = cgroups if self.options.hide_root: del self.cgroups['/']
def run(self): if self.args.show_autogroup and self.args.target_subsystem != "cpu": print("Error: autogroup is meaningless for %s subsystem" % self.args.target_subsystem) sys.exit(1) root_cgroup = cgroup.scan_cgroups(self.args.target_subsystem) if self.args.debug: print(root_cgroup) def build_container_tree(container): _cgroup = container.this for child in _cgroup.childs: child.update() n_childs = len(child.childs) n_pids = len(child.pids) if self.args.hide_empty and n_childs == 0 and n_pids == 0: continue cont = TreeContainer(child) container.childs.append(cont) build_container_tree(cont) if not self.args.show_procs: return _cgroup.update() if self.args.debug: print(_cgroup.pids) if self.args.show_autogroup and container == root_container: # Autogroup is effective only when processes don't belong # to any cgroup groups = self._build_autogroup_container_tree(_cgroup.pids) container.childs.extend(groups) else: procs = self._build_process_container_tree(_cgroup.pids) container.childs.extend(procs) root_container = TreeContainer(root_cgroup) build_container_tree(root_container) def print_containers_recursively(cont, indents): if self.args.debug: print(cont) if isinstance(cont.this, cgroup.CGroup): self._print_cgroup(cont.this, indents) elif isinstance(cont.this, process.Process): self._print_process(cont.this, indents) else: self._print_autogroup(cont.this, indents) for child in cont.childs: if child == cont.childs[-1]: _indents = indents + ["last"] else: _indents = indents + ["cont"] print_containers_recursively(child, _indents) print_containers_recursively(root_container, [])
def run(self): root_cgroup = cgroup.scan_cgroups(self.args.target_subsystem) def collect_configs(_cgroup, store): if self.args.debug: print(_cgroup) if self.args.hide_empty and _cgroup.n_procs == 0: return if self.args.show_default: if self.args.json: store[_cgroup.path] = _cgroup.get_configs() else: # To calculate rates, default values are required store[_cgroup.path] = (_cgroup.get_configs(), _cgroup.get_default_configs()) return configs = self._collect_changed_configs(_cgroup) if configs: if self.args.json: store[_cgroup.path] = configs else: # To calculate rates, default values are required store[_cgroup.path] = (configs, _cgroup.get_default_configs()) cgroups = {} cgroup.walk_cgroups(root_cgroup, collect_configs, cgroups) if self.args.json: import json json.dump(cgroups, sys.stdout, indent=4) else: for name, (configs, defaults) in cgroups.items(): print(name) self._print_configs(configs, defaults)
def run(self): root_cgroup = cgroup.scan_cgroups(self.args.target_subsystem) def print_matched(cg, dummy): mypid = os.getpid() cg.update() for pid in cg.pids: if pid == mypid: continue proc = process.Process(pid) if self.args.cmdline: comp = proc.cmdline else: comp = proc.name if self.args.ignore_case: comp = comp.lower() _procname = self.args.procname.lower() else: _procname = self.args.procname if _procname in comp: if self.args.show_name: if self.args.cmdline: output = "%d %s" % (proc.pid, proc.cmdline) else: output = "%d %s" % (proc.pid, proc.name) else: output = str(proc.pid) print('%s: %s' % (cg.path, output)) cgroup.walk_cgroups(root_cgroup, print_matched, None)
def get_container_cgroup(cgroupname, container): def recur(cg): if cg.name == container or cg.name == "docker-{}.scope".format(container): return cg else: for child in cg.childs: g = recur(child) if g is not None: return g return None cgroup_obj = cgroup.scan_cgroups(cgroupname) return recur(cgroup_obj)
def run(self): root_cgroup = cgroup.scan_cgroups(self.target_subsystem) def collect_configs(_cgroup, store): store[_cgroup.path] = _cgroup.get_stats() cgroups = {} cgroup.walk_cgroups(root_cgroup, collect_configs, cgroups) if self.debug: json.dump(cgroups, sys.stdout, indent=4) return cgroups
def get_container_cgroup(cgroupname, container): def recur(cg): if cg.name == container or cg.name == "docker-{}.scope".format( container): return cg else: for child in cg.childs: g = recur(child) if g is not None: return g return None cgroup_obj = cgroup.scan_cgroups(cgroupname) return recur(cgroup_obj)
def _update_cgroups(self): def collect_by_name(cg, store): if cg.fullname not in store: store[cg.fullname] = [] store[cg.fullname].append(cg) # Collect cgroups by group name (path) cgroups = {} for name in self.SUBSYSTEMS: try: root_cgroup = cgroup.scan_cgroups(name, self.FILTERS[name]) cgroup.walk_cgroups(root_cgroup, collect_by_name, cgroups) except EnvironmentError, e: # Don't annoy users by showing error messages pass
def run(self): root_cgroup = cgroup.scan_cgroups(self.args.target_subsystem) def collect_configs(_cgroup, store): if self.args.debug: print(_cgroup) if self.args.hide_empty and _cgroup.n_procs == 0: pass else: store[_cgroup.path] = _cgroup.get_stats() cgroups = {} cgroup.walk_cgroups(root_cgroup, collect_configs, cgroups) if self.args.json: import json json.dump(cgroups, sys.stdout, indent=4) else: for cgname, stats in cgroups.items(): self._print_stats(cgname, stats)
def run(self, args): root_cgroup = cgroup.scan_cgroups(self.options.target_subsystem) def collect_configs(_cgroup, store): if self.options.debug: print(_cgroup) if self.options.hide_empty and _cgroup.n_procs == 0: pass else: store[_cgroup.path] = _cgroup.get_stats() cgroups = {} cgroup.walk_cgroups(root_cgroup, collect_configs, cgroups) if self.options.json: import json json.dump(cgroups, sys.stdout, indent=4) else: for cgname, stats in cgroups.iteritems(): self._print_stats(cgname, stats)
def run(self, args): if len(args) < 1: self.parser.error('Less argument') if len(args) > 1: self.parser.error('Too many arguments: ' + ' '.join(args)) procname = args[0] root_cgroup = cgroup.scan_cgroups(self.options.target_subsystem) def print_matched(cg, dummy): mypid = os.getpid() cg.update() for pid in cg.pids: if pid == mypid: continue proc = process.Process(pid) if self.options.cmdline: comp = proc.cmdline else: comp = proc.name if self.options.ignore_case: comp = comp.lower() _procname = procname.lower() else: _procname = procname if _procname in comp: if self.options.show_name: if self.options.cmdline: output = "%d %s" % (proc.pid, proc.cmdline) else: output = "%d %s" % (proc.pid, proc.name) else: output = str(proc.pid) print('%s: %s' % (cg.path, output)) cgroup.walk_cgroups(root_cgroup, print_matched, None)
def test_basic_docker_conf(self, backend, remote_host=None, remote_docker_port=None, use_tls=None): """ Test if the configuration given for connecting to Docker works""" try: if backend == "remote": if isinstance(use_tls, basestring): tls_config = docker.tls.TLSConfig( client_cert=(use_tls + '/cert.pem', use_tls + '/key.pem'), verify=use_tls + '/ca.pem') protocol = "https" elif use_tls is True: tls_config = True protocol = "https" else: tls_config = False protocol = "http" docker_connection = docker.Client(base_url=protocol + "://" + remote_host + ":" + str(remote_docker_port), tls=tls_config) elif backend == "local": docker_connection = docker.Client(**kwargs_from_env()) else: self._display_warning( "- The setup tool does not support remote manual agents. The configuration will not be checked." ) return True except Exception as e: self._display_error("- Unable to connect to Docker. Error was %s" % str(e)) return False try: self._display_info("- Asking Docker some info") if docker.utils.compare_version( '1.19', docker_connection.version()['ApiVersion']) < 0: self._display_error("- Docker version >= 1.7.0 is required.") return False except Exception as e: self._display_error("- Unable to contact Docker. Error was %s" % str(e)) return False self._display_info( "- Successfully got info from Docker. Docker connection works.") if backend == "local": self._display_info("- Verifying access to cgroups") try: from cgutils import cgroup except: self._display_error( "- Cannot import cgroup-utils. Is the package installed?") return False try: cgroup.scan_cgroups("memory") except: self._display_error( "- Cannot find cgroup. Are you sure the Docker instance is local?" ) return False return True
def run(self, args): if self.options.show_autogroup and self.options.target_subsystem != 'cpu': print("Error: autogroup is meaningless for %s subsystem" % self.options.target_subsystem) sys.exit(1) root_cgroup = cgroup.scan_cgroups(self.options.target_subsystem) if self.options.debug: print(root_cgroup) def build_container_tree(container): _cgroup = container.this for child in _cgroup.childs: child.update() n_childs = len(child.childs) n_pids = len(child.pids) if self.options.hide_empty and n_childs == 0 and n_pids == 0: continue cont = TreeContainer(child) container.childs.append(cont) build_container_tree(cont) if not self.options.show_procs: return _cgroup.update() if self.options.debug: print(_cgroup.pids) if self.options.show_autogroup and container == root_container: # Autogroup is effective only when processes don't belong # to any cgroup groups = self._build_autogroup_container_tree(_cgroup.pids) container.childs.extend(groups) else: procs = self._build_process_container_tree(_cgroup.pids) container.childs.extend(procs) root_container = TreeContainer(root_cgroup) build_container_tree(root_container) def print_containers_recursively(cont, indents): if self.options.debug: print(cont) if isinstance(cont.this, cgroup.CGroup): self._print_cgroup(cont.this, indents) elif isinstance(cont.this, process.Process): self._print_process(cont.this, indents) else: self._print_autogroup(cont.this, indents) for child in cont.childs: if child == cont.childs[-1]: _indents = indents + ['last'] else: _indents = indents + ['cont'] print_containers_recursively(child, _indents) print_containers_recursively(root_container, [])