Tag:default python background run action import psutil
#!/usr/bin/env python# coding:utf-8import psutilimport timeimport sysfrom Optparse import optionparserfrom datetime import date,datetimeimport xlwtimport xlsxwriterf = open ("D:/1.txt", ' w+ ') Parser = optionparser () parser.add_option ("- T ", "--time ", dest=" Time ", help= "This parameter can see the current download bandwidth,-T is the test time", metavar= ") 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 ())) type (Boot_start) 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 * (1024x768)) 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 >> f, ' boot time:%s ' % boot_ Start print >> f, " CPU usage:%s%s" % (CPU_USAGE, BFH) p rint >> f, "Memory:%dm\t Memory Utilization:%s%s" % (RAM, RAM_PERCENT, BFH) print >> f, "Swap partition memory:%dm\t Swap partition usage:%s%s" % (swap, swap_percent, BFH) print >> f, " Send:%d byte\t number of packets Sent:%d" % (net_sent , net_spkg) print >>f, " Receive:%d byte\t Received packets:%d" % ( net_recv, net_rpkg) for i in psutil.disk_partitions (): print >> f, " Drive letter: %s mount point: %s disk usage : %s%s " % (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 >> f, "Average speed:%.2f kb/s in 1;33m%s seconds" % (x, result /1024.0) if __name__ == "__main__": (Options, args) = parser.parse_args () if options.time: &nBsp; net_io (int (options.time)) else: sysinfo ()
Final results
650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/97/D8/wKiom1kz3snRqVYNAAC2kxZJdOM110.png-wh_500x0-wm_ 3-wmp_4-s_2174281169.png "title=" qq picture 20170604181855.png "alt=" Wkiom1kz3snrqvynaac2kxzjdom110.png-wh_50 "/>
Because of the scholarship, the great god please more guidance, thank you, would like to directly want to insert the results into Excel, is still in the study, such as the Great God, please enlighten me, thank you.
This article is from a "glass of water" blog, please be sure to keep this source http://6528161.blog.51cto.com/6518161/1932094
Python gets system-based performance parameter implementation write file