python擷取系統基礎績效參數實現寫入檔案

來源:互聯網
上載者:User

標籤:default   python   後台運行   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="此參數可查看當前下載占的頻寬,-t是測試時間", metavar="10")parser.add_option("-d", "--deamon", action="store_false", dest="deamon", default=True,                  help="後台運行此指令碼")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 * 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 >> f, " 開機時間:%s" % Boot_Start    print >> f," CPU使用率:%s%s" % (Cpu_usage, BFH)    print >> f,"記憶體:%dM\t記憶體使用量率:%s%s" % (RAM, RAM_percent, BFH)    print >> f,"交換分區記憶體:%dM\t交換分區使用率:%s%s" % (Swap, Swap_percent, BFH)    print >> f," 發送:%d Byte\t發送包數:%d個" % (Net_sent, Net_spkg)    print >>f," 接收:%d Byte\t接收包數:%d個" % (Net_recv, Net_rpkg)    for i in psutil.disk_partitions():        print >> f," 盤符: %s 掛載點: %s 磁碟使用率: %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,"1;33m%s秒內平均速度:%.2f Kb/s " % (x, result  /1024.0)if __name__ == "__main__":    (options, args) = parser.parse_args()    if options.time:        Net_io(int(options.time))    else:        Sysinfo()

最後結果

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圖片20170604181855.png" alt="wKiom1kz3snRqVYNAAC2kxZJdOM110.png-wh_50" />


由於才學,大神請多指導,謝謝,本來想直接想把結果插入excel中,目前還在研究,如有大神,請不吝賜教,感謝。

本文出自 “一杯水” 部落格,請務必保留此出處http://6528161.blog.51cto.com/6518161/1932094

python擷取系統基礎績效參數實現寫入檔案

聯繫我們

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