def is_task_executor_process(task: Task, process: psutil.Process): """ check the process if task executor or not by command :param task: :param process: :return: """ # Todo: The same map should be used for run task command run_cmd_map = { 3: "f_job_id", 5: "f_component_name", 7: "f_task_id", 9: "f_task_version", 11: "f_role", 13: "f_party_id" } try: cmdline = process.cmdline() schedule_logger(task.f_job_id).info(cmdline) except Exception as e: # Not sure whether the process is a task executor process, operations processing is required schedule_logger(task.f_job_id).warning(e) return False for i, k in run_cmd_map.items(): if len(cmdline) > i and cmdline[i] == str(getattr(task, k)): continue else: # todo: The logging level should be obtained first if len(cmdline) > i: schedule_logger(task.f_job_id).debug( f"cmd map {i} {k}, cmd value {cmdline[i]} task value {getattr(task, k)}" ) return False else: return True
def is_task_executor_process(task: Task, process: psutil.Process): """ check the process if task executor or not by command :param task: :param process: :return: """ try: cmdline = process.cmdline() except Exception as e: # Not sure whether the process is a task executor process, operations processing is required schedule_logger(task.f_job_id).warning(e) return False else: schedule_logger(task.f_job_id).info(cmdline) if task.f_worker_id and task.f_worker_id in cmdline: return True if len(cmdline) != len(task.f_cmd): return False for i, v in enumerate(task.f_cmd): if cmdline[i] != str(v): return False return True
def get_l_commandline_from_psutil_process( process: psutil.Process) -> List[str]: """ if there are blanks in the parameters, psutil.cmdline does not work correctly on linux, even if they are '\x00' separated see Error Report for PSUTIL : https://github.com/giampaolo/psutil/issues/1179 sometimes the parameters are separated with blanks, in that case we use shlex in Linux for instance postgrey, or some other scripts started with systemd services that happens also on some windows programs >>> # test the "good" commandline, '\x00' terminated and '\x00' separated >>> import getpass >>> import importlib >>> import importlib.util >>> if lib_platform.is_platform_linux: ... process = subprocess.Popen(['nano', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['nano', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() ... save_actual_directory = str(pathlib.Path().cwd().absolute()) ... test_directory = lib_path.get_test_directory_path('lib_shell', test_directory_name='tests') ... os.chdir(str(test_directory)) ... # for travis we need to be owner - otherwise we get permission error ... lib_path.make_test_directory_and_subdirs_fully_accessible_by_current_user(test_directory) ... process = subprocess.Popen(['./test test/test test.sh', './test test/some_parameter', 'p1', 'p2']) ... psutil_process=psutil.Process(process.pid) ... expected = ['/bin/bash', './test test/test test.sh', './test test/some_parameter', 'p1', 'p2'] ... assert get_l_commandline_from_psutil_process(psutil_process) == expected ... psutil_process.kill() ... os.chdir(save_actual_directory) ... elif lib_platform.is_platform_darwin: ... process = subprocess.Popen(['open', '-a', 'TextEdit', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['open', '-a', 'TextEdit', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() ... else: ... process = subprocess.Popen(['notepad', './mäßig böse büßer', './müßige bärtige blödmänner']) ... psutil_process=psutil.Process(process.pid) ... assert get_l_commandline_from_psutil_process(psutil_process) == ['notepad', './mäßig böse büßer', './müßige bärtige blödmänner'] ... psutil_process.kill() """ if lib_platform.is_platform_linux: with open(f'/proc/{process.pid}/cmdline', mode='r') as proc_commandline: l_commands = proc_commandline.read().split('\x00') else: l_commands = process.cmdline() l_commands = lib_list.ls_strip_elements(l_commands) l_commands = lib_list.ls_del_empty_elements(l_commands) if len(l_commands) == 1: # pragma: no cover s_command = l_commands[0] # for the case the command executable contains blank, the part after the blank would be interpreted as parameter # for instance "/home/user/test test.sh parameter1 parameter2" if lib_platform.is_platform_linux: s_command = get_quoted_command(s_command, process) l_commands = lib_shell_shlex.shlex_split_multi_platform( s_command) # pragma: no cover return l_commands
def is_ardupilot_process(process: psutil.Process) -> bool: """Checks if given process is using a Ardupilot's firmware file, for any known platform.""" for platform in Platform: firmware_path = self.firmware_manager.firmware_path(platform) if str(firmware_path) in " ".join(process.cmdline()): return True return False
def _discover_proc_name(self, process: psutil.Process, ns_id: int) \ -> Optional[str]: """Discovers the process "name" for a given process. The name is taken from the most senior process in the process tree which is still in the same (PID or user) namespace as the process initially specified to this function. """ parent = process.parent() while parent: try: parent_ns_id = os.stat( '/proc/%d/ns/%s' % (parent.pid, self._nstypename))\ .st_ino except PermissionError: parent_ns_id = -1 if parent_ns_id != ns_id: break process = parent parent = process.parent() # prepare the pretty-print-able process name: only use the last # executable path element, and strip of a leading "-" indicating a # login shell. In case the process' command line is inaccessible to # us, then go for the process name. try: proc_name = process.cmdline()[0].split('/')[-1] except IndexError: # Mimic what the "ps" CLI tool does in case of process names... proc_name = "[%s]" % process.name() if proc_name[:1] == '-': proc_name = proc_name[1:] return '%s (%d)' % (proc_name, process.pid)
def get_airflow_data( process: psutil.Process ) -> t.Optional[t.Dict[str, t.Union[str, bool]]]: cmdline = process.cmdline() if not cmdline or not cmdline[0].startswith('/usr/bin/python'): return None for cmd_arg in cmdline: if 'airflow run' not in cmd_arg: continue airflow_args = cmd_arg.split() dag = airflow_args[3] operator = airflow_args[4] exec_date = airflow_args[5][5:25] is_local = any([i == '--local' for i in airflow_args]) is_raw = any([i == '--raw' for i in airflow_args]) return { 'dag': dag, 'operator': operator, 'exec_date': exec_date, 'is_local': is_local, 'is_raw': is_raw, }
def get_procinfo_by_address(ip_src, ip_dst, port_src=None, port_dst=None): """ Gets Infos about the Prozess associated with the given address information Both port_src and port_dst must be not None or None at the same time. return -- [pid, "/path/to/command", "user", "hash"] or [] """ result = [] if port_src is not None: pids = [c.pid for c in net_connections() if len(c.raddr) != 0 and c.pid is not None and (c.laddr[0], c.raddr[0], c.laddr[1], c.raddr[1]) == (ip_src, ip_dst, port_src, port_dst)] else: pids = [c.pid for c in net_connections() if len(c.raddr) != 0 and c.pid is not None and (c.laddr[0], c.raddr[0]) == (ip_src, ip_dst)] try: if len(pids) > 1: logger.warning("more than 1 matching process: %r", pids) proc = Process(pids[0]) cmd = [pids[0], proc.cmdline(), proc.username()] hash_input = "%d%s%s" % (cmd[0], cmd[1], cmd[2]) procinfo_hash = hashlib.sha256(hash_input.encode("UTF-8")) cmd.append(procinfo_hash) logger.debug("process info: %r", cmd) return cmd except IndexError: pass return []
def get_soledad_server_pid(): output = check_output(['pidof', 'python']) for pid in output.split(): proc = Process(int(pid)) cmdline = proc.cmdline() if args.issubset(set(cmdline)): return int(pid)
def get_panel_pid(): for pid in pids(): try: p = Process(pid) n = p.cmdline()[-1] if n.find('runserver') != -1 or n.find('BT-Panel') != -1: return pid except: pass return None
def cmdline(p: Process) -> str: """There must be a bug in psutil and sometimes `cmdline()` raises an exception. I don't want that, so I'll override this behavior for now. """ try: return " ".join(p.cmdline()) except psutil.NoSuchProcess: return ""
def wait_for_process_then_notify(pid, message=None, priority=None): process = Process(pid) default_message = "Process {} ({}) has finished.".format( ' '.join(process.cmdline()), pid) process.wait() notify(message or default_message, priority=priority)
def query_process(process=None): if not isinstance(process, Process): process = Process(process) cmd_line = u" ".join((u'"%s"' % x.replace('"', r'\"') if " " in x else x.replace('"', r'\"') \ for x in process.cmdline())) return { "pid": process.pid, "cmd_line": cmd_line, "owner": process.uids()[0] }
def F_Memory(pname): a = pids() for i in a: try: p = Process(i) if p.name() == 'java': cmd = ' '.join(p.cmdline()) if re.search(pname, cmd): print round(p.memory_percent(), 2) except Exception, e: continue
def F_Threads(pname): a = pids() for i in a: try: p = Process(i) if p.name() == 'java': cmd = ' '.join(p.cmdline()) if re.search(pname, cmd): print p.num_threads() except Exception, e: continue
def get_panel_pid(): pid = ReadFile('{}/logs/panel.pid'.format(base_path)) if pid: return int(pid) for pid in pids(): try: p = Process(pid) n = p.cmdline()[-1] if n.find('runserver') != -1 or n.find('BT-Panel') != -1: return pid except: pass return None
def F_Process(pname): result = [] a = pids() for i in a: try: p = Process(i) if p.name() == 'java': cmd = ' '.join(p.cmdline()) if re.search(pname, cmd): result.append(0) else: result.append(1) except Exception, e: continue
def main(): connections = psutil.net_connections() listen = list(filter(lambda x: x.status == 'LISTEN', connections)) listen_sorted = sorted(listen, key=lambda x: x.laddr.port) print(f"{'ip':<12} {'port':<6} {'user':<16} {'started':<15} {'parent':<15} {'cwd':<30} {'cmdline'}".upper()) for l in listen_sorted: ip = l.laddr.ip port = l.laddr.port if not l.pid: cwd, user, cmdline, started = 4 * ["NOPERM"] else: p = Process(l.pid) parent = p.parent().name() cwd = p.cwd() user = p.username() cmdline = " ".join(p.cmdline()) started = arrow.get(p.create_time()).humanize() print(f"{ip:<12} {port:<6} {user:<16} {started:<15} {parent:<15} {cwd:<30} {cmdline}")
def __init__(self, proc: psutil.Process, proctable): """ Class constructor """ _dead = False self._children = list() self._parent = 0 parent = None self._proc, self._pt = proc, proctable try: self._pgid = os.getpgid(proc.pid) except: self._pgid = 0 _dead = True if not _dead: parent = proc.parent() if parent: self._parent = parent.pid if not _dead: self._children = [ p.pid for p in proc.children() ] with proc.oneshot(): if proc.is_running(): self.rss = proc.memory_info().rss self.vms = proc.memory_info().vms self.ctx_vol = proc.num_ctx_switches().voluntary self.ctx_invol = proc.num_ctx_switches().involuntary self._cmdline = proc.cmdline() self.pcpu = None #self.pcpu += proc.cpu_percent(interval=DEFAULT_INTERVAL) else: self.rss = 0 self.vms = 0 self.ctx_vol = 0 self.ctx_invol = 0 self.cmdline = [ ] self.pcpu = 0.0
def findProcess(process, command="", isPid=False): from psutil import pids, Process if isinstance(process, int): if process in pids(): return True else: for pid in pids(): try: p = Process(pid) if process in p.name(): for arg in p.cmdline(): if command in str(arg): return True if not isPid else str(pid) else: pass else: pass except: continue
def is_multimc(p: Process) -> bool: name, cmdline = "", "" try: name = p.name() cmdline = p.cmdline() except (PermissionError, psutil.AccessDenied, ProcessLookupError, psutil.NoSuchProcess) as e: # Windows can do this if isinstance(e, PermissionError) or isinstance(e, psutil.AccessDenied): print("Not allowed to view process {}".format(p.pid)) if isinstance(e, ProcessLookupError) or isinstance(e, psutil.NoSuchProcess): print("Process {} does not exist. Race condition?".format(p.pid)) pass # print(name, cmdline) # if 'MultiMC' in name: if 'multimc' in name.lower() or 'multimc' in ' '.join(cmdline).lower(): print(p) return True return False
def _is_same_cmd(self, p: psutil.Process, second_cmd: List): first_cmdline = [ c for c in p.cmdline() if (c != "--gapplication-service" and not c.startswith('--pid=')) ] second_cmd = [ c for c in second_cmd if (c != "--gapplication-service" and not c.startswith('--pid=')) ] if len(first_cmdline) <= 0 or len(second_cmd) <= 0: return first_one_is_snap_app, first_snap_app_name = snapd_workaround.Snapd.is_snap_app( first_cmdline[0]) if first_one_is_snap_app: second_one_also_is_snap_app, second_snap_app_name = snapd_workaround.Snapd.is_snap_app( second_cmd[0]) if second_one_also_is_snap_app: return first_snap_app_name == second_snap_app_name return first_cmdline == second_cmd
def get_procinfo_by_address(ip_src, ip_dst, port_src=None, port_dst=None): """ Gets Infos about the Prozess associated with the given address information Both port_src and port_dst must be not None or None at the same time. return -- [pid, "/path/to/command", "user", "hash"] or [] """ result = [] if port_src is not None: pids = [ c.pid for c in net_connections() if len(c.raddr) != 0 and c.pid is not None and ( c.laddr[0], c.raddr[0], c.laddr[1], c.raddr[1]) == (ip_src, ip_dst, port_src, port_dst) ] else: pids = [ c.pid for c in net_connections() if len(c.raddr) != 0 and c.pid is not None and ( c.laddr[0], c.raddr[0]) == (ip_src, ip_dst) ] try: if len(pids) > 1: logger.warning("more than 1 matching process: %r", pids) proc = Process(pids[0]) cmd = [pids[0], proc.cmdline(), proc.username()] hash_input = "%d%s%s" % (cmd[0], cmd[1], cmd[2]) procinfo_hash = hashlib.sha256(hash_input.encode("UTF-8")) cmd.append(procinfo_hash) logger.debug("process info: %r", cmd) return cmd except IndexError: pass return []
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 query_process(process=None): if not isinstance(process, Process): process = Process(process) cmd_line = u" ".join((u'"%s"' % x.replace('"', r'\"') if " " in x else x.replace('"', r'\"') \ for x in process.cmdline())) return {"pid": process.pid, "cmd_line": cmd_line, "owner": process.uids()[0]}
def __init__(self, p: psutil.Process): self.pid = p.pid self.name = p.name() self.cmdline = p.cmdline() self.create_time = p.create_time()
def is_ardupilot_process(process: psutil.Process) -> bool: """Checks if given process is using Ardupilot's firmware file for current platform.""" return str(self.current_firmware_path()) in " ".join( process.cmdline())
def show_process_pid_name(): '''Show a list of process PID and names.''' process_ids = pids() for pid in process_ids: p = Process(pid) print("{} - {} {}".format(p.pid, p.name(), p.cmdline()))
def _matches(self, process: psutil.Process): try: cmd_lines = process.cmdline() return re.search(self.proc_regex, ' '.join(cmd_lines)) is not None except psutil.AccessDenied: return False
def _get_win_pid(): from psutil import pids, Process for pid in pids(): proc = Process(pid) if proc.name() == f'{CMD_NAME}.exe' and "run" in proc.cmdline(): return pid