Use psutil to obtain system information in python

Source: Internet
Author: User

Use psutil to obtain system information in python
[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 = "this parameter shows the bandwidth occupied by the current download.-t indicates the test time", metavar = "10 ")
  10. Parser. add_option ("-d", "-- deamon", action = "store_false", dest = "deamon", default = True,
  11. Help = "run this script in the background ")
  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; 32 m boot time: % s \ 033 [1; m" % Boot_Start
  26. Print "\ 033 [1; 32 m current CPU usage: % s \ 033 [1; m" % (Cpu_usage, BFH)
  27. Print "\ 033 [1; 32 m physical memory: % dM \ t usage: % s \ 033 [1; m" % (RAM, RAM_percent, BFH)
  28. Print "\ 033 [1; 32mSwap memory: % dM \ t usage: % s \ 033 [1; m" % (Swap, Swap_percent, BFH)
  29. Print "\ 033 [1; 32 m sent: % dByte \ t sent packets: % d \ 033 [1; m" % (Net_sent, Net_spkg)
  30. Print "\ 033 [1; 32 m Received: % dByte \ t received packets: % d \ 033 [1; m" % (Net_recv, Net_rpkg)
  31. Foriinpsutil. disk_partitions ():
  32. Print "\ 033 [1; 32 m drive letter: % s mount point: % s usage: % 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; average speed of 32 m % 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 ()

Running result:

Start Time: 08:39:45
Current CPU usage: 1.0%
Physical memory: 985 M usage: 26.5%
Swap memory: 2073 MB utilization: 0.0%
Sent: 140263274 Bytes Sent packets: 213610
Received: 1522552624 bytes received packets: 25313640
Drive letter:/dev/mapper/centos-root mount point:/usage: 32.8
Drive letter:/dev/xvda1 mount point:/boot usage: 19.2%

Systeminfo. py-t 10 usage:

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
Average speed within 10 seconds: 1.05 Kb/s

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.