llnnmc
12/8/2019 - 1:47 PM

diskinfo.py

#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
Show disk information.

$ python diskinfo.py
磁盘分区
-------
Device             Total     Used     Free  Use %     Type    Opts            Mount
C:\               118.5G    61.5G    56.9G    52%     NTFS    rw,fixed        C:\
D:\               223.6G   143.9G    79.7G    64%     NTFS    rw,fixed        D:\
E:\               431.5G   182.6G   248.9G    42%     NTFS    rw,fixed        E:\
F:\               500.0G   117.0G   383.0G    23%     NTFS    rw,fixed        F:\
G:\               100.0M    14.1M    85.8M    14%     NTFS    rw,fixed        G:\

磁盘I/O
-------
PhysicalDrive#    read_count  write_count   read_bytes  write_bytes    read_time   write_time
PhysicalDrive0         34653        98722         1.1G         1.6G          325          289
PhysicalDrive1        705355       579242         8.5G        17.6G          336          680
PhysicalDrive2       1650614      1428164        39.2G        21.7G         2700         1942

"""

import psutil
import os

def bytes2human(n):
    # http://code.activestate.com/recipes/578019
    # >>> bytes2human(10000)
    # '9.8K'
    # >>> bytes2human(100001221)
    # '95.4M'
    symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.1f%s' % (value, s)
    return "%sB" % n

def main():
    #磁盘分区信息
    print('磁盘分区')
    print('-------')
    temp = '%-15s %8s %8s %8s %5s%% %8s    %-15s %s'
    print(temp % ('Device', 'Total', 'Used', 'Free', 'Use ', 'Type', 'Opts', '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(temp % (
            part.device,
            bytes2human(usage.total),
            bytes2human(usage.used),
            bytes2human(usage.free),
            int(usage.percent),
            part.fstype,
            part.opts,
            part.mountpoint))
    print()

    #磁盘I/O信息
    print('磁盘I/O')
    print('-------')
    temp = "%-15s %12s %12s %12s %12s %12s %12s"
    print(temp % ('PhysicalDrive#', 'read_count', 'write_count', 'read_bytes', 'write_bytes', 'read_time', 'write_time'))
    for k in psutil.disk_io_counters(perdisk = True):
        print(temp % (
            k,
            psutil.disk_io_counters(perdisk = True)[k].read_count,
            psutil.disk_io_counters(perdisk = True)[k].write_count,
            bytes2human(psutil.disk_io_counters(perdisk = True)[k].read_bytes),
            bytes2human(psutil.disk_io_counters(perdisk = True)[k].write_bytes),
            psutil.disk_io_counters(perdisk = True)[k].read_time,
            psutil.disk_io_counters(perdisk = True)[k].write_time))

if __name__ == '__main__':
    main()