linux获取内存、cpu、负载、网口流量、磁盘信息

时间:2023-03-10 04:29:12
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 == '\n':
  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. net.append(intf)
  46. 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