機器效能數據收集

2024/02/10閱讀時間約 9 分鐘

1. 程式開發 (程式名稱: WMI_CMD2.py):

1.1程式功能:

Windows系統運行中,當時的效能數據,寫出檔案供後續利用,

效能數據有CPU,Memory, Disk Partition Size等

1.2 開發工具為 Python 3.8.3 [MSC v.1916 64 bit (AMD64)]。

*可免費使用,縮編式,易於閱讀,行數短少簡易

1.3 編輯工具 Spyder 4.1.4

*直譯式,不必compile 原始螞碼可直接執行,結果易於debug

1.4 使用模組Module/套件 Package : import wmi , time, json, os

*標準模組函數,各第三方套件 可import到程式使用,效率高

1.5 使用函數 : getSysInfo(),get_fs_info()

*自定函數運用, 收集當時的效能狀況

包括 CPU,Mem, Disk Partition Size ,freesize, useage%

1.6 收集資訊寫出: 顯示及出力效能 txt file

1.7 打包執行檔 :pyinstaller -F D:\Users\linct\XXX.py

==> 系統管理者身分執行後,產生exe file,本例WMI_CMD2.exe

*執行檔可以移至其它Windows機器執行,注意相關外部檔案須同一folder,否則

程式內需特別指定路徑


2. 實際運用:

2.1 檔案配置:

*程式配置: 於D:\XXX-IT\WMI\ 存放程式執行檔(WMI_CMD2.exe),及

寫出記錄file ( CMD_jsonFile.txt) ==>本程式exe file, output file 須同目錄之下

*收集JOB: 各個機器內,於 D:\XXX-IT\WMI\WMI_CMD.bat 配置該JOB,

例如 機器排定每周1,3,5 執行收集程式之JOB

*連線主機: MAP 至 D:\XXXIT\WMI\,命名 W槽

*收集對象機器 : 四台 (1)DB1 , (2) DB2 (3) AP (4)SF

*收集資料 : 各機器收集的資料,集中寫入於 主機機器 上單一txt file 保存

3. 擴充應用:

3.1 設定排程,自動執行本程式,可以週期性執行,取得多數個數據後,

計算出該週期之平均值,如每時,每日,每周,每月之該機器效能變化

與趨勢。新系統導入運用時,機器效能可利用幾秒 幾分 迴圈執行取得評價資料

3.2 Windows機器皆可適用,只要呼叫出本python程式執行檔即可收集

(CALL WMI_CMD2.exe)

3.3 出力歷史紀錄檔,可當作稽查受查時,說明之憑證提出

3.4 可定期eMail 寄出收集之效能記錄txt file,讓擔當者可即時確認,

提早對應避免事件發生


4. 程式(WMI_CMD2.py)撰寫內容:

-------- Python coding ------------

# -*- coding: utf-8 -*-

"""

Created on ---

@author: -----

"""

import wmi

import time

import json

import os

# get PC name and OS time , then write to txt file

PcName = os.getenv('ComputerName')

# timestamp = time.strftime('%a, %Y-%m-%d %H:%M:%S', time.localtime())

timestamp = time.strftime('%a, %Y-%m-%d %H:%M', time.localtime())

PcNameTime = PcName + " at " + timestamp

file1 = open('CMD_jsonFile.txt', 'at') # 附加和文字模式

file1.write('\n')

file1.write(PcNameTime) # 把資料寫入檔案

print (PcNameTime)


#====CPU,MEM

def getSysInfo(wmiService = None):

result = {}

if wmiService == None:

wmiService = wmi.WMI()

# cpu

for cpu in wmiService.Win32_Processor():

result['CPU_%'] = cpu.loadPercentage

# memory

cs = wmiService.Win32_ComputerSystem()

os = wmiService.Win32_OperatingSystem()

result['MemTot_GB'] = round(int(cs[0].TotalPhysicalMemory)/1024/1024/1024,2)

#result['MemFree_GB'] = round(int(os[0].FreePhysicalMemory)/1024/1024,2)

result['MemUse_GB'] = result['MemTot_GB'] - round(int(os[0].FreePhysicalMemory)/1024/1024,2)

return result


if __name__ == '__main__':

wmiService = wmi.WMI()

while True:

print (getSysInfo(wmiService))

fs = getSysInfo(wmiService)

jsObj = json.dumps(fs)

# file1.write('\n')

file1.write(jsObj)

# time.sleep(3)

break


#====DISK 獲取檔案系統資訊。包含分割槽的大小、已用量、可用量、使用率、掛載點資訊


def get_fs_info() :

tmplist = []

c = wmi.WMI ()

for physical_disk in c.Win32_DiskDrive ():

for partition in physical_disk.associators ("Win32_DiskDriveToDiskPartition"):

for logical_disk in partition.associators ("Win32_LogicalDiskToPartition"):

tmpdict = {}

tmpdict["Part"] = logical_disk.Caption

tmpdict["TOT_GB"] = round(int(logical_disk.Size)/1024/1024/1024,2)

# tmpdict["FreeSpace"] = round(int(logical_disk.FreeSpace)/1024/1024/1024,2)

tmpdict["Use_GB"] =round(tmpdict["PartTOT_GB"] - round(int(logical_disk.FreeSpace)/1024/1024/1024,2),2)

tmpdict["Use_%"] = int(100.0*(int(logical_disk.Size)-int(logical_disk.FreeSpace))/int(logical_disk.Size))

tmplist.append(tmpdict)

return tmplist


if __name__ == "__main__":

fs = get_fs_info()

print (fs) # 可省

jsObj = json.dumps(fs[0: ])

# file1.write('\n')

file1.write(jsObj)

file1.close()

------------------ end of coding -------------------



8會員
49內容數
樂趣體驗紀錄,沙龍房間有歌唱,生活,科技
留言0
查看全部
發表第一個留言支持創作者!
從 Google News 追蹤更多 vocus 的最新精選內容