linux系統下 python擷取 記憶體、cpu、負載、網路流量、硬碟等資訊

來源:互聯網
上載者:User

記憶體資訊 / meminfo

 返回dict

#!/usr/bin/env pythondef memory_stat():    mem = {}    f = open("/proc/meminfo")    lines = f.readlines()    f.close()    for line in lines:        if len(line) < 2: continue        name = line.split(':')[0]        var = line.split(':')[1].split()[0]        mem[name] = long(var) * 1024.0    mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']    return mem

CPU資訊 / cpuinfo
返回list,每核心一dict

#!/usr/bin/env pythondef cpu_stat():    cpu = []    cpuinfo = {}    f = open("/proc/cpuinfo")    lines = f.readlines()    f.close()    for line in lines:        if line == '\n':            cpu.append(cpuinfo)            cpuinfo = {}        if len(line) < 2: continue        name = line.split(':')[0].rstrip()        var = line.split(':')[1]        cpuinfo[name] = var    return cpu

負載資訊 / loadavg
返回dict

#!/usr/bin/env pythondef load_stat():    loadavg = {}    f = open("/proc/loadavg")    con = f.read().split()    f.close()    loadavg['lavg_1']=con[0]    loadavg['lavg_5']=con[1]    loadavg['lavg_15']=con[2]    loadavg['nr']=con[3]    loadavg['last_pid']=con[4]    return loadavg

運轉時間 / Uptime
返回dict

#!/usr/bin/env pythondef uptime_stat():    uptime = {}    f = open("/proc/uptime")    con = f.read().split()    f.close()    all_sec = float(con[0])    MINUTE,HOUR,DAY = 60,3600,86400    uptime['day'] = int(all_sec / DAY )    uptime['hour'] = int((all_sec % DAY) / HOUR)    uptime['minute'] = int((all_sec % HOUR) / MINUTE)    uptime['second'] = int(all_sec % MINUTE)    uptime['Free rate'] = float(con[1]) / float(con[0])    return uptime

擷取網卡流量資訊 /proc/net/dev
返回dict,單位byte

#!/usr/bin/env pythondef net_stat():    net = []    f = open("/proc/net/dev")    lines = f.readlines()    f.close()    for line in lines[2:]:        con = line.split()        """        intf = {}        intf['interface'] = con[0].lstrip(":")        intf['ReceiveBytes'] = int(con[1])        intf['ReceivePackets'] = int(con[2])        intf['ReceiveErrs'] = int(con[3])        intf['ReceiveDrop'] = int(con[4])        intf['ReceiveFifo'] = int(con[5])        intf['ReceiveFrames'] = int(con[6])        intf['ReceiveCompressed'] = int(con[7])        intf['ReceiveMulticast'] = int(con[8])        intf['TransmitBytes'] = int(con[9])        intf['TransmitPackets'] = int(con[10])        intf['TransmitErrs'] = int(con[11])        intf['TransmitDrop'] = int(con[12])        intf['TransmitFifo'] = int(con[13])        intf['TransmitFrames'] = int(con[14])        intf['TransmitCompressed'] = int(con[15])        intf['TransmitMulticast'] = int(con[16])        """        intf = dict(            zip(                ( 'interface','ReceiveBytes','ReceivePackets',                  'ReceiveErrs','ReceiveDrop','ReceiveFifo',                  'ReceiveFrames','ReceiveCompressed','ReceiveMulticast',                  'TransmitBytes','TransmitPackets','TransmitErrs',                  'TransmitDrop', 'TransmitFifo','TransmitFrames',                  'TransmitCompressed','TransmitMulticast' ),                ( con[0].rstrip(":"),int(con[1]),int(con[2]),                  int(con[3]),int(con[4]),int(con[5]),                  int(con[6]),int(con[7]),int(con[8]),                  int(con[9]),int(con[10]),int(con[11]),                  int(con[12]),int(con[13]),int(con[14]),                  int(con[15]),int(con[16]), )            )        )        net.append(intf)    return net

磁碟空間使用
使用內建python內建函數,返回dict,單位byte

#!/usr/bin/env pythondef disk_stat():    import os    hd={}    disk = os.statvfs("/")    hd['available'] = disk.f_bsize * disk.f_bavail    hd['capacity'] = disk.f_bsize * disk.f_blocks    hd['used'] = disk.f_bsize * disk.f_bfree    return hd

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.