我正在尝试使用python收集系统统计信息。主要是寻找一个已经执行此操作的libery或github项目。我希望能够收集/记录以下内容。
编辑: 我主要是在Linux系统中寻找支持我稍后将转向Windows系统。
答案 0 :(得分:2)
尝试使用psutil模块here,看看显示的示例。但是,对于DISK IO
,您可能必须使用wmi
和pywin32
模块。
例如,对于使用Windows的DISK QUEUE LENGTH
,请使用:
from win32pdh import *
import time
while True:
path = MakeCounterPath((None,"PhysicalDisk","_Total",None,0,"Avg. Disk Queue Length"),0)
query_handle = OpenQuery()
counter_handle = AddCounter(query_handle, path)
query_handle = OpenQuery()
counter_handle = AddCounter(query_handle, path)
CollectQueryData(query_handle)
time.sleep(2)
CollectQueryData(query_handle)
(counter_type, value) = GetFormattedCounterValue(counter_handle, PDH_FMT_DOUBLE)
print value
CloseQuery(query_handle)
它使用windows上的perfmon应用程序的计数器。对于Linux,DISK IO有多种工具。
对于Linux DISK IO
,请查看iotop
here
此外,Linux diskstats使用此示例,它会定期解析/proc/diskstats
def diskstats_parse(dev=None):
file_path = '/proc/diskstats'
result = {}
# ref: http://lxr.osuosl.org/source/Documentation/iostats.txt
columns_disk = ['m', 'mm', 'dev', 'reads', 'rd_mrg', 'rd_sectors',
'ms_reading', 'writes', 'wr_mrg', 'wr_sectors',
'ms_writing', 'cur_ios', 'ms_doing_io', 'ms_weighted']
columns_partition = ['m', 'mm', 'dev', 'reads', 'rd_sectors', 'writes', 'wr_sectors']
lines = open(file_path, 'r').readlines()
for line in lines:
if line == '': continue
split = line.split()
if len(split) == len(columns_disk):
columns = columns_disk
elif len(split) == len(columns_partition):
columns = columns_partition
else:
# No match
continue
data = dict(zip(columns_disk, split))
if dev != None and dev != data['dev']:
continue
for key in data:
if key != 'dev':
data[key] = int(data[key])
result[data['dev']] = data
return result
我希望这会有所帮助。 :)