Use python to obtain system information psutil, pythonpsutil

Source: Internet
Author: User

Use python to obtain system information psutil, pythonpsutil

#! /Usr/bin/env python # coding: utf-8import psutilimport timeimport sysfrom optparse import OptionParserparser = OptionParser () parser. add_option ("-t", "-- time", dest = "time", help = "this parameter allows you to view the bandwidth occupied by the current download.-t indicates the test time ", metavar = "10") parser. add_option ("-d", "-- deamon", action = "store_false", dest = "deamon", default = True, help = "run this script in the background ") def Sysinfo (): Boot_Start = time. strftime ("% Y-% m-% d % H: % M: % S", time. localtime (psutil. boot_time () time. sleep (0.5) Cpu_usage = psutil. cpu_percent () RAM = int (psutil. virtual_memory (). total/(1027*1024) RAM_percent = psutil. virtual_memory (). percent Swap = int (psutil. swap_memory (). total/(1027*1024) Swap_percent = psutil. swap_memory (). percent Net_sent = psutil.net _ io_counters (). bytes_sent Net_recv = psutil.net _ io_counters (). bytes_recv Net_spkg = psutil.net _ io_counters (). packets_sent Net_rpkg = psutil.net _ io_counters (). packets_recv BFH = R' % 'print "\ 033 [1; 32 m boot time: % s \ 033 [1; m" % Boot_Start print "\ 033 [1; 32 m current CPU usage: % s \ 033 [1; m "% (Cpu_usage, BFH) print" \ 033 [1; 32 m physical memory: % dM \ t usage: % s \ 033 [1; m "% (RAM, RAM_percent, BFH) print" \ 033 [1; 32mSwap memory: % dM \ t usage: % s \ 033 [1; m "% (Swap, Swap_percent, BFH) print" \ 033 [1; 32 m send: % d Byte \ t Number of sent packets: % d packets \ 033 [1; m "% (Net_sent, Net_spkg) print" \ 033 [1; 32 m Received: % d Byte \ t received packets: % d \ 033 [1; m "% (Net_recv, Net_rpkg) for I in psutil. disk_partitions (): 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) def Net_io (s): x = 0 sum = 0 while True: if x> = s: break r1 = psutil.net _ io_counters (). bytes_recv time. sleep (1) r2 = psutil.net _ io_counters (). bytes_recv y = r2-r1 print "%. 2f Kb/s "% (y/1024.0) sum + = y x + = 1 result = sum/x print" \ 033 [1; average speed of 32 m % s: %. 2f Kb/s \ 033 [1; m "% (x, result/1024.0) if _ name _ =" _ main _ ": (options, args) = parser. parse_args () if options. time: Net_io (int (options. time) else: 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.