通过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()

本文转自 luoguo 51CTO博客,原文链接:http://blog.51cto.com/luoguoling/1876585


相关文章
|
人工智能 JavaScript API
零基础构建MCP服务器:TypeScript/Python双语言实战指南
作为一名深耕技术领域多年的博主摘星,我深刻感受到了MCP(Model Context Protocol)协议在AI生态系统中的革命性意义。MCP作为Anthropic推出的开放标准,正在重新定义AI应用与外部系统的交互方式,它不仅解决了传统API集成的复杂性问题,更为开发者提供了一个统一、安全、高效的连接框架。在过去几个月的实践中,我发现许多开发者对MCP的概念理解透彻,但在实际动手构建MCP服务器时却遇到了各种技术壁垒。从环境配置的细节问题到SDK API的深度理解,从第一个Hello World程序的调试到生产环境的部署优化,每一个环节都可能成为初学者的绊脚石。因此,我决定撰写这篇全面的实
3055 67
零基础构建MCP服务器:TypeScript/Python双语言实战指南
|
人工智能 自然语言处理 安全
Python构建MCP服务器:从工具封装到AI集成的全流程实践
MCP协议为AI提供标准化工具调用接口,助力模型高效操作现实世界。
2037 1
|
人工智能 安全 Shell
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
Jupyter MCP服务器基于模型上下文协议(MCP),实现大型语言模型与Jupyter环境的无缝集成。它通过标准化接口,让AI模型安全访问和操作Jupyter核心组件,如内核、文件系统和终端。本文深入解析其技术架构、功能特性及部署方法。MCP服务器解决了传统AI模型缺乏实时上下文感知的问题,支持代码执行、变量状态获取、文件管理等功能,提升编程效率。同时,严格的权限控制确保了安全性。作为智能化交互工具,Jupyter MCP为动态计算环境与AI模型之间搭建了高效桥梁。
1050 2
Jupyter MCP服务器部署实战:AI模型与Python环境无缝集成教程
|
安全 Linux 网络安全
Python极速搭建局域网文件共享服务器:一行命令实现HTTPS安全传输
本文介绍如何利用Python的http.server模块,通过一行命令快速搭建支持HTTPS的安全文件下载服务器,无需第三方工具,3分钟部署,保障局域网文件共享的隐私与安全。
3633 0
|
弹性计算 运维 安全
阿里云轻量应用服务器与云服务器ECS啥区别?新手帮助教程
阿里云轻量应用服务器适合个人开发者搭建博客、测试环境等低流量场景,操作简单、成本低;ECS适用于企业级高负载业务,功能强大、灵活可扩展。二者在性能、网络、镜像及运维管理上差异显著,用户应根据实际需求选择。
1075 10
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
1056 8
|
运维 安全 Ubuntu
阿里云渠道商:服务器操作系统怎么选?
阿里云提供丰富操作系统镜像,涵盖Windows与主流Linux发行版。选型需综合技术兼容性、运维成本、安全稳定等因素。推荐Alibaba Cloud Linux、Ubuntu等用于Web与容器场景,Windows Server支撑.NET应用。建议优先选用LTS版本并进行测试验证,通过标准化镜像管理提升部署效率与一致性。
|
存储 监控 安全
阿里云渠道商:云服务器价格有什么变动?
阿里云带宽与存储费用呈基础资源降价、增值服务差异化趋势。企业应结合业务特点,通过阶梯计价、智能分层、弹性带宽等策略优化成本,借助云监控与预算预警机制,实现高效、可控的云资源管理。
|
弹性计算 运维 安全
区别及选择指南:阿里云轻量应用服务器与ECS云服务器有什么区别?
阿里云轻量应用服务器适合个人开发者、学生搭建博客、测试环境,易用且性价比高;ECS功能更强大,适合企业级应用如大数据、高流量网站。根据需求选择:轻量入门首选,ECS专业之选。
900 2

推荐镜像

更多