示例#1
0
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))
示例#2
0
def main():
    print_("Network Information\n------")
    # bytes_sent= psutil.network_io_counters().bytes_sent
    # print (bytes2human(bytes_sent))

    # check()
    value = network_all()
    print value
示例#3
0
def main():
    print_('Network Information\n------')
    #bytes_sent= psutil.network_io_counters().bytes_sent
    #print (bytes2human(bytes_sent))

   # check()
    value = network_all()
    print value
示例#4
0
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))
示例#5
0
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))
示例#6
0
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 '?'))
示例#7
0
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 '?'))
示例#8
0
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))
示例#9
0
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", "", ""))
示例#10
0
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', '', ''))
示例#11
0
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 '?'))
示例#12
0
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))
示例#13
0
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))
示例#14
0
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))
示例#15
0
文件: netstat.py 项目: ztop/psutil
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]))
示例#16
0
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]))
示例#17
0
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],
        ))
示例#18
0
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],
        ))
示例#19
0
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), '', '', ''))
示例#20
0
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),
                             '', '', ''))
示例#21
0
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]))
示例#22
0
def main():
    print_('MEMORY\n------')
    pprint_ntuple(psutil.virtual_memory())
    print_('\nSWAP\n----')
    pprint_ntuple(psutil.swap_memory())
示例#23
0
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))