def main(): users = psutil.get_users() for user in users: print_("%-15s %-15s %s (%s)" % (user.name, user.terminal or '-', datetime.fromtimestamp( user.started).strftime("%Y-%m-%d %H:%M"), user.host))
def main(): print_("Network Information\n------") # bytes_sent= psutil.network_io_counters().bytes_sent # print (bytes2human(bytes_sent)) # check() value = network_all() print value
def main(): print_('Network Information\n------') #bytes_sent= psutil.network_io_counters().bytes_sent #print (bytes2human(bytes_sent)) # check() value = network_all() print value
def main(): users = psutil.get_users() for user in users: print_("%-15s %-15s %s (%s)" % ( user.name, user.terminal or '-', datetime.fromtimestamp(user.started).strftime("%Y-%m-%d %H:%M"), user.host))
def main(): templ = "%-17s %8s %8s %8s %5s%% %9s %s" print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) for part in psutil.disk_partitions(all=False): usage = psutil.disk_usage(part.mountpoint) print_(templ % (part.device, bytes2human( usage.total), bytes2human(usage.used), bytes2human( usage.free), int(usage.percent), part.fstype, part.mountpoint))
def test(): """List info of all currently running processes emulating ps aux output. """ import datetime from psutil._compat import print_ today_day = datetime.date.today() templ = "%-10s %5s %4s %4s %7s %7s %-13s %5s %7s %s" attrs = [ 'pid', 'get_cpu_percent', 'get_memory_percent', 'name', 'get_cpu_times', 'create_time', 'get_memory_info' ] if os.name == 'posix': attrs.append('uids') attrs.append('terminal') print_(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "START", "TIME", "COMMAND")) for p in sorted(process_iter(), key=lambda p: p.pid): try: pinfo = p.as_dict(attrs, ad_value='') except NoSuchProcess: pass else: if pinfo['create_time']: ctime = datetime.datetime.fromtimestamp(pinfo['create_time']) if ctime.date() == today_day: ctime = ctime.strftime("%H:%M") else: ctime = ctime.strftime("%b%d") else: ctime = '' cputime = time.strftime("%M:%S", time.localtime(sum(pinfo['cpu_times']))) try: user = p.username except KeyError: if os.name == 'posix': if pinfo['uids']: user = str(pinfo['uids'].real) else: user = '' else: raise except Error: user = '' if os.name == 'nt' and '\\' in user: user = user.split('\\')[1] vms = pinfo['memory_info'] and \ int(pinfo['memory_info'].vms / 1024) or '?' rss = pinfo['memory_info'] and \ int(pinfo['memory_info'].rss / 1024) or '?' memp = pinfo['memory_percent'] and \ round(pinfo['memory_percent'], 1) or '?' print_(templ % (user[:10], pinfo['pid'], pinfo['cpu_percent'], memp, vms, rss, pinfo.get('terminal', '') or '?', ctime, cputime, pinfo['name'].strip() or '?'))
def main(): today_day = datetime.date.today() templ = "%-10s %5s %4s %4s %7s %7s %-13s %5s %7s %s" attrs = ['pid', 'cpu_percent', 'memory_percent', 'name', 'cpu_times', 'create_time', 'memory_info'] if os.name == 'posix': attrs.append('uids') attrs.append('terminal') print_(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "START", "TIME", "COMMAND")) for p in psutil.process_iter(): try: pinfo = p.as_dict(attrs, ad_value='') except psutil.NoSuchProcess: pass else: if pinfo['create_time']: ctime = datetime.datetime.fromtimestamp(pinfo['create_time']) if ctime.date() == today_day: ctime = ctime.strftime("%H:%M") else: ctime = ctime.strftime("%b%d") else: ctime = '' cputime = time.strftime("%M:%S", time.localtime(sum(pinfo['cpu_times']))) try: user = p.username() except KeyError: if os.name == 'posix': if pinfo['uids']: user = str(pinfo['uids'].real) else: user = '' else: raise except psutil.Error: user = '' if os.name == 'nt' and '\\' in user: user = user.split('\\')[1] vms = pinfo['memory_info'] and \ int(pinfo['memory_info'].vms / 1024) or '?' rss = pinfo['memory_info'] and \ int(pinfo['memory_info'].rss / 1024) or '?' memp = pinfo['memory_percent'] and \ round(pinfo['memory_percent'], 1) or '?' print_(templ % (user[:10], pinfo['pid'], pinfo['cpu_percent'], memp, vms, rss, pinfo.get('terminal', '') or '?', ctime, cputime, pinfo['name'].strip() or '?'))
def main(): templ = "%-17s %8s %8s %8s %5s%% %9s %s" print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) for part in psutil.disk_partitions(all=False): usage = psutil.disk_usage(part.mountpoint) print_(templ % (part.device, bytes2human(usage.total), bytes2human(usage.used), bytes2human(usage.free), int(usage.percent), part.fstype, part.mountpoint))
def main(): if len(sys.argv) != 2: sys.exit("usage: pmap pid") p = psutil.Process(int(sys.argv[1])) print_("pid=%s, name=%s" % (p.pid, p.name)) templ = "%-16s %10s %-7s %s" print_(templ % ("Address", "RSS", "Mode", "Mapping")) total_rss = 0 for m in p.get_memory_maps(grouped=False): total_rss += m.rss print_(templ % (m.addr.split("-")[0].zfill(16), str(m.rss / 1024) + "K", m.perms, m.path)) print_("-" * 33) print_(templ % ("Total", str(total_rss / 1024) + "K", "", ""))
def main(): if len(sys.argv) != 2: sys.exit('usage: pmap <pid>') p = psutil.Process(int(sys.argv[1])) print_("pid=%s, name=%s" % (p.pid, p.name())) templ = "%-16s %10s %-7s %s" print_(templ % ("Address", "RSS", "Mode", "Mapping")) total_rss = 0 for m in p.memory_maps(grouped=False): total_rss += m.rss print_(templ % (m.addr.split('-')[0].zfill(16), str(m.rss / 1024) + 'K', m.perms, m.path)) print_("-" * 33) print_(templ % ("Total", str(total_rss / 1024) + 'K', '', ''))
def test(): """List info of all currently running processes emulating ps aux output. """ import datetime from psutil._compat import print_ today_day = datetime.date.today() templ = "%-10s %5s %4s %4s %7s %7s %-13s %5s %7s %s" attrs = ['pid', 'username', 'get_cpu_percent', 'get_memory_percent', 'name', 'get_cpu_times', 'create_time', 'get_memory_info'] if os.name == 'posix': attrs.append('terminal') print_(templ % ("USER", "PID", "%CPU", "%MEM", "VSZ", "RSS", "TTY", "START", "TIME", "COMMAND")) for p in sorted(process_iter(), key=lambda p: p.pid): try: pinfo = p.as_dict(attrs, ad_value='') except NoSuchProcess: pass else: if pinfo['create_time']: ctime = datetime.datetime.fromtimestamp(pinfo['create_time']) if ctime.date() == today_day: ctime = ctime.strftime("%H:%M") else: ctime = ctime.strftime("%b%d") else: ctime = '' cputime = time.strftime("%M:%S", time.localtime(sum(pinfo['cpu_times']))) user = pinfo['username'] if os.name == 'nt' and '\\' in user: user = user.split('\\')[1] vms = pinfo['memory_info'] and \ int(pinfo['memory_info'].vms / 1024) or '?' rss = pinfo['memory_info'] and \ int(pinfo['memory_info'].rss / 1024) or '?' memp = pinfo['memory_percent'] and \ round(pinfo['memory_percent'], 1) or '?' print_(templ % (user[:10], pinfo['pid'], pinfo['cpu_percent'], memp, vms, rss, pinfo.get('terminal', '') or '?', ctime, cputime, pinfo['name'].strip() or '?'))
def main(): templ = "%-17s %8s %8s %8s %5s%% %9s %s" print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) for part in psutil.disk_partitions(all=False): if os.name == 'nt': if 'cdrom' in part.opts or part.fstype == '': # skip cd-rom drives with no disk in it; they may raise # ENOENT, pop-up a Windows GUI error for a non-ready # partition or just hang. continue usage = psutil.disk_usage(part.mountpoint) print_(templ % (part.device, bytes2human( usage.total), bytes2human(usage.used), bytes2human( usage.free), int(usage.percent), part.fstype, part.mountpoint))
def main(): templ = "%-17s %8s %8s %8s %5s%% %9s %s" print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) for part in psutil.disk_partitions(all=False): if os.name == 'nt' and 'cdrom' in part.opts: # may raise ENOENT if there's no cd-rom in the drive continue usage = psutil.disk_usage(part.mountpoint) print_(templ % (part.device, bytes2human(usage.total), bytes2human(usage.used), bytes2human(usage.free), int(usage.percent), part.fstype, part.mountpoint))
def main(): templ = "%-17s %8s %8s %8s %5s%% %9s %s" print_(templ % ("Device", "Total", "Used", "Free", "Use ", "Type", "Mount")) for part in psutil.disk_partitions(all=False): if os.name == 'nt': if 'cdrom' in part.opts or part.fstype == '': # skip cd-rom drives with no disk in it; they may raise # ENOENT, pop-up a Windows GUI error for a non-ready # partition or just hang. continue usage = psutil.disk_usage(part.mountpoint) print_(templ % (part.device, bytes2human(usage.total), bytes2human(usage.used), bytes2human(usage.free), int(usage.percent), part.fstype, part.mountpoint))
def main(): templ = "%-5s %-22s %-22s %-13s %-6s %s" print_(templ % ( "Proto", "Local addr", "Remote addr", "Status", "PID", "Program name")) for p in psutil.process_iter(): name = '?' try: name = p.name() cons = p.connections(kind='inet') except psutil.AccessDenied: print_(templ % (AD, AD, AD, AD, p.pid, name)) except psutil.NoSuchProcess: continue else: for c in cons: raddr = "" laddr = "%s:%s" % (c.laddr) if c.raddr: raddr = "%s:%s" % (c.raddr) print_(templ % ( proto_map[(c.family, c.type)], laddr, raddr, c.status, p.pid, name[:15]))
def main(): templ = "%-5s %-22s %-22s %-13s %-6s %s" print_(templ % ( "Proto", "Local addr", "Remote addr", "Status", "PID", "Program name")) for p in psutil.process_iter(): name = '?' try: name = p.name cons = p.connections(kind='inet') except psutil.AccessDenied: print_(templ % (AD, AD, AD, AD, p.pid, name)) except psutil.NoSuchProcess: continue else: for c in cons: raddr = "" laddr = "%s:%s" % (c.laddr) if c.raddr: raddr = "%s:%s" % (c.raddr) print_(templ % ( proto_map[(c.family, c.type)], laddr, raddr, c.status, p.pid, name[:15]))
def main(): templ = "%-5s %-30s %-30s %-13s %-6s %s" print_(templ % ("Proto", "Local address", "Remote address", "Status", "PID", "Program name")) proc_names = {} for p in psutil.process_iter(): try: proc_names[p.pid] = p.name() except psutil.Error: pass for c in psutil.net_connections(kind='inet'): laddr = "%s:%s" % (c.laddr) raddr = "" if c.raddr: raddr = "%s:%s" % (c.raddr) print_(templ % ( proto_map[(c.family, c.type)], laddr, raddr or AD, c.status, c.pid or AD, proc_names.get(c.pid, '?')[:15], ))
def main(): templ = "%-5s %-30s %-30s %-13s %-6s %s" print_(templ % ( "Proto", "Local address", "Remote address", "Status", "PID", "Program name")) proc_names = {} for p in psutil.process_iter(): try: proc_names[p.pid] = p.name() except psutil.Error: pass for c in psutil.net_connections(kind='inet'): laddr = "%s:%s" % (c.laddr) raddr = "" if c.raddr: raddr = "%s:%s" % (c.raddr) print_(templ % ( proto_map[(c.family, c.type)], laddr, raddr or AD, c.status, c.pid or AD, proc_names.get(c.pid, '?')[:15], ))
def main(): virt = psutil.virtual_memory() swap = psutil.swap_memory() templ = "%-7s %10s %10s %10s %10s %10s %10s" print_(templ % ('', 'total', 'used', 'free', 'shared', 'buffers', 'cache')) print_(templ % ('Mem:', int(virt.total / 1024), int(virt.used / 1024), int(virt.free / 1024), int(getattr(virt, 'shared', 0) / 1024), int(getattr(virt, 'buffers', 0) / 1024), int(getattr(virt, 'cached', 0) / 1024))) print_(templ % ('Swap:', int(swap.total / 1024), int( swap.used / 1024), int(swap.free / 1024), '', '', ''))
def main(): virt = psutil.virtual_memory() swap = psutil.swap_memory() templ = "%-7s %10s %10s %10s %10s %10s %10s" print_(templ % ('', 'total', 'used', 'free', 'shared', 'buffers', 'cache')) print_(templ % ('Mem:', int(virt.total / 1024), int(virt.used / 1024), int(virt.free / 1024), int(getattr(virt, 'shared', 0) / 1024), int(getattr(virt, 'buffers', 0) / 1024), int(getattr(virt, 'cached', 0) / 1024))) print_(templ % ('Swap:', int(swap.total / 1024), int(swap.used / 1024), int(swap.free / 1024), '', '', ''))
def main(): templ = "%-5s %-22s %-22s %-13s %-6s %s" print_(templ % ("Proto", "Local addr", "Remote addr", "Status", "PID", "Program name")) for p in psutil.process_iter(): name = "?" try: name = p.name cons = p.get_connections(kind="inet") except psutil.AccessDenied: print_(templ % (AD, AD, AD, AD, p.pid, name)) else: for c in cons: raddr = "" laddr = "%s:%s" % (c.local_address) if c.remote_address: raddr = "%s:%s" % (c.remote_address) print_(templ % (proto_map[(c.family, c.type)], laddr, raddr, str(c.status), p.pid, name[:15]))
def main(): print_('MEMORY\n------') pprint_ntuple(psutil.virtual_memory()) print_('\nSWAP\n----') pprint_ntuple(psutil.swap_memory())
def pprint_ntuple(nt): for name in nt._fields: value = getattr(nt, name) if name != 'percent': value = bytes2human(value) print_('%-10s : %7s' % (name.capitalize(), value))