def get_resource_usage(self) -> ResourceUsage: """ Return various resource usage statistics about the running process. """ assert not self.returncode, "Can't collect data on stopped process" proc = Process(self.ps.pid) if 'bitcoind' in self.cmd: def find_process(proc_): """ Process graph looks like this: sh(327)───time(334)───bitcoind(335) """ name = proc_.name() # Recurse into child processes if need be. if name in ['sh', 'time']: assert len(proc_.children()) == 1 return find_process(proc_.children()[0]) assert (name.startswith('bitcoin') or name.startswith('b-')) return proc_ proc = find_process(proc) with proc.oneshot(): return ResourceUsage( cpu_percent=proc.cpu_percent(), memory_info=proc.memory_info(), num_fds=proc.num_fds(), )
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