通過python查出伺服器磁碟容量

來源:互聯網
上載者:User

標籤:伺服器磁碟總和 python

#coding:utf-8
import os  
from collections import namedtuple  
 
disk_ntuple = namedtuple(‘partition‘,  ‘device mountpoint fstype‘)  
usage_ntuple = namedtuple(‘usage‘,  ‘total used free percent‘)  
#擷取當前作業系統下所有磁碟  
def disk_partitions(all=False):  
    """Return all mountd partitions as a nameduple.
    If all == False return phyisical partitions only.
    """  
    phydevs = []  
    f = open("/proc/filesystems", "r")  
    for line in f:  
        if not line.startswith("nodev"):  
            phydevs.append(line.strip())  
 
    retlist = []  
    f = open(‘/etc/mtab‘, "r")  
    for line in f:  
        if not all and line.startswith(‘none‘):  
            continue  
        fields = line.split()  
        device = fields[0]  
        mountpoint = fields[1]  
        fstype = fields[2]  
        if not all and fstype not in phydevs:  
            continue  
        if device == ‘none‘:  
            device = ‘‘  
        ntuple = disk_ntuple(device, mountpoint, fstype)  
        retlist.append(ntuple)  
    return retlist  
#統計某磁碟使用方式,返回對象  
def disk_usage(path):  
    """Return disk usage associated with path."""  
    st = os.statvfs(path)  
    free = (st.f_bavail * st.f_frsize)  
    total = (st.f_blocks * st.f_frsize)  
    used = (st.f_blocks - st.f_bfree) * st.f_frsize  
    try:  
        percent = ret = (float(used) / total) * 100  
    except ZeroDivisionError:  
        percent = 0  
    # NB: the percentage is -5% than what shown by df due to  
    # reserved blocks that we are currently not considering:  
    # http://goo.gl/sWGbH  
    return usage_ntuple(total, used, free, round(percent, 1))
print disk_partitions()

def getPath():
    """
    擷取磁碟的分區
    """
    disklist = []
    list = disk_partitions()
    for i in list:
    disklist.append(i[1])
    return disklist
#pathlist = getPath()
#print "path list is ....%s" % pathlist
#print disk_usage(‘/home‘)
def getDiskTotal():
    """
    擷取磁碟總的大小
    """
    newpathlist = []
    pathlist = []
    pathlist = getPath()
    print "pathlist type is %s ,and the pathlist value is %s" % (type(pathlist),pathlist)
    pathlist.append("/dev/shm/")
    totalDiskList = []
#    print newpathlist
    sum = 0
    for path in pathlist:
        disktotal = disk_usage(path)[0] / 1073741824 + 1
        totalDiskList.append(disktotal)
    for count in totalDiskList:
        sum += count
    print sum

getDiskTotal()

本文出自 “自動化rolin” 部落格,請務必保留此出處http://luoguoling.blog.51cto.com/1568501/1876585

通過python查出伺服器磁碟容量

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.