def add_process(self, process: Process): if process.exe() in self.execs: self._processes.add(process) else: raise ValueError( f"The process exe [{process.exe()}] doesn't match with the game execs: {self.execs}" )
def collect_status(pid, appname, site): ip_out = get_host_info()[0] ip_inner = get_host_info()[1] server_id = get_host_info()[2] physical_mem = psutil.virtual_memory().total / 1024 / 1024 #Unit of M cpu_count = psutil.cpu_count() its = int(time.time()) p_ins = Process(pid) pstatus = p_ins.status() create_time = time.strftime("%Y%m%d %H:%M:%S", time.localtime(p_ins.create_time())) memory_percent = p_ins.memory_percent() memory_used = memory_percent * physical_mem cpu_calc_list = [] for i in range(6): cpu_calc = p_ins.cpu_percent(interval=0.1) cpu_calc_list.append(cpu_calc) cpu_percent = float(sum(cpu_calc_list) / len(cpu_calc_list)) num_fds = p_ins.num_fds() connections = p_ins.connections() connections_num = len(connections) #appname=p_ins.cwd() if p_ins.name() == 'jsvc': app_path = p_ins.exe().split('/')[:-2] else: app_path = p_ins.cwd() #appname = app_path.split('/')[-1] appname = appname if p_ins.children(recursive=True): children_list = str(p_ins.children(recursive=True)) else: children_list = None message = { 'site': site, 'ip': ip_out, 'ip_inner': ip_inner, 'server_id': server_id, 'pstatus': pstatus, 'metric_name': 'app_monitor', 'its': its, 'pid': pid, 'physical_mem': physical_mem, 'memory_used': memory_used, 'memory_percent': memory_percent, 'cpu_count': cpu_count, 'cpu_percent': cpu_percent, 'num_fds': num_fds, 'connections_num': connections_num, 'create_time': create_time, 'appname': appname, 'app_path': app_path, 'children': children_list } return message
def proc_version_tag(proc: Process): exe = proc.__dict__.get("info", {}).get("exe", None) if exe is None: try: exe = proc.exe() except (NoSuchProcess, AccessDenied): return version = get_version_number(exe) if version is not None: return github_version_tag(version)
def profile_process(self, process: Process) -> Optional[Mapping[str, int]]: logger.info(f"Profiling java process {process.pid}...") assert_program_installed("nsenter") # Get Java version if os.path.basename( process.exe()) not in self.SKIP_VERSION_CHECK_BINARIES: try: java_version_cmd_output = run_process([ "nsenter", "-t", str(process.pid), "--mount", "--pid", "--", os.readlink(f"/proc/{process.pid}/exe"), "-version", ]) except CalledProcessError as e: raise Exception("Failed to get java version: {}".format(e)) # Version is printed to stderr if not self.is_jdk_version_supported( java_version_cmd_output.stderr.decode()): logger.warning( f"Process {process.pid} running unsupported Java version, skipping..." ) return None process_root = f"/proc/{process.pid}/root" if is_same_ns(process.pid, "mnt"): # processes running in my namespace can use my (temporary) storage dir tmp_dir = self._storage_dir else: # processes running in other namespaces will use the base path tmp_dir = TEMPORARY_STORAGE_PATH # we'll use separated storage directories per process: since multiple processes may run in the # same namespace, one may accidentally delete the storage directory of another. storage_dir_host = resolve_proc_root_links( process_root, os.path.join(tmp_dir, str(process.pid))) try: os.makedirs(storage_dir_host) return self._profile_process_with_dir(process, storage_dir_host, process_root) finally: # ignore_errors because we are deleting paths via /proc/pid/root - and those processes # might have went down already. shutil.rmtree(storage_dir_host, ignore_errors=True)
def add_process(self, process: Process): try: if process.exe() in self.execs: self._processes.add(process) else: raise ValueError( f"The process exe [{process.exe()}] doesn't match with the game execs: {self.execs}" ) except AccessDenied: if isinstance(self.info, ClassicGame): if self.info.exe in process.name(): self._processes.add(process) else: raise ValueError( f"The process name [{process.name()}] doesn't match with the game exe: {self.info.exe}" )
def get_processes(self): for p in pids(): try: pr = Process(p) path = pr.exe() name = pr.name() file_open = open(path, 'rb') read = file_open.read() arch = "?? bits" if b"PE\0\0L" in read: arch = "32 bits" elif b"PE\0\0d" in read: arch = "64 bits" print(name +"(" + str(p) + "): " + arch) file_open.close() except: pass
def Executable(self): p = Process(self.pid) return p.exe()
def get_proc_exe_name(process: psutil.Process) -> str: return process.exe().replace("\\", "/").split("/")[-1].split(".")[0]
def track_process(self, p: psutil.Process): with p.oneshot(): key = None if p.pid in self.pids: key = self.pids[p.pid] if self.processes[key].name != p.name(): key = None if key is None: key = len(self.processes) self.processes.append(ProcessInfo(key, p.pid, p.name())) self.pids[p.pid] = key self.data.update({ f'process.{key}.name': p.name(), f'process.{key}.pid': p.pid, f'process.{key}.ppid': p.ppid(), f'process.{key}.create_time': p.create_time(), }) try: self.data.update({ f'process.{key}.exe': p.exe(), }) except (psutil.AccessDenied, psutil.ZombieProcess): pass try: self.data.update({ f'process.{key}.cmdline': '\n'.join(p.cmdline()), }) except (psutil.AccessDenied, psutil.ZombieProcess): pass self.processes[key].active = True try: res = p.memory_info() self.data.update({ f'process.{key}.rss': res.rss, f'process.{key}.vms': res.vms, }) except (psutil.AccessDenied, psutil.ZombieProcess): pass try: res = p.cpu_times() self.data.update({ f'process.{key}.user': res.user, f'process.{key}.system': res.system, }) if hasattr(res, 'iowait'): self.data.update({ f'process.{key}.iowait': res.iowait, }) except (psutil.AccessDenied, psutil.ZombieProcess): pass try: res = p.cpu_percent() self.data.update({ f'process.{key}.cpu': res, }) except (psutil.AccessDenied, psutil.ZombieProcess): pass try: res = p.num_threads() self.data.update({ f'process.{key}.threads': res, }) except (psutil.AccessDenied, psutil.ZombieProcess): pass
def _update(self, connections): listeners_tcp = set() listeners_udp = set() ingress_tcp = set() ingress_udp = set() egress_tcp = set() egress_udp = set() # Register listeners first for connection in connections: if connection.raddr: continue program = '' if connection.pid: try: process = Process(connection.pid) program = process.exe() except Error: program = 'pid={}'.format(connection.pid) listeners = None if connection.type == SOCK_DGRAM: listeners = listeners_udp elif connection.type == SOCK_STREAM and connection.status == 'LISTEN': listeners = listeners_tcp else: continue listeners.add( (program, connection.laddr.ip, connection.laddr.port)) new_listeners_tcp = listeners_tcp - self.known_listeners_tcp new_listeners_udp = listeners_udp - self.known_listeners_udp for new_listener_udp in new_listeners_udp: if new_listener_udp not in self.pending_udp_listeners: self.pending_udp_listeners[new_listener_udp] = 1 else: self.pending_udp_listeners[new_listener_udp] += 1 for old_listener_udp in self.pending_udp_listeners.keys(): if old_listener_udp not in new_listener_udp: del self.pending_udp_listeners[old_listener_udp] new_listeners_udp = set(new_listener_udp for new_listener_udp, cnt in self.pending_udp_listeners.iteritems() if cnt > 16) for new_listener_udp in new_listeners_udp: del self.pending_udp_listeners[new_listener_udp] self.known_listeners_tcp.update(listeners_tcp) self.known_listeners_udp.update(new_listeners_udp) known_listeners_udp = set( (ip, port) for _, ip, port in self.known_listeners_udp) known_listeners_tcp = set( (ip, port) for _, ip, port in self.known_listeners_tcp) # Now update ingress/egress connections for connection in connections: if not connection.raddr: continue program = '' if connection.pid: try: process = Process(connection.pid) program = process.exe() except Error: program = 'pid={}'.format(connection.pid) remote_ip = connection.raddr.ip remote_tuple = connection.raddr.ip, connection.raddr.port local = connection.laddr.ip, connection.laddr.port connlist = None connitem = None if connection.type == SOCK_DGRAM: if any(candidate in known_listeners_udp for candidate in (local, ('::', local[1]), ('0.0.0.0', local[1]), ('127.0.0.1', local[1]), '::ffff:127.0.0.1', local[1])): connlist = ingress_udp connitem = program, local, remote_ip else: connlist = egress_udp connitem = program, remote_tuple elif connection.type == SOCK_STREAM: if any(candidate in known_listeners_tcp for candidate in (local, ('::', local[1]), ('0.0.0.0', local[1]), ('127.0.0.1', local[1]), '::ffff:127.0.0.1', local[1])): connlist = ingress_tcp connitem = program, local, remote_ip else: connlist = egress_tcp connitem = program, remote_tuple else: continue connlist.add(connitem) new_ingress_udp = ingress_udp - self.known_ingress_udp new_ingress_tcp = ingress_tcp - self.known_ingress_tcp new_egress_udp = egress_udp - self.known_egress_udp new_egress_tcp = egress_tcp - self.known_egress_tcp self.known_ingress_udp.update(ingress_udp) self.known_ingress_tcp.update(ingress_tcp) self.known_egress_udp.update(egress_udp) self.known_egress_tcp.update(egress_tcp) new_objects = tuple( tuple(x) for x in (new_listeners_tcp, new_listeners_udp, new_ingress_tcp, new_ingress_udp, new_egress_tcp, new_egress_udp)) if not any(x for x in new_objects): return self.append(new_objects)