python中系統資訊擷取psutil使用

來源:互聯網
上載者:User

python中系統資訊擷取psutil使用
[python]view plaincopyprint?

  1. #!/usr/bin/envpython
  2. #coding:utf-8
  3. importpsutil
  4. importtime
  5. importsys
  6. fromoptparseimportOptionParser
  7. parser=OptionParser()
  8. parser.add_option("-t","--time",dest="time",
  9. help="此參數可查看當前下載占的頻寬,-t是測試時間",metavar="10")
  10. parser.add_option("-d","--deamon",action="store_false",dest="deamon",default=True,
  11. help="後台運行此指令碼")
  12. defSysinfo():
  13. Boot_Start=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime(psutil.boot_time()))
  14. time.sleep(0.5)
  15. Cpu_usage=psutil.cpu_percent()
  16. RAM=int(psutil.virtual_memory().total/(1027*1024))
  17. RAM_percent=psutil.virtual_memory().percent
  18. Swap=int(psutil.swap_memory().total/(1027*1024))
  19. Swap_percent=psutil.swap_memory().percent
  20. Net_sent=psutil.net_io_counters().bytes_sent
  21. Net_recv=psutil.net_io_counters().bytes_recv
  22. Net_spkg=psutil.net_io_counters().packets_sent
  23. Net_rpkg=psutil.net_io_counters().packets_recv
  24. BFH=r'%'
  25. print"\033[1;32m開機時間:%s\033[1;m"%Boot_Start
  26. print"\033[1;32m當前CPU使用率:%s%s\033[1;m"%(Cpu_usage,BFH)
  27. print"\033[1;32m實體記憶體:%dM\t使用率:%s%s\033[1;m"%(RAM,RAM_percent,BFH)
  28. print"\033[1;32mSwap記憶體:%dM\t使用率:%s%s\033[1;m"%(Swap,Swap_percent,BFH)
  29. print"\033[1;32m發送:%dByte\t發送包數:%d個\033[1;m"%(Net_sent,Net_spkg)
  30. print"\033[1;32m接收:%dByte\t接收包數:%d個\033[1;m"%(Net_recv,Net_rpkg)
  31. foriinpsutil.disk_partitions():
  32. print"\033[1;32m盤符:%s掛載點:%s使用率:%s%s\033[1;m"%(i[0],i[1],psutil.disk_usage(i[1])[3],BFH)
  33. defNet_io(s):
  34. x=0
  35. sum=0
  36. whileTrue:
  37. ifx>=s:
  38. break
  39. r1=psutil.net_io_counters().bytes_recv
  40. time.sleep(1)
  41. r2=psutil.net_io_counters().bytes_recv
  42. y=r2-r1
  43. print"%.2fKb/s"%(y/1024.0)
  44. sum+=y
  45. x+=1
  46. result=sum/x
  47. print"\033[1;32m%s秒內平均速度:%.2fKb/s\033[1;m"%(x,result/1024.0)
  48. if__name__=="__main__":
  49. (options,args)=parser.parse_args()
  50. ifoptions.time:
  51. Net_io(int(options.time))
  52. else:
  53. Sysinfo()

運行結果:

開機時間:2015-04-24 08:39:45
當前CPU使用率:1.0%
實體記憶體:985M 使用率:26.5%
Swap記憶體:2073M 使用率:0.0%
發送:140263274 Byte 發送包數:213610個
接收:1522552624 Byte 接收包數:25313640個
盤符: /dev/mapper/centos-root 掛載點: / 使用率: 32.8
盤符: /dev/xvda1 掛載點: /boot 使用率: 19.2%

systeminfo.py -t 10使用:

1.18 Kb/s
0.98 Kb/s
1.11 Kb/s
0.94 Kb/s
0.89 Kb/s
0.92 Kb/s
1.29 Kb/s
1.24 Kb/s
1.21 Kb/s
0.73 Kb/s
10秒內平均速度:1.05 Kb/s

聯繫我們

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