zoukankan      html  css  js  c++  java
  • linux获取内存、cpu、负载、网口流量、磁盘信息

    内存信息 / meminfo

     返回dict

    1.  
      #!/usr/bin/env python
    2.  
      def memory_stat():
    3.  
      mem = {}
    4.  
      f = open("/proc/meminfo")
    5.  
      lines = f.readlines()
    6.  
      f.close()
    7.  
      for line in lines:
    8.  
      if len(line) < 2: continue
    9.  
      name = line.split(':')[0]
    10.  
      var = line.split(':')[1].split()[0]
    11.  
      mem[name] = long(var) * 1024.0
    12.  
      mem['MemUsed'] = mem['MemTotal'] - mem['MemFree'] - mem['Buffers'] - mem['Cached']
    13.  
      return mem


     

    CPU信息 / cpuinfo
    返回list,每核心一dict

    1.  
      #!/usr/bin/env python
    2.  
      def cpu_stat():
    3.  
      cpu = []
    4.  
      cpuinfo = {}
    5.  
      f = open("/proc/cpuinfo")
    6.  
      lines = f.readlines()
    7.  
      f.close()
    8.  
      for line in lines:
    9.  
      if line == ' ':
    10.  
      cpu.append(cpuinfo)
    11.  
      cpuinfo = {}
    12.  
      if len(line) < 2: continue
    13.  
      name = line.split(':')[0].rstrip()
    14.  
      var = line.split(':')[1]
    15.  
      cpuinfo[name] = var
    16.  
      return cpu


    负载信息 / loadavg
    返回dict

    1.  
      #!/usr/bin/env python
    2.  
      def load_stat():
    3.  
      loadavg = {}
    4.  
      f = open("/proc/loadavg")
    5.  
      con = f.read().split()
    6.  
      f.close()
    7.  
      loadavg['lavg_1']=con[0]
    8.  
      loadavg['lavg_5']=con[1]
    9.  
      loadavg['lavg_15']=con[2]
    10.  
      loadavg['nr']=con[3]
    11.  
      loadavg['last_pid']=con[4]
    12.  
      return loadavg


    运转时间 / Uptime
    返回dict

    1.  
      #!/usr/bin/env python
    2.  
      def uptime_stat():
    3.  
      uptime = {}
    4.  
      f = open("/proc/uptime")
    5.  
      con = f.read().split()
    6.  
      f.close()
    7.  
      all_sec = float(con[0])
    8.  
      MINUTE,HOUR,DAY = 60,3600,86400
    9.  
      uptime['day'] = int(all_sec / DAY )
    10.  
      uptime['hour'] = int((all_sec % DAY) / HOUR)
    11.  
      uptime['minute'] = int((all_sec % HOUR) / MINUTE)
    12.  
      uptime['second'] = int(all_sec % MINUTE)
    13.  
      uptime['Free rate'] = float(con[1]) / float(con[0])
    14.  
      return uptime


    获取网卡流量信息 /proc/net/dev
    返回dict,单位byte

    1.  
      #!/usr/bin/env python
    2.  
      def net_stat():
    3.  
      net = []
    4.  
      f = open("/proc/net/dev")
    5.  
      lines = f.readlines()
    6.  
      f.close()
    7.  
      for line in lines[2:]:
    8.  
      con = line.split()
    9.  
      """
    10.  
      intf = {}
    11.  
      intf['interface'] = con[0].lstrip(":")
    12.  
      intf['ReceiveBytes'] = int(con[1])
    13.  
      intf['ReceivePackets'] = int(con[2])
    14.  
      intf['ReceiveErrs'] = int(con[3])
    15.  
      intf['ReceiveDrop'] = int(con[4])
    16.  
      intf['ReceiveFifo'] = int(con[5])
    17.  
      intf['ReceiveFrames'] = int(con[6])
    18.  
      intf['ReceiveCompressed'] = int(con[7])
    19.  
      intf['ReceiveMulticast'] = int(con[8])
    20.  
      intf['TransmitBytes'] = int(con[9])
    21.  
      intf['TransmitPackets'] = int(con[10])
    22.  
      intf['TransmitErrs'] = int(con[11])
    23.  
      intf['TransmitDrop'] = int(con[12])
    24.  
      intf['TransmitFifo'] = int(con[13])
    25.  
      intf['TransmitFrames'] = int(con[14])
    26.  
      intf['TransmitCompressed'] = int(con[15])
    27.  
      intf['TransmitMulticast'] = int(con[16])
    28.  
      """
    29.  
      intf = dict(
    30.  
      zip(
    31.  
      ( 'interface','ReceiveBytes','ReceivePackets',
    32.  
      'ReceiveErrs','ReceiveDrop','ReceiveFifo',
    33.  
      'ReceiveFrames','ReceiveCompressed','ReceiveMulticast',
    34.  
      'TransmitBytes','TransmitPackets','TransmitErrs',
    35.  
      'TransmitDrop', 'TransmitFifo','TransmitFrames',
    36.  
      'TransmitCompressed','TransmitMulticast' ),
    37.  
      ( con[0].rstrip(":"),int(con[1]),int(con[2]),
    38.  
      int(con[3]),int(con[4]),int(con[5]),
    39.  
      int(con[6]),int(con[7]),int(con[8]),
    40.  
      int(con[9]),int(con[10]),int(con[11]),
    41.  
      int(con[12]),int(con[13]),int(con[14]),
    42.  
      int(con[15]),int(con[16]), )
    43.  
      )
    44.  
      )
    45.  
       
    46.  
      net.append(intf)
    47.  
      return net


    磁盘空间使用
    使用内置python内置函数,返回dict,单位byte

    1.  
      #!/usr/bin/env python
    2.  
      def disk_stat():
    3.  
      import os
    4.  
      hd={}
    5.  
      disk = os.statvfs("/")
    6.  
      hd['available'] = disk.f_bsize * disk.f_bavail
    7.  
      hd['capacity'] = disk.f_bsize * disk.f_blocks
    8.  
      hd['used'] = disk.f_bsize * disk.f_bfree
    9.  
      return hd


     

  • 相关阅读:
    The Future of Middleware and the BizTalk Roadmap
    FW: How to spawn a process that runs under the context of the impersonated user in Microsoft ASP.NET pages
    Strips illegal Xml characters
    luogu P2280 激光炸弹(二维前缀和)
    luogu P2704 炮兵阵地(经典状态压缩DP)
    SP1716 GSS3 Can you answer these queries III (线段树维护最大连续子段和)
    二分图判定、匹配问题
    C++语法综合 | 基于char*设计一个字符串类MyString
    luogu P1044 火车进出栈问题(Catalan数)
    C++设计模式 | 三种设计模式基础
  • 原文地址:https://www.cnblogs.com/ruiy/p/9802727.html
Copyright © 2011-2022 走看看