日期:2014-05-17  浏览次数:21259 次

在windows下如何将CPU和内存消耗,定时追加输入到一个文件里
类似Unix下的sar命令,sar 1 10 >> t.txt
在windows下如何将CPU和内存消耗,定时追加输入到一个文件里。

谢谢!


------解决方案--------------------
运行这个批处理
或者把它加到计划任务里面

BatchFile code
@echo off
set DstFile=C:\test\cpu_mem.log
if not exist %DstFile% (
  type nul>"%DstFile%"
)
:cpu
for /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Processor get PercentProcessorTime /value
 ^| findstr "PercentProcessorTime"') do (
  set UseCPU=%%a
  goto :mem
)
:mem
for /f "tokens=2 delims==" %%a in ('wmic path Win32_PhysicalMemory get * /value ^| findstr "Capacity"') do (
  set TalMem=%%a
)
for /f "tokens=2 delims==" %%a in ('wmic path Win32_PerfFormattedData_PerfOS_Memory get * /value
 ^| findstr "AvailableBytes"') do (
  set UseMem=%%a
)
:show
>>"%DstFile%" echo CPU使用率:%UseCPU%%%
>>"%DstFile%" echo 物理内存总量:%TalMem%
>>"%DstFile%" echo 当前可用内存:%UseMem%

------解决方案--------------------
内存使用率计算一下就可以啦``~
完整代码如下:
VBScript code
'/************************************************************************/
'
' FileName: WriteUsage.vbs
'
'   Author: Chinafish
'
'    Email: china_fish@163.com
'
' Homepage: http://www.csinx.org
'
'     Desc: Write CPU & Memory usage 
'
'/************************************************************************/

on error resume next

const TargetFile = "C:\Usage.log"

Dim dwTotalMem, dwAvailMem

'定时循环记录
do while true

const ForReading = 1
const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")

'没有文件先创建文件
Set MyFile = fso.CreateTextFile(TargetFile, false)
MyFile.Close

'打开创建的文件读出原来的内容 
Set MyFile = fso.OpenTextFile(TargetFile, ForReading)
strContent = MyFile.ReadAll
MyFile.Close

'先写原文件内容
Set MyFile = fso.OpenTextFile(TargetFile, ForWriting)

'文件无内容时写文件头
if strContent <> "" then
    MyFile.WriteLine(strContent)
else
    MyFile.WriteLine("/*Usage.log file write by Chinafish.*/")
    MyFile.WriteBlankLines 1
End if

'写开始时间
MyFile.WriteLine("程序启动时间: " & Now())

'本地电脑
strComputer = "."

'连接到本地电脑
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.InstancesOf("Win32_LogicalMemoryConfiguration")

'总计物理内存
For Each colItem In colItems
    dwTotalMem = CLng(colItem.TotalPhysicalMemory / 1024)
    MyFile.WriteLine("总计物理内存: " & CLng(colItem.TotalPhysicalMemory / 1024) & "MB")
Next

'剩余内存
Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_Memory",,48)

For Each objItem in colItems
    dwAvailMem = objItem.AvailableMBytes
    MyFile.WriteLine("剩余物理内存: " & objItem.AvailableMBytes & "MB")
Next

'物理内存使用率(第一行精确到小数后2位, 第二行取整数) 
'MyFile.WriteLine("物理内存使用: " & CInt((1 - dwAvailMem/dwTotalMem)*100*100)/100 & "%")
MyFile.WriteLine("物理内存使用: " & CInt((1 - dwAvailMem/dwTotalMem)*100) & "%")

'CPU数和使用率
Set colItems = objWMIService.ExecQuery("Select * from Win32_Processor",,48)
i = 0

For Each objItem in colItems
    MyFile.WriteLine("CPU(" & i & ")使用率: " & objItem.LoadPercentage & "%")
    i = i + 1
Next

'写结束时间
MyFile.WriteLine("程序结束时间: " & Now())
MyFile.Close

Set fso = Nothing
Set MyFile = Nothing

'睡眠达到定时操作目的 参数是ms 1000是1秒
WScript.Sleep 1000*60*0.1

Loop