#!/usr/bin/env python#coding:utf-8import psutilimport timeimport sysfrom optparse Import OptionParserparser = Optionparser () parser.add_option ("-T", "--time", dest= "Time", help= "This parameter can see the bandwidth of the current download,-t is the test duration", 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.vir Tual_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;32m boot time :%s\033[1;m "% boot_start print"\033[1;32m Current CPU usage:%s%s\033[1;m"% (CPU_USAGE,BFH) print "\033[1;32m Physical Memory:%dm\t usage:%s%s\033[1;m"% (Ram,ram_percent, BFH) print "\033[1;32mswap Memory:%dm\t usage:%s%s\033[1;m"% (SWAP,SWAP_PERCENT,BFH) print "\033[1;32m send:%d byte\t send packets:%d \033[1;m "% (net_sent,net_spkg) print" \033[1;32m Received:%d Byte\t received packets:%d \033[1;m "% (net_recv,net_rpkg) for I in Psuti L.disk_partitions (): print "\033[1;32m drive letter:%s mount point:%s usage:%s%s\033[1;m"% (I[0],i[1],psutil.disk_usage (i[1)) [3],BF H) def net_io (s): x = 0 sum = 0 while true:if x >= s:break r1 = Psutil.net_io_counte RS (). 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;32m%s seconds Average Speed:%.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 ()
Operation Result:
Start time: 2015-04-24 08:39:45
Current CPU Usage: 1%
Physical Memory: 985M Usage: 26.5%
Swap memory: 2073M usage: 0%
Send: 140263274 bytes Sent: 213,610 packets
Receive: 1522552624 Byte received packets: 25,313,640
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 Use:
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 in 10 seconds: 1.05 kb/s
Use of System Information acquisition Psutil in Python