zoukankan      html  css  js  c++  java
  • 查看Linux服务器CPU使用率、内存使用率、磁盘空间占用率、负载情况

    [root@server script]# vi monitor.py

    #!/usr/bin/env python
    # -*- coding:utf-8 -*- 
    #Author: nulige
    
    import os, time
    
    last_worktime=0
    last_idletime=0
    
    def get_cpu():
            global last_worktime, last_idletime
            f=open("/proc/stat","r")
            line=""
            while not "cpu " in line: line=f.readline()
            f.close()
            spl=line.split(" ")
            worktime=int(spl[2])+int(spl[3])+int(spl[4])
            idletime=int(spl[5])
            dworktime=(worktime-last_worktime)
            didletime=(idletime-last_idletime)
            rate=float(dworktime)/(didletime+dworktime)
            last_worktime=worktime
            last_idletime=idletime
            if(last_worktime==0): return 0
            return rate
    
    def get_mem_usage_percent():
        try:
            f = open('/proc/meminfo', 'r')
            for line in f:
                if line.startswith('MemTotal:'):
                    mem_total = int(line.split()[1])
                elif line.startswith('MemFree:'):
                    mem_free = int(line.split()[1])
                elif line.startswith('Buffers:'):
                    mem_buffer = int(line.split()[1])
                elif line.startswith('Cached:'):
                    mem_cache = int(line.split()[1])
                elif line.startswith('SwapTotal:'):
                    vmem_total = int(line.split()[1])
                elif line.startswith('SwapFree:'):
                    vmem_free = int(line.split()[1])
                else:
                    continue
            f.close()
        except:
            return None
        physical_percent = usage_percent(mem_total - (mem_free + mem_buffer + mem_cache), mem_total)
        virtual_percent = 0
        if vmem_total > 0:
            virtual_percent = usage_percent((vmem_total - vmem_free), vmem_total)
        return physical_percent, virtual_percent
    
    def usage_percent(use, total):
        try:
            ret = (float(use) / total) * 100
        except ZeroDivisionError:
            raise Exception("ERROR - zero division error")
        return ret
    
    statvfs = os.statvfs('/')
    
    total_disk_space = statvfs.f_frsize * statvfs.f_blocks
    free_disk_space = statvfs.f_frsize * statvfs.f_bfree
    disk_usage = (total_disk_space - free_disk_space) * 100.0 / total_disk_space
    disk_usage = int(disk_usage)
    disk_tip = "硬盘空间使用率(最大100%):"+str(disk_usage)+"%"
    print(disk_tip)
    
    mem_usage = get_mem_usage_percent()
    mem_usage = int(mem_usage[0])
    mem_tip = "物理内存使用率(最大100%):"+str(mem_usage)+"%"
    print(mem_tip)
    
    cpu_usage = int(get_cpu()*100)
    cpu_tip = "CPU使用率(最大100%):"+str(cpu_usage)+"%"
    print(cpu_tip)
    
    load_average = os.getloadavg()
    load_tip = "系统负载(三个数值中有一个超过3就是高):"+str(load_average)
    print(load_tip)

    #执行结果

    [root@server script]# python monitor.py
    硬盘空间使用率(最大100%):0%
    物理内存使用率(最大100%):3%
    CPU使用率(最大100%):5%
    系统负载(三个数值中有一个超过3就是高):(0.0, 0.0, 0.0)

  • 相关阅读:
    2019 icpc南昌全国邀请赛-网络选拔赛J题 树链剖分+离线询问
    Android小项目之十二 设置中心的界面
    【Mood-5】14条建议,使你的IT职业生涯更上一层楼
    【Android 界面效果15】Android UI 之一步步教你自定义控件(自定义属性、合理设计onMeasure、合理设计onDraw等)
    单线程模型中Message、Handler、Message Queue、Looper之间的关系
    140个google面试题
    Android小项目之十一 应用程序的主界面
    Android小项目之十 应用程序更新的签名问题
    Android小项目之九 两种上下文的区别
    Android小项目之八 界面细节
  • 原文地址:https://www.cnblogs.com/nulige/p/7802197.html
Copyright © 2011-2022 走看看