• CPU、内存、硬盘分区的检测.py


    cpu_mem_directories.py   CPU、内存、硬盘分区的检测

    #!/usr/bin/env python
    #coding:utf-8
    import psutil
    import time
    import sys
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-t", "--time", dest="time",
                      help="此参数可查看当前下载占的带宽,-t是测试时间", metavar="10")
    parser.add_option("-d", "--deamon",action="store_false", dest="deamon", default=True,
                      help="后台运行此脚本")
    
    def Sysinfo():
        Boot_Start = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(psutil.boot_time()))
        time.sleep(0.5)
        Cpu_usage = psutil.cpu_percent()
        RAM = int(psutil.virtual_memory().total/(1027*1024))
        RAM_percent = psutil.virtual_memory().percent
        Swap = int(psutil.swap_memory().total/(1027*1024))
        Swap_percent = psutil.swap_memory().percent
        Net_sent = psutil.net_io_counters().bytes_sent
        Net_recv = psutil.net_io_counters().bytes_recv
        Net_spkg = psutil.net_io_counters().packets_sent
        Net_rpkg = psutil.net_io_counters().packets_recv
        BFH = r'%'
        print " 33[1;32m开机时间:%s33[1;m"  % Boot_Start
        print " 33[1;32m当前CPU使用率:%s%s33[1;m" % (Cpu_usage,BFH)
        print " 33[1;32m物理内存:%dM	使用率:%s%s33[1;m" % (RAM,RAM_percent,BFH)
        print "33[1;32mSwap内存:%dM	使用率:%s%s33[1;m" % (Swap,Swap_percent,BFH)
        print " 33[1;32m发送:%d Byte	发送包数:%d个33[1;m" % (Net_sent,Net_spkg)
        print " 33[1;32m接收:%d Byte	接收包数:%d个33[1;m" % (Net_recv,Net_rpkg)
    
        for i in psutil.disk_partitions():
            print " 33[1;32m盘符: %s 挂载点: %s 使用率: %s%s33[1;m" % (i[0],i[1],psutil.disk_usage(i[1])[3],BFH)
    def Net_io(s):
        x = 0
        sum = 0
        while True:
            if x >= s:
                break
            r1 = psutil.net_io_counters().bytes_recv
            time.sleep(1)
            r2 = psutil.net_io_counters().bytes_recv
            y = r2 - r1
            print "%.2f Kb/s" % (y / 1024.0)
            sum += y
            x += 1
        result = sum / x
        print "33[1;32m%s秒内平均速度:%.2f Kb/s 33[1;m"  % (x,result / 1024.0)
    if __name__ == "__main__":
        (options, args) = parser.parse_args()
        if options.time:
            Net_io(int(options.time))
        else:
            Sysinfo()
  • 相关阅读:
    关于冲动 CQ
    关于在虚拟机上使用boost库 CQ
    新项目奖金分配方案 CQ
    关于桌子 CQ
    linux 添加本地yum源
    linux samba 服务器 简单配置
    linux 修改密码
    linux ntp 服务器和用户端
    linux 修改ip 地址
    Union 和 Union All 的区别
  • 原文地址:https://www.cnblogs.com/liuyansheng/p/6281859.html
Copyright © 2020-2023  润新知