#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()
通过python查出服务器磁盘容量
原创
©著作权归作者所有:来自51CTO博客作者luoguo的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:django多对多关联表的操作
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
硬盘容量单位及换算
1Kb=1024b 1Mb=1024kb 1GB=1024Mb &nb
职场 休闲 硬盘 容量 单位 -
python 硬盘容量换算
计算机容量 1位 = 1bit 8bit = 1byte = 1字节 1024bytes = 1kbytes =1KB 1024个字符,小文档 ,几百k可以表示一张图片 1024KB = 1Million Bytes = 1MB = 1兆 , 几万字的文档, 大图片 1024MB = 1Gi
python 硬盘容量换算 php javascript python ViewUI